Boost logo

Boost :

From: (nil)
Date: 2000-02-07 11:58:43


> > > This functionality required adding a bool to the base class. This may
> > > increase the sizeof(linked_ptr), an exception is the case where
> > > struct alignment is 64 bits and a pointer is 32, as is MSVC's
> > > default. I imagine this is a fairly common condition. I don't have
> > > access to my linux box right now, or I would see what gcc defaults
> > > to.
> >
> > I've just checked this with MSVC and gcc. It tells me that a
> > linked_ptr (without flag) is 12 bytes (3 ptrs), so adding the flag is
> > not for free - in fact it will add 4 bytes to the struct size.
>
> Do you have struct alignment settings leftover from some previous
> project? MSVC's default is 8 byte alignment.
>

I haven't really been following this thread, but here's an idea you may be
able to use:

There's a trick I picked up from Doug Lea's memory allocator
implementation -- we can usually hide some bool flags inside a pointer,
since the least significant bits are usually zero.

Since the linked_ptr_base class has 2 pointers, it will be (on 32-bit
architectures) 8 bytes in size. All the compilers I know of will align
objects of this type at least on 4-byte alignment by default. This means
these pointers will always have the lowest 2 bits zero, so we can use these
bits to store boolean values.

Advantage: We don't have to possibly double the size of the class.

Disadvantages:
  1) Both accessing the bit flag and accessing the pointer now take slightly
longer due to masking operations.
  2) Some compilers have options to force 1 byte-alignment of all
structures, which will not guarantee the LSBit to be free.

        -Steve


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk