Skip to content
AllocatorResize_dyn

AllocatorResize_dyn

Description

Try to grow / shrink an allocation in place. The pointer never moves. Use this when the caller can NOT tolerate a relocation (e.g. external code holds pointers into the buffer). Returns 1 when the allocator could satisfy the new size without moving; returns 0 when the caller should either fall back to AllocatorRemap (accepting a possible move) or give up.

Parameters

Name Direction Description
self in,out Allocator base.
ptr in Existing allocation pointer (must be non-NULL – resize of nothing is meaningless).
new_size in Requested new allocation size in bytes (must be non-zero – shrink-to-zero is a free, not a resize).

Success

Returns 1. ptr remains valid for new_size bytes; if growing, new bytes are uninitialised.

Failure

Returns 0. The allocation is unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
    #endif
    
    i8 AllocatorResize_dyn(Allocator *self, void *ptr, size new_size) {
        ValidateAllocator(self);
        // Resize requires a real allocation and a real new size. Anything
        // pointer moved should use AllocatorResize / AllocatorRemap
        // directly. ValidateAllocator runs inside each sub-call.
        if (ptr && new_size > 0 && AllocatorResize_dyn(self, ptr, new_size)) {
            return ptr;
        }
    ///
    #define AllocatorResize(self, ptr, new_size)                                                                                                                                                                                                                                                                    \
        _Generic((self), HeapAllocator *: heap_allocator_resize, PageAllocator *: page_allocator_resize, ArenaAllocator *: arena_allocator_resize, SlabAllocator *: slab_allocator_resize, BudgetAllocator *: budget_allocator_resize, DebugAllocator *: debug_allocator_resize, Allocator *: AllocatorResize_dyn)( \
            (Allocator *)(self),                                                                                                                                                                                                                                                                                    \
            (ptr),                                                                                                                                                                                                                                                                                                  \
Last updated on