Mail Archives: djgpp-workers/2002/03/05/12:14:08
> Date: Tue, 05 Mar 2002 08:20:38 -0500
> From: CBFalconer <cbfalconer AT yahoo DOT com>
> >
> > That was just an example. A hook is more general than any specific code,
> > because it allows a programmer to implement many features that the
> > original author never had in mind.
>
> Nothing prevents you implementing a hook. You have lost nothing
> compared to the present methods, but have gained the ability to
> see inside the system, with a minimum of danger. In fact existing
> hook implementations that intercept malloc etc. calls and record
> parameters and return values should continue to work unchanged.
> The only exception is where they make unclean assumptions about
> internal structure, which they can now find automatically and
> correctly.
Sorry, I don't understand: these are very general statements, whereas
I was looking for a specific technical answer. Could you please
elaborate?
What I had in mind was this: if malloc, realloc, and free call
certain hooks in strategic places, then a programmer can use those
hooks to implement additional features without any modifications to
the source of the library functions. If there are no such hooks,
what is the alternative method you offer in your implementation for
getting the same functionality?
Here's an example from the version of malloc in CVS:
void *
malloc(size_t size)
{
int b, chunk_size;
BLOCK *rv, **prev;
static BLOCK *expected_sbrk = 0;
/* Refuse ridiculously large requests right away. Anything beyond
2GB will be treated by sbrk as a negative request, i.e. as a
request to _decrease_ the heap size. */
if (size > 0x7fffffffU - 0x10000U) /* sbrk rounds up to 64KB */
{
if (__libc_malloc_fail_hook)
__libc_malloc_fail_hook(size);
return 0;
}
Here you see that a programmer could point __libc_malloc_fail_hook to
some function, and that function will then get called when malloc is
about to fail. That hook could, for example, gather statistics about
failed malloc calls.
How can I do this with your implementation?
- Raw text -