Class instance alignment

tsbockman thomas.bockman at gmail.com
Tue Feb 23 03:53:00 UTC 2021


On Monday, 22 February 2021 at 02:23:27 UTC, Steven Schveighoffer 
wrote:
> Hm... but does TypeInfo detail alignment? If so, we can make 
> this work anyway, just bump up the size needed to a power-of-2 
> pool.

It doesn't even need to be a power-of-2, assuming the pools 
themselves are properly aligned - just a multiple of the 
alignment:

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;
}

(This CTFE-able and can be memoized with a template, if desired. 
It's also just a few branchless instructions at runtime, if it's 
needed then for some reason.)


More information about the Digitalmars-d-learn mailing list