Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 17 Nov 1998 03:31:45 -0600
From:      "SnowFox" <snowfox@newtoy.com>
To:        "Nate Williams" <nate@mt.sri.com>, <hackers@FreeBSD.ORG>
Subject:   Re: Wrapping a function
Message-ID:  <009301be120d$54045f00$4a2ca4cd@yellow.newtoy.com>

next in thread | raw e-mail | index | archive | help
There's no way to manage redundant symbols on the linker level, so
unfortunately managing what you're after is going to require a complete
rebuild when you want to remove the alloc thunker. The way I've done it has
been...

In a common header file for all .c files -

#ifdef RAMTRACKER_SUPPORT
    #define malloc(x) my_alloc(x)
    #define free(x) my_free(x)
    ...etc
#endif /* RAMTRACKER_SUPPORT */

Then, in my_alloc.c...

#ifdef RAMTRACKER_SUPPORT /* make entire file go away if not defined */
#undef RAMTRACKER_SUPPORT /* don't override functions in the scope of this
file */
#include "my_header.h"

/* implement my_alloc, my_free, anything else */

#endif /* RAMTRACKER_SUPPORT */


Also of note, you can do this too...
#define malloc(X) my_alloc((x), __FILE__, __LINE__)

I'm sure you see the use there. ;) For my own C++ projects, I've written a
suite of functions that tell me exactly what leaks where and watch for half
a dozen obscure C++ memory management mistakes, dumping everything to a log
file when the program terminates. If you want it so you can extract
whatever's useful to you, drop me a note outside the list.

-----Original Message-----
From: Nate Williams <nate@mt.sri.com>
To: hackers@FreeBSD.ORG <hackers@FreeBSD.ORG>
Date: Monday, November 16, 1998 11:17 PM
Subject: Wrapping a function


>Does anyone have an easy way of 'wrapping' an already existing library
>function so that any programs linked against your .o will call your
>function, but so your function can call the 'real' library function?
>
>Example:
>
>my_malloc.c:
>
>void *malloc(size_t size)
>{
>    void *ret;
>
>    printf("Calling malloc\n");
>    ret = REALMALLOC(size);
>    printf("Leaving malloc\n");
>    return ret;
>}
>
>Ignoring all of the functions where there is loss of errno and such, are
>they any good ideas?  Note, the use of the dl* functions is explicitly
>not allowed since those happen to be the functions I want to wrap in
>this case.
>
>I'm at a loss here how to do this in C, so any good hacks are welcomed.
>
>
>Nate
>
>To Unsubscribe: send mail to majordomo@FreeBSD.org
>with "unsubscribe freebsd-hackers" in the body of the message
>


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?009301be120d$54045f00$4a2ca4cd>