OK to do bit-packing with GC pointers?
Steven Schveighoffer
schveiguy at gmail.com
Fri Jul 22 16:57:21 UTC 2022
On 7/22/22 12:50 PM, Ben Jones wrote:
> I'm looking to store a pointer to one of 2 unrelated (no inheritance
> relationship) classes and use the LSb to track which type I have. Is
> this going to cause any problems with the GC? For one of the classes
> I'll have a "pointer" to 1 byte past the start of the object. It seems
> like std.bitmanip.taggedClassRep does something similar, so I assume
> it's OK, but wanted to double check.
It's specifically undefined behavior by the spec, but in practice, I
think it will work, as long as the block you have isn't marked as not
allowing interior pointers.
See: https://dlang.org/spec/garbage.html#pointers_and_gc
Specifically "Do not take advantage of alignment of pointers to store
bit flags in the low order bits"
-Steve
More information about the Digitalmars-d-learn
mailing list