This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: sizeof(function) functionality
On Monday 7 July 2003 4:55 pm, Alexandre Oliva wrote:
> On Jul 6, 2003, Kristis Makris <kristis.makris@asu.edu> wrote:
> > Other than instantiating a volatile variable right after a function and
> > evaluating variable - &funcname, is there another way of retrieving the
> > size a function occupies in memory using gcc ?
>
> First, this won't get what you want, because variable will likely be
> placed in a different section. I can't think of any clean way to do
> this in C, especially if you consider functions-as-sections as a
> possibility.
Allegro <http://alleg.sf.net/> has used the following technique for a long
time:
#define END_OF_FUNCTION(name) void name##_end(void) { }
#define END_OF_STATIC_FUNCTION(name) static void name##_end(void) { }
void func1(void) { ... } END_OF_FUNCTION(func1);
void func2(void) { ... } END_OF_FUNCTION(func2);
Then there are more macros for locking the functions in memory, and these
macros merely find the difference between pointers to the two functions. The
purpose of this is to prevent any disk access from occurring when the
functions are called.
Note that this is only done on DJGPP; other operating systems supported by
Allegro (like Windows and Linux) use threads and have re-entrant disk access
functions, so the locking is unnecessary and the macros expand to nothing. So
I have no idea how portable this technique would be. It could conceivably be
broken by a compiler that set func2's entry point to be the 'return'
instruction of func1, for instance ...
Assuming it doesn't exist already, I think sizeof(function) or something like
it would be a very useful GNU Extension for future GCC versions :)
Ben