next up previous
Next: Limitations Up: xmemory module Previous: Linux

Module usage

The module has been completely implemented in a single source file called xmemory.c, associated to its header file xmemory.h. Users who want to add extended memory functionalities to their programs only have to include xmemory.h:

#include "xmemory.h"

The module appears to compile and run as expected on at least Linux, HPUX, Solaris, BSD and derivatives, AIX and OSF/1.

Notice that using this module, you will never get a NULL value from a malloc, calloc or strdup call. This comes from the fact that xmemory_malloc is configured to exit the current process (with exit()) if it cannot return a valid memory block of the requested size. This makes sense, because this module will already give you far more memory than your machine can in theory handle. If you really get out of memory using this module, you have probably run out of pointers or something just as tragic. Better quit the current program than try to make do.

This being said, if you are worried about that you can deactivate the exit statement in xmemory_malloc to make it return NULL in case it runs out of memory too.

The main advantage of a trustful malloc() is that it allows you to get rid of all the usual tests done to avoid writing to a NULL pointer. The usual code looks like this:

    if ((p=malloc(10 * sizeof(int)))==NULL) {
        fprintf(stderr, "out of memory: exiting\n");
        return -1 ;

Whereas the code without error-checking looks like that:

    p = malloc(10 * sizeof(int));

If you are using lots of objects (in the OO sense) defined as structs which are allocated using malloc, you can also clean out all object constructor calls, because if an object is allocated using malloc, it either returns a valid object or it dies in the process before it even returns to the caller.

The xmemory module will print out its error messages (few) on the stderr stream. If that is a problem for your application or if you would like to hook up your own error message routine, you will need to replace all calls to fprintf(stderr,...) by your own function. There are not so many error messages in xmemory.c, especially if the debug level is zero.

next up previous
Next: Limitations Up: xmemory module Previous: Linux
Nicolas Devillard 2002-05-03