Sunday, December 27, 2009

link once (simulating c++ template code in pure c with GCC extensions)

The following definition will be merged in all compiled object files, i.e. the first function implementation will be linked into the produced binary and the following copies rejected.


__attribute__((section(".gnu.linkonce.c"))) int commonFunc(int a, int b) {
return a-b;
}

Simmulating "exceptions" in pure C

 #include <stdio.h>  
#include <setjmp.h>
typedef struct ExcHandlerDesc_ {
struct ExcHandlerDesc_ * prev;
jmp_buf jmpBuf;
int handled; // 0 = did not handle the exception
int excId; // the id of unhandled exception for rethrow
} ExcHandlerDesc;
static ExcHandlerDesc * xroot;
#define XR_CODE_BLOCK 0
#define XR_FINALLY_BLOCK -1
// starting exception id
#define XR_EXC_ID 2
//#define X_TRY(exchd_) addXHandler(exchd_); switch (setjmp(exchd_.jmpBuf)) {
//#define X_END(exchd_) }; removeXHandler(exchd_);
////////
void throwIt(int v) {
if (xroot == 0) {
printf("Unhandled exception %i !!!\n", v);
exit(1);
}
xroot->excId = v;
//xroot->handled = 1;
longjmp(xroot->jmpBuf, v);
}
void addXHandler(ExcHandlerDesc*d) {
d->handled = 1;
d->prev = xroot;
xroot = d;
}
void removeXHandler() {
if (xroot) {
xroot = xroot->prev;
}
}
////////
void funcWithExc() {
// throw
throwIt(2);
}
void func1() {
funcWithExc();
}
void func0()
{
int a;
// try
ExcHandlerDesc xhd;
addXHandler(&xhd);
a = setjmp(xhd.jmpBuf);
if ((a != XR_CODE_BLOCK) && (xroot == &xhd)) {
removeXHandler();
}
switch(a) {
case XR_CODE_BLOCK:
func1();
break;
case XR_FINALLY_BLOCK:
// do cleanup
if (!xhd.handled)
throwIt(xhd.excId);
break;
case XR_EXC_ID:
// handle it
printf("Caught %i !\n", xhd.excId);
// handler is removed and rethrow here is allowed
throwIt(2);
break;
default:
// unhandled!
xhd.handled = 0;
break;
}
// cleanup
if (a != XR_FINALLY_BLOCK)
longjmp(xhd.jmpBuf, XR_FINALLY_BLOCK);
/// end exception handling
}
int main() {
func0();
return 0;
}