9.2.1.2 Memory Management
A useful idiom common to many GNU projects is to wrap the memory
management functions to localise out of memory handling, naming
them with an `x' prefix. By doing this, the rest of the project is
relieved of having to remember to check for `NULL' returns from the
various memory functions. These wrappers use the error API
to report memory exhaustion and abort the program. I have placed the
implementation code in `xmalloc.c':
Notice in the code above, that xcalloc is implemented in terms of
xmalloc , since calloc itself is not available in some
older C libraries. Also, the bzero function is actually
deprecated in favour of memset in modern C libraries --
I'll explain how to take this into account later in 9.2.3 Beginnings of a `configure.in'.
Rather than create a separate `xmalloc.h' file, which would need to
be #include d from almost everywhere else, the logical place to
declare these functions is in `common.h', since the wrappers will
be called from most everywhere else in the code:
By using the macros defined here, allocating and freeing heap memory is
reduced from:
|
char **argv = (char **) xmalloc (sizeof (char *) * 3);
do_stuff (argv);
if (argv)
free (argv);
|
to the simpler and more readable:
|
char **argv = XMALLOC (char *, 3);
do_stuff (argv);
XFREE (argv);
|
In the same spirit, I have borrowed `xstrdup.c' and
`xstrerror.c' from project GNU's libiberty. See section 9.1.5 Fallback Function Implementations.
|