memory_arena
Loading...
Searching...
No Matches
Functions
Arena Allocation Internals

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.
 

Detailed Description

Internal helpers for memory allocation logic.

Function Documentation

◆ arena_alloc_internal()

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:

  • Alignment-aware allocation.
  • Out-of-bounds detection and safe growth if enabled.
  • Labeling for diagnostics and debugging.
  • Allocation hooks for instrumentation.

The flow is:

  1. Validate input arguments (alignment, size, etc.).
  2. Acquire the arena lock and perform consistency checks.
  3. Ensure the arena is not being destroyed.
  4. Check for arithmetic overflow.
  5. Calculate aligned offset and check capacity.
  6. Optionally grow the arena.
  7. Commit the allocation (update offset and stats).
  8. Zero the memory if requested via label.
  9. Trigger the allocation hook if registered.
  10. Return a pointer to the allocated memory.
Parameters
arenaThe arena from which to allocate.
sizeThe number of bytes to allocate.
alignmentThe alignment in bytes (must be power of two).
labelA descriptive label for logging and debugging.
Returns
A pointer to the allocated memory, or NULL on failure.
Note
Use public wrappers like arena_alloc() or arena_alloc_aligned() for convenience. This function is not meant to be called directly by users.
See also
arena_alloc
arena_alloc_aligned
arena_alloc_labeled

Definition at line 721 of file arena_alloc.c.

722{
723 if (!arena_alloc_validate_input(arena, size, alignment, label))
724 return NULL;
725
726 ARENA_LOCK(arena);
727 ARENA_CHECK(arena);
728
729 if (arena_is_being_destroyed(arena, label))
730 {
731 ARENA_UNLOCK(arena);
732 return NULL;
733 }
734
735 if (arena_check_overflow(arena, size, label))
736 {
737 ARENA_UNLOCK(arena);
738 return NULL;
739 }
740
741 size_t aligned_offset = 0;
742 size_t wasted = 0;
743 if (!arena_ensure_capacity(arena, size, alignment, label, &aligned_offset, &wasted))
744 {
745 arena->stats.failed_allocations++;
746 arena_report_error(arena, "%s failed: out of memory (requested: %zu)", label, size);
747 ARENA_UNLOCK(arena);
748 return NULL;
749 }
750
751 arena_commit_allocation(arena, size, wasted, aligned_offset);
752 void* result = arena->buffer + aligned_offset;
753 arena_zero_if_needed(result, size, label);
754 arena_invoke_allocation_hook(arena, result, size, aligned_offset, wasted, label);
755
756 ALOG("[arena] %s: Allocated %zu bytes @ offset %zu (arena %p)\n", label, size, aligned_offset, (void*) arena);
757
758 ARENA_CHECK(arena);
759 ARENA_UNLOCK(arena);
760 return result;
761}
#define ALOG(...)
Debug logging macro (enabled only if ARENA_DEBUG_LOG is set).
#define ARENA_CHECK(arena)
#define ARENA_UNLOCK(arena)
#define ARENA_LOCK(arena)
Acquire the mutex lock if thread safety is enabled.
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 bool arena_is_being_destroyed(t_arena *arena, const char *label)
Check whether the arena is currently being destroyed.
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_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 void arena_commit_allocation(t_arena *arena, size_t size, size_t wasted, size_t aligned_offset)
Update offset, peak usage, and statistics.
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 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.
void arena_report_error(t_arena *arena, const char *fmt,...)
Report an error from an arena using its configured error callback.

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().

◆ arena_alloc_validate_input()

static bool arena_alloc_validate_input ( t_arena *  arena,
size_t  size,
size_t  alignment,
const char *  label 
)
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:

  • The arena pointer is not NULL.
  • The requested size is greater than zero.
  • The 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.

Parameters
arenaPointer to the t_arena to validate.
sizeSize of the memory to allocate, in bytes.
alignmentRequested alignment of the memory block (must be power-of-two).
labelName of the calling function, used in error messages.
Returns
true if all inputs are valid, false otherwise.
See also
arena_alloc_internal
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_alloc.c.

Definition at line 311 of file arena_alloc.c.

312{
313 if (!arena)
314 {
315 arena_report_error(NULL, "%s failed: NULL arena", label);
316 return false;
317 }
318 if (size == 0)
319 {
320 arena_report_error(arena, "%s failed: zero-size allocation", label);
321 return false;
322 }
323 if (alignment == 0 || (alignment & (alignment - 1)) != 0)
324 {
325 arena_report_error(arena, "%s failed: alignment (%zu) is not a power-of-two", label, alignment);
326 return false;
327 }
328 return true;
329}

References arena_report_error().

◆ arena_calc_aligned_offset()

static size_t arena_calc_aligned_offset ( t_arena *  arena,
size_t  alignment 
)
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.

Parameters
arenaPointer to the arena structure.
alignmentThe desired alignment (must be a power of two).
Returns
The aligned offset (relative to the buffer base).
See also
align_up
arena_ensure_capacity
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_alloc.c.

Definition at line 422 of file arena_alloc.c.

423{
424 size_t curr = (size_t) (arena->buffer + arena->offset);
425 size_t aligned = align_up(curr, alignment);
426 return aligned - (size_t) arena->buffer;
427}
static size_t align_up(size_t value, size_t alignment)
Align a value upwards to the nearest multiple of alignment.
Definition arena_math.h:52

References align_up().

◆ arena_check_overflow()

static bool arena_check_overflow ( t_arena *  arena,
size_t  size,
const char *  label 
)
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:

  • Increments the failed_allocations counter in the arena stats.
  • Logs an error message with the given label.
  • Returns true to indicate failure.
Parameters
arenaPointer to the arena being used for allocation.
sizeThe requested allocation size in bytes.
labelLabel or name of the calling function for error context.
Returns
true if an overflow would occur, false otherwise.
See also
arena_alloc_internal
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_alloc.c.

Definition at line 388 of file arena_alloc.c.

389{
390 if (size > SIZE_MAX - arena->offset)
391 {
392 arena->stats.failed_allocations++;
393 arena_report_error(arena, "%s failed: size overflow (requested: %zu)", label, size);
394 return true;
395 }
396 return false;
397}

References arena_report_error().

◆ arena_commit_allocation()

static void arena_commit_allocation ( t_arena *  arena,
size_t  size,
size_t  wasted,
size_t  aligned_offset 
)
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().

Parameters
arenaPointer to the arena being updated.
sizeSize in bytes of the allocation.
wastedNumber of alignment bytes wasted.
aligned_offsetOffset where the allocation begins in the buffer.
Note
This function assumes that allocation boundaries and capacity have already been validated via arena_ensure_capacity().
See also
arena_alloc_internal
arena_update_stats
arena_update_peak
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_alloc.c.

Definition at line 593 of file arena_alloc.c.

594{
595 arena->offset = aligned_offset + size;
596 arena_update_peak(arena);
597 arena_update_stats(arena, size, wasted);
598}
static void arena_update_stats(t_arena *arena, size_t size, size_t wasted)
Update allocation statistics after a successful arena allocation.
void arena_update_peak(t_arena *arena)
Update the arena’s peak memory usage if the current usage exceeds it.

References arena_update_peak(), and arena_update_stats().

◆ arena_ensure_capacity()

static bool arena_ensure_capacity ( t_arena *  arena,
size_t  size,
size_t  alignment,
const char *  label,
size_t *  aligned_offset,
size_t *  wasted 
)
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.

Parameters
arenaPointer to the arena being used.
sizeSize of the requested allocation (in bytes).
alignmentRequired alignment (must be power-of-two).
labelLabel for logging and error reporting.
aligned_offsetOutput: aligned offset where the allocation would start.
wastedOutput: number of bytes wasted due to alignment padding.
Returns
true if capacity is sufficient, false otherwise.
See also
arena_calc_aligned_offset
arena_try_grow
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_alloc.c.

Definition at line 503 of file arena_alloc.c.

505{
506 *aligned_offset = arena_calc_aligned_offset(arena, alignment);
507 *wasted = *aligned_offset - arena->offset;
508
509 if (*aligned_offset + size <= arena->size)
510 return true;
511
512 if (!arena_try_grow(arena, size, label))
513 return false;
514
515 *aligned_offset = arena_calc_aligned_offset(arena, alignment);
516 *wasted = *aligned_offset - arena->offset;
517
518 return *aligned_offset + size <= arena->size;
519}
static size_t arena_calc_aligned_offset(t_arena *arena, size_t alignment)
Compute the next aligned offset within the arena.
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.

References arena_calc_aligned_offset(), and arena_try_grow().

◆ arena_invoke_allocation_hook()

static void arena_invoke_allocation_hook ( t_arena *  arena,
void *  ptr,
size_t  size,
size_t  offset,
size_t  wasted,
const char *  label 
)
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:

  • Arena pointer
  • Allocation ID
  • Pointer to the allocated memory
  • Size of the allocation
  • Offset in the arena buffer
  • Wasted bytes due to alignment
  • Allocation label

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).

Parameters
arenaPointer to the arena that performed the allocation.
ptrPointer to the allocated memory block.
sizeNumber of bytes allocated.
offsetOffset in the arena buffer where the allocation began.
wastedNumber of wasted bytes due to alignment padding.
labelLabel associated with this allocation.
Note
If no hook is set, this function does nothing.
See also
arena_set_allocation_hook
arena_alloc_internal
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_alloc.c.

Definition at line 672 of file arena_alloc.c.

674{
675 arena_allocation_hook hook = atomic_load_explicit(&arena->hooks.hook_cb, memory_order_acquire);
676 if (hook)
677 hook(arena, (int) arena->stats.alloc_id_counter, ptr, size, offset, wasted, label);
678}
void(* arena_allocation_hook)(t_arena *arena, int alloc_id, void *ptr, size_t size, size_t offset, size_t wasted, const char *label)
Allocation hook callback type.
Definition arena_hooks.h:58

◆ arena_is_being_destroyed()

static bool arena_is_being_destroyed ( t_arena *  arena,
const char *  label 
)
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.

Parameters
arenaPointer to the arena being queried.
labelName of the calling function, used for contextual error messages.
Returns
true if the arena is being destroyed, false otherwise.
See also
arena_destroy
arena_alloc_internal
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_alloc.c.

Definition at line 356 of file arena_alloc.c.

357{
358 (void)label;
359 if (atomic_load_explicit(&arena->is_destroying, memory_order_acquire))
360 return true;
361 return false;
362}

◆ arena_realloc_validate()

static bool arena_realloc_validate ( t_arena *  arena,
void *  old_ptr,
size_t  new_size 
)
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:

  • The arena pointer is not NULL.
  • The old_ptr is not NULL.
  • The new_size is greater than zero.

If any check fails, a descriptive error is reported using arena_report_error().

Parameters
arenaPointer to the arena where the reallocation is performed.
old_ptrPointer to the previously allocated memory.
new_sizeNew size requested for the reallocation (in bytes).
Returns
true if inputs are valid, false otherwise.
See also
arena_realloc_last
arena_report_error
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_realloc_last.c.

Definition at line 167 of file arena_realloc_last.c.

168{
169 if (!arena)
170 return arena_report_error(NULL, "arena_realloc_last failed: NULL arena"), false;
171 if (!old_ptr)
172 return arena_report_error(arena, "arena_realloc_last failed: NULL old_ptr"), false;
173 if (new_size == 0)
174 return arena_report_error(arena, "arena_realloc_last failed: zero-size reallocation"), false;
175 return true;
176}

References arena_report_error().

◆ arena_try_grow()

static bool arena_try_grow ( t_arena *  arena,
size_t  size,
const char *  label 
)
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.

Parameters
arenaPointer to the arena to grow.
sizeMinimum number of bytes required to satisfy the next allocation.
labelAllocation label used for context in error messages.
Returns
true if growth succeeded or was unnecessary, false otherwise.
See also
arena_grow
arena_ensure_capacity
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_alloc.c.

Definition at line 454 of file arena_alloc.c.

455{
456 if (!arena->can_grow)
457 {
458 arena_report_error(arena, "%s failed: cannot grow", label);
459 return false;
460 }
461 if (!arena_grow(arena, size))
462 {
463 arena_report_error(arena, "%s failed: growth failed", label);
464 return false;
465 }
466 return true;
467}
bool arena_grow(t_arena *arena, size_t required_size)

References arena_grow(), and arena_report_error().

◆ arena_update_stats()

static void arena_update_stats ( t_arena *  arena,
size_t  size,
size_t  wasted 
)
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:

  • Total number of allocations (allocations)
  • Current number of live allocations (live_allocations)
  • Total number of bytes allocated (bytes_allocated)
  • Total number of wasted bytes due to alignment (wasted_alignment_bytes)
  • Last allocation size (last_alloc_size)
  • Offset of the last allocation (last_alloc_offset)
  • Allocation ID counter (alloc_id_counter)

The stats are updated inside a critical section to ensure thread safety when ARENA_ENABLE_THREAD_SAFE is enabled.

Parameters
arenaPointer to the arena whose statistics are to be updated.
sizeNumber of bytes allocated.
wastedNumber of bytes wasted due to alignment padding.
Note
This function performs locking internally (via ARENA_LOCK / ARENA_UNLOCK), so it should not be called within an existing locked section to avoid deadlocks.
See also
arena_commit_allocation
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_alloc.c.

Definition at line 551 of file arena_alloc.c.

552{
553 if (!arena)
554 return;
555
556 ARENA_LOCK(arena);
557 arena->stats.allocations++;
558 arena->stats.live_allocations++;
559 arena->stats.bytes_allocated += size;
560 arena->stats.wasted_alignment_bytes += wasted;
561 arena->stats.alloc_id_counter++;
562 arena->stats.last_alloc_size = size;
563 arena->stats.last_alloc_offset = arena->offset - size;
564 ARENA_UNLOCK(arena);
565}

References ARENA_LOCK, and ARENA_UNLOCK.

◆ arena_zero_if_needed()

static void arena_zero_if_needed ( void *  ptr,
size_t  size,
const char *  label 
)
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.

Parameters
ptrPointer to the memory block to initialize.
sizeNumber of bytes to initialize.
labelAllocation label string used to determine behavior.
Note
The label comparison is string-based; other labels will trigger poisoning in debug mode.
See also
arena_alloc_internal
arena_poison_memory
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_alloc.c.

Definition at line 628 of file arena_alloc.c.

629{
630 if (label && strcmp(label, "arena_calloc_zero") == 0)
631 memset(ptr, 0, size);
632 else
633 arena_poison_memory(ptr, size);
634}
#define arena_poison_memory(ptr, size)
No-op poison macro when poisoning is disabled.

References arena_poison_memory.

◆ is_last_allocation()

static bool is_last_allocation ( t_arena *  arena,
void *  old_ptr,
size_t  old_size 
)
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.

Parameters
arenaPointer to the arena.
old_ptrPointer to the memory block to check.
old_sizeSize of the previously allocated memory block.
Returns
true if old_ptr is the last allocation, false otherwise.
See also
arena_realloc_last
realloc_in_place
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_realloc_last.c.

Definition at line 201 of file arena_realloc_last.c.

202{
203 uint8_t* expected = arena->buffer + (arena->offset - old_size);
204 return (uint8_t*) old_ptr == expected;
205}

◆ realloc_fallback()

static void * realloc_fallback ( t_arena *  arena,
void *  old_ptr,
size_t  old_size,
size_t  new_size 
)
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:

  • Allocates a new memory block of new_size bytes.
  • Copies min(old_size, new_size) bytes from the old block to the new block.
  • Poisons the old memory region for debugging purposes.
  • Updates internal reallocation statistics and invokes the allocation hook, if set.

This approach ensures consistent tracking of memory usage even when in-place reallocation is not possible.

Parameters
arenaPointer to the arena from which to allocate new memory.
old_ptrPointer to the original memory block.
old_sizeSize of the original allocation.
new_sizeDesired size of the new allocation.
Returns
Pointer to the newly allocated memory block, or NULL on failure.
See also
arena_realloc_last
realloc_in_place
update_realloc_stats
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_realloc_last.c.

Definition at line 330 of file arena_realloc_last.c.

331{
332 void* new_ptr = arena_alloc(arena, new_size);
333 if (!new_ptr)
334 return NULL;
335
336 memcpy(new_ptr, old_ptr, old_size < new_size ? old_size : new_size);
337
338 ARENA_LOCK(arena);
339 arena_poison_memory(old_ptr, old_size);
340 ARENA_UNLOCK(arena);
341
342 update_realloc_stats(arena, new_ptr, new_size, old_size, "arena_realloc_last (fallback)");
343 return new_ptr;
344}
void * arena_alloc(t_arena *arena, size_t size)
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.

References arena_alloc(), ARENA_LOCK, arena_poison_memory, ARENA_UNLOCK, and update_realloc_stats().

◆ realloc_in_place()

static void * realloc_in_place ( t_arena *  arena,
void *  old_ptr,
size_t  old_size,
size_t  new_size 
)
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.

Parameters
arenaPointer to the arena managing the memory.
old_ptrPointer to the memory block being reallocated.
old_sizeSize of the current allocation in bytes.
new_sizeDesired new size in bytes.
Returns
old_ptr if the resize succeeded in place, or NULL on failure.
See also
arena_grow
update_realloc_stats
arena_realloc_last
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_realloc_last.c.

Definition at line 284 of file arena_realloc_last.c.

285{
286 size_t new_end = arena->offset - old_size + new_size;
287
288 if (new_end > arena->size && !arena_grow(arena, new_size - old_size))
289 return arena_report_error(arena, "arena_realloc_last failed: growth failed (needed %zu bytes)",
290 new_size - old_size),
291 NULL;
292
293 if (new_size < old_size)
294 arena_poison_memory((uint8_t*) old_ptr + new_size, old_size - new_size);
295
296 update_realloc_stats(arena, old_ptr, new_size, old_size, "arena_realloc_last (in-place)");
297 return old_ptr;
298}

References arena_grow(), arena_poison_memory, arena_report_error(), and update_realloc_stats().

◆ update_realloc_stats()

static void update_realloc_stats ( t_arena *  arena,
void *  ptr,
size_t  new_size,
size_t  old_size,
const char *  label 
)
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:

  • Adjusts the offset to account for the new allocation size.
  • Updates peak usage via arena_update_peak().
  • Increments allocation-related counters.
  • Records metadata about the last allocation (size, offset).
  • Triggers the allocation hook if one is registered.

This function is used exclusively during in-place reallocation, where the existing memory block remains valid and grows or shrinks.

Parameters
arenaPointer to the arena being updated.
ptrPointer to the reallocated memory block.
new_sizeNew size of the allocation (in bytes).
old_sizePrevious size of the allocation (in bytes).
labelDescriptive label for logging and tracking.
See also
realloc_in_place
realloc_fallback
arena_realloc_last
arena_update_peak
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_realloc_last.c.

Definition at line 236 of file arena_realloc_last.c.

237{
238 arena->offset = (uint8_t*) ptr + new_size - arena->buffer;
239 arena_update_peak(arena);
240
241 arena->stats.reallocations++;
242 arena->stats.live_allocations++;
243 arena->stats.bytes_allocated += (new_size - old_size);
244 arena->stats.last_alloc_size = new_size;
245 arena->stats.last_alloc_offset = (size_t) ((uint8_t*) ptr - arena->buffer);
246 arena->stats.alloc_id_counter++;
247
248 if (arena->hooks.hook_cb)
249 arena->hooks.hook_cb(arena, (int) arena->stats.alloc_id_counter, ptr, new_size, arena->stats.last_alloc_offset,
250 0, label);
251}

References arena_update_peak().