.\" .\" -*- nroff -*- .\" .\" Copyright (c) 2002-2004 Abraham vd Merwe .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. Neither the name of the author nor the names of other contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, .\" THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER .\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE .\" OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .TH MEM_OPEN 3debug "August 2004" Unix "Library calls" .SH NAME mem_open, mem_check, mem_alloc, mem_realloc, mem_free, mem_close - memory allocation routines for debugging .SH LIBRARIES Debug Library (\-ldebug) .SH SYNOPSIS .nf .B #include .sp .BI "void mem_open(void (*" fail ")(const char *" fmt ", ...)); .br .BI "void mem_check(void); .br .BI "void *mem_alloc(size_t " size "); .br .BI "void *mem_realloc(void *" ptr ", size_t " size "); .br .BI "void mem_free(void *" ptr "); .br .BI "void mem_close(void); .fi .SH DESCRIPTION \fBmem_open()\fP 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. .PP \fBmem_check()\fP 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. .PP \fBmem_alloc()\fP allocates \fIsize\fP bytes and returns a pointer to the allocated memory. The memory is not cleared. .PP \fBmem_realloc()\fP changes the size of the memory block pointed to by \fIptr\fP to \fIsize\fP bytes. The contents will be unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitialized. If \fIptr\fP is \fBNULL\fP, the call is equivalent to \fBmem_alloc(size)\fP; if size is equal to zero, the call is equivalent to \fBmem_free(ptr)\fP. Unless \fIptr\fP is \fBNULL\fP, it must have been returned by an earlier call to \fBmem_alloc()\fP or \fBmem_realloc()\fP. .PP \fBmem_free()\fP frees the memory space pointed to by \fIptr\fP, which must have been returned by a previous call to \fBmem_alloc()\fP or \fBmem_realloc()\fP. If \fIptr\fP is \fBNULL\fP, no operation is performed. .PP \fBmem_close()\fP checks for leaks and possible memory corruptions. .SH RETURN VALUE For \fBmem_alloc()\fP, the value returned is a pointer to the allocated memory, which is suitably aligned for any kind of variable, or \fBNULL\fP if the request fails. .PP \fBmem_realloc()\fP returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from \fIptr\fP, or \fBNULL\fP if the request fails or if size was equal to 0. If \fBmem_realloc()\fP fails the original block is left untouched - it is not freed or moved. .PP All other functions returns no value. .SH "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: .RS .nf .ta 4n 11n 17n mem_open (NULL); log_open (NULL,LOG_NORMAL,LOG_HAVE_COLORS | LOG_PRINT_FUNCTION); atexit (mem_close); atexit (log_close); .fi .RE Of course, \fBatexit\fP(3) should only be used if the program will not forked. .PP 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/ .SH "SEE ALSO" log_open(3), atexit(3) .SH "AUTHOR" Written by Abraham vd Merwe