Re: [RFC] [PATCH] C exceptions in kernel

Edgar Toernig (froese@gmx.de)
Sat, 23 Feb 2002 04:37:27 +0100


This is a multi-part message in MIME format.
--------------BC6CCA47544425EA45E8E072
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

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.
--------------BC6CCA47544425EA45E8E072
Content-Type: text/plain; charset=us-ascii;
name="try-n-catch.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="try-n-catch.c"

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

--------------BC6CCA47544425EA45E8E072--

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/