Low-level Memory Management

Yimmo memory management API.

This file defines a set of convenience macros used to wrap common memory allocation/deallocation functions. Which functions are actually invoked are dependent on configuration options.

Allocator Macros

YMO_ALLOC(n)

Allocates n bytes, aligned to at least sizeof(void*).

Parameters
  • n – number of bytes to allocate.

YMO_ALLOC0(n)

Allocates n bytes, zeroed and aligned to at least sizeof(void*).

Parameters
  • n – number of bytes to allocate.

YMO_FREE(p)

free n bytes of memory pointed to by p.

Parameters
  • n – size of the block to free, as passed to YMO_ALLOC

  • p – a void* to the memory being freed

Example:

size_t n = ...;
char* p = YMO_ALLOC(n);
do_stuff(p);
YMO_FREE(p);
YMO_NEW(t)

Allocate sufficient space to accommodate an object of type t.

Parameters
  • t – a type name for which we need to allocate space

Example:

// Allocates sizeof(my_type_t) bytes:
my_type_t* my_obj = YMO_NEW(my_type_t);
YMO_NEW0(t)

Allocate and zero sufficient space for an object of type t.

Parameters
  • t – a type name for which we need to allocate space

Example:

// Allocates sizeof(my_type_t) bytes:
my_type_t* my_obj = YMO_NEW0(my_type_t);
YMO_DELETE(t, p)

Deallocate memory allocated via YMO_NEW or YMO_NEW0.

Parameters
  • t – the type that was allocated via YMO_NEW or YMO_NEW0

  • p – a void* to the object being freed

Example:

// Allocates sizeof(my_type_t) bytes:
my_type_t* my_obj = YMO_NEW0(my_type_t);
do_stuff(my_obj);
YMO_DELETE(my_type_t, my_obj);

Customization

The allocation macros above are all wrappers for one of the following three functions: ymo_malloc(), ymo_calloc(), ymo_free().

(By default, these are either aliases or wrappers for malloc, calloc, and free).

On platforms that support weakref, weak, alias, or weak, these are defined to be weak symbols. This allows the core memory routines to be overridden by user-supplied routines by providing strong symbols at link time.

You can check to see if this mechanism is available using the YMO_ALLOC_LT_OVERRIDE macro.

Example:

#include "ymo_alloc.h"

#if YMO_ALLOC_LT_OVERRIDE

// Here's a silly ymo_malloc override which
// just prints the number of bytes allocated:
void* ymo_malloc(size_t n)
{
    fprintf(stderr, "Allocating %zu bytes!\n", n);
    return malloc(n);
}
#endif

Default Allocator Functions

YMO_ALLOC_LT_OVERRIDE

Set to 1 if link-time memory customization is possible; 0, otherwise.

void *ymo_malloc(size_t n)

Wrapper/alias for malloc.

void *ymo_calloc(size_t c, size_t n)

Wrapper/alias for calloc.

void ymo_free(void *p)

Wrapper/alias for free.

Customizing at Compile-Time

On platforms which lack support for the linking mechanisms above, it is possible to override these functions at compile-time by defining the symbols via the preprocessor, e.g.:

../configure CFLAGS="-Dymo_malloc=my_custom_malloc"