This is the mail archive of the
libc-hacker@cygnus.com
mailing list for the glibc project.
Re: Old malloc change -- explanation wanted
- To: Wolfram Gloger <wmglo@dent.med.uni-muenchen.de>
- Subject: Re: Old malloc change -- explanation wanted
- From: Ulrich Drepper <drepper@cygnus.com>
- Date: 19 Feb 1999 09:53:11 -0800
- Cc: libc-hacker@cygnus.com
- References: <19990219165012.23280.qmail@md.dent.med.uni-muenchen.de>
- Reply-To: drepper@cygnus.com (Ulrich Drepper)
Wolfram Gloger <wmglo@dent.med.uni-muenchen.de> writes:
> * malloc/malloc.c (malloc_hook_ini): Don't overwrite realloc and
> memalign hook.
> (realloc_hook_ini): Don't overwrite memalign hook.
> (memalign_hook_ini): Don't overwrite malloc and memalign hooks.
> Reported by Philippe Troin <phil@fifi.org>.
>
> Why was/is overwriting the hooks in question a problem ?
It can lead to problems if a program uses the hooks. Something like this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void *(*save_malloc) (size_t, void *);
void (*save_free) (void *, void *);
void *(*save_realloc) (size_t, void *, void *);
void
install (void)
{
save_malloc = __malloc_hook;
save_free = __free_hook;
save_realloc = __realloc_hook;
__malloc_hook = ma_malloc;
...
}
void *
my_malloc (size_t n, void *caller)
{
/* do something */
__malloc_hook = save_malloc;
if (__malloc_hook != NULL)
p = (*__malloc_hook) (n, caller);
else
p = malloc (n);
__malloc_hook = my_malloc;
/* maybe do something else */
return p;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If the init hooks would overwrite all hooks the effect of the install
function would be partly undone since the registered functions do not
set and restore all hook values.
Ideally all registered functions would set and restore all hooks. But
not all code is doing this and who knows, maybe we'll introduce
another hook in future. So the init functions are very conservative
and change only those hooks which have to be changed.
No great solution but the best available. Otherwise existing code
breaks.
--
---------------. drepper at gnu.org ,-. 1325 Chesapeake Terrace
Ulrich Drepper \ ,-------------------' \ Sunnyvale, CA 94089 USA
Cygnus Solutions `--' drepper at cygnus.com `------------------------