Sometimes, we want to define data structures, whose memory allocation can be parametrized. If we wish to squeeze out the last bit of performance, we tie the structure to a certain allocator in compile time (as we do for hash tables). If performance is not so critical, allocators can be swapped in run time.

This module defines a generic interface to memory allocators. You can use the following pre-defined allocators, or define some of your own.

These data structures accept an allocator (more will come later):

  • Growing arrays


struct ucw_allocator {
  void * (*alloc)(struct ucw_allocator *alloc, size_t size);
  void * (*realloc)(struct ucw_allocator *alloc, void *ptr, size_t old_size, size_t new_size);
  void (*free)(struct ucw_allocator *alloc, void *ptr);

This structure describes a generic allocator. It provides pointers to three functions, which handle the actual (re)allocations.

extern struct ucw_allocator ucw_allocator_std;

This allocator uses xmalloc(), xrealloc() and xfree(). The memory it allocates is left unitialized.

extern struct ucw_allocator ucw_allocator_zeroed;

This allocator uses xmalloc(), xrealloc() and xfree(). All memory is zeroed upon allocation.