NAME
SYNOPSIS
#include "alloc.h"
DESCRIPTION
OPTIONAL DIAGNOSTICS
SIMULATING ALLOCATION FAILURES
SEE ALSO
AUTHOR

NAME

malloc_tkx, calloc_tkx, realloc_tkx, free_tkx, MALLOC, CALLOC, REALLOC, FREE - allocators with optional diagnostics

SYNOPSIS

#include "alloc.h"

void * malloc_tkx(size_t s, char *f, int l);
void * calloc_tkx(size_t n, size_t s, char *f, int l);
void * realloc_tkx(void *p, size_t s, char *f, int l);
void free_tkx(void *p, char *f, int l);
void * MALLOC(size_t s);
void * CALLOC(size_t
n, size_t s);
void * REALLOC(void *
p, size_t s);
void FREE(void *
p);


DESCRIPTION

malloc_tkx, calloc_tkx, realloc_tkx, and free_tkx allocate and free memory using the standard library memory management functions, and perform optional diagnostic tasks described below. They are normally called with macros MALLOC, CALLOC, REALLOC, and FREE.

malloc_tkx calls malloc with argument s and returns the result. calloc_tkx calls calloc with arguments n and s and returns the result. realloc_tkx calls realloc with arguments p and s and returns the result. free_tkx calls free with argument p. Allocations made with malloc_tkx, calloc_tkx, and realloc_tkx should be freed with free_tkx.

Arguments f and l are assumed to be set to compiler macros __FILE__ and __LINE__. The functions use these values in diagnostic output or failure simulations. The macro for each function normally provides f and l.

MALLOC(s) is a macro for

malloc_tkx(s, __FILE__, __LINE__)

CALLOC(n, s) is a macro for

alloc_tkx(n, s, __FILE__, __LINE__)

REALLOC(x, s) is a macro for

realloc_tkx(x, s, __FILE__, __LINE__)

FREE(x) is a macro for

free_tkx(x, __FILE__, __LINE__)

MALLOC, CALLOC, REALLOC and FREE behave like malloc, calloc, realloc, and free respectively. Alloca- tions made with MALLOC, CALLOC, REALLOC should be freed with FREE.

OPTIONAL DIAGNOSTICS

If the MEM_DEBUG environment variable is defined, the macros also arrange for output of diagnostic informa- tion. The diagnostic output can help identify memory leaks.

If MEM_DEBUG represents an integer, it is interpreted as a file descriptor. Thus, in the following example, app will send diagnostic information to file descriptor 2 (standard error).

export MEM_DEBUG=2
app

Otherwise, MEM_DEBUG is interpreted as a file name. Diagnostic output will go to this file, over-writing any previous contents. In the following example, app will send diagnostic information to file app.mem.out

export
app

When MALLOC

export MEM_DEBUG=app.mem.out
app

or CALLOC allocates memory with

with MEM_DEBUG defined, it prints a line of form:


address (counter) allocated at file:line

If REALLOC returns p, it prints a line of form:

address (counter) reallocated at file:line

If REALLOC returns a new address, it prints two lines of form:

address (counter) freed by realloc at file:line

address (counter) allocated by realloc at file:line

When FREE frees memory, it prints a line of form:

address (counter) freed at file:line

Above, address is the address of the new or modified allocation, counter is the number of times one of the alloca- tion macros has been called, and file and line identify the C source file and line number where the call occurred.

The resulting output gives a history of allocations and calls to free made by the process. The chkalloc application can scan this output and report on any allocations made that have not been freed when the process exits. These are assumed to be memory leaks, and the diagnostic output can help find where and when they occurred.

SIMULATING ALLOCATION FAILURES

If environment variable MEM_FAIL exists and has form file:line, and line line of source file file has a call to MALLOC, CALLOC, or REALLOC, then the allocator will always return NULL. This exercises code branches that handle allocation failures.

SEE ALSO

malloc (3), calloc (3), realloc (3), free (3), chkalloc (1)

AUTHOR

Gordon Carrie (dev0 at this site)