J
jacob navia
Sometimes you need to have two functions that have the same body.
For instance, in my container library I have a function
"Add" that will add an element at the end of the list,
and another "Pop" that will take out the first element of the list.
If you implement a queue as a list, the function "Enqueue" will be
the same as "Add", and "Dequeue" the same as "Pop". The only
way to do this in standardese is:
int Enqueue(List *l, void *item)
{
return Add(l,item);
}
This is wasteful since the call overhead is payed for
no reason.
The syntax for doing that under lcc-win is:
int __declspec(naked) SomeNewName(int a, int b) { }
int SomeOtherName(int a,int b)
{
// Here we have the real body of the function
}
The first declaration specifies a "naked" function i.e. one which
the compiler will treat as special and not generate any prologue
nor epilogue to it, and will not complain about missing return
values.
The second declaration is the body of the function. The first one
will be just an assembler label. It wilk take zero bytes of space.
"Naked" functions are supposed to be assembly language ones, by the way.
All this syntax is quite horrible. I am conforted however, because gcc
has an even uglier syntax:
http://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Function-Attributes.html:
The alias attribute causes the declaration to be emitted as an alias for another symbol, which must
be specified. For instance,
void __f () { /* Do something. */; }
void f () __attribute__ ((weak, alias ("__f")));
defines `f' to be a weak alias for `__f'. It is an error if `__f' is not defined in the same
translation unit.
It is not clear what "weak" means in this context, and I am not sure that
gcc does the same thing as lcc-win...
Any ideas? Which syntax would be better?
Thanks
For instance, in my container library I have a function
"Add" that will add an element at the end of the list,
and another "Pop" that will take out the first element of the list.
If you implement a queue as a list, the function "Enqueue" will be
the same as "Add", and "Dequeue" the same as "Pop". The only
way to do this in standardese is:
int Enqueue(List *l, void *item)
{
return Add(l,item);
}
This is wasteful since the call overhead is payed for
no reason.
The syntax for doing that under lcc-win is:
int __declspec(naked) SomeNewName(int a, int b) { }
int SomeOtherName(int a,int b)
{
// Here we have the real body of the function
}
The first declaration specifies a "naked" function i.e. one which
the compiler will treat as special and not generate any prologue
nor epilogue to it, and will not complain about missing return
values.
The second declaration is the body of the function. The first one
will be just an assembler label. It wilk take zero bytes of space.
"Naked" functions are supposed to be assembly language ones, by the way.
All this syntax is quite horrible. I am conforted however, because gcc
has an even uglier syntax:
http://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Function-Attributes.html:
The alias attribute causes the declaration to be emitted as an alias for another symbol, which must
be specified. For instance,
void __f () { /* Do something. */; }
void f () __attribute__ ((weak, alias ("__f")));
defines `f' to be a weak alias for `__f'. It is an error if `__f' is not defined in the same
translation unit.
It is not clear what "weak" means in this context, and I am not sure that
gcc does the same thing as lcc-win...
Any ideas? Which syntax would be better?
Thanks