[cairo] Using bit fields a bit more in cairo

Jim Gettys jg at laptop.org
Wed Dec 20 08:12:54 PST 2006


Carl,

I'd do a bit more homework on this topic before going the bitfield
direction in API's, though in many ways, I agree with most of the points
you've made.

There are two places you can get bit (if you don't mind the pun):
  o wire protocols (probably not applicable to this circumstance), where
    you have to be careful about alignment and byte ordering.
  o differing compilers on the same system.

Last I knew (maybe more recent C standards have "fixed" this), C did not
define which bits get used to represent a bit field in a larger object.

The issue is, if one compiler does it one way and another does it
differently on the same platform, you can lose when code from different
compilers get mixed (not all platforms only use gcc...).

And I don't know if ABI conventions on platforms are clear on this
point.

If someone has data on the current state of C compilers cross platform,
now is the time to speak up.
                        Regards,
                                  - Jim


On Tue, 2006-12-19 at 18:23 -0500, Behdad Esfahbod wrote:
> On Tue, 2006-12-19 at 16:59 -0500, Carl Worth wrote:
> > On Tue, 19 Dec 2006 13:36:26 -0800, Carl Worth wrote:
> > > I originally started writing this email because I thought this was a
> > > feature of C we weren't using in cairo yet. But now I do notice that
> > > pixman is already using bit fields. So I don't foresee any problem
> > > here. Some of the problems of bit fields that prevent their use in
> > > some circumstances, (such as no guarantee of packing order), aren't
> > > relevant here where we're just using flags and don't care about the
> > > order.
> > 
> > By the way, there's another potential nuisance with bit fields that I
> > think is actually a good thing. With a single flags approach, (single
> > integer manipulated with masks), it's much easier to initialize all
> > the bits at once:
> > 
> > 	path->flags = 0;
> > 
> > or to copy the flags:
> > 
> > 	path->flags = other->flags;
> > 
> > compared with flag-by-flag initialization and copying required for bit
> > fields.
> > 
> > 	path->has_current_point = 0;
> > 	path->has_curve_to = 0;
> > 	...
> > and:
> > 	path->has_current_point = other->has_current_point;
> > 	path->has_curve_to = other->curve_to;
> > 	...
> > 
> > So that looks like a nuisance in the bit field approach, but it's
> > actually a big advantage.
> 
> All agreed.  I don't see any reason not to use bitfields.  We use them
> in Pango in public api and never had any problem that I know of.
> 
> 
-- 
Jim Gettys
One Laptop Per Child




More information about the cairo mailing list