2 module traits_getPointerBitmap;
4 import core.stdc.stdio;
12 enum bool RTInfoMark__Monitor = false; // is __monitor GC allocated?
15 enum bytesPerPtr = (size_t.sizeof);
16 enum bytesPerBitmapWord = bytesPerPtr * bytesPerPtr * 8;
18 template allocatedSize(T)
20 static if (is (T == class))
21 enum allocatedSize = __traits(classInstanceSize, T);
23 enum allocatedSize = T.sizeof;
26 bool testBit(const(size_t)* p, size_t biti)
28 enum BITS_SHIFT = (size_t.sizeof == 8 ? 6 : 5);
29 enum BITS_MASK = (bytesPerPtr - 1);
31 return (p[biti >> BITS_SHIFT] & (1 << (biti & BITS_MASK))) != 0;
34 void __testType(T)(size_t[] expected)
36 // check compile time info
37 enum bits = (T.sizeof + bytesPerPtr - 1) / bytesPerPtr;
38 enum words = (T.sizeof + bytesPerBitmapWord - 1) / bytesPerBitmapWord;
40 enum info = RTInfoImpl2!(Unqual!T); // we want the array, not the pointer
42 enum info = __traits(getPointerBitmap,T); // we want the array, not the pointer
44 debug(LOG) writef("%-20s:", T.stringof);
45 debug(LOG) writef(" CT:%s", info);
46 debug(LOG) writef(" EXP:%d %s", allocatedSize!T, expected);
47 assert(info[0] == allocatedSize!T);
48 assert(info[1..$] == expected);
49 assert(words == expected.length);
54 ///////////////////////////////////////
55 struct S(T, aliasTo = void)
57 static if(!is(aliasTo == void))
71 enum tOff = T.t.offsetof / bytesPerPtr;
76 enum pOff = T.p.offsetof / bytesPerPtr;
79 class C(T, aliasTo = void)
81 static if(!is(aliasTo == void))
92 ///////////////////////////////////////
94 void _testType(T)(size_t[] expected)
96 __testType!(T)(expected);
97 __testType!(const(T))(expected);
98 __testType!(immutable(T))(expected);
99 version(RTInfo) {} else // Unqual does not work with shared(T[N])
100 __testType!(shared(T))(expected);
103 void testType(T)(size_t[] expected)
105 _testType!(T)(expected);
107 // generate bit pattern for S!T
108 assert(expected.length == 1);
111 sexp ~= (expected[0] << tOff!(S!T)) | (1 << pOff!((S!T)));
112 _testType!(S!T)(sexp);
115 sexp[0] = (expected[0] << tOff!(S!(T, Object))) | (1 << pOff!(S!(T, Object))) | 1;
116 _testType!(S!(T, Object))(sexp);
119 sexp[0] = (expected[0] << tOff!(S!(T, string))) | (1 << pOff!(S!(T, string))) | 2; // arr ptr
120 _testType!(S!(T, string))(sexp);
122 // generate bit pattern for C!T
124 size_t mutexBit = (RTInfoMark__Monitor ? 2 : 0);
125 size_t ctpOff = ct.p.offsetof / bytesPerPtr;
126 size_t cttOff = ct.t.offsetof / bytesPerPtr;
127 sexp[0] = (expected[0] << cttOff) | (1 << ctpOff) | mutexBit;
128 _testType!(C!(T))(sexp);
130 C!(T, string) cts = null;
131 size_t ctspOff = cts.p.offsetof / bytesPerPtr;
132 size_t ctstOff = cts.t.offsetof / bytesPerPtr;
133 // generate bit pattern for C!T
134 sexp[0] = (expected[0] << ctstOff) | (1 << ctspOff) | mutexBit | 0b1000; // arr ptr
135 _testType!(C!(T, string))(sexp);
138 ///////////////////////////////////////
139 alias void[2*size_t.sizeof] void2;
140 alias size_t[3] int3;
141 alias size_t*[3] pint3;
142 alias string[3] sint3;
143 alias string[3][2] sint3_2;
144 alias int delegate() dg;
145 alias int function() fn;
146 alias typeof(null) NullType;
148 // span multiple bitmap elements
168 // no outer for structs
173 void foo() {} // need member fnction to not be POD
177 // implicit vtptr,monitor
183 class CNestedDerived : CNested
193 Large*[] arr; // { length, ptr }
206 testType!(bool) ([ 0b0 ]);
207 testType!(ubyte) ([ 0b0 ]);
208 testType!(short) ([ 0b0 ]);
209 testType!(int) ([ 0b0 ]);
210 testType!(long) ([ 0b00 ]);
211 testType!(double) ([ 0b00 ]);
212 testType!(dg) ([ 0b01 ]);
213 testType!(fn) ([ 0b0 ]);
214 testType!(S!fn) ([ 0b100 ]);
215 testType!(NullType) ([ 0b0 ]);
216 static if (__traits(compiles, __vector(float[4])))
217 testType!(__vector(float[4])) ([ 0b00 ]);
219 testType!(Object[int]) ([ 0b1 ]);
220 testType!(Object[]) ([ 0b10 ]);
221 testType!(string) ([ 0b10 ]);
223 testType!(int3) ([ 0b000 ]);
224 testType!(pint3) ([ 0b111 ]);
225 testType!(sint3) ([ 0b101010 ]);
226 testType!(sint3_2) ([ 0b101010101010 ]);
227 testType!(void2) ([ 0b11 ]);
228 testType!(U) ([ 0b1010 ]);
231 _testType!(Large) ([ 0x1000_0000__4000_0000, 0x0001_0000 ]);
233 _testType!(Large) ([ 0x4000_0000, 0x1000_0000, 0x0001_0000 ]);
235 _testType!(N.CNested) ([ 0b101000 ]);
236 _testType!(N.CNestedDerived) ([ 0b1000101000 ]);
238 testType!(N.Nested) ([ 0b110 ]);
247 void foo() {} // need member fnction to not be POD
252 // implicit vtptr,monitor
258 testType!(SFNested) ([ 0b10100 ]);
259 _testType!(CFNested) ([ 0b110000 ]);