What means 'Save underlying hooks' in the documentation of malloc hooks?
The documentation for malloc hooks can be found here http://www.gnu.org/s/hello/manual/libc/Hook开发者_开发技巧s-for-Malloc.html.
When implementing the hook function for malloc()
and free()
, one has to save the underlying hooks.
/* Save underlying hooks */
old_malloc_hook = __malloc_hook;
old_free_hook = __free_hook;
I understand malloc hooks but not this part. Why should old_malloc_hook
set again. I thought it gives a reference (or sth like this) to the original malloc()
function?
Thanks in advance :)
The idea is that once your hook is activated and your hook function has control, you then restore any existing hook and resume the call inwards toward the one true malloc().
If every subsystem that wants to hook malloc does this, then every hook gets activated, regardless of who went first or last for setup.
精彩评论