pound-emu_pound/core/memory/arena.h
Ronald Caesar ba45834583
feat!: rewrote program in a data oriented style.
This is because the source code is objected oriented which is not cpu cache
friendly, making the program slower than it has to be. Yuzu's entire
codebase is written in a objected oriented way and I wonder how much faster
it could if they had use DoD principles from the very beginning.

That's why I want to instill DoD fundamentals early on so this won't be a
problem going forward.

Signed-off-by: Ronald Caesar <github43132@proton.me>
2025-08-02 04:05:05 -04:00

115 lines
3.3 KiB
C++

#ifndef POUND_ARENA_H
#define POUND_ARENA_H
#include <cstddef>
#include <cstdint>
#include <cstdlib>
namespace memory
{
/* Defines the default size (in bytes) for memory arenas created via arena_init() */
#define MEMORY_CAPACITY 20480 // 20 KB
#define POISON_PATTERN 0xAA
/*
* NAME
* arena_t - memory management structure for efficient allocation and de-allocation.
*
* SYNOPSIS
* typedef struct {
* std::size_t capacity; Total number of bytes allocated.
* std::size_t size; The current number of bytes consumed.
* void* data; A pointer to the base address of the allocated memory buffer.
* } arena_t;
*
* DESCRIPTION
* The arena struct handles allocating and managing contiguous memory blocks.
*
* RATIONALE
* A memory arena offers a safer alternative to malloc/realloc by
* maintaining a single contiguous block eliminates heap fragmentation
* that occurs with frequent small allocations.
*/
typedef struct
{
std::size_t capacity;
std::size_t size;
void* data;
} arena_t;
/*
* NAME
* arena_init - Initialize a memory arena with default capacity.
*
* SYNOPSIS
* arema_t memory::arena_init();
*
* DESCRIPTION
* The function creates and returns a new memory arena instance with a
* pre-allocated capacity. See the definition MEMORY_CAPACITY to change the
* default capacity.
*
* RETURN VALUE
* Returns a valid arema_t object on success.
*/
extern memory::arena_t arena_init(size_t capacity);
/*
* NAME
* arena_allocate - Allocate memory from a pre-initialized arena.
*
* SYNOPSIS
* const void* memory::arena_allocate(memory::arena_t* arena, std::size_t size);
*
* DESCRIPTION
* The function allocates size bytes from the specified arena. It assumes
* the arena has sufficient capacity and returns a pointer to the allocated
* memory region.
*
* RETURN VALUE
* Returns a pointer to the first byte of the allocated memory. The returned
* pointer is valid until the arena is reset or destroyed.
*
* NOTES
* Requires arema_t to be initialized with arena_init() or similar.
*/
const void* arena_allocate(arena_t* arena, const std::size_t size);
/*
* NAME
* arena_reset - Reset a memory arena's allocation size to zero.
*
* SYNOPSIS
* void memory::arena_reset(memory::arena_t* arena);
*
* DESCRIPTION
* The function resets the allocation size of a pre-initialized arema_t to zero.
* This effectively "frees" all memory allocated from the arena without
* deallocating the underlying buffer, allowing reuse of the capacity for
* future allocations.
*
* NOTES
* Resets arena->size to 0 while preserving arena->capacity.
* Does not free the underlying memory buffer.
* Useful for reusing arenas without reallocation.
*/
void arena_reset(arena_t* arena);
/**
* NAME
* arena_free - Free the memory allocated by an arena
*
* SYNOPSIS
* void memory::arena_free(memory::arena_t* arena);
*
* DESCRIPTION
* The function releases the memory buffer associated with a arema_t and
* resets its capacity and size to zero. This marks the arena as invalid for
* future allocation unless reinitialized.
*/
void arena_free(memory::arena_t* arena);
} // namespace memory
#endif //POUND_ARENA_H