CppCommon  1.0.4.1
C++ Common Library
allocator_pool.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_MEMORY_ALLOCATOR_POOL_H
10 #define CPPCOMMON_MEMORY_ALLOCATOR_POOL_H
11 
12 #include "allocator.h"
13 
14 namespace CppCommon {
15 
17 
31 template <class TAuxMemoryManager = DefaultMemoryManager>
33 {
34 public:
36 
41  explicit PoolMemoryManager(TAuxMemoryManager& auxiliary) : PoolMemoryManager(auxiliary, 65536, 0) {}
43 
48  explicit PoolMemoryManager(TAuxMemoryManager& auxiliary, size_t page, size_t pages = 0);
50 
55  explicit PoolMemoryManager(TAuxMemoryManager& auxiliary, void* buffer, size_t capacity);
59 
62 
64  size_t allocated() const noexcept { return _allocated; }
66  size_t allocations() const noexcept { return _allocations; }
67 
69  size_t page() const noexcept { return _page; }
71  size_t pages() const noexcept { return _max_pages; }
72 
74  size_t max_size() const noexcept { return _auxiliary.max_size(); }
75 
77  TAuxMemoryManager& auxiliary() noexcept { return _auxiliary; }
78 
80 
85  void* malloc(size_t size, size_t alignment = alignof(std::max_align_t));
87 
91  void free(void* ptr, size_t size);
92 
94  void reset();
96 
100  void reset(size_t page, size_t pages = 0);
102 
106  void reset(void* buffer, size_t capacity);
107 
109  void clear();
110 
111 private:
112  // Pool page contains allocated and free blocks
113  struct Page
114  {
115  uint8_t* buffer;
116  Page* prev;
117  Page* next;
118  };
119  // Allocated block
120  struct AllocBlock
121  {
122  size_t size;
123  size_t adjustment;
124  };
125  // Free block
126  struct FreeBlock
127  {
128  size_t size;
129  FreeBlock* next;
130  };
131 
132  // Allocation statistics
133  size_t _allocated;
134  size_t _allocations;
135 
136  // Auxiliary memory manager
137  TAuxMemoryManager& _auxiliary;
138 
139  // Pool pages
140  bool _external;
141  size_t _max_pages;
142  size_t _pages;
143  size_t _page;
144  Page* _current;
145 
146  // Free block
147  FreeBlock* _free_block;
148 
150  size_t AlignAdjustment(const void* address, size_t alignment);
152  size_t AlignAdjustment(const void* address, size_t alignment, size_t header);
153 
155  Page* AllocateMemoryPool(size_t capacity, Page* prev);
157  void ClearMemoryPool();
158 };
159 
161 template <typename T, class TAuxMemoryManager = DefaultMemoryManager, bool nothrow = false>
163 
166 } // namespace CppCommon
167 
168 #include "allocator_pool.inl"
169 
170 #endif // CPPCOMMON_MEMORY_ALLOCATOR_POOL_H
Memory allocator definition.
Memory pool allocator inline implementation.
Memory allocator class.
Definition: allocator.h:25
Memory pool manager class.
PoolMemoryManager & operator=(PoolMemoryManager &&)=delete
size_t pages() const noexcept
Max pages size.
void reset()
Reset the memory manager.
PoolMemoryManager(TAuxMemoryManager &auxiliary)
Initialize memory pool manager with an auxiliary memory manager.
size_t allocated() const noexcept
Allocated memory in bytes.
void * malloc(size_t size, size_t alignment=alignof(std::max_align_t))
Allocate a new memory block of the given size.
PoolMemoryManager(PoolMemoryManager &&)=delete
void clear()
Clear memory pool.
PoolMemoryManager & operator=(const PoolMemoryManager &)=delete
size_t max_size() const noexcept
Maximum memory block size, that could be allocated by the memory manager.
size_t page() const noexcept
Page size in bytes.
size_t allocations() const noexcept
Count of active memory allocations.
TAuxMemoryManager & auxiliary() noexcept
Auxiliary memory manager.
PoolMemoryManager(const PoolMemoryManager &)=delete
void free(void *ptr, size_t size)
Free the previously allocated memory block.
C++ Common project definitions.
Definition: token_bucket.h:15