From owner-freebsd-hackers Tue Nov 17 01:32:29 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id BAA14880 for freebsd-hackers-outgoing; Tue, 17 Nov 1998 01:32:29 -0800 (PST) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from blue.newtoy.com (snowfox.pr.mcs.net [205.164.44.72]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id BAA14870 for ; Tue, 17 Nov 1998 01:32:25 -0800 (PST) (envelope-from snowfox@newtoy.com) Received: from yellow (yellow.newtoy.com [205.164.44.74]) by blue.newtoy.com (8.8.8/8.8.8) with SMTP id DAA07990; Tue, 17 Nov 1998 03:30:59 -0600 (CST) (envelope-from snowfox@newtoy.com) Message-ID: <009301be120d$54045f00$4a2ca4cd@yellow.newtoy.com> From: "SnowFox" To: "Nate Williams" , Subject: Re: Wrapping a function Date: Tue, 17 Nov 1998 03:31:45 -0600 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 4.72.3155.0 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3155.0 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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 To: 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