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

From: Edgar Toernig (froese@gmx.de)
Date: Fri Feb 22 2002 - 22:37:27 EST


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

-
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/



This archive was generated by hypermail 2b29 : Sat Feb 23 2002 - 21:00:48 EST