whalloc  WhallocFallback

Allocation fallback

By default, the allocators will fail if their pool does not contain enough contiguous memory to provide the requested allocation. However, some of the allocators have a member structure of type whalloc_fallback) with these members:

  • void * (*realloc)( void *, size_t size )
  • void (*free)( void * mem )

If either is set to non-null, both must be set to complementary functions. If they are non-null, the realloc member will be used to satisfy (re)allocation requests and the free member will be used to free any memory passed to the equivalent whalloc_XX_free() function which is not in the memory range reserved by the allocator. The allocators can decide, with a simple range comparison, whether or not a given address belongs to them, so they can "safely" pass on the call to free() (well, as safely as that gets, anyway).

For example, you could set the fallback to be the standard realloc(3) and free(3) functions. The easiest way to do that is:

myBtAllocator.base.fallback = whalloc_fallback_stdalloc;

However, that must be done before any allocations are made using the allocator object, ideally immediately after initialization (e.g., via whalloc_bt_init()).