taggedPointer to char array on heap

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 10 16:26:27 PST 2017


On 01/10/2017 04:14 PM, Nordlöw wrote:
> On Wednesday, 11 January 2017 at 00:11:47 UTC, Nordlöw wrote:
>> If taggedPointer!char* allocated on the heap the pointer
>
> A taggedPointer!char* cannot use any tags, eventhough a heap-allocated
> pointer clearly has non-byte alignment. Is there a way around this?

Brute-forced with property functions:

import std.stdio;
import std.bitmanip;

struct S {
     mixin (taggedPointer!(
                uint*, "p_",
                bool, "b1", 1,
                bool, "b2", 1));

     @property void p(char* c) {
         assert((cast(ulong)c & 0b11) == 0);
         p_ = cast(uint*)c;
     }

     @property char* p() {
         return cast(char*)p_;
     }
}

void main() {
     auto s = S();
     s.p = "hello".dup.ptr;
     s.b1 = true;
     s.b2 = false;

     writefln("%s", s.p[0..6]);
}

Ali



More information about the Digitalmars-d-learn mailing list