Skip to content

Commit

Permalink
Memory allocators naming refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
chronoxor committed Sep 26, 2023
1 parent 1039d2b commit ccf993e
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 194 deletions.
22 changes: 11 additions & 11 deletions include/memory/allocator_arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ class ArenaMemoryManager
public:
//! Initialize arena memory manager with an auxiliary memory manager
/*!
Arena chunk capacity will be 65536.
Arena page capacity will be 65536.
\param auxiliary - Auxiliary memory manager
*/
explicit ArenaMemoryManager(TAuxMemoryManager& auxiliary) : ArenaMemoryManager(auxiliary, 65536) {}
//! Initialize arena memory manager with an auxiliary memory manager and a given chunk capacity
//! Initialize arena memory manager with an auxiliary memory manager and a given page capacity
/*!
\param auxiliary - Auxiliary memory manager
\param capacity - Arena chunk capacity in bytes
\param capacity - Arena page capacity in bytes
*/
explicit ArenaMemoryManager(TAuxMemoryManager& auxiliary, size_t capacity);
//! Initialize arena memory manager with an auxiliary memory manager and a given buffer
Expand Down Expand Up @@ -90,9 +90,9 @@ class ArenaMemoryManager

//! Reset the memory manager
void reset();
//! Reset the memory manager with a given chunk capacity
//! Reset the memory manager with a given page capacity
/*!
\param capacity - Arena chunk capacity in bytes
\param capacity - Arena page capacity in bytes
*/
void reset(size_t capacity);
//! Reset the memory manager with a given buffer
Expand All @@ -106,13 +106,13 @@ class ArenaMemoryManager
void clear();

private:
// Arena chunk
struct Chunk
// Arena page
struct Page
{
uint8_t* buffer;
size_t capacity;
size_t size;
Chunk* prev;
Page* prev;
};

// Allocation statistics
Expand All @@ -122,8 +122,8 @@ class ArenaMemoryManager
// Auxiliary memory manager
TAuxMemoryManager& _auxiliary;

// Arena chunks
Chunk* _current;
// Arena pages
Page* _current;
size_t _reserved;

// External buffer
Expand All @@ -133,7 +133,7 @@ class ArenaMemoryManager
size_t _size;

//! Allocate arena
Chunk* AllocateArena(size_t capacity, Chunk* prev);
Page* AllocateArena(size_t capacity, Page* prev);
//! Clear arena
void ClearArena();
};
Expand Down
42 changes: 21 additions & 21 deletions include/memory/allocator_arena.inl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ inline void* ArenaMemoryManager<TAuxMemoryManager>::malloc(size_t size, size_t a
{
if (_current != nullptr)
{
// Allocate memory from the current arena chunk
// Allocate memory from the current arena page
uint8_t* buffer = _current->buffer + _current->size;
uint8_t* aligned = Memory::Align(buffer, alignment);
size_t aligned_size = size + (aligned - buffer);
Expand All @@ -105,17 +105,17 @@ inline void* ArenaMemoryManager<TAuxMemoryManager>::malloc(size_t size, size_t a
while (next_reserved < size)
next_reserved *= 2;

// Allocate a new arena chunk
Chunk* current = AllocateArena(next_reserved, _current);
// Allocate a new arena page
Page* current = AllocateArena(next_reserved, _current);
if (current != nullptr)
{
// Update the current arena chunk
// Update the current arena page
_current = current;

// Increase the required reserved memory size
_reserved = next_reserved;

// Allocate memory from the current arena chunk
// Allocate memory from the current arena page
uint8_t* buffer = _current->buffer + _current->size;
uint8_t* aligned = Memory::Align(buffer, alignment);
size_t aligned_size = size + (aligned - buffer);
Expand Down Expand Up @@ -182,8 +182,8 @@ inline void ArenaMemoryManager<TAuxMemoryManager>::reset(size_t capacity)
// Clear previous allocations
clear();

// Allocate a new arena chunk
Chunk* current = AllocateArena(capacity, _current);
// Allocate a new arena page
Page* current = AllocateArena(capacity, _current);
if (current != nullptr)
_current = current;

Expand Down Expand Up @@ -230,19 +230,19 @@ inline void ArenaMemoryManager<TAuxMemoryManager>::clear()
}

template <class TAuxMemoryManager>
inline typename ArenaMemoryManager<TAuxMemoryManager>::Chunk* ArenaMemoryManager<TAuxMemoryManager>::AllocateArena(size_t capacity, Chunk* prev)
inline typename ArenaMemoryManager<TAuxMemoryManager>::Page* ArenaMemoryManager<TAuxMemoryManager>::AllocateArena(size_t capacity, Page* prev)
{
// Allocate a new arena chunk
uint8_t* buffer = (uint8_t*)_auxiliary.malloc(sizeof(Chunk) + capacity + alignof(std::max_align_t));
Chunk* chunk = (Chunk*)buffer;
if (chunk != nullptr)
// Allocate a new arena page
uint8_t* buffer = (uint8_t*)_auxiliary.malloc(sizeof(Page) + capacity + alignof(std::max_align_t));
Page* page = (Page*)buffer;
if (page != nullptr)
{
// Prepare and return a new arena chunk
chunk->buffer = buffer + sizeof(Chunk);
chunk->capacity = capacity;
chunk->size = 0;
chunk->prev = prev;
return chunk;
// Prepare and return a new arena page
page->buffer = buffer + sizeof(Page);
page->capacity = capacity;
page->size = 0;
page->prev = prev;
return page;
}

// Out of memory...
Expand All @@ -254,11 +254,11 @@ inline void ArenaMemoryManager<TAuxMemoryManager>::ClearArena()
{
if (!_external)
{
// Clear all arena chunks
// Clear all arena pages
while (_current != nullptr)
{
Chunk* prev = _current->prev;
_auxiliary.free(_current, sizeof(Chunk) + _current->capacity + alignof(std::max_align_t));
Page* prev = _current->prev;
_auxiliary.free(_current, sizeof(Page) + _current->capacity + alignof(std::max_align_t));
_current = prev;
}
}
Expand Down
50 changes: 25 additions & 25 deletions include/memory/allocator_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ namespace CppCommon {
//! Memory pool manager class
/*!
Memory pool manager uses a pre-allocated memory buffer or several memory
chunks in order to create an effective free-list data structure, that
pages in order to create an effective free-list data structure, that
allows to allocate and free memory.
During the allocation memory pool manager will return a first-fit memory
block in the free list with concatenating joint blocks to avoid memory
defragmentation.
If the allocated block is huge and does not fit into the memory pool chunk
If the allocated block is huge and does not fit into the memory pool page
then it will be allocated directly from auxiliary memory manager.
Not thread-safe.
Expand All @@ -34,18 +34,18 @@ class PoolMemoryManager
public:
//! Initialize memory pool manager with an auxiliary memory manager
/*!
Memory pool will have unlimited chunks of size 65536.
Memory pool will have unlimited pages of size 65536.
\param auxiliary - Auxiliary memory manager
*/
explicit PoolMemoryManager(TAuxMemoryManager& auxiliary) : PoolMemoryManager(auxiliary, 65536, 0) {}
//! Initialize memory pool manager with an auxiliary memory manager, single chunk size and max chunks count
//! Initialize memory pool manager with an auxiliary memory manager, single page size and max pages count
/*!
\param auxiliary - Auxiliary memory manager
\param chunk - Chunk size in bytes
\param chunks - Max chunks count. Zero value means unlimited count (default is 0)
\param page - Page size in bytes
\param pages - Max pages count. Zero value means unlimited count (default is 0)
*/
explicit PoolMemoryManager(TAuxMemoryManager& auxiliary, size_t chunk, size_t chunks = 0);
explicit PoolMemoryManager(TAuxMemoryManager& auxiliary, size_t page, size_t pages = 0);
//! Initialize memory pool manager with an auxiliary memory manager and a given buffer
/*!
\param auxiliary - Auxiliary memory manager
Expand All @@ -65,10 +65,10 @@ class PoolMemoryManager
//! Count of active memory allocations
size_t allocations() const noexcept { return _allocations; }

//! Chunk size in bytes
size_t chunk() const noexcept { return _chunk; }
//! Max chunks size
size_t chunks() const noexcept { return _max_chunks; }
//! Page size in bytes
size_t page() const noexcept { return _page; }
//! Max pages size
size_t pages() const noexcept { return _max_pages; }

//! Maximum memory block size, that could be allocated by the memory manager
size_t max_size() const noexcept { return _auxiliary.max_size(); }
Expand All @@ -92,12 +92,12 @@ class PoolMemoryManager

//! Reset the memory manager
void reset();
//! Reset the memory manager with a given signle chunk size and max chunks count
//! Reset the memory manager with a given signle page size and max pages count
/*!
\param chunk - Chunk size in bytes
\param chunks - Max chunks count. Zero value means unlimited count (default is 0)
\param page - Page size in bytes
\param pages - Max pages count. Zero value means unlimited count (default is 0)
*/
void reset(size_t chunk, size_t chunks = 0);
void reset(size_t page, size_t pages = 0);
//! Reset the memory manager with a given buffer
/*!
\param buffer - Pool buffer
Expand All @@ -109,12 +109,12 @@ class PoolMemoryManager
void clear();

private:
// Pool chunk contains allocated and free blocks
struct Chunk
// Pool page contains allocated and free blocks
struct Page
{
uint8_t* buffer;
Chunk* prev;
Chunk* next;
Page* prev;
Page* next;
};
// Allocated block
struct AllocBlock
Expand All @@ -136,12 +136,12 @@ class PoolMemoryManager
// Auxiliary memory manager
TAuxMemoryManager& _auxiliary;

// Pool chunks
// Pool pages
bool _external;
size_t _max_chunks;
size_t _chunks;
size_t _chunk;
Chunk* _current;
size_t _max_pages;
size_t _pages;
size_t _page;
Page* _current;

// Free block
FreeBlock* _free_block;
Expand All @@ -152,7 +152,7 @@ class PoolMemoryManager
size_t AlignAdjustment(const void* address, size_t alignment, size_t header);

//! Allocate memory pool
Chunk* AllocateMemoryPool(size_t capacity, Chunk* prev);
Page* AllocateMemoryPool(size_t capacity, Page* prev);
//! Clear memory pool
void ClearMemoryPool();
};
Expand Down
Loading

0 comments on commit ccf993e

Please sign in to comment.