|
memory_arena
|
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. | |
Internal helpers for growing and shrinking memory buffers.
|
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:
NULL.new_size is not smaller than the current offset.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().
| arena | Pointer to the t_arena instance. |
| new_size | Proposed size for shrinking the buffer. |
true if the arena is eligible to shrink, false otherwise.new_size == arena->offset, shrinking is always allowed (tight fit).Definition at line 640 of file arena_resize.c.
References ARENA_MIN_SHRINK_RATIO.
|
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:
SIZE_MAX.If the computed size is invalid (too small or overflowing), the function returns 0 to indicate failure.
| arena | Pointer to the arena requesting growth. |
| required_size | The number of additional bytes needed. |
0 if the value is invalid.Definition at line 553 of file arena_resize.c.
References default_grow_cb().
|
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:
If reallocation fails, an error is reported and false is returned.
| arena | Pointer to the arena being resized. |
| new_size | Target buffer size after reallocation (in bytes). |
| old_size | Original size of the buffer before reallocation. |
true if reallocation succeeded, false otherwise.Definition at line 594 of file arena_resize.c.
References ALOG, arena_record_growth(), and arena_report_error().
|
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:
can_grow == true).owns_buffer == true).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.
| arena | Pointer to the arena to validate. |
| required_size | The number of additional bytes needed. |
true if the arena is eligible for growth, false otherwise.Definition at line 513 of file arena_resize.c.
References arena_report_error(), and arena_should_grow().
|
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.
| arena | Pointer to the arena whose growth is being tracked. |
| old_size | The size of the arena before a successful growth. |
Definition at line 473 of file arena_resize.c.
|
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:
owns_buffer == true).can_grow == true).If both conditions are met, the arena can attempt to grow when additional memory is needed.
| arena | Pointer to the t_arena structure. |
true if the arena can grow, false otherwise.arena is non-NULL. It is called by a function that validates the pointer before calling.Definition at line 441 of file arena_resize.c.
|
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:
| used | Number of bytes currently used in the arena. |
| size | Total capacity of the arena buffer. |
true if the usage is low enough to consider shrinking, false otherwise.size is 0, this function returns false to avoid division-by-zero.Definition at line 788 of file arena_resize.c.
References ARENA_MIN_SHRINK_RATIO, and used.
|
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:
buffer pointer and size field are updated.shrinks counter in arena statistics is incremented.If realloc() fails, the arena remains unchanged and the function returns false.
| arena | Pointer to the arena to shrink. |
| new_size | New desired size of the arena buffer (in bytes). |
true on successful shrink, false on failure.Definition at line 720 of file arena_resize.c.
References ALOG.
|
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.
| used | Number of bytes currently used in the arena. |
Definition at line 819 of file arena_resize.c.
References ARENA_SHRINK_PADDING, and used.
|
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().
| arena | Pointer to the arena to validate. |
| new_size | Proposed size to shrink the arena to (in bytes). |
true if the arena is not NULL and is allowed to shrink, false otherwise.Definition at line 681 of file arena_resize.c.
References arena_can_shrink().
|
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:
arena pointer is not NULL.can_grow is true).This function is typically used before triggering automatic shrinking logic, such as in arena_might_shrink(), to avoid unnecessary operations.
| arena | Pointer to the arena to check. |
true if the arena can potentially be shrunk, false otherwise.Definition at line 755 of file arena_resize.c.