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.
Contents
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_ALLOCp – 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);
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
1if 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.
Disabling Link-Time Overrides¶
The link-time override methods can be disabled at configure-time, even on
platforms which support them, by setting any of the following flags to 0:
YMO_ALLOC_ALLOW_WEAKREFYMO_ALLOC_ALLOW_WEAKYMO_ALLOC_ALLOW_ALIAS
To disable link-time overrides entirely, for example, you would do the following:
../configure \
YMO_ALLOC_ALLOW_WEAKREF=0 \
YMO_ALLOC_ALLOW_WEAK=0 \
YMO_ALLOC_ALLOW_ALIAS=0
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"