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;
}

Sunday, May 17, 2009

Ubuntu 9.04 on HP Compaq 6510b

Core2Duo T7250, 2GB, intel 965

LiveCD runs fine, all hardware works.
After installation everything works just fine except compiz -- it doesn't run, although 3D games like TuxRacer, Chromium etc. run smooth.

Thursday, March 19, 2009

Beholder BeholdTV 507 Lirc settings

/etc/lircd.conf

begin remote
name Beholder
bits 16
eps 30
aeps 100
one 0 0
zero 0 0
pre_data_bits 16
pre_data 0x8001
gap 132757
toggle_bit 0

begin codes
1 0x0002
2 0x0003
3 0x0004
4 0x0005
5 0x0006
6 0x0007
7 0x0008
8 0x0009
9 0x000A
0 0x000B
UP 0x0067
LEFT 0x0069
RIGHT 0x006A
DOWN 0x006C
MUTE 0x0071
VOL- 0x0072
VOL+ 0x0073
POWER 0x0074
RECALL 0x0081
MENU 0x008B
SLEEP 0x008E
OK 0x0160
INFO 0x0166
MODE 0x0175
TV/AV 0x0179
FUNCTION 0x0181
AUDIO 0x0188
CHANNEL+ 0x0192
CHANNEL- 0x0193
DIGITS 0x019D
end codes
end remote