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

Internal helpers for growing and shrinking memory buffers. More...

+ Collaboration diagram for Arena Resize Internals:

Functions

static bool arena_can_shrink (t_arena *arena, size_t new_size)
 Determine whether the arena is eligible for shrinking to a smaller size.
 
static size_t arena_grow_compute_new_size (t_arena *arena, size_t required_size)
 Compute the new size for the arena based on the growth policy.
 
static bool arena_grow_realloc_buffer (t_arena *arena, size_t new_size, size_t old_size)
 Reallocate the arena's buffer to a new size.
 
static bool arena_grow_validate (t_arena *arena, size_t required_size)
 Validate whether the arena is allowed to grow for a given request.
 
static void arena_record_growth (t_arena *arena, size_t old_size)
 Record the previous size of the arena in the growth history.
 
static bool arena_should_grow (const t_arena *arena)
 Check whether the arena is allowed to grow.
 
static bool arena_should_maybe_shrink (size_t used, size_t size)
 Decide whether the arena buffer should be shrunk based on usage ratio.
 
static bool arena_shrink_apply (t_arena *arena, size_t new_size)
 Apply the memory shrink operation to an arena's buffer.
 
static size_t arena_shrink_target (size_t used)
 Compute the target buffer size for a shrink operation.
 
static bool arena_shrink_validate (t_arena *arena, size_t new_size)
 Validate whether an arena can be safely shrunk to a new size.
 
static bool should_attempt_shrink (t_arena *arena)
 Determine whether the arena is eligible for dynamic shrinking.
 

Detailed Description

Internal helpers for growing and shrinking memory buffers.

Function Documentation

◆ arena_can_shrink()

static bool arena_can_shrink ( t_arena *  arena,
size_t  new_size 
)
inlinestatic

#include <arena_resize.c>

Determine whether the arena is eligible for shrinking to a smaller size.

This internal helper checks if the arena can be safely shrunk to new_size based on ownership, growth policy, and current memory usage.

Shrinking is allowed only if:

  • The arena is not NULL.
  • The arena owns its buffer and is allowed to grow/shrink.
  • The proposed new_size is not smaller than the current offset.
  • The ratio of new_size / current_size is less than or equal to the configured threshold (ARENA_MIN_SHRINK_RATIO), unless it matches the offset.

This function is used as a precondition check before applying memory reduction via arena_shrink().

Parameters
arenaPointer to the t_arena instance.
new_sizeProposed size for shrinking the buffer.
Returns
true if the arena is eligible to shrink, false otherwise.
Note
If new_size == arena->offset, shrinking is always allowed (tight fit).
See also
arena_shrink
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_resize.c.

Definition at line 640 of file arena_resize.c.

641{
642 if (!arena)
643 return false;
644
645 bool owns = atomic_load_explicit(&arena->owns_buffer, memory_order_acquire);
646 bool grow = atomic_load_explicit(&arena->can_grow, memory_order_acquire);
647
648 if (!owns || !grow)
649 return false;
650
651 if (new_size < arena->offset)
652 return false;
653
654 if (new_size == arena->offset)
655 return true;
656
657 double shrink_ratio = (double) new_size / (double) arena->size;
658 return shrink_ratio <= ARENA_MIN_SHRINK_RATIO;
659}
#define ARENA_MIN_SHRINK_RATIO
Shrink threshold: offset < ratio * size.

References ARENA_MIN_SHRINK_RATIO.

◆ arena_grow_compute_new_size()

static size_t arena_grow_compute_new_size ( t_arena *  arena,
size_t  required_size 
)
inlinestatic

#include <arena_resize.c>

Compute the new size for the arena based on the growth policy.

This internal function calculates an appropriate new buffer size that satisfies the required_size request, starting from the current size and offset. It uses the user-defined growth callback if set, or defaults to default_grow_cb otherwise.

The result is validated to ensure it is:

  • Large enough to satisfy the requested offset + size.
  • Not exceeding SIZE_MAX.

If the computed size is invalid (too small or overflowing), the function returns 0 to indicate failure.

Parameters
arenaPointer to the arena requesting growth.
required_sizeThe number of additional bytes needed.
Returns
The computed new size in bytes, or 0 if the value is invalid.
See also
arena_grow
arena_grow_validate
arena_grow_callback
default_grow_cb
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_resize.c.

Definition at line 553 of file arena_resize.c.

554{
555 arena_grow_callback cb = arena->grow_cb ? arena->grow_cb : default_grow_cb;
556 size_t requested = arena->offset + required_size;
557 size_t new_size = cb(arena->size, required_size);
558
559 if (new_size < requested)
560 return 0;
561 if (new_size > SIZE_MAX)
562 return 0;
563
564 return new_size;
565}
size_t(* arena_grow_callback)(size_t current_size, size_t requested_size)
Definition arena.h:79
size_t default_grow_cb(size_t current_size, size_t requested_size)
Default growth policy callback for arena resizing.

References default_grow_cb().

◆ arena_grow_realloc_buffer()

static bool arena_grow_realloc_buffer ( t_arena *  arena,
size_t  new_size,
size_t  old_size 
)
inlinestatic

#include <arena_resize.c>

Reallocate the arena's buffer to a new size.

This internal function performs the actual realloc() of the arena's buffer during a grow operation. It attempts to allocate a new buffer of new_size bytes, and if successful:

  • Updates the buffer pointer and size.
  • Increments the reallocation counter.
  • Records the previous size in the arena's growth history.
  • Emits a debug log message indicating the resize.

If reallocation fails, an error is reported and false is returned.

Parameters
arenaPointer to the arena being resized.
new_sizeTarget buffer size after reallocation (in bytes).
old_sizeOriginal size of the buffer before reallocation.
Returns
true if reallocation succeeded, false otherwise.
See also
arena_grow
arena_record_growth
realloc
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_resize.c.

Definition at line 594 of file arena_resize.c.

595{
596 void* new_buf = realloc(arena->buffer, new_size);
597 if (!new_buf)
598 return arena_report_error(arena, "arena_grow failed: realloc failed"), false;
599
600 arena->buffer = (uint8_t*) new_buf;
601 arena->size = new_size;
602 arena->stats.reallocations++;
603
604 arena_record_growth(arena, old_size);
605
606 ALOG("[arena_grow] Arena %p grown from %zu to %zu bytes\n", (void*) arena, old_size, new_size);
607 return true;
608}
#define ALOG(...)
Debug logging macro (enabled only if ARENA_DEBUG_LOG is set).
void arena_report_error(t_arena *arena, const char *fmt,...)
Report an error from an arena using its configured error callback.
static void arena_record_growth(t_arena *arena, size_t old_size)
Record the previous size of the arena in the growth history.

References ALOG, arena_record_growth(), and arena_report_error().

◆ arena_grow_validate()

static bool arena_grow_validate ( t_arena *  arena,
size_t  required_size 
)
inlinestatic

#include <arena_resize.c>

Validate whether the arena is allowed to grow for a given request.

This internal helper checks if the arena meets the conditions necessary to perform a growth operation. It verifies:

  • That the arena is marked as growable (can_grow == true).
  • That the arena owns its buffer (owns_buffer == true).
  • That the requested growth does not overflow size_t when added to the current offset.

If any condition fails, an error is reported using arena_report_error() and the function returns false.

Parameters
arenaPointer to the arena to validate.
required_sizeThe number of additional bytes needed.
Returns
true if the arena is eligible for growth, false otherwise.
Note
This function is called from within a section where the arena is already locked for thread safety.
See also
arena_grow
arena_should_grow
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_resize.c.

Definition at line 513 of file arena_resize.c.

514{
515 if (!arena_should_grow(arena))
516 return arena_report_error(arena, "arena_grow failed: growth not allowed"), false;
517
518 if (required_size > SIZE_MAX - arena->offset)
519 return arena_report_error(arena, "arena_grow failed: size overflow"), false;
520
521 return true;
522}
static bool arena_should_grow(const t_arena *arena)
Check whether the arena is allowed to grow.

References arena_report_error(), and arena_should_grow().

◆ arena_record_growth()

static void arena_record_growth ( t_arena *  arena,
size_t  old_size 
)
inlinestatic

#include <arena_resize.c>

Record the previous size of the arena in the growth history.

This internal helper appends the old_size of the arena to its growth_history array. This array keeps track of all prior sizes before each successful call to arena_grow(), allowing for optional introspection, debugging, or visualization of growth patterns.

It reallocates the history buffer with enough room for the new entry. If realloc fails, the history is not updated, but the function fails silently to avoid interrupting allocation logic.

Parameters
arenaPointer to the arena whose growth is being tracked.
old_sizeThe size of the arena before a successful growth.
Note
This function is called from within a section that already holds the arena lock.
See also
arena_grow
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_resize.c.

Definition at line 473 of file arena_resize.c.

474{
475 size_t* temp = realloc(arena->stats.growth_history, sizeof(size_t) * (arena->stats.growth_history_count + 1));
476 if (temp)
477 {
478 arena->stats.growth_history = temp;
479 arena->stats.growth_history[arena->stats.growth_history_count++] = old_size;
480 }
481}

◆ arena_should_grow()

static bool arena_should_grow ( const t_arena *  arena)
inlinestatic

#include <arena_resize.c>

Check whether the arena is allowed to grow.

This internal helper verifies if the arena is configured to support dynamic growth. It performs the following checks:

  • The arena must own its memory buffer (owns_buffer == true).
  • The arena must have growth enabled (can_grow == true).

If both conditions are met, the arena can attempt to grow when additional memory is needed.

Parameters
arenaPointer to the t_arena structure.
Returns
true if the arena can grow, false otherwise.
Note
This function assumes arena is non-NULL. It is called by a function that validates the pointer before calling.
See also
arena_grow
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_resize.c.

Definition at line 441 of file arena_resize.c.

442{
443 bool owns = atomic_load_explicit(&arena->owns_buffer, memory_order_acquire);
444 bool grow = atomic_load_explicit(&arena->can_grow, memory_order_acquire);
445 return owns && grow;
446}

◆ arena_should_maybe_shrink()

static bool arena_should_maybe_shrink ( size_t  used,
size_t  size 
)
inlinestatic

#include <arena_resize.c>

Decide whether the arena buffer should be shrunk based on usage ratio.

This helper compares the ratio of used bytes to total allocated buffer size. If the usage ratio falls below ARENA_MIN_SHRINK_RATIO, the arena is considered underutilized and may be eligible for shrinking.

Typical usage scenarios include:

  • Periodic memory trimming in long-running programs.
  • Adaptive memory management after large but short-lived allocations.
Parameters
usedNumber of bytes currently used in the arena.
sizeTotal capacity of the arena buffer.
Returns
true if the usage is low enough to consider shrinking, false otherwise.
Note
If size is 0, this function returns false to avoid division-by-zero.
See also
arena_might_shrink
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_resize.c.

Definition at line 788 of file arena_resize.c.

789{
790 if (size == 0)
791 return false;
792
793 double ratio = (double) used / (double) size;
794 return ratio < ARENA_MIN_SHRINK_RATIO;
795}
size_t used
Number of bytes used in the arena buffer.
Definition arena_io.h:2

References ARENA_MIN_SHRINK_RATIO, and used.

◆ arena_shrink_apply()

static bool arena_shrink_apply ( t_arena *  arena,
size_t  new_size 
)
inlinestatic

#include <arena_resize.c>

Apply the memory shrink operation to an arena's buffer.

This internal function performs the actual memory reallocation to reduce the arena's backing buffer to new_size bytes.

On success:

  • The arena's buffer pointer and size field are updated.
  • The shrinks counter in arena statistics is incremented.
  • A debug log message is printed.

If realloc() fails, the arena remains unchanged and the function returns false.

Parameters
arenaPointer to the arena to shrink.
new_sizeNew desired size of the arena buffer (in bytes).
Returns
true on successful shrink, false on failure.
Note
This function is only called after validating the shrink operation.
See also
arena_can_shrink
arena_shrink
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_resize.c.

Definition at line 720 of file arena_resize.c.

721{
722 void* new_buf = realloc(arena->buffer, new_size);
723 if (!new_buf)
724 return false;
725
726 arena->buffer = (uint8_t*) new_buf;
727 arena->size = new_size;
728 arena->stats.shrinks++;
729
730 ALOG("[arena_shrink] Arena %p shrunk to %zu bytes\n", (void*) arena, new_size);
731 return true;
732}

References ALOG.

◆ arena_shrink_target()

static size_t arena_shrink_target ( size_t  used)
inlinestatic

#include <arena_resize.c>

Compute the target buffer size for a shrink operation.

This helper calculates a reasonable new size for the arena buffer when shrinking is considered. It adds a small safety margin (ARENA_SHRINK_PADDING) to the current usage (used) to avoid immediate re-growth if a small allocation occurs after shrinking.

The padding ensures that the buffer remains slightly larger than strictly needed, providing better performance and avoiding fragmentation.

Parameters
usedNumber of bytes currently used in the arena.
Returns
Suggested new size for the buffer after shrinking.
See also
arena_might_shrink
arena_shrink
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_resize.c.

Definition at line 819 of file arena_resize.c.

820{
821 return used + ARENA_SHRINK_PADDING;
822}
#define ARENA_SHRINK_PADDING
Extra padding when computing shrink sizes.

References ARENA_SHRINK_PADDING, and used.

◆ arena_shrink_validate()

static bool arena_shrink_validate ( t_arena *  arena,
size_t  new_size 
)
inlinestatic

#include <arena_resize.c>

Validate whether an arena can be safely shrunk to a new size.

This internal helper wraps the logic of arena_can_shrink() with an initial null-check on the arena itself. It is used to guard shrink operations such as arena_shrink() and arena_might_shrink().

Parameters
arenaPointer to the arena to validate.
new_sizeProposed size to shrink the arena to (in bytes).
Returns
true if the arena is not NULL and is allowed to shrink, false otherwise.
See also
arena_can_shrink
arena_shrink
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_resize.c.

Definition at line 681 of file arena_resize.c.

682{
683 if (!arena)
684 return false;
685
686 if (!arena_can_shrink(arena, new_size))
687 return false;
688
689 return true;
690}
static bool arena_can_shrink(t_arena *arena, size_t new_size)
Determine whether the arena is eligible for shrinking to a smaller size.

References arena_can_shrink().

◆ should_attempt_shrink()

static bool should_attempt_shrink ( t_arena *  arena)
inlinestatic

#include <arena_resize.c>

Determine whether the arena is eligible for dynamic shrinking.

This helper checks if the arena is in a state that allows shrinking. Shrinking is only permitted if:

  • The arena pointer is not NULL.
  • The arena has dynamic growth enabled (can_grow is true).

This function is typically used before triggering automatic shrinking logic, such as in arena_might_shrink(), to avoid unnecessary operations.

Parameters
arenaPointer to the arena to check.
Returns
true if the arena can potentially be shrunk, false otherwise.
See also
arena_might_shrink
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_resize.c.

Definition at line 755 of file arena_resize.c.

756{
757 if (!arena)
758 return false;
759
760 return atomic_load_explicit(&arena->can_grow, memory_order_acquire);
761}