CppCommon  1.0.4.1
C++ Common Library
allocator_stack.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_MEMORY_ALLOCATOR_STACK_H
10 #define CPPCOMMON_MEMORY_ALLOCATOR_STACK_H
11 
12 #include "allocator.h"
13 
14 namespace CppCommon {
15 
17 
31 template <size_t N>
33 {
34 public:
38  ~StackMemoryManager() noexcept { reset(); }
39 
42 
44  size_t allocated() const noexcept { return _allocated; }
46  size_t allocations() const noexcept { return _allocations; }
47 
49  const uint8_t* buffer() const noexcept { return _buffer; }
51  size_t capacity() const noexcept { return _capacity; }
53  size_t size() const noexcept { return _size; }
54 
56  size_t max_size() const noexcept { return _capacity; }
57 
59 
64  void* malloc(size_t size, size_t alignment = alignof(std::max_align_t));
66 
70  void free(void* ptr, size_t size);
71 
73  void reset();
74 
75 private:
76  // Allocation statistics
77  size_t _allocated;
78  size_t _allocations;
79  // Stack buffer
80  uint8_t _buffer[N];
81  size_t _capacity;
82  size_t _size;
83 };
84 
86 template <typename T, size_t N, bool nothrow = false>
88 
89 } // namespace CppCommon
90 
91 #include "allocator_stack.inl"
92 
93 #endif // CPPCOMMON_MEMORY_ALLOCATOR_STACK_H
Memory allocator definition.
Stack memory allocator inline implementation.
Memory allocator class.
Definition: allocator.h:25
Stack memory manager class.
void free(void *ptr, size_t size)
Free the previously allocated memory block.
const uint8_t * buffer() const noexcept
Stack buffer.
void * malloc(size_t size, size_t alignment=alignof(std::max_align_t))
Allocate a new memory block of the given size.
size_t max_size() const noexcept
Maximum memory block size, that could be allocated by the memory manager.
size_t allocations() const noexcept
Count of active memory allocations.
StackMemoryManager(const StackMemoryManager &)=delete
size_t size() const noexcept
Stack allocated size.
StackMemoryManager & operator=(const StackMemoryManager &)=delete
void reset()
Reset the memory manager.
StackMemoryManager & operator=(StackMemoryManager &&)=delete
size_t capacity() const noexcept
Stack capacity.
StackMemoryManager(StackMemoryManager &&)=delete
size_t allocated() const noexcept
Allocated memory in bytes.
C++ Common project definitions.
Definition: token_bucket.h:15