aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/memory.h
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/messagebus/memory.h
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/messagebus/memory.h')
-rw-r--r--library/cpp/messagebus/memory.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/library/cpp/messagebus/memory.h b/library/cpp/messagebus/memory.h
new file mode 100644
index 0000000000..b2c0544491
--- /dev/null
+++ b/library/cpp/messagebus/memory.h
@@ -0,0 +1,42 @@
+#pragma once
+
+#ifndef CACHE_LINE_SIZE
+#define CACHE_LINE_SIZE 64
+#endif
+
+#define CONCAT(a, b) a##b
+#define LABEL(a) CONCAT(UniqueName_, a)
+#define UNIQUE_NAME LABEL(__LINE__)
+
+#define CACHE_LINE_PADDING char UNIQUE_NAME[CACHE_LINE_SIZE];
+
+static inline void* MallocAligned(size_t size, size_t alignment) {
+ void** ptr = (void**)malloc(size + alignment + sizeof(size_t*));
+ if (!ptr) {
+ return nullptr;
+ }
+
+ size_t mask = ~(alignment - 1);
+ intptr_t roundedDown = intptr_t(ptr) & mask;
+ void** alignedPtr = (void**)(roundedDown + alignment);
+ alignedPtr[-1] = ptr;
+ return alignedPtr;
+}
+
+static inline void FreeAligned(void* ptr) {
+ if (!ptr) {
+ return;
+ }
+
+ void** typedPtr = (void**)ptr;
+ void* originalPtr = typedPtr[-1];
+ free(originalPtr);
+}
+
+static inline void* MallocCacheAligned(size_t size) {
+ return MallocAligned(size, CACHE_LINE_SIZE);
+}
+
+static inline void FreeCacheAligned(void* ptr) {
+ return FreeAligned(ptr);
+}