apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Cliff Woolley <cliffwool...@yahoo.com>
Subject Re: Buckets code question
Date Wed, 11 Apr 2001 15:50:16 GMT
On Wed, 11 Apr 2001, William A. Rowe, Jr. wrote:

>         } while (0);
>         return ap__b;
>     } while(0);
> }
>
> So the final While(0); is definately unreachable.  No compiler error.
>
> My only question, why do {} while(0); rather than {} ?

It's not do {} while(0); rather than {} , it's that rather than {}; .
The semicolon is added by the caller of apr_bucket_do_create(), who treats
it as a function call.

   apr_bucket_do_create(); <---the semicolon

{}; is a syntax error on some platforms because empty statements are not
allowed, and here the semicolon is essentially delimiting an empty
statement which occurs between the closing brace and the semicolon.
do {} while(0); is actually the preferred way to get around this issue,
since it gets compiled away by an optimizing compiler anyhow.

Our problem, obviously, is the explicit return within the block. Maybe we
could change the call to look like this:

{
   apr_bucket *the_new_bucket;

   apr_bucket_do_create(the_new_bucket, ...);
   return the_new_bucket;
}

And adjust apr_bucket_do_create to look like this:

#define apr_bucket_do_create(ap__b,do_make)     \
    do {                                        \
        apr_bucket *b;                          \
        b = calloc(1, sizeof(*b));              \
        if (b == NULL) {                        \
            return NULL;                        \
        }                                       \
        ap__b = do_make;                        \
        if (ap__b == NULL) {                    \
            free(b);                            \
            return NULL;                        \
        }                                       \
        APR_RING_ELEM_INIT(ap__b, link);        \
    } while(0)


While this causes some very minor redundancy in the
apr_bucket_foo_create() functions, it has several benefits:

(1) both warnings and errors eliminated
(2) the magical return is made explicit, which makes
    apr_bucket_foo_create() easier to understand for someone not
    familiar with apr_bucket_do_create()'s innards.

Thoughts?

--Cliff

--------------------------------------------------------------
   Cliff Woolley
   cliffwoolley@yahoo.com
   Charlottesville, VA





Mime
View raw message