From b2f46c3c0e2119545e52a01e1d73a93d21ebbc0d Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Fri, 12 Nov 1999 17:15:18 +0000 Subject: [PATCH] Update. 1999-11-01 Wolfram Gloger * malloc/malloc.h: Describe __malloc_initialize_hook. * manual/memory.texi: Document __malloc_initialize_hook. * sysdeps/unix/sysv/linux/setrlimit.c: Correctly use rlimits. --- ChangeLog | 7 ++++++- malloc/malloc.h | 5 ++++- manual/memory.texi | 48 ++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index a04c827..753183c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,11 @@ +1999-11-01 Wolfram Gloger + + * malloc/malloc.h: Describe __malloc_initialize_hook. + * manual/memory.texi: Document __malloc_initialize_hook. + 1999-11-11 Ulrich Drepper - * sysdeps/unix/sysv/linux/setrlimit.c: COrrectly use rlimits. + * sysdeps/unix/sysv/linux/setrlimit.c: Correctly use rlimits. 1999-11-09 Andreas Jaeger diff --git a/malloc/malloc.h b/malloc/malloc.h index c453d72..4f02b5a 100644 --- a/malloc/malloc.h +++ b/malloc/malloc.h @@ -206,8 +206,11 @@ extern __malloc_ptr_t malloc_get_state __MALLOC_P ((void)); extern int malloc_set_state __MALLOC_P ((__malloc_ptr_t __ptr)); #if defined __GLIBC__ || defined MALLOC_HOOKS -/* Hooks for debugging versions. */ +/* Called once when malloc is initialized; redefining this variable in + the application provides the preferred way to set up the hook + pointers. */ extern void (*__malloc_initialize_hook) __MALLOC_PMT ((void)); +/* Hooks for debugging and user-defined versions. */ extern void (*__free_hook) __MALLOC_PMT ((__malloc_ptr_t __ptr, __const __malloc_ptr_t)); extern __malloc_ptr_t (*__malloc_hook) __MALLOC_PMT ((size_t __size, diff --git a/manual/memory.texi b/manual/memory.texi index c957c2f..28c619f 100644 --- a/manual/memory.texi +++ b/manual/memory.texi @@ -731,6 +731,34 @@ should make sure to restore all the hooks to their previous value. When coming back from the recursive call, all the hooks should be resaved since a hook might modify itself. +@comment malloc.h +@comment GNU +@defvar __malloc_initialize_hook +The value of this variable is a pointer to a function that is called +once when the malloc implementation is initialized. This is a weak +variable, so it can be overridden in the application with a definition +like the following: + +@smallexample +void (*@var{__malloc_initialize_hook}) (void) = my_init_hook; +@end smallexample +@end defvar + +An issue to look out for is the time at which the malloc hook functions +can be safely installed. If the hook functions call the malloc-related +functions recursively, it is necessary that malloc has already properly +initialized itself at the time when @code{__malloc_hook} etc. is +assigned to. On the other hand, if the hook functions provide a +complete malloc implementation of their own, it is vital that the hooks +are assigned to @emph{before} the very first @code{malloc} call has +completed, because otherwise a chunk obtained from the ordinary, +un-hooked malloc may later be handed to @code{__free_hook}, for example. + +In both cases, the problem can be solved by setting up the hooks from +within a user-defined function pointed to by +@code{__malloc_initialize_hook}---then the hooks will be set up safely +at the right time. + Here is an example showing how to use @code{__malloc_hook} and @code{__free_hook} properly. It installs a function that prints out information every time @code{malloc} or @code{free} is called. We just @@ -743,8 +771,21 @@ static void *(*old_malloc_hook) (size_t); static void (*old_free_hook) (void*); /* Prototypes for our hooks. */ +static void *my_init_hook (void); static void *my_malloc_hook (size_t); -static void my_free_hook(void*); +static void my_free_hook (void*); + +/* Override initializing hook from the C library. */ +void (*__malloc_initialize_hook) (void) = my_init_hook; + +static void +my_init_hook (void) +@{ + old_malloc_hook = __malloc_hook; + old_free_hook = __free_hook; + __malloc_hook = my_malloc_hook; + __free_hook = my_free_hook; +@} static void * my_malloc_hook (size_t size) @@ -787,11 +828,6 @@ my_free_hook (void *ptr) main () @{ ... - old_malloc_hook = __malloc_hook; - old_free_hook = __free_hook; - __malloc_hook = my_malloc_hook; - __free_hook = my_free_hook; - ... @} @end smallexample -- 2.7.4