CppCommon  1.0.4.1
C++ Common Library
allocator_arena.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_MEMORY_ALLOCATOR_ARENA_H
10 #define CPPCOMMON_MEMORY_ALLOCATOR_ARENA_H
11 
12 #include "allocator.h"
13 
14 namespace CppCommon {
15 
17 
28 template <class TAuxMemoryManager = DefaultMemoryManager>
30 {
31 public:
33 
38  explicit ArenaMemoryManager(TAuxMemoryManager& auxiliary) : ArenaMemoryManager(auxiliary, 65536) {}
40 
44  explicit ArenaMemoryManager(TAuxMemoryManager& auxiliary, size_t capacity);
46 
51  explicit ArenaMemoryManager(TAuxMemoryManager& auxiliary, void* buffer, size_t capacity);
55 
58 
60  size_t allocated() const noexcept { return _allocated; }
62  size_t allocations() const noexcept { return _allocations; }
63 
65  const uint8_t* buffer() const noexcept { return _buffer; }
67  size_t capacity() const noexcept { return _capacity; }
69  size_t size() const noexcept { return _size; }
70 
72  size_t max_size() const noexcept { return _auxiliary.max_size(); }
73 
75  TAuxMemoryManager& auxiliary() noexcept { return _auxiliary; }
76 
78 
83  void* malloc(size_t size, size_t alignment = alignof(std::max_align_t));
85 
89  void free(void* ptr, size_t size);
90 
92  void reset();
94 
97  void reset(size_t capacity);
99 
103  void reset(void* buffer, size_t capacity);
104 
106  void clear();
107 
108 private:
109  // Arena page
110  struct Page
111  {
112  uint8_t* buffer;
113  size_t capacity;
114  size_t size;
115  Page* prev;
116  };
117 
118  // Allocation statistics
119  size_t _allocated;
120  size_t _allocations;
121 
122  // Auxiliary memory manager
123  TAuxMemoryManager& _auxiliary;
124 
125  // Arena pages
126  Page* _current;
127  size_t _reserved;
128 
129  // External buffer
130  bool _external;
131  uint8_t* _buffer;
132  size_t _capacity;
133  size_t _size;
134 
136  Page* AllocateArena(size_t capacity, Page* prev);
138  void ClearArena();
139 };
140 
142 template <typename T, class TAuxMemoryManager = DefaultMemoryManager, bool nothrow = false>
144 
147 } // namespace CppCommon
148 
149 #include "allocator_arena.inl"
150 
151 #endif // CPPCOMMON_MEMORY_ALLOCATOR_ARENA_H
Memory allocator definition.
Arena memory allocator inline implementation.
Memory allocator class.
Definition: allocator.h:25
Arena memory manager class.
void clear()
Clear arena memory allocator.
void reset()
Reset the memory manager.
size_t allocations() const noexcept
Count of active memory allocations.
size_t size() const noexcept
Arena allocated size.
ArenaMemoryManager & operator=(ArenaMemoryManager &&)=delete
void * malloc(size_t size, size_t alignment=alignof(std::max_align_t))
Allocate a new memory block of the given size.
ArenaMemoryManager & operator=(const ArenaMemoryManager &)=delete
ArenaMemoryManager(ArenaMemoryManager &&)=delete
size_t allocated() const noexcept
Allocated memory in bytes.
size_t capacity() const noexcept
Arena capacity.
ArenaMemoryManager(const ArenaMemoryManager &)=delete
TAuxMemoryManager & auxiliary() noexcept
Auxiliary memory manager.
void free(void *ptr, size_t size)
Free the previously allocated memory block.
size_t max_size() const noexcept
Maximum memory block size, that could be allocated by the memory manager.
ArenaMemoryManager(TAuxMemoryManager &auxiliary)
Initialize arena memory manager with an auxiliary memory manager.
const uint8_t * buffer() const noexcept
Arena buffer.
C++ Common project definitions.
Definition: token_bucket.h:15