memory_arena
Loading...
Searching...
No Matches
arena_resize.c
Go to the documentation of this file.
1
34#include "arena.h"
35#include <math.h>
36
37/*
38 * INTERNAL HELPERS DEFINITIONS
39 */
40
41static inline bool arena_should_grow(const t_arena* arena);
42static inline void arena_record_growth(t_arena* arena, size_t old_size);
43static inline bool arena_grow_validate(t_arena* arena, size_t required_size);
44static inline size_t arena_grow_compute_new_size(t_arena* arena, size_t required_size);
45static inline bool arena_grow_realloc_buffer(t_arena* arena, size_t new_size, size_t old_size);
46
47static inline bool arena_can_shrink(t_arena* arena, size_t new_size);
48static inline bool arena_shrink_validate(t_arena* arena, size_t new_size);
49static inline bool arena_shrink_apply(t_arena* arena, size_t new_size);
50
51static inline bool should_attempt_shrink(t_arena* arena);
52static inline bool arena_should_maybe_shrink(size_t used, size_t size);
53static inline size_t arena_shrink_target(size_t used);
54
55/*
56 * PUBLIC API
57 */
58
142bool arena_grow(t_arena* arena, size_t required_size)
143{
144 if (!arena)
145 return false;
146
147 if (required_size == 0)
148 return true;
149
150 ARENA_LOCK(arena);
151
152 if (!arena_grow_validate(arena, required_size))
153 {
154 ARENA_UNLOCK(arena);
155 return false;
156 }
157
158 size_t old_size = arena->size;
159 size_t new_size = arena_grow_compute_new_size(arena, required_size);
160 if (new_size == 0)
161 {
162 arena_report_error(arena, "arena_grow failed: computed size invalid");
163 ARENA_UNLOCK(arena);
164 return false;
165 }
166
167 if (new_size > (size_t)(1ULL << 32)) // 4GiB
168 {
169 arena_report_error(arena, "arena_grow rejected size: %zu", new_size);
170 return false;
171 }
172 bool success = arena_grow_realloc_buffer(arena, new_size, old_size);
173 ARENA_UNLOCK(arena);
174 return success;
175}
176
267void arena_shrink(t_arena* arena, size_t new_size)
268{
269 if (!arena)
270 return;
271
272 ARENA_LOCK(arena);
273 ARENA_CHECK(arena);
274
275 bool ok = arena_shrink_validate(arena, new_size) && arena_shrink_apply(arena, new_size);
276
277 (void) ok;
278 ARENA_UNLOCK(arena);
279}
280
386{
387 if (!should_attempt_shrink(arena))
388 return false;
389
390 ARENA_LOCK(arena);
391 ARENA_CHECK(arena);
392
393 const size_t used = arena->offset;
394 const size_t size = arena->size;
395
397 {
398 const size_t target = arena_shrink_target(used);
399
400 if (target < size)
401 {
402 arena_shrink(arena, target);
403 ARENA_UNLOCK(arena);
404 return true;
405 }
406 }
407
408 ARENA_UNLOCK(arena);
409 return false;
410}
411
412/*
413 * INTERNAL HELPERS IMPLEMENTATION
414 */
415
441static inline bool arena_should_grow(const t_arena* arena)
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}
447
473static inline void arena_record_growth(t_arena* arena, size_t old_size)
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}
482
513static inline bool arena_grow_validate(t_arena* arena, size_t required_size)
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}
523
553static inline size_t arena_grow_compute_new_size(t_arena* arena, size_t required_size)
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}
566
594static inline bool arena_grow_realloc_buffer(t_arena* arena, size_t new_size, size_t old_size)
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}
609
640static inline bool arena_can_shrink(t_arena* arena, size_t new_size)
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}
660
681static inline bool arena_shrink_validate(t_arena* arena, size_t new_size)
682{
683 if (!arena)
684 return false;
685
686 if (!arena_can_shrink(arena, new_size))
687 return false;
688
689 return true;
690}
691
720static inline bool arena_shrink_apply(t_arena* arena, size_t new_size)
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}
733
755static inline bool should_attempt_shrink(t_arena* arena)
756{
757 if (!arena)
758 return false;
759
760 return atomic_load_explicit(&arena->can_grow, memory_order_acquire);
761}
762
788static inline bool arena_should_maybe_shrink(size_t used, size_t size)
789{
790 if (size == 0)
791 return false;
792
793 double ratio = (double) used / (double) size;
794 return ratio < ARENA_MIN_SHRINK_RATIO;
795}
796
819static inline size_t arena_shrink_target(size_t used)
820{
821 return used + ARENA_SHRINK_PADDING;
822}
Core public API for the Arena memory allocator.
size_t(* arena_grow_callback)(size_t current_size, size_t requested_size)
Definition arena.h:79
#define ARENA_SHRINK_PADDING
Extra padding when computing shrink sizes.
#define ARENA_MIN_SHRINK_RATIO
Shrink threshold: offset < ratio * size.
#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.
size_t used
Number of bytes used in the arena buffer.
Definition arena_io.h:2
bool arena_grow(t_arena *arena, size_t required_size)
bool arena_might_shrink(t_arena *arena)
void arena_shrink(t_arena *arena, size_t new_size)
void arena_report_error(t_arena *arena, const char *fmt,...)
Report an error from an arena using its configured error callback.
size_t default_grow_cb(size_t current_size, size_t requested_size)
Default growth policy callback for arena resizing.
static size_t arena_shrink_target(size_t used)
Compute the target buffer size for a shrink operation.
static bool arena_should_grow(const t_arena *arena)
Check whether the arena is allowed to grow.
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_shrink_validate(t_arena *arena, size_t new_size)
Validate whether an arena can be safely shrunk 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 bool arena_shrink_apply(t_arena *arena, size_t new_size)
Apply the memory shrink operation to an arena's buffer.
static bool should_attempt_shrink(t_arena *arena)
Determine whether the arena is eligible for dynamic shrinking.
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 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_maybe_shrink(size_t used, size_t size)
Decide whether the arena buffer should be shrunk based on usage ratio.
The main memory arena structure used for fast allocation.