delorie.com/djgpp/doc/libc/libc_552.html   search  
libc.a reference

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

malloc hook functions

Syntax

 
#include <stdlib.h>
#include <libc/malloc.h>

void (*__libc_malloc_hook)(size_t size, void *block);
void (*__libc_malloc_fail_hook)(size_t size);
void (*__libc_free_hook)(void *block);
void (*__libc_free_null_hook)(void);
void (*__libc_realloc_hook)(void *block, size_t size);

Description

These hooks are provided for building custom malloc debugging packages. Such packages typically need to be notified when memory is allocated and freed by the application, in order to be able to find memory leaks, code that writes beyond the limits of allocated buffers or attempts to free buffers which were not allocated by malloc, etc. These hooks can be used to define callback functions which will be called by the library at strategic points. Each callback is only called if it is non-NULL; by default, all of them are initialized to a NULL value.

__libc_malloc_hook
Called just before a chunk of memory is about to be returned to the application in response to an allocation request. size is the size requested by the application (not the actual size of the allocated buffer, which may be larger). block is a pointer to the block that was allocated, which is 4 bytes before the pointer that malloc will return to the application; these 4 bytes are used to record the actual size of the buffer. An additional copy of the block's size is recorded immediately after the buffer's end. Thus, *(size_t *)((char *)block + 4 + (BLOCK *)block->size) gives the second copy of the block's size.

__libc_malloc_fail_hook
Called if malloc failed to find a free block large enough to satisfy a request, and also failed to obtain additional memory from sbrk. size is the requested allocation size.

__libc_free_hook
Called when a buffer is about to be freed. block is a pointer 4 bytes before the address passed to free by the application, i.e. it is a pointer to the beginning of the size information maintained before the user buffer.

__libc_free_null_hook
Called whenever a NULL pointer is passed to free. ANSI C specifically rules that this is allowed and should have no effect, but you might want to catch such cases if your program needs to be portable to old compilers whose libraries don't like NULL pointers in free.

__libc_realloc_hook
Called at entry to realloc, before the actual reallocation. block is a pointer 4 bytes before the address passed to free by the application, i.e. it is a pointer to the beginning of the size information maintained before the user buffer. size is the new size requested by the application. (This hook is called in addition to the other hooks which will be called by free and malloc if and when realloc calls them.)

The BLOCK data type is used by malloc and free to maintain the heap. The only member which is always guaranteed to be valid is size (the additional copy of the size, recorded beyond the buffer's end, is also guaranteed to be valid). The next member is valid in all blocks that are part of the free list. This means that __libc_malloc_hook can use the next member, but __libc_free_hook cannot.

Portability

ANSI/ISO C No
POSIX No

These hooks are specific to DJGPP.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright © 2004     Updated Apr 2004