Class instance alignment

tsbockman thomas.bockman at gmail.com
Tue Feb 23 06:08:09 UTC 2021


On Tuesday, 23 February 2021 at 03:53:00 UTC, tsbockman wrote:
> size_t alignedSize(size_t typeSize, size_t typeAlignment) pure 
> @safe nothrow @nogc {
>     version(assert) {
>         import core.bitop : bsr;
>         assert(typeAlignment == (size_t(1) << 
> bsr(typeAlignment)));
>     }
>     size_t ret = typeSize & ~(typeAlignment - 1);
>     ret += (ret < typeSize)? typeAlignment : 0;
>     return ret;
> }

Better:

size_t alignedSize(size_t typeSize, size_t typeAlignment) pure 
@safe nothrow @nogc {
     version(assert) {
         import core.bitop : bsr;
         assert(typeAlignment == (size_t(1) << 
bsr(typeAlignment)));
     }
     const alignMask = typeAlignment - 1;
     return (typeSize + alignMask) & ~alignMask;
}


More information about the Digitalmars-d-learn mailing list