Thread overview
Alligned gc allocation of struct
Oct 05, 2018
Sjoerd Nijboer
Oct 05, 2018
Kagamin
Oct 05, 2018
Sjoerd Nijboer
Oct 05, 2018
Dennis
Oct 05, 2018
Sjoerd Nijboer
Oct 07, 2018
Stanislav Blinov
October 05, 2018
I've got a `struct Foo{ubyte16 field1, field2.... fieldn;}` for which I would like a heap allocation `Foo* foo = new Foo();` But the fields itsself must be 16 bytes aligned for SIMD instructions.
Is there a neat way to do this in D?
As far as I can tell the GC_allocator doesn't do aligned allocations by default.
October 05, 2018
GC allocations are 16 bytes aligned.
October 05, 2018
On Friday, 5 October 2018 at 10:03:35 UTC, Kagamin wrote:
> GC allocations are 16 bytes aligned.

That's perfect. Thank you!
October 05, 2018
On Friday, 5 October 2018 at 10:03:35 UTC, Kagamin wrote:
> GC allocations are 16 bytes aligned.

Is that an implementation detail or well-defined behavior?
October 05, 2018
On Friday, 5 October 2018 at 14:55:04 UTC, Dennis wrote:
> On Friday, 5 October 2018 at 10:03:35 UTC, Kagamin wrote:
>> GC allocations are 16 bytes aligned.
>
> Is that an implementation detail or well-defined behavior?

The GC_Allocator doesn't support alignedAllocate from the IAllocate interface, which is kinda unfortunate.
So I'm a little worried that this might change in the future.
October 07, 2018
On Friday, 5 October 2018 at 20:41:15 UTC, Sjoerd Nijboer wrote:
> On Friday, 5 October 2018 at 14:55:04 UTC, Dennis wrote:
>> On Friday, 5 October 2018 at 10:03:35 UTC, Kagamin wrote:
>>> GC allocations are 16 bytes aligned.
>>
>> Is that an implementation detail or well-defined behavior?
>
> The GC_Allocator doesn't support alignedAllocate from the IAllocate interface, which is kinda unfortunate.
> So I'm a little worried that this might change in the future.

I don't think you should rely on them being specifically 16-bytes aligned, if portability is your goal. The `GC`'s methods such as `GC.malloc` do indeed return an "aligned" memory block,  but that's about it as far as the spec goes. If, however, your target is specifically amd64 and SIMD, then you can safely assume the allocations are indeed always 16-bytes aligned.

On a side note, I think the `alignedAllocate` from the allocator interface in std.experimental was a poor API decision. Better have a specific allocator instance that allocates in an appropriate alignment requirement. If I ever see an allocator that supports allocating with both an unspecified alignment and arbitrary alignment, I'd steer clear from it even without looking at the implementation.