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

From: Larry McVoy (lm@bitmover.com)
Date: Sat Feb 23 2002 - 11:22:11 EST


On Sat, Feb 23, 2002 at 06:05:48PM +0200, Dan Aloni wrote:
> But, it CAN be used in *local* driver call branches. Writing a new
> driver? have a lot of local nested calls? Hate goto's? You can use
> exceptions.

Is this really anything other than syntactic sugar? Maybe it's different
in drivers, but I find myself doing the following in user space all the time

        #define unless(x) if (!(x)) /* perl/BCPL corrupted me */

        function(...)
        {
                char *foo = 0, *bar = 0;
                int locked = 0;
                int rc = -1;

                if (bad args or something) {
        out: if (foo) free(foo);
                        if (bar) free(bar);
                        if (locked) unlock();
                        return (rc);
                }

                unless (locked = get_the_lock()) goto out;
                unless (foo = allocate_foo()) goto out;
                unless (bar = allocate_bar()) goto out;

                more code....

                rc = 0;
                goto out;
        }

It seems ugly at first but it has some nice attributes:

    a) all the cleanup is in one place, for both the error path and the
       non-error path. I could put it at the bottom, I like it at the
       top because that's where I tend to have the list of things needed
       to be cleaned.

    b) all the error cases are branches, the normal path is straightline.

    c) it's as dense as I can make it.

So how would you do the same thing with exceptions?

-- 
---
Larry McVoy            	 lm at bitmover.com           http://www.bitmover.com/lm 
-
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:50 EST