malloc_tkx, calloc_tkx, realloc_tkx, free_tkx, MALLOC, CALLOC, REALLOC, FREE - allocators with optional diagnostics
|
void * malloc_tkx(size_t s, char *f, int l); |
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.
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 |
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 When MALLOC |
export MEM_DEBUG=app.mem.out 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.
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.
malloc (3), calloc (3), realloc (3), free (3), chkalloc (1)
Gordon Carrie (dev0 at this site)