Re: MemPools rewrite

From: Andres Kroonmaa <andre@dont-contact.us>
Date: Sat, 28 Oct 2000 01:04:37 +0200

On 20 Oct 2000, at 12:52, Alex Rousskov <rousskov@measurement-factory.com> wrote:

> On Fri, 20 Oct 2000, Andres Kroonmaa wrote:
>
> > On 20 Oct 2000, at 10:36, Alex Rousskov <rousskov@measurement-factory.com> wrote:
> >
> > > Just keep in mind that pointers/counters may need to be aligned on some
> > > platforms. For example, a "struct { char buf[9] }" in an array may not
> > > be aligned, and using its first bytes for a pointer or offset counter is
> > > dangerous.
> > >
> > > Thus, if you use first bytes of a free object, you probably want to copy
> > > the pointer into a local variable before de-referencing it. Not a big
> > > deal, of course.
> >
> > well, I think we should be safe within pools, as poolCreate is passed
> > obj size that should be suitably aligned and padded for use in arrays.
>
> No. What is suitable for one object, may not be suitable for another.
> Each "struct { char buf[9]; }" will consume 9 bytes in an array.
> Hence, most of the objects of the above type will not be appropriately
> aligned to stuff a pointer or even an integer in them.

struct _Array {
   char buf[9];
} Array;

struct _Array_pt {
   char buf[9];
   char *puf;
} Array_pt;

struct _Array_pp {
   char *puf;
   char buf[9];
   char *uff;
} Array_pp;

main(int argc, char *argv[])
{
    printf("Array: %d\n",sizeof(Array));
    printf("Array_pt: %d\n",sizeof(Array_pt));
    printf("Array_pp: %d\n",sizeof(Array_pp));
}

./a.out
Array: 9
Array_pt: 16
Array_pp: 20

 from this I tend to conclude that sizeof() returns sizes suitably aligned
 for use in arrays.

> > But for the safe side, we could forcibly round up obj size to a multiple
> > of pointer. this should make us safely aligned to sizeof(pointer)
> > With a list we can't pool smaller objects than sizeof pointer anyway.
>
> Won't help if my example above is correct.

 why not? lets say pointer is 8bytes, by rounding up size to next multiple
 of 8, we get good alignment, don't we? If we really don't risk, we can
 round up to 16, which is what malloc does. then alignment issues would be
 no way worse than with malloc().

------------------------------------
 Andres Kroonmaa <andre@online.ee>
 Delfi Online
 Tel: 6501 731, Fax: 6501 708
 Pärnu mnt. 158, Tallinn,
 11317 Estonia
Received on Fri Oct 27 2000 - 17:08:31 MDT

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 16:12:53 MST