# 2.2.1 Allocator

typedef void *(*ZixAlignedAllocFunc)(ZixAllocator *allocator, size_t alignment, size_t size)

  General aligned_alloc-like memory deallocation function.

  This works like the standard C aligned_alloc(), except has an additional handle parameter for implementing stateful allocators without static data.

typedef void (*ZixAlignedFreeFunc)(ZixAllocator *allocator, void *ptr)

  General aligned memory deallocation function.

  This works like the standard C free(), but must be used to free memory allocated with the aligned_alloc() method of the allocator. This allows portability to systems (like Windows) that can’t use the same free function in these cases.

typedef struct ZixAllocatorImpl ZixAllocator

  A memory allocator.

  This object-like structure provides an interface like the standard C functions malloc(), calloc(), realloc(), free(), and aligned_alloc(). It contains function pointers that differ from their standard counterparts by taking a context parameter (a pointer to this struct), which allows the user to implement custom stateful allocators.

typedef void *(*ZixCallocFunc)(ZixAllocator *allocator, size_t nmemb, size_t size)

  General calloc-like memory allocation function.

  This works like the standard C calloc(), except has an additional handle parameter for implementing stateful allocators without static data.

typedef void (*ZixFreeFunc)(ZixAllocator *allocator, void *ptr)

  General free-like memory deallocation function.

  This works like the standard C remalloc(), except has an additional handle parameter for implementing stateful allocators without static data.

typedef void *(*ZixMallocFunc)(ZixAllocator *allocator, size_t size)

  General malloc-like memory allocation function.

  This works like the standard C malloc(), except has an additional handle parameter for implementing stateful allocators without static data.

typedef void *(*ZixReallocFunc)(ZixAllocator *allocator, void *ptr, size_t size)

  General realloc-like memory reallocation function.

  This works like the standard C remalloc(), except has an additional handle parameter for implementing stateful allocators without static data.

struct ZixAllocatorImpl

  Definition of `ZixAllocator`.

  ZixMallocFunc malloc

    Allocate.

  ZixCallocFunc calloc

    Allocate and zero.

  ZixReallocFunc realloc

    Reallocate.

  ZixFreeFunc free

    Free.

  ZixAlignedAllocFunc aligned_alloc

    Allocate aligned.

  ZixAlignedFreeFunc aligned_free

    Free aligned.

void *zix_aligned_alloc(ZixAllocator *const allocator, const size_t alignment, const size_t size)

  Convenience wrapper that defers to the system allocator if allocator is null.

void zix_aligned_free(ZixAllocator *const allocator, void *const ptr)

  Convenience wrapper that defers to the system allocator if allocator is null.

void *zix_calloc(ZixAllocator *const allocator, const size_t nmemb, const size_t size)

  Convenience wrapper that defers to calloc() if allocator is null.

ZixAllocator *zix_default_allocator(void)

  Return the default allocator which simply uses the system allocator.

void zix_free(ZixAllocator *const allocator, void *const ptr)

  Convenience wrapper that defers to free() if allocator is null.

void *zix_malloc(ZixAllocator *const allocator, const size_t size)

  Convenience wrapper that defers to malloc() if allocator is null.

void *zix_realloc(ZixAllocator *const allocator, void *const ptr, const size_t size)

  Convenience wrapper that defers to realloc() if allocator is null.