OK to do bit-packing with GC pointers?

Steven Schveighoffer schveiguy at gmail.com
Sat Jul 23 00:55:14 UTC 2022


On 7/22/22 2:34 PM, Ben Jones wrote:

> 
> Can you elaborate on why it's probably OK in practice?

Because the GC deals with interior pointers just fine. Blocks with the 
"no interior" bit set are very rare, and for only specialized use, so 
normally this should not be a problem.

I have argued in the past that the spec is effectively impotent on this, 
since you can easily construct an equivalent pointer without bit 
manipulation, and the GC *must* handle this case.

```d
ubyte *ptr = cast(ubyte *)intptr;
ptr += lowbits;
```

> 
> I guess the alternative to is to to make them structs instead of classes 
> and manually alloc/free them (or keep them as classes, but still avoid 
> the GC)?

The alternative is to store the bits in a separate piece of memory than 
the poitner.

> Seems like std.bitmanip.taggedClassRef should have a big warning on it, 
> right?

Probably. Though like I said, I doubt it matters. Maybe someone with 
more type theory or GC knowledge knows whether it should be OK or not.

-Steve


More information about the Digitalmars-d-learn mailing list