1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
module traits_getPointerBitmap;
import core.stdc.stdio;
// version = RTInfo;
// debug = LOG;
version(RTInfo)
import gc.rtinfo;
else
enum bool RTInfoMark__Monitor = false; // is __monitor GC allocated?
enum bytesPerPtr = (size_t.sizeof);
enum bytesPerBitmapWord = bytesPerPtr * bytesPerPtr * 8;
template allocatedSize(T)
{
static if (is (T == class))
enum allocatedSize = __traits(classInstanceSize, T);
else
enum allocatedSize = T.sizeof;
}
bool testBit(const(size_t)* p, size_t biti)
{
enum BITS_SHIFT = (size_t.sizeof == 8 ? 6 : 5);
enum BITS_MASK = (bytesPerPtr - 1);
return (p[biti >> BITS_SHIFT] & (1 << (biti & BITS_MASK))) != 0;
}
void __testType(T)(size_t[] expected)
{
// check compile time info
enum bits = (T.sizeof + bytesPerPtr - 1) / bytesPerPtr;
enum words = (T.sizeof + bytesPerBitmapWord - 1) / bytesPerBitmapWord;
version(RTInfo)
enum info = RTInfoImpl2!(Unqual!T); // we want the array, not the pointer
else
enum info = __traits(getPointerBitmap,T); // we want the array, not the pointer
debug(LOG) writef("%-20s:", T.stringof);
debug(LOG) writef(" CT:%s", info);
debug(LOG) writef(" EXP:%d %s", allocatedSize!T, expected);
assert(info[0] == allocatedSize!T);
assert(info[1..$] == expected);
assert(words == expected.length);
debug(LOG) writeln();
}
///////////////////////////////////////
struct S(T, aliasTo = void)
{
static if(!is(aliasTo == void))
{
aliasTo a;
alias a this;
}
size_t x;
T t = void;
void* p;
}
template tOff(T)
{
enum tOff = T.t.offsetof / bytesPerPtr;
}
template pOff(T)
{
enum pOff = T.p.offsetof / bytesPerPtr;
}
class C(T, aliasTo = void)
{
static if(!is(aliasTo == void))
{
aliasTo a;
alias a this;
}
size_t x;
T t = void;
void* p;
}
///////////////////////////////////////
void _testType(T)(size_t[] expected)
{
__testType!(T)(expected);
__testType!(const(T))(expected);
__testType!(immutable(T))(expected);
version(RTInfo) {} else // Unqual does not work with shared(T[N])
__testType!(shared(T))(expected);
}
void testType(T)(size_t[] expected)
{
_testType!(T)(expected);
// generate bit pattern for S!T
assert(expected.length == 1);
size_t[] sexp;
sexp ~= (expected[0] << tOff!(S!T)) | (1 << pOff!((S!T)));
_testType!(S!T)(sexp);
// prepend Object
sexp[0] = (expected[0] << tOff!(S!(T, Object))) | (1 << pOff!(S!(T, Object))) | 1;
_testType!(S!(T, Object))(sexp);
// prepend string
sexp[0] = (expected[0] << tOff!(S!(T, string))) | (1 << pOff!(S!(T, string))) | 2; // arr ptr
_testType!(S!(T, string))(sexp);
// generate bit pattern for C!T
C!T ct = null;
size_t mutexBit = (RTInfoMark__Monitor ? 2 : 0);
size_t ctpOff = ct.p.offsetof / bytesPerPtr;
size_t cttOff = ct.t.offsetof / bytesPerPtr;
sexp[0] = (expected[0] << cttOff) | (1 << ctpOff) | mutexBit;
_testType!(C!(T))(sexp);
C!(T, string) cts = null;
size_t ctspOff = cts.p.offsetof / bytesPerPtr;
size_t ctstOff = cts.t.offsetof / bytesPerPtr;
// generate bit pattern for C!T
sexp[0] = (expected[0] << ctstOff) | (1 << ctspOff) | mutexBit | 0b1000; // arr ptr
_testType!(C!(T, string))(sexp);
}
///////////////////////////////////////
alias void[2*size_t.sizeof] void2;
alias size_t[3] int3;
alias size_t*[3] pint3;
alias string[3] sint3;
alias string[3][2] sint3_2;
alias int delegate() dg;
alias int function() fn;
alias typeof(null) NullType;
// span multiple bitmap elements
struct Large
{
size_t[30] data1;
void* p1;
size_t[1] val1;
size_t[28] data2;
void* p2;
size_t[3] val2;
size_t[16] data3;
void* p3;
size_t[15] val3;
}
class N
{
struct Nested
{
// no outer for structs
size_t x;
void* p1;
Large* s;
void foo() {} // need member fnction to not be POD
}
class CNested
{
// implicit vtptr,monitor
size_t x;
void* p1;
size_t y;
// implicit outer
}
class CNestedDerived : CNested
{
size_t[3] z;
void* p;
}
}
union U
{
size_t[4] data;
Large*[] arr; // { length, ptr }
struct
{
size_t d1;
size_t d2;
size_t d3;
void* p;
}
}
void testRTInfo()
{
testType!(bool) ([ 0b0 ]);
testType!(ubyte) ([ 0b0 ]);
testType!(short) ([ 0b0 ]);
testType!(int) ([ 0b0 ]);
testType!(long) ([ 0b00 ]);
testType!(double) ([ 0b00 ]);
testType!(dg) ([ 0b01 ]);
testType!(fn) ([ 0b0 ]);
testType!(S!fn) ([ 0b100 ]);
testType!(NullType) ([ 0b0 ]);
static if (__traits(compiles, __vector(float[4])))
testType!(__vector(float[4])) ([ 0b00 ]);
testType!(Object[int]) ([ 0b1 ]);
testType!(Object[]) ([ 0b10 ]);
testType!(string) ([ 0b10 ]);
testType!(int3) ([ 0b000 ]);
testType!(pint3) ([ 0b111 ]);
testType!(sint3) ([ 0b101010 ]);
testType!(sint3_2) ([ 0b101010101010 ]);
testType!(void2) ([ 0b11 ]);
testType!(U) ([ 0b1010 ]);
version(D_LP64)
_testType!(Large) ([ 0x1000_0000__4000_0000, 0x0001_0000 ]);
else
_testType!(Large) ([ 0x4000_0000, 0x1000_0000, 0x0001_0000 ]);
_testType!(N.CNested) ([ 0b101000 ]);
_testType!(N.CNestedDerived) ([ 0b1000101000 ]);
testType!(N.Nested) ([ 0b110 ]);
struct SFNested
{
size_t[2] d;
void* p1;
fn f;
// implicite outer
void foo() {} // need member fnction to not be POD
}
class CFNested
{
// implicit vtptr,monitor
size_t[2] d;
void* p1;
// implicite outer
}
testType!(SFNested) ([ 0b10100 ]);
_testType!(CFNested) ([ 0b110000 ]);
}
void main()
{
testRTInfo();
}
|