lkml.org 
[lkml]   [2002]   [Feb]   [22]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [RFC] [PATCH] C exceptions in kernel
Dan Aloni wrote:
>
> The attached patch implements C exceptions in the kernel,

Bad idea ...

> which *don't* depend on special support from the compiler.

... which can be implemented in simple ANSI-C. See below.

Ciao, ET.#include <setjmp.h>

struct _catch
{
struct _catch *next;
jmp_buf buf;
};
static struct _catch _catch_top[1];
struct _catch *_catch = _catch_top;

#define throw() \
do { \
_catch = _catch->next; \
longjmp(_catch->buf, 1); \
} while (0)

#define try \
if (setjmp(_catch->buf) == 0) { \
struct _catch _catch_new[1]; \
_catch_new->next = _catch; \
_catch = _catch_new;
#define catch \
_catch = _catch->next; \
} else

/**** example below ****/
#include <stdio.h>

int a = 1;

void
foo(int x)
{
a=2;
if (x & 1)
throw();
}
int
main(int argc, char **argv)
{
int i;
for (i = 0; i < 10; ++i)
{
a=i;
try
{
try foo(i); catch
{
printf("2: caught at %d (a=%d)\n", i, a);
if ((i&3)==1)
throw();
}
}
catch
{
printf("1: caught at %d (a=%d)\n", i, a);
}
printf("a=%d\n", a);
}
return 0;
}
\
 
 \ /
  Last update: 2005-03-22 13:24    [from the cache]
©2003-2011 Jasper Spaans