On Mon, Jun 27, 2005 at 05:11:10PM -0400, Andy Dougherty wrote:
> Well, I think there are already way too many pointer casts and related 
> games in the source.  Perhaps more to the point, not all casts are going 
> to work.

Well, there is that.  :-)

In this case, as I understand it, we're looking at one pointer where
some data are above the pointer and other data are below it.  (I'm
basing this on my memory of a design discussion in Austria; if I'm
mistaken, then ignore the rest.)

Given "struct below" and "struct above", this should be portable C:

        struct group {
            struct below b;
            struct above a;
        };

        struct group *g = malloc(sizeof *g)  /* OR WHATEVER METHOD OF ALLOC */

        /* now take an 'above' pointer, so access to 'a' is fast & easy */

        struct above *a = &g->a;        /* addr is  (char*)g + offsetof(struct 
group, a) */

        /* now make macros that easily let us point from the 'above' to the 
'below' */

        #define GROUP(a)  (struct group *)((char *)(a) - offsetof(struct group, 
a))
        #define BELOW(a)  (&GROUP(a)->b)

        struct below *b = B(a);

Look, Ma!  No unions!  And no worries about alignment, either.

(Of course if this were C++ we could just do multiple inheritance and
let the static_cast<> template handle it.  But whatever.  :-))

> In particular, the last time I checked with gcc on SPARC, I got over 7,000 
> warnings of the form 'cast increases required alignment of target type'.

Then gcc should just shut up, IMO.  When I cast from void* or char* to
whatever*, it's my lookout as programmer if that's an error, and
there's nothing about it that deserves a compiler warning ... mostly
because there are some things in C that require it, and the compiler
can't possibly distinguish the good from the bogus.  It's like warning
about '=' in boolean expressions.  Yeah, I know.  :-P

> More importantly, though I wonder if that particular micro-optimization
> is important to do at this point in parrot's development.

Probably not.  It's more important pedagogically.  We[*] are learning
about C by running into what doesn't work.

[*] not the editorial 'we' this time
-- 
Chip Salzenberg <[EMAIL PROTECTED]>

Reply via email to