Debian Codegraph @

libphobos/libdruntime/core/internal/gc/blockmeta.d

raw
1 /**
2 Functions to manipulate metadata in-block.
3
4 functionality was moved from rt.lifetime
5 */
6 module core.internal.gc.blockmeta;
7
8 import core.memory;
9
10 alias BlkInfo = GC.BlkInfo;
11 alias BlkAttr = GC.BlkAttr;
12
13 enum : size_t
14 {
15 PAGESIZE = 4096,
16 BIGLENGTHMASK = ~(PAGESIZE - 1),
17 SMALLPAD = 1,
18 MEDPAD = ushort.sizeof,
19 LARGEPREFIX = 16, // 16 bytes padding at the front of the array
20 LARGEPAD = LARGEPREFIX + 1,
21 MAXSMALLSIZE = 256-SMALLPAD,
22 MAXMEDSIZE = (PAGESIZE / 2) - MEDPAD
23 }
24
25 // size used to store the TypeInfo at the end of an allocation for structs that have a destructor
26 size_t structTypeInfoSize(const TypeInfo ti) pure nothrow @nogc
27 {
28 if (ti && typeid(ti) is typeid(TypeInfo_Struct)) // avoid a complete dynamic type cast
29 {
30 auto sti = cast(TypeInfo_Struct)cast(void*)ti;
31 if (sti.xdtor)
32 return size_t.sizeof;
33 }
34 return 0;
35 }
36
37 /**
38 Set the allocated length of the array block. This is called
39 any time an array is appended to or its length is set.
40
41 The allocated block looks like this for blocks < PAGESIZE:
42
43 |elem0|elem1|elem2|...|elemN-1|emptyspace|N*elemsize|
44
45
46 The size of the allocated length at the end depends on the block size:
47
48 a block of 16 to 256 bytes has an 8-bit length.
49
50 a block with 512 to pagesize/2 bytes has a 16-bit length.
51
52 For blocks >= pagesize, the length is a size_t and is at the beginning of the
53 block. The reason we have to do this is because the block can extend into
54 more pages, so we cannot trust the block length if it sits at the end of the
55 block, because it might have just been extended. If we can prove in the
56 future that the block is unshared, we may be able to change this, but I'm not
57 sure it's important.
58
59 In order to do put the length at the front, we have to provide 16 bytes
60 buffer space in case the block has to be aligned properly. In x86, certain
61 SSE instructions will only work if the data is 16-byte aligned. In addition,
62 we need the sentinel byte to prevent accidental pointers to the next block.
63 Because of the extra overhead, we only do this for page size and above, where
64 the overhead is minimal compared to the block size.
65
66 So for those blocks, it looks like:
67
68 |N*elemsize|padding|elem0|elem1|...|elemN-1|emptyspace|sentinelbyte|
69
70 where elem0 starts 16 bytes after the first byte.
71 */
72 bool __setArrayAllocLength(ref BlkInfo info, size_t newlength, bool isshared, const TypeInfo tinext, size_t oldlength = size_t.max) pure nothrow
73 {
74 __setBlockFinalizerInfo(info, tinext);
75
76 size_t typeInfoSize = (info.attr & BlkAttr.STRUCTFINAL) ? size_t.sizeof : 0;
77 return __setArrayAllocLengthImpl(info, newlength, isshared, oldlength, typeInfoSize);
78 }
79
80 // the impl function, used both above and in core.internal.array.utils
81 bool __setArrayAllocLengthImpl(ref BlkInfo info, size_t newlength, bool isshared, size_t oldlength, size_t typeInfoSize) pure nothrow
82 {
83 import core.atomic;
84
85 if (info.size <= 256)
86 {
87 import core.checkedint;
88
89 bool overflow;
90 auto newlength_padded = addu(newlength,
91 addu(SMALLPAD, typeInfoSize, overflow),
92 overflow);
93
94 if (newlength_padded > info.size || overflow)
95 // new size does not fit inside block
96 return false;
97
98 auto length = cast(ubyte *)(info.base + info.size - typeInfoSize - SMALLPAD);
99 if (oldlength != size_t.max)
100 {
101 if (isshared)
102 {
103 return cas(cast(shared)length, cast(ubyte)oldlength, cast(ubyte)newlength);
104 }
105 else
106 {
107 if (*length == cast(ubyte)oldlength)
108 *length = cast(ubyte)newlength;
109 else
110 return false;
111 }
112 }
113 else
114 {
115 // setting the initial length, no cas needed
116 *length = cast(ubyte)newlength;
117 }
118 }
119 else if (info.size <= PAGESIZE / 2)
120 {
121 if (newlength + MEDPAD + typeInfoSize > info.size)
122 // new size does not fit inside block
123 return false;
124 auto length = cast(ushort *)(info.base + info.size - typeInfoSize - MEDPAD);
125 if (oldlength != size_t.max)
126 {
127 if (isshared)
128 {
129 return cas(cast(shared)length, cast(ushort)oldlength, cast(ushort)newlength);
130 }
131 else
132 {
133 if (*length == oldlength)
134 *length = cast(ushort)newlength;
135 else
136 return false;
137 }
138 }
139 else
140 {
141 // setting the initial length, no cas needed
142 *length = cast(ushort)newlength;
143 }
144 }
145 else
146 {
147 if (newlength + LARGEPAD > info.size)
148 // new size does not fit inside block
149 return false;
150 auto length = cast(size_t *)(info.base);
151 if (oldlength != size_t.max)
152 {
153 if (isshared)
154 {
155 return cas(cast(shared)length, cast(size_t)oldlength, cast(size_t)newlength);
156 }
157 else
158 {
159 if (*length == oldlength)
160 *length = newlength;
161 else
162 return false;
163 }
164 }
165 else
166 {
167 // setting the initial length, no cas needed
168 *length = newlength;
169 }
170 }
171 return true; // resize succeeded
172 }
173
174 /**
175 The block finalizer info is set separately from the array length, as that is
176 only needed on the initial setup of the block. No shared is needed, since
177 this should only happen when the block is new.
178 If the STRUCTFINAL bit is not set, no finalizer is stored (but if needed the
179 slot is zeroed)
180 */
181 void __setBlockFinalizerInfo(ref BlkInfo info, const TypeInfo ti) pure nothrow
182 {
183 if ((info.attr & BlkAttr.APPENDABLE) && info.size > PAGESIZE / 2)
184 {
185 // if the structfinal bit is not set, we don't have a finalizer. But we
186 // should still zero out the finalizer slot.
187 auto context = (info.attr & BlkAttr.STRUCTFINAL) ? cast(void*)ti : null;
188
189 // array used size goes at the beginning. We can stuff the typeinfo
190 // right after it, as we need to use 16 bytes anyway.
191 //
192 auto typeInfo = cast(void**)info.base + 1;
193 *typeInfo = context;
194 version (D_LP64) {} else
195 {
196 // zero out the extra padding
197 (cast(size_t*)info.base)[2 .. 4] = 0;
198 }
199 }
200 else if(info.attr & BlkAttr.STRUCTFINAL)
201 {
202 // all other cases the typeinfo gets put at the end of the block
203 auto typeInfo = cast(void**)(info.base + info.size) - 1;
204 *typeInfo = cast(void*) ti;
205 }
206 }
207
208 /**
209 Get the finalizer info from the block (typeinfo).
210 If called on a block, without STRUCTFINAL set, returns null.
211 */
212 const(TypeInfo) __getBlockFinalizerInfo(ref BlkInfo info) pure nothrow
213 {
214 return __getBlockFinalizerInfo(info.base, info.size, info.attr);
215 }
216
217 /// ditto
218 const(TypeInfo) __getBlockFinalizerInfo(void* base, size_t size, uint attr) pure nothrow
219 {
220 if (!(attr & BlkAttr.STRUCTFINAL))
221 return null;
222
223 bool isLargeArray = (attr & BlkAttr.APPENDABLE) && size > PAGESIZE / 2;
224 auto typeInfo = isLargeArray ?
225 base + size_t.sizeof :
226 base + size - size_t.sizeof;
227 assert(*cast(size_t*)typeInfo != 0);
228 return *cast(TypeInfo*)typeInfo;
229 }
230
231 /**
232 get the used size of the array for the given block
233 */
234 size_t __arrayAllocLength(ref BlkInfo info) pure nothrow
235 in(info.attr & BlkAttr.APPENDABLE)
236 {
237 auto typeInfoSize = (info.attr & BlkAttr.STRUCTFINAL) ? size_t.sizeof : 0;
238 if (info.size <= 256)
239 return *cast(ubyte *)(info.base + info.size - typeInfoSize - SMALLPAD);
240
241 if (info.size <= PAGESIZE / 2)
242 return *cast(ushort *)(info.base + info.size - typeInfoSize - MEDPAD);
243
244 return *cast(size_t *)(info.base);
245 }
246
247 /**
248 Atomically get the used size of the array for the given block
249 */
250 size_t __arrayAllocLengthAtomic(ref BlkInfo info) pure nothrow
251 in(info.attr & BlkAttr.APPENDABLE)
252 {
253 import core.atomic;
254 auto typeInfoSize = (info.attr & BlkAttr.STRUCTFINAL) ? size_t.sizeof : 0;
255 if (info.size <= 256)
256 return atomicLoad(*cast(shared(ubyte)*)(info.base + info.size - typeInfoSize - SMALLPAD));
257
258 if (info.size <= PAGESIZE / 2)
259 return atomicLoad(*cast(shared(ushort)*)(info.base + info.size - typeInfoSize - MEDPAD));
260
261 return atomicLoad(*cast(shared(size_t)*)(info.base));
262 }
263
264 /**
265 Get the maximum bytes that can be stored in the given block.
266 */
267 size_t __arrayAllocCapacity(ref BlkInfo info) pure nothrow
268 in(info.attr & BlkAttr.APPENDABLE)
269 {
270 // Capacity is a calculation based solely on the block info.
271 if (info.size > PAGESIZE / 2)
272 return info.size - LARGEPAD;
273
274 auto typeInfoSize = (info.attr & BlkAttr.STRUCTFINAL) ? size_t.sizeof : 0;
275 auto padsize = info.size <= 256 ? SMALLPAD : MEDPAD;
276 return info.size - typeInfoSize - padsize;
277 }
278
279 /**
280 get the padding required to allocate size bytes. Note that the padding is
281 NOT included in the passed in size. Therefore, do NOT call this function
282 with the size of an allocated block.
283 */
284 size_t __arrayPad(size_t size, const TypeInfo tinext) nothrow pure @trusted
285 {
286 return size > MAXMEDSIZE ? LARGEPAD : ((size > MAXSMALLSIZE ? MEDPAD : SMALLPAD) + structTypeInfoSize(tinext));
287 }
288
289 /**
290 get the padding required to allocate size bytes, use the bits to determine
291 which metadata must be stored.
292 */
293 size_t __allocPad(size_t size, uint bits) nothrow pure @trusted
294 {
295 auto finalizerSize = (bits & BlkAttr.STRUCTFINAL) ? (void*).sizeof : 0;
296 if (bits & BlkAttr.APPENDABLE)
297 {
298 if (size > MAXMEDSIZE - finalizerSize)
299 return LARGEPAD;
300 auto pad = (size > MAXSMALLSIZE - finalizerSize) ? MEDPAD : SMALLPAD;
301 return pad + finalizerSize;
302 }
303
304 return finalizerSize;
305 }
306
307 /**
308 * Get the start of the array for the given block.
309 *
310 * Params:
311 * info = array metadata
312 * base = pointer to base of memory block
313 * size = size of memory block.
314 * Returns:
315 * pointer to the start of the array
316 */
317 void *__arrayStart()(return scope BlkInfo info) nothrow pure
318 {
319 return __arrayStart(info.base, info.size);
320 }
321 /// Ditto
322 void *__arrayStart()(return scope void* base, size_t size) nothrow pure
323 {
324 return base + ((size & BIGLENGTHMASK) ? LARGEPREFIX : 0);
325 }
326
327 /**
328 * Trim a block's extents to the known valid data that is not metadata. This
329 * takes into account the finalizer and array metadata.
330 */
331 void __trimExtents(ref scope void* base, ref size_t blockSize, uint attr) nothrow pure @nogc
332 {
333 if (attr & BlkAttr.APPENDABLE)
334 {
335 if (blockSize > PAGESIZE / 2)
336 {
337 // large block, it's always LARGEPREFIX bytes at the front.
338 blockSize = *(cast(size_t*)base);
339 base += LARGEPREFIX;
340 return;
341 }
342
343 void *pend = base + blockSize;
344 if (attr & BlkAttr.STRUCTFINAL)
345 // skip the finalizer
346 pend -= (void*).sizeof;
347
348 // get the actual length
349 if (blockSize <= 256)
350 blockSize = *(cast(ubyte*)pend - 1);
351 else // medium array block
352 blockSize = *(cast(ushort*)pend - 1);
353 }
354 else if (attr & BlkAttr.STRUCTFINAL)
355 {
356 // not appendable, but has a finalizer in the block. This is always
357 // stored at the end.
358 blockSize -= (void*).sizeof;
359 }
360 }