2.5 Memory Allocation |
The standard C library functions malloc() and free() are the usual way to obtain and release memory for dynamic data structures like tables. Most users will be satisfied if libavl uses these routines for memory management. On the other hand, some users will want to supply their own methods for allocating and freeing memory, perhaps even different methods from table to table. For these users' benefit, each table is associated with a memory allocator, which provides functions for memory allocation and deallocation. This allocator has the same form in each table implementation. It looks like this:
6. <Memory allocator 6> = #ifndef LIBAVL_ALLOCATOR #define LIBAVL_ALLOCATOR /* Memory allocator. */ struct libavl_allocator
{ void *(*libavl_malloc) (struct libavl_allocator *, size_t libavl_size); void (*libavl_free) (struct libavl_allocator *, void *libavl_block); }; #endif
This code is included in 15, 100, and 651.
Members of struct libavl_allocator have the same interfaces as the like-named standard C library functions, except that they are each additionally passed a pointer to the struct libavl_allocator * itself as their first argument. The table implementations never call tbl_malloc() with a zero size or tbl_free() with a null pointer block.
The struct libavl_allocator type is shared between all of libavl's modules, so its name begins with libavl_, not with the specific module prefix that we've been representing generically here as tbl_. This makes it possible for a program to use a single allocator with multiple libavl table modules, without the need to declare instances of different structures.
The default allocator is just a wrapper around malloc() and free(). Here it is:
7. <Default memory allocation functions 7> = /* Allocates size bytes of space using malloc().
Returns a null pointer if allocation fails. */ void *
tbl_malloc (struct libavl_allocator *allocator, size_t size)
{ assert (allocator != NULL && size > 0); return malloc (size); } /* Frees block. */ void
tbl_free (struct libavl_allocator *allocator, void *block)
{ assert (allocator != NULL && block != NULL); free (block); } /* Default memory allocator that uses malloc() and free(). */ struct libavl_allocator tbl_allocator_default =
{
tbl_malloc,
tbl_free
};
This code is included in 30, 147, 198, 253, 302, 338, 377, 420, 457, 491, 524, 556, and 651.
The default allocator comes along with header file declarations:
8. <Default memory allocator header 8> = /* Default memory allocator. */ extern struct libavl_allocator tbl_allocator_default; void *tbl_malloc (struct libavl_allocator *, size_t); void tbl_free (struct libavl_allocator *, void *);
This code is included in 15 and 651.
See also: [FSF 1999], nodes “Malloc Examples” and “Changing Block Size”.
Exercises:
1. This structure is named with a libavl_ prefix because it is shared among all of libavl's module. Other types are shared among libavl modules, too, such as tbl_item_func. Why don't the names of these other types also begin with libavl_? [answer]
2. Supply an alternate allocator, still using malloc() and free(), that prints an error message to stderr and aborts program execution when memory allocation fails. [answer]
*3. Some kinds of allocators may need additional arguments. For instance, if memory for each table is taken from a separate Apache-style “memory pool”, then a pointer to the pool structure is needed. Show how this can be done without modifying existing types. [answer]