Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
ForwardAllocator.hxx
Go to the documentation of this file.
1
37#ifndef _UTILS_FORWARDALLOCATOR_HXX_
38#define _UTILS_FORWARDALLOCATOR_HXX_
39
40#include <cstdint>
41
42#include "os/OS.hxx"
43#include "utils/Buffer.hxx"
44#include "utils/SimpleQueue.hxx"
45
46class DynamicPool;
47
52{
53public:
55 static constexpr size_t BLOCK_BYTE_SIZE = 1024;
56
60 void *allocate(size_t size, size_t align);
61
64
65#ifdef GTEST
67 static void TEST_recreate_pool();
68#endif
69
70private:
72 typedef uint64_t primitive_t;
75 static_assert(sizeof(AlignedPayload) == BLOCK_BYTE_SIZE,
76 "aligned payload size mismatch");
79
83 uint8_t *head_ptr()
84 {
85 if (allocatedBlocks_.empty())
86 {
87 return nullptr;
88 }
89 return (uint8_t *)allocatedBlocks_.front()->data();
90 }
91
97
98public:
100 size_t allocSize_ {0};
102 size_t allocWasted_ {0};
103
104private:
111};
112
113#endif // _UTILS_FORWARDALLOCATOR_HXX_
Base class for all QMember types that hold data in an expandable format.
Definition Buffer.hxx:195
A specialization of a pool which can allocate new elements dynamically upon request.
Definition Buffer.hxx:491
An arena allocator, which is optimized to not be able to free individual entries, only the entire all...
void * allocate(size_t size, size_t align)
Allocates a block of memory.
Buffer< AlignedPayload > BufferType
Buffer type that we will be allocating from the pool.
TypedQueue< BufferType > allocatedBlocks_
Holds all blocks that we allocated (ever).
OSMutex lock_
Lock that protects the offset in last variable and the queue of blocks.
size_t offsetInLast_
Points into the last allocated block to the next emtpy space.
static constexpr size_t BLOCK_BYTE_SIZE
Allocated blocks will be this size.
primitive_t AlignedPayload[BLOCK_BYTE_SIZE/sizeof(primitive_t)]
An array type with the BLOCK_BYTE_SIZE size.
size_t allocWasted_
Number of bytes lost due to alignment and end-of-block chunks.
static DynamicPool * kbytePool_
This buffer pool will have one bucket to allocate 1kb objects each and kep them on a free list.
uint64_t primitive_t
A primitive type that has sufficient alignment to what we support.
size_t allocSize_
Total number of bytes allocated.
This class provides a mutex API.
Definition OS.hxx:427
A simple, fast, type-safe single-linked queue class with non-virtual methods.