mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-13 16:46:47 +00:00
shm: Move ShmHeader into Common.h
This commit is contained in:
parent
1036e204d0
commit
68038c4693
|
@ -48,6 +48,79 @@ using Str = boost::interprocess::basic_string<char, std::char_traits<
|
|||
using StrAlloc = boost::interprocess::allocator<Str, SegmentManager>;
|
||||
using StrVector = boost::interprocess::vector<Str, StrAlloc>;
|
||||
|
||||
// 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<uint16_t> refCount;
|
||||
};
|
||||
|
||||
static Hdr* HdrPtr(char* ptr)
|
||||
{
|
||||
// [HdrOffset(uint16_t)][Hdr alignment][Hdr][user buffer alignment][user buffer]
|
||||
// ^
|
||||
return reinterpret_cast<Hdr*>(ptr + sizeof(uint16_t) + *(reinterpret_cast<uint16_t*>(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<uint16_t>& 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<uintptr_t>(ptr) % 2 == 0);
|
||||
|
||||
// offset to the beginning of the Hdr. store it in the beginning
|
||||
uint16_t hdrOffset = alignof(Hdr) - ((reinterpret_cast<uintptr_t>(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<uintptr_t>(ptr) + HdrPartSize()) % alignment);
|
||||
new(ptr + sizeof(uint16_t) + hdrOffset) Hdr{ userOffset, std::atomic<uint16_t>(1) };
|
||||
}
|
||||
|
||||
static void Destruct(char* ptr) { RefCountPtr(ptr).~atomic(); }
|
||||
};
|
||||
|
||||
enum class AllocationAlgorithm : int
|
||||
{
|
||||
rbtree_best_fit,
|
||||
|
|
|
@ -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<uint16_t> refCount;
|
||||
};
|
||||
|
||||
static Hdr* HdrPtr(char* ptr)
|
||||
{
|
||||
// [HdrOffset(uint16_t)][Hdr alignment][Hdr][user buffer alignment][user buffer]
|
||||
// ^
|
||||
return reinterpret_cast<Hdr*>(ptr + sizeof(uint16_t) + *(reinterpret_cast<uint16_t*>(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<uint16_t>& 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<uintptr_t>(ptr) % 2 == 0);
|
||||
|
||||
// offset to the beginning of the Hdr. store it in the beginning
|
||||
uint16_t hdrOffset = alignof(Hdr) - ((reinterpret_cast<uintptr_t>(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<uintptr_t>(ptr) + HdrPartSize()) % alignment);
|
||||
new(ptr + sizeof(uint16_t) + hdrOffset) Hdr{ userOffset, std::atomic<uint16_t>(1) };
|
||||
}
|
||||
|
||||
static void Destruct(char* ptr) { RefCountPtr(ptr).~atomic(); }
|
||||
};
|
||||
|
||||
class Manager
|
||||
{
|
||||
public:
|
||||
|
|
Loading…
Reference in New Issue
Block a user