Class Instance allocations

Steven Schveighoffer schveiguy at yahoo.com
Thu Jan 14 04:37:12 PST 2010


On Wed, 13 Jan 2010 12:16:30 -0500, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Justin Johansson:
>> So why on earth, since Visual Studio 2001 (and before?), was the default
>> alignment set to 8 bytes for the M$ C/C++ compiler code generation
>> option?
>
> Maybe because for compatibility reasons they can't require a 16 bytes  
> alignment (as OS X does, I think) as SSE registers appreciate.
>

It depends on the architecture/register.  On most chips, if you load a  
register from a segment of memory, that memory has to be n-byte aligned  
where n is the number of bytes for the register.  But there are  
exceptions.  I'm not sure if SSE register requires alignment.  Typically  
on systems that do not support loading from a byte-aligned memory segment  
will throw a bus error if you try to do it.

>
>>> Is that true? Is this required by the tag bits added by the GC scans?  
>>> Does std.c.stdlib.malloc always return aligned to 8 on Windows?<<
>
> And no one has given me such answers yet.
> And currently the D GC seems to always return aligned to 16 bytes (even  
> chunks of memory smaller than 16 bytes).

I would guess that pointers on a 32-bit architecture need to be 4-byte  
aligned, otherwise, a struct like this:

struct S
{
    int *x;
    int *y;
}

would have to be minimum of 16 bytes!

I think the reason the allocator must return 8-byte aligned memory is  
because the allocator doesn't know what you're going to put in there, so  
it must handle the worst case scenario (doubles).

BTW, 16 bytes is the minimum size the GC returns, and I think it's a  
somewhat arbitrary decision.  The minimum you could possibly return is 8  
bytes, and it becomes a judgement call that depends on other factors if  
you want to increase the minimum size (less overhead per block vs. more  
wasted space).

-Steve



More information about the Digitalmars-d mailing list