LIBRARIES

Debug Library (-ldebug)

SYNOPSIS

#include <debug/memory.h>

void mem_open(void (*fail)(const char *fmt, ...));

void mem_check(void);

void *mem_alloc(size_t size);

void *mem_realloc(void *ptr, size_t size);

void mem_free(void *ptr);

void mem_close(void);

DESCRIPTION

mem_open() initializes the memory debugging system. It should be called before any of the other routines. You can specify a callback function which should be called whenever something bad happens, or NULL in which case the default error handler will be used. The default error handler logs error messages using the debug logging routines and exit.

mem_check() check all the allocated memory areas. This is called every time memory is allocated or freed. You can also call it anytime you think memory might be corrupted.

mem_alloc() allocates size bytes and returns a pointer to the allocated memory. The memory is not cleared.

mem_realloc() changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitialized. If ptr is NULL, the call is equivalent to mem_alloc(size); if size is equal to zero, the call is equivalent to mem_free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to mem_alloc() or mem_realloc().

mem_free() frees the memory space pointed to by ptr, which must have been returned by a previous call to mem_alloc() or mem_realloc(). If ptr is NULL, no operation is performed.

mem_close() checks for leaks and possible memory corruptions.

RETURN VALUE

For mem_alloc(), the value returned is a pointer to the allocated memory, which is suitably aligned for any kind of variable, or NULL if the request fails.

mem_realloc() returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from ptr, or NULL if the request fails or if size was equal to 0. If mem_realloc() fails the original block is left untouched - it is not freed or moved.

All other functions returns no value.

NOTES

If the default fail callback is used or if this routines are combined with the log routines, care should be taken to call open and close functions in the right order. The correct order is as follows:

mem_open (NULL);
log_open (NULL,LOG_NORMAL,LOG_HAVE_COLORS | LOG_PRINT_FUNCTION);
atexit (mem_close);
atexit (log_close);

Of course, atexit(3) should only be used if the program will not forked.

None of the libdebug routines are thread-safe. I'm not planning to change this either! For more information, please see http://threading.2038bug.com/

RELATED TO mem_check…

log_open(3), atexit(3)

AUTHOR

Written by Abraham vd Merwe <[email protected]>