How to do alligned allocation?
tsbockman
thomas.bockman at gmail.com
Sat Oct 1 00:36:30 UTC 2022
On Saturday, 1 October 2022 at 00:32:28 UTC, tsbockman wrote:
> alias Chunk = AliasSeq!(ubyte, ushort, uint, ulong,
> Chunk16)[shift];
Oops, I forgot that `ulong.alignof` is platform dependent. It's
probably best to just go ahead and explicitly specify the
alignment for all `Chunk` types:
```D
private template Aligned(size_t alignment)
if(1 <= alignment && isPowerOf2(alignment))
{
enum int shift = bsr(alignment);
enum size_t mask = alignment - 1;
static if(alignment <= 16) {
enum chunkShift = shift, chunkMask = mask;
align(alignment) struct Chunk {
void[alignment] data;
}
} else {
enum chunkShift = Aligned!(16).shift, chunkMask =
Aligned!(16).mask;
alias Chunk = Aligned!(16).Chunk;
}
}
```
(This also eliminates the `std.meta : AliasSeq` dependency.)
More information about the Digitalmars-d-learn
mailing list