|
memory_arena
|
Internal helpers for memory allocation logic. More...
Collaboration diagram for Arena Allocation Internals:Functions | |
| void * | arena_alloc_internal (t_arena *arena, size_t size, size_t alignment, const char *label) |
| Core internal allocator with alignment and labeling support. | |
| static bool | arena_alloc_validate_input (t_arena *arena, size_t size, size_t alignment, const char *label) |
| Validate input parameters for arena allocation. | |
| static size_t | arena_calc_aligned_offset (t_arena *arena, size_t alignment) |
| Compute the next aligned offset within the arena. | |
| static bool | arena_check_overflow (t_arena *arena, size_t size, const char *label) |
| Check whether an allocation would cause a size_t overflow. | |
| static void | arena_commit_allocation (t_arena *arena, size_t size, size_t wasted, size_t aligned_offset) |
| Update offset, peak usage, and statistics. | |
| static bool | arena_ensure_capacity (t_arena *arena, size_t size, size_t alignment, const char *label, size_t *aligned_offset, size_t *wasted) |
| Ensure that the arena has enough space for a new allocation with alignment. | |
| static void | arena_invoke_allocation_hook (t_arena *arena, void *ptr, size_t size, size_t offset, size_t wasted, const char *label) |
| Invoke the user-defined allocation hook, if registered. | |
| static bool | arena_is_being_destroyed (t_arena *arena, const char *label) |
| Check whether the arena is currently being destroyed. | |
| static bool | arena_realloc_validate (t_arena *arena, void *old_ptr, size_t new_size) |
Validate input arguments for arena_realloc_last. | |
| static bool | arena_try_grow (t_arena *arena, size_t size, const char *label) |
| Attempt to grow the arena's buffer to satisfy an upcoming allocation. | |
| static void | arena_update_stats (t_arena *arena, size_t size, size_t wasted) |
| Update allocation statistics after a successful arena allocation. | |
| static void | arena_zero_if_needed (void *ptr, size_t size, const char *label) |
| Zero or poison a memory region depending on allocation label. | |
| static bool | is_last_allocation (t_arena *arena, void *old_ptr, size_t old_size) |
| Check if a given pointer corresponds to the most recent allocation. | |
| static void * | realloc_fallback (t_arena *arena, void *old_ptr, size_t old_size, size_t new_size) |
| Perform a fallback reallocation by allocating new memory and copying data. | |
| static void * | realloc_in_place (t_arena *arena, void *old_ptr, size_t old_size, size_t new_size) |
| Attempt to resize the last allocation in place. | |
| static void | update_realloc_stats (t_arena *arena, void *ptr, size_t new_size, size_t old_size, const char *label) |
| Update arena statistics after a successful reallocation. | |
Internal helpers for memory allocation logic.
| void * arena_alloc_internal | ( | t_arena * | arena, |
| size_t | size, | ||
| size_t | alignment, | ||
| const char * | label | ||
| ) |
#include <arena_alloc.c>
Core internal allocator with alignment and labeling support.
This function performs the complete logic for allocating memory from an arena. It supports:
The flow is:
| arena | The arena from which to allocate. |
| size | The number of bytes to allocate. |
| alignment | The alignment in bytes (must be power of two). |
| label | A descriptive label for logging and debugging. |
NULL on failure.arena_alloc() or arena_alloc_aligned() for convenience. This function is not meant to be called directly by users.Definition at line 721 of file arena_alloc.c.
References ALOG, arena_alloc_validate_input(), ARENA_CHECK, arena_check_overflow(), arena_commit_allocation(), arena_ensure_capacity(), arena_invoke_allocation_hook(), arena_is_being_destroyed(), ARENA_LOCK, arena_report_error(), ARENA_UNLOCK, and arena_zero_if_needed().
|
inlinestatic |
#include <arena_alloc.c>
Validate input parameters for arena allocation.
This internal helper checks that all parameters passed to an arena allocation function are valid before proceeding. It performs the following checks:
arena pointer is not NULL.size is greater than zero.alignment is a non-zero power of two (required for valid memory alignment).If any of these checks fail, an appropriate error is reported using arena_report_error() with a descriptive message and the provided label.
| arena | Pointer to the t_arena to validate. |
| size | Size of the memory to allocate, in bytes. |
| alignment | Requested alignment of the memory block (must be power-of-two). |
| label | Name of the calling function, used in error messages. |
true if all inputs are valid, false otherwise.Definition at line 311 of file arena_alloc.c.
References arena_report_error().
|
inlinestatic |
#include <arena_alloc.c>
Compute the next aligned offset within the arena.
This helper calculates the next available offset within the arena's buffer that satisfies the specified alignment requirement. It adds the current offset to the base buffer address, rounds the result up to the nearest aligned address, and then subtracts the base address to return the aligned offset.
This value is used to ensure that allocations start at properly aligned memory locations, which is essential for performance and correctness on many platforms.
| arena | Pointer to the arena structure. |
| alignment | The desired alignment (must be a power of two). |
Definition at line 422 of file arena_alloc.c.
References align_up().
|
inlinestatic |
#include <arena_alloc.c>
Check whether an allocation would cause a size_t overflow.
This helper ensures that the requested allocation does not exceed the bounds of the addressable memory space by checking for integer overflow when adding size to the current arena->offset.
If an overflow would occur, the function:
failed_allocations counter in the arena stats.label.true to indicate failure.| arena | Pointer to the arena being used for allocation. |
| size | The requested allocation size in bytes. |
| label | Label or name of the calling function for error context. |
true if an overflow would occur, false otherwise.Definition at line 388 of file arena_alloc.c.
References arena_report_error().
|
inlinestatic |
#include <arena_alloc.c>
Update offset, peak usage, and statistics.
This internal helper is used after determining that an allocation can safely be performed. It updates the arena's offset to reflect the new allocation, invokes arena_update_peak() to track maximum usage, and updates internal statistics such as allocation count, total allocated bytes, and alignment waste via arena_update_stats().
| arena | Pointer to the arena being updated. |
| size | Size in bytes of the allocation. |
| wasted | Number of alignment bytes wasted. |
| aligned_offset | Offset where the allocation begins in the buffer. |
arena_ensure_capacity().Definition at line 593 of file arena_alloc.c.
References arena_update_peak(), and arena_update_stats().
|
inlinestatic |
#include <arena_alloc.c>
Ensure that the arena has enough space for a new allocation with alignment.
This function calculates the aligned offset needed to satisfy an allocation of size bytes with the given alignment. If the allocation fits in the current arena buffer, the function returns true.
If the buffer is too small and the arena is allowed to grow, it attempts to expand the buffer via arena_try_grow(), and then recalculates the offset and checks again.
This function returns:
true if enough space is available (after optional growth).false if the arena cannot grow or is still too small.The final aligned offset and wasted bytes (padding due to alignment) are written to the provided output pointers.
| arena | Pointer to the arena being used. |
| size | Size of the requested allocation (in bytes). |
| alignment | Required alignment (must be power-of-two). |
| label | Label for logging and error reporting. |
| aligned_offset | Output: aligned offset where the allocation would start. |
| wasted | Output: number of bytes wasted due to alignment padding. |
true if capacity is sufficient, false otherwise.Definition at line 503 of file arena_alloc.c.
References arena_calc_aligned_offset(), and arena_try_grow().
|
inlinestatic |
#include <arena_alloc.c>
Invoke the user-defined allocation hook, if registered.
This internal utility checks whether an allocation hook has been registered in the arena’s debug hook system (arena->hooks.hook_cb). If a hook is set, it is called with detailed allocation metadata:
The hook is loaded using atomic_load_explicit to ensure safe concurrent access. This mechanism enables advanced users to track allocations (e.g., for profiling, leak detection, or debugging).
| arena | Pointer to the arena that performed the allocation. |
| ptr | Pointer to the allocated memory block. |
| size | Number of bytes allocated. |
| offset | Offset in the arena buffer where the allocation began. |
| wasted | Number of wasted bytes due to alignment padding. |
| label | Label associated with this allocation. |
Definition at line 672 of file arena_alloc.c.
|
inlinestatic |
#include <arena_alloc.c>
Check whether the arena is currently being destroyed.
This internal helper inspects the is_destroying atomic flag to determine whether the arena is in the process of being torn down (e.g., via arena_destroy()).
If the flag is set, an error is reported with the provided label, and the function returns true to indicate that no further operations should be performed on the arena.
This check prevents undefined behavior and race conditions by avoiding memory access or mutation while destruction is in progress.
| arena | Pointer to the arena being queried. |
| label | Name of the calling function, used for contextual error messages. |
true if the arena is being destroyed, false otherwise.Definition at line 356 of file arena_alloc.c.
|
inlinestatic |
#include <arena_realloc_last.c>
Validate input arguments for arena_realloc_last.
This internal helper checks that the inputs to arena_realloc_last are valid before proceeding with reallocation logic. It ensures:
arena pointer is not NULL.old_ptr is not NULL.new_size is greater than zero.If any check fails, a descriptive error is reported using arena_report_error().
| arena | Pointer to the arena where the reallocation is performed. |
| old_ptr | Pointer to the previously allocated memory. |
| new_size | New size requested for the reallocation (in bytes). |
true if inputs are valid, false otherwise.Definition at line 167 of file arena_realloc_last.c.
References arena_report_error().
|
inlinestatic |
#include <arena_alloc.c>
Attempt to grow the arena's buffer to satisfy an upcoming allocation.
This internal helper checks whether the arena is allowed to grow (can_grow flag) and, if so, attempts to expand its memory region by calling arena_grow().
If growth is disallowed or fails, an appropriate error is reported with the given label for context, and the function returns false.
This function helps support dynamic allocation models where the arena is allowed to resize its memory buffer when out of space.
| arena | Pointer to the arena to grow. |
| size | Minimum number of bytes required to satisfy the next allocation. |
| label | Allocation label used for context in error messages. |
true if growth succeeded or was unnecessary, false otherwise.Definition at line 454 of file arena_alloc.c.
References arena_grow(), and arena_report_error().
|
inlinestatic |
#include <arena_alloc.c>
Update allocation statistics after a successful arena allocation.
This function increments and records key allocation-related statistics inside the arena->stats structure. These statistics include:
allocations)live_allocations)bytes_allocated)wasted_alignment_bytes)last_alloc_size)last_alloc_offset)alloc_id_counter)The stats are updated inside a critical section to ensure thread safety when ARENA_ENABLE_THREAD_SAFE is enabled.
| arena | Pointer to the arena whose statistics are to be updated. |
| size | Number of bytes allocated. |
| wasted | Number of bytes wasted due to alignment padding. |
ARENA_LOCK / ARENA_UNLOCK), so it should not be called within an existing locked section to avoid deadlocks.Definition at line 551 of file arena_alloc.c.
References ARENA_LOCK, and ARENA_UNLOCK.
|
inlinestatic |
#include <arena_alloc.c>
Zero or poison a memory region depending on allocation label.
This internal utility determines whether a newly allocated block of memory should be zero-initialized or poisoned for debugging purposes.
If the provided label is exactly "arena_calloc_zero", the memory is cleared using memset(ptr, 0, size). Otherwise, the memory is poisoned using arena_poison_memory() to help detect uninitialized access in debugging mode.
This is typically used in arena_alloc_internal() after computing the memory location of a new allocation.
| ptr | Pointer to the memory block to initialize. |
| size | Number of bytes to initialize. |
| label | Allocation label string used to determine behavior. |
Definition at line 628 of file arena_alloc.c.
References arena_poison_memory.
|
inlinestatic |
#include <arena_realloc_last.c>
Check if a given pointer corresponds to the most recent allocation.
This helper determines whether old_ptr is the last allocation made by the arena. It does so by comparing the pointer to the expected end of the buffer based on arena->offset and the known old_size.
This check is necessary before attempting an in-place reallocation, as only the last allocation can be safely resized in place.
| arena | Pointer to the arena. |
| old_ptr | Pointer to the memory block to check. |
| old_size | Size of the previously allocated memory block. |
true if old_ptr is the last allocation, false otherwise.Definition at line 201 of file arena_realloc_last.c.
|
inlinestatic |
#include <arena_realloc_last.c>
Perform a fallback reallocation by allocating new memory and copying data.
This internal function is used when arena_realloc_last() cannot perform an in-place reallocation (e.g., when the old pointer is not the last allocation).
It performs the following steps:
new_size bytes.min(old_size, new_size) bytes from the old block to the new block.This approach ensures consistent tracking of memory usage even when in-place reallocation is not possible.
| arena | Pointer to the arena from which to allocate new memory. |
| old_ptr | Pointer to the original memory block. |
| old_size | Size of the original allocation. |
| new_size | Desired size of the new allocation. |
NULL on failure.Definition at line 330 of file arena_realloc_last.c.
References arena_alloc(), ARENA_LOCK, arena_poison_memory, ARENA_UNLOCK, and update_realloc_stats().
|
inlinestatic |
#include <arena_realloc_last.c>
Attempt to resize the last allocation in place.
This internal function resizes the most recent allocation made by the arena, assuming that old_ptr points to the last allocated block.
If the new size extends beyond the current buffer capacity, it attempts to grow the arena using arena_grow(). If growth fails, the function reports an error and returns NULL.
If the new size is smaller than the original, the unused tail of the allocation is poisoned to help catch accidental use of stale memory in debug mode.
The function updates internal statistics and triggers the allocation hook with the "arena_realloc_last (in-place)" label to reflect the successful resize.
| arena | Pointer to the arena managing the memory. |
| old_ptr | Pointer to the memory block being reallocated. |
| old_size | Size of the current allocation in bytes. |
| new_size | Desired new size in bytes. |
old_ptr if the resize succeeded in place, or NULL on failure.Definition at line 284 of file arena_realloc_last.c.
References arena_grow(), arena_poison_memory, arena_report_error(), and update_realloc_stats().
|
inlinestatic |
#include <arena_realloc_last.c>
Update arena statistics after a successful reallocation.
This internal helper updates the arena's internal state to reflect a successful in-place reallocation. It performs the following:
offset to account for the new allocation size.arena_update_peak().This function is used exclusively during in-place reallocation, where the existing memory block remains valid and grows or shrinks.
| arena | Pointer to the arena being updated. |
| ptr | Pointer to the reallocated memory block. |
| new_size | New size of the allocation (in bytes). |
| old_size | Previous size of the allocation (in bytes). |
| label | Descriptive label for logging and tracking. |
Definition at line 236 of file arena_realloc_last.c.
References arena_update_peak().