HAllocator
A Simple C++ Memory Allocator
Loading...
Searching...
No Matches
Halloc.hpp
Go to the documentation of this file.
1
14#pragma once
15
16#include <fstream>
17#include <memory>
18
19#include "BlocksContainer.hpp"
20
21const int DEFAULT_BLOCK_SIZE = (128 * 1024 * 1024);
23
24namespace hh::halloc {
45template <typename T = void, int BlockSize = DEFAULT_BLOCK_SIZE,
46 int MaxNumBlocks = DEFAULT_MAX_NUM_BLOCKS>
47class Halloc {
48 // Use shared_ptr so allocator can be copied (required by STL containers)
49 std::shared_ptr<BlocksContainer<BlockSize, MaxNumBlocks>>
50 blocks;
51
52public:
53 // ==================== C++ Allocator Requirements ====================
54
55 using value_type = T;
56 using pointer = T*;
57 using const_pointer = const T*;
58 using reference = T&;
59 using const_reference = const T&;
60 using size_type = std::size_t;
61 using difference_type = std::ptrdiff_t;
62
69 template <typename U>
73
78 Halloc();
79
85 Halloc(const Halloc& other) : blocks(other.blocks) {}
86
96 template <typename U>
97 Halloc(const Halloc<U, BlockSize, MaxNumBlocks>& other) : blocks(other.blocks) {}
98
104 Halloc& operator=(const Halloc& other) {
105 blocks = other.blocks;
106 return *this;
107 }
108
109 // Allow rebind copy constructor to access private members
110 template <typename U, int BS, int MNB>
111 friend class Halloc;
112
131 T* allocate(std::size_t count);
132
150 void deallocate(T* ptr, std::size_t count);
151
161 bool operator==(const Halloc& other) const { return blocks == other.blocks; }
162
169 bool operator!=(const Halloc& other) const { return !(*this == other); }
170
179 ~Halloc();
180
186 void log_container_state(const char* logfile_dir) const {
187 std::ofstream logfile;
188 logfile.open(logfile_dir, std::ios::app);
189 logfile << "#####################################################\n";
190 logfile << "Halloc Allocator State:\n";
191 blocks->log_container_state(logfile);
192 logfile << "#####################################################\n";
193 logfile.close();
194 }
195};
196} // namespace hh::halloc
197
198namespace hh::halloc {
211template <typename T, int BlockSize, int MaxNumBlocks>
213 : blocks(std::make_shared<BlocksContainer<BlockSize, MaxNumBlocks>>()) {
214 // BlocksContainer constructor handles initialization
215}
216
232template <typename T, int BlockSize, int MaxNumBlocks>
234 return static_cast<T*>(blocks->allocate(count * sizeof(T)));
235}
236
252template <typename T, int BlockSize, int MaxNumBlocks>
253void Halloc<T, BlockSize, MaxNumBlocks>::deallocate(T* ptr, std::size_t count) {
254 blocks->deallocate(ptr, count * sizeof(T));
255}
256
269template <typename T, int BlockSize, int MaxNumBlocks>
271 // shared_ptr handles cleanup when reference count reaches zero
272}
273
274} // namespace hh::halloc
Container managing multiple memory blocks for large-scale allocations.
const int DEFAULT_MAX_NUM_BLOCKS
Default max blocks: 1.
Definition Halloc.hpp:22
const int DEFAULT_BLOCK_SIZE
Default block size: 128 MB.
Definition Halloc.hpp:21
Manages multiple memory blocks for scalable allocation.
Definition BlocksContainer.hpp:36
Type-safe allocator using Red-Black tree for best-fit allocation.
Definition Halloc.hpp:47
bool operator!=(const Halloc &other) const
Inequality comparison.
Definition Halloc.hpp:169
void log_container_state(const char *logfile_dir) const
Logs the current state of the container to a file.
Definition Halloc.hpp:186
void deallocate(T *ptr, std::size_t count)
Deallocates memory previously allocated for 'count' objects.
Definition Halloc.hpp:253
~Halloc()
Destructor - releases all blocks back to the OS.
Definition Halloc.hpp:270
std::size_t size_type
Type for sizes.
Definition Halloc.hpp:60
T value_type
Type of allocated objects.
Definition Halloc.hpp:55
T & reference
Reference to value_type.
Definition Halloc.hpp:58
T * pointer
Pointer to value_type.
Definition Halloc.hpp:56
friend class Halloc
Definition Halloc.hpp:111
std::ptrdiff_t difference_type
Type for pointer differences.
Definition Halloc.hpp:61
Halloc(const Halloc &other)
Copy constructor - shares underlying BlocksContainer.
Definition Halloc.hpp:85
const T & const_reference
Const reference to value_type.
Definition Halloc.hpp:59
Halloc & operator=(const Halloc &other)
Assignment operator.
Definition Halloc.hpp:104
bool operator==(const Halloc &other) const
Equality comparison - checks if allocators share same container.
Definition Halloc.hpp:161
Halloc(const Halloc< U, BlockSize, MaxNumBlocks > &other)
Rebind copy constructor - shares BlocksContainer across types.
Definition Halloc.hpp:97
const T * const_pointer
Const pointer to value_type.
Definition Halloc.hpp:57
T * allocate(std::size_t count)
Allocates memory for 'count' objects of type T.
Definition Halloc.hpp:233
Definition Block.hpp:35
Rebind allocator to allocate different type U.
Definition Halloc.hpp:70