Skip to content
AllocatorResize

AllocatorResize

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).
old_size in Previous allocation size in bytes.
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. ptr and old_size are unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
    #endif
    
    i8 AllocatorResize(Allocator *self, void *ptr, size old_size, 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(self, ptr, old_size, new_size)) {
            return ptr;
        }
Last updated on