From 68038c46930db779ec195415f612b7402e453899 Mon Sep 17 00:00:00 2001 From: Alexey Rybalchenko Date: Tue, 12 Sep 2023 12:57:32 +0200 Subject: [PATCH] shm: Move ShmHeader into Common.h --- fairmq/shmem/Common.h | 73 ++++++++++++++++++++++++++++++++++++++++++ fairmq/shmem/Manager.h | 73 ------------------------------------------ 2 files changed, 73 insertions(+), 73 deletions(-) diff --git a/fairmq/shmem/Common.h b/fairmq/shmem/Common.h index 21d44c8f..4026fbc9 100644 --- a/fairmq/shmem/Common.h +++ b/fairmq/shmem/Common.h @@ -48,6 +48,79 @@ using Str = boost::interprocess::basic_string; using StrVector = boost::interprocess::vector; +// ShmHeader stores user buffer alignment and the reference count in the following structure: +// [HdrOffset(uint16_t)][Hdr alignment][Hdr][user buffer alignment][user buffer] +// The alignment of Hdr depends on the alignment of std::atomic and is stored in the first entry +struct ShmHeader +{ + struct Hdr + { + uint16_t userOffset; + std::atomic refCount; + }; + + static Hdr* HdrPtr(char* ptr) + { + // [HdrOffset(uint16_t)][Hdr alignment][Hdr][user buffer alignment][user buffer] + // ^ + return reinterpret_cast(ptr + sizeof(uint16_t) + *(reinterpret_cast(ptr))); + } + + static uint16_t HdrPartSize() // [HdrOffset(uint16_t)][Hdr alignment][Hdr] + { + // [HdrOffset(uint16_t)][Hdr alignment][Hdr][user buffer alignment][user buffer] + // <---------------------------------------> + return sizeof(uint16_t) + alignof(Hdr) + sizeof(Hdr); + } + + static std::atomic& RefCountPtr(char* ptr) + { + // get the ref count ptr from the Hdr + return HdrPtr(ptr)->refCount; + } + + static uint16_t UserOffset(char* ptr) + { + return HdrPartSize() + HdrPtr(ptr)->userOffset; + } + + static char* UserPtr(char* ptr) + { + // [HdrOffset(uint16_t)][Hdr alignment][Hdr][user buffer alignment][user buffer] + // ^ + return ptr + HdrPartSize() + HdrPtr(ptr)->userOffset; + } + + static uint16_t RefCount(char* ptr) { return RefCountPtr(ptr).load(); } + static uint16_t IncrementRefCount(char* ptr) { return RefCountPtr(ptr).fetch_add(1); } + static uint16_t DecrementRefCount(char* ptr) { return RefCountPtr(ptr).fetch_sub(1); } + + static size_t FullSize(size_t size, size_t alignment) + { + // [HdrOffset(uint16_t)][Hdr alignment][Hdr][user buffer alignment][user buffer] + // <---------------------------------------------------------------------------> + return HdrPartSize() + alignment + size; + } + + static void Construct(char* ptr, size_t alignment) + { + // place the Hdr in the aligned location, fill it and store its offset to HdrOffset + + // the address alignment should be at least 2 + assert(reinterpret_cast(ptr) % 2 == 0); + + // offset to the beginning of the Hdr. store it in the beginning + uint16_t hdrOffset = alignof(Hdr) - ((reinterpret_cast(ptr) + sizeof(uint16_t)) % alignof(Hdr)); + memcpy(ptr, &hdrOffset, sizeof(hdrOffset)); + + // offset to the beginning of the user buffer, store in Hdr together with the ref count + uint16_t userOffset = alignment - ((reinterpret_cast(ptr) + HdrPartSize()) % alignment); + new(ptr + sizeof(uint16_t) + hdrOffset) Hdr{ userOffset, std::atomic(1) }; + } + + static void Destruct(char* ptr) { RefCountPtr(ptr).~atomic(); } +}; + enum class AllocationAlgorithm : int { rbtree_best_fit, diff --git a/fairmq/shmem/Manager.h b/fairmq/shmem/Manager.h index 8e00e427..ec325eb5 100644 --- a/fairmq/shmem/Manager.h +++ b/fairmq/shmem/Manager.h @@ -51,79 +51,6 @@ namespace fair::mq::shmem { -// ShmHeader stores user buffer alignment and the reference count in the following structure: -// [HdrOffset(uint16_t)][Hdr alignment][Hdr][user buffer alignment][user buffer] -// The alignment of Hdr depends on the alignment of std::atomic and is stored in the first entry -struct ShmHeader -{ - struct Hdr - { - uint16_t userOffset; - std::atomic refCount; - }; - - static Hdr* HdrPtr(char* ptr) - { - // [HdrOffset(uint16_t)][Hdr alignment][Hdr][user buffer alignment][user buffer] - // ^ - return reinterpret_cast(ptr + sizeof(uint16_t) + *(reinterpret_cast(ptr))); - } - - static uint16_t HdrPartSize() // [HdrOffset(uint16_t)][Hdr alignment][Hdr] - { - // [HdrOffset(uint16_t)][Hdr alignment][Hdr][user buffer alignment][user buffer] - // <---------------------------------------> - return sizeof(uint16_t) + alignof(Hdr) + sizeof(Hdr); - } - - static std::atomic& RefCountPtr(char* ptr) - { - // get the ref count ptr from the Hdr - return HdrPtr(ptr)->refCount; - } - - static uint16_t UserOffset(char* ptr) - { - return HdrPartSize() + HdrPtr(ptr)->userOffset; - } - - static char* UserPtr(char* ptr) - { - // [HdrOffset(uint16_t)][Hdr alignment][Hdr][user buffer alignment][user buffer] - // ^ - return ptr + HdrPartSize() + HdrPtr(ptr)->userOffset; - } - - static uint16_t RefCount(char* ptr) { return RefCountPtr(ptr).load(); } - static uint16_t IncrementRefCount(char* ptr) { return RefCountPtr(ptr).fetch_add(1); } - static uint16_t DecrementRefCount(char* ptr) { return RefCountPtr(ptr).fetch_sub(1); } - - static size_t FullSize(size_t size, size_t alignment) - { - // [HdrOffset(uint16_t)][Hdr alignment][Hdr][user buffer alignment][user buffer] - // <---------------------------------------------------------------------------> - return HdrPartSize() + alignment + size; - } - - static void Construct(char* ptr, size_t alignment) - { - // place the Hdr in the aligned location, fill it and store its offset to HdrOffset - - // the address alignment should be at least 2 - assert(reinterpret_cast(ptr) % 2 == 0); - - // offset to the beginning of the Hdr. store it in the beginning - uint16_t hdrOffset = alignof(Hdr) - ((reinterpret_cast(ptr) + sizeof(uint16_t)) % alignof(Hdr)); - memcpy(ptr, &hdrOffset, sizeof(hdrOffset)); - - // offset to the beginning of the user buffer, store in Hdr together with the ref count - uint16_t userOffset = alignment - ((reinterpret_cast(ptr) + HdrPartSize()) % alignment); - new(ptr + sizeof(uint16_t) + hdrOffset) Hdr{ userOffset, std::atomic(1) }; - } - - static void Destruct(char* ptr) { RefCountPtr(ptr).~atomic(); } -}; - class Manager { public: