132
|
1 /**
|
|
2 * This module contains all functions related to an object's lifetime:
|
|
3 * allocation, resizing, deallocation, and finalization.
|
|
4 *
|
|
5 * Copyright: Copyright (C) 2004-2007 Digital Mars, www.digitalmars.com.
|
|
6 * All rights reserved.
|
|
7 * License:
|
|
8 * This software is provided 'as-is', without any express or implied
|
|
9 * warranty. In no event will the authors be held liable for any damages
|
|
10 * arising from the use of this software.
|
|
11 *
|
|
12 * Permission is granted to anyone to use this software for any purpose,
|
|
13 * including commercial applications, and to alter it and redistribute it
|
|
14 * freely, in both source and binary form, subject to the following
|
|
15 * restrictions:
|
|
16 *
|
|
17 * o The origin of this software must not be misrepresented; you must not
|
|
18 * claim that you wrote the original software. If you use this software
|
|
19 * in a product, an acknowledgment in the product documentation would be
|
|
20 * appreciated but is not required.
|
|
21 * o Altered source versions must be plainly marked as such, and must not
|
|
22 * be misrepresented as being the original software.
|
|
23 * o This notice may not be removed or altered from any source
|
|
24 * distribution.
|
|
25 * Authors: Walter Bright, Sean Kelly, Tomas Lindquist Olsen
|
|
26 */
|
|
27 module lifetime;
|
|
28
|
|
29
|
|
30 private
|
|
31 {
|
|
32 import tango.stdc.stdlib;
|
|
33 import tango.stdc.string;
|
|
34 import tango.stdc.stdarg;
|
|
35 debug(PRINTF) import tango.stdc.stdio;
|
|
36 }
|
|
37
|
|
38
|
|
39 private
|
|
40 {
|
|
41 enum BlkAttr : uint
|
|
42 {
|
|
43 FINALIZE = 0b0000_0001,
|
|
44 NO_SCAN = 0b0000_0010,
|
|
45 NO_MOVE = 0b0000_0100,
|
|
46 ALL_BITS = 0b1111_1111
|
|
47 }
|
|
48
|
|
49 struct BlkInfo
|
|
50 {
|
|
51 void* base;
|
|
52 size_t size;
|
|
53 uint attr;
|
|
54 }
|
|
55
|
|
56 extern (C) uint gc_getAttr( void* p );
|
|
57 extern (C) uint gc_setAttr( void* p, uint a );
|
|
58 extern (C) uint gc_clrAttr( void* p, uint a );
|
|
59
|
|
60 extern (C) void* gc_malloc( size_t sz, uint ba = 0 );
|
|
61 extern (C) void* gc_calloc( size_t sz, uint ba = 0 );
|
|
62 extern (C) size_t gc_extend( void* p, size_t mx, size_t sz );
|
|
63 extern (C) void gc_free( void* p );
|
|
64
|
|
65 extern (C) void* gc_addrOf( void* p );
|
|
66 extern (C) size_t gc_sizeOf( void* p );
|
|
67 extern (C) BlkInfo gc_query( void* p );
|
|
68
|
|
69 extern (C) bool onCollectResource( Object o );
|
|
70 extern (C) void onFinalizeError( ClassInfo c, Exception e );
|
|
71 extern (C) void onOutOfMemoryError();
|
|
72
|
|
73 extern (C) void _d_monitordelete(Object h, bool det = true);
|
|
74
|
|
75 enum
|
|
76 {
|
|
77 PAGESIZE = 4096
|
|
78 }
|
|
79 }
|
|
80
|
|
81
|
|
82 /**
|
|
83 *
|
|
84 */
|
|
85 extern (C) Object _d_newclass(ClassInfo ci)
|
|
86 {
|
|
87 void* p;
|
|
88
|
|
89 debug(PRINTF) printf("_d_newclass(ci = %p, %s)\n", ci, cast(char *)ci.name);
|
|
90 if (ci.flags & 1) // if COM object
|
|
91 { /* COM objects are not garbage collected, they are reference counted
|
|
92 * using AddRef() and Release(). They get free'd by C's free()
|
|
93 * function called by Release() when Release()'s reference count goes
|
|
94 * to zero.
|
|
95 */
|
|
96 p = tango.stdc.stdlib.malloc(ci.init.length);
|
|
97 if (!p)
|
|
98 onOutOfMemoryError();
|
|
99 }
|
|
100 else
|
|
101 {
|
|
102 p = gc_malloc(ci.init.length,
|
|
103 BlkAttr.FINALIZE | (ci.flags & 2 ? BlkAttr.NO_SCAN : 0));
|
|
104 debug(PRINTF) printf(" p = %p\n", p);
|
|
105 }
|
|
106
|
|
107 debug(PRINTF)
|
|
108 {
|
|
109 printf("p = %p\n", p);
|
|
110 printf("ci = %p, ci.init = %p, len = %d\n", ci, ci.init, ci.init.length);
|
|
111 printf("vptr = %p\n", *cast(void**) ci.init);
|
|
112 printf("vtbl[0] = %p\n", (*cast(void***) ci.init)[0]);
|
|
113 printf("vtbl[1] = %p\n", (*cast(void***) ci.init)[1]);
|
|
114 printf("init[0] = %x\n", (cast(uint*) ci.init)[0]);
|
|
115 printf("init[1] = %x\n", (cast(uint*) ci.init)[1]);
|
|
116 printf("init[2] = %x\n", (cast(uint*) ci.init)[2]);
|
|
117 printf("init[3] = %x\n", (cast(uint*) ci.init)[3]);
|
|
118 printf("init[4] = %x\n", (cast(uint*) ci.init)[4]);
|
|
119 }
|
|
120
|
|
121 // initialize it
|
|
122 (cast(byte*) p)[0 .. ci.init.length] = ci.init[];
|
|
123
|
|
124 debug(PRINTF) printf("initialization done\n");
|
|
125 return cast(Object) p;
|
|
126 }
|
|
127
|
|
128 /+
|
|
129
|
|
130 /**
|
|
131 *
|
|
132 */
|
|
133 extern (C) void _d_delinterface(void** p)
|
|
134 {
|
|
135 if (*p)
|
|
136 {
|
|
137 Interface* pi = **cast(Interface ***)*p;
|
|
138 Object o = cast(Object)(*p - pi.offset);
|
|
139
|
|
140 _d_delclass(&o);
|
|
141 *p = null;
|
|
142 }
|
|
143 }
|
|
144
|
|
145 +/
|
|
146
|
|
147 // used for deletion
|
|
148 private extern (D) alias void function(Object) fp_t;
|
|
149
|
|
150 /+
|
|
151
|
|
152
|
|
153 /**
|
|
154 *
|
|
155 */
|
|
156 extern (C) void _d_delclass(Object* p)
|
|
157 {
|
|
158 if (*p)
|
|
159 {
|
|
160 debug(PRINTF) printf("_d_delclass(%p)\n", *p);
|
|
161
|
|
162 ClassInfo **pc = cast(ClassInfo **)*p;
|
|
163 if (*pc)
|
|
164 {
|
|
165 ClassInfo c = **pc;
|
|
166
|
|
167 rt_finalize(cast(void*) *p);
|
|
168
|
|
169 if (c.deallocator)
|
|
170 {
|
|
171 fp_t fp = cast(fp_t)c.deallocator;
|
|
172 (*fp)(*p); // call deallocator
|
|
173 *p = null;
|
|
174 return;
|
|
175 }
|
|
176 }
|
|
177 else
|
|
178 {
|
|
179 rt_finalize(cast(void*) *p);
|
|
180 }
|
|
181 gc_free(cast(void*) *p);
|
|
182 *p = null;
|
|
183 }
|
|
184 }
|
|
185
|
|
186
|
|
187 /**
|
|
188 *
|
|
189 */
|
|
190 struct Array
|
|
191 {
|
|
192 size_t length;
|
|
193 byte* data;
|
|
194 }
|
|
195
|
|
196
|
|
197 /**
|
|
198 * Allocate a new array of length elements.
|
|
199 * ti is the type of the resulting array, or pointer to element.
|
|
200 * (For when the array is initialized to 0)
|
|
201 */
|
|
202 extern (C) Array _d_newarrayT(TypeInfo ti, size_t length)
|
|
203 {
|
|
204 void* p;
|
|
205 Array result;
|
|
206 auto size = ti.next.tsize(); // array element size
|
|
207
|
|
208 debug(PRINTF) printf("_d_newarrayT(length = x%x, size = %d)\n", length, size);
|
|
209 if (length == 0 || size == 0)
|
|
210 return Array();
|
|
211
|
|
212 version (D_InlineAsm_X86)
|
|
213 {
|
|
214 asm
|
|
215 {
|
|
216 mov EAX,size ;
|
|
217 mul EAX,length ;
|
|
218 mov size,EAX ;
|
|
219 jc Loverflow ;
|
|
220 }
|
|
221 }
|
|
222 else
|
|
223 size *= length;
|
|
224 p = gc_malloc(size + 1, !(ti.next.flags() & 1) ? BlkAttr.NO_SCAN : 0);
|
|
225 debug(PRINTF) printf(" p = %p\n", p);
|
|
226 memset(p, 0, size);
|
|
227 return Array(length, p);
|
|
228
|
|
229 Loverflow:
|
|
230 onOutOfMemoryError();
|
|
231 }
|
|
232
|
|
233 /**
|
|
234 * For when the array has a non-zero initializer.
|
|
235 */
|
|
236 extern (C) Array _d_newarrayiT(TypeInfo ti, size_t length)
|
|
237 {
|
|
238 Array result;
|
|
239 auto size = ti.next.tsize(); // array element size
|
|
240
|
|
241 debug(PRINTF) printf("_d_newarrayiT(length = %d, size = %d)\n", length, size);
|
|
242
|
|
243 if (length == 0 || size == 0)
|
|
244 result = Array();
|
|
245 else
|
|
246 {
|
|
247 auto initializer = ti.next.init();
|
|
248 auto isize = initializer.length;
|
|
249 auto q = initializer.ptr;
|
|
250 version (D_InlineAsm_X86)
|
|
251 {
|
|
252 asm
|
|
253 {
|
|
254 mov EAX,size ;
|
|
255 mul EAX,length ;
|
|
256 mov size,EAX ;
|
|
257 jc Loverflow ;
|
|
258 }
|
|
259 }
|
|
260 else
|
|
261 size *= length;
|
|
262 auto p = gc_malloc(size + 1, !(ti.next.flags() & 1) ? BlkAttr.NO_SCAN : 0);
|
|
263 debug(PRINTF) printf(" p = %p\n", p);
|
|
264 if (isize == 1)
|
|
265 memset(p, *cast(ubyte*)q, size);
|
|
266 else if (isize == int.sizeof)
|
|
267 {
|
|
268 int init = *cast(int*)q;
|
|
269 size /= int.sizeof;
|
|
270 for (size_t u = 0; u < size; u++)
|
|
271 {
|
|
272 (cast(int*)p)[u] = init;
|
|
273 }
|
|
274 }
|
|
275 else
|
|
276 {
|
|
277 for (size_t u = 0; u < size; u += isize)
|
|
278 {
|
|
279 memcpy(p + u, q, isize);
|
|
280 }
|
|
281 }
|
|
282 va_end(q);
|
|
283 result = Array(length, p);
|
|
284 }
|
|
285 return result;
|
|
286
|
|
287 Loverflow:
|
|
288 onOutOfMemoryError();
|
|
289 }
|
|
290
|
|
291 /**
|
|
292 *
|
|
293 */
|
|
294 extern (C) Array _d_newarraymT(TypeInfo ti, int ndims, ...)
|
|
295 {
|
|
296 Array result;
|
|
297
|
|
298 debug(PRINTF) printf("_d_newarraymT(ndims = %d)\n", ndims);
|
|
299 if (ndims == 0)
|
|
300 result = Array();
|
|
301 else
|
|
302 { va_list q;
|
|
303 va_start!(int)(q, ndims);
|
|
304
|
|
305 void[] foo(TypeInfo ti, size_t* pdim, int ndims)
|
|
306 {
|
|
307 size_t dim = *pdim;
|
|
308 void[] p;
|
|
309
|
|
310 debug(PRINTF) printf("foo(ti = %p, ti.next = %p, dim = %d, ndims = %d\n", ti, ti.next, dim, ndims);
|
|
311 if (ndims == 1)
|
|
312 {
|
|
313 return _d_newarrayT(ti, dim);
|
|
314 }
|
|
315 else
|
|
316 {
|
|
317 p = gc_malloc(dim * (void[]).sizeof + 1)[0 .. dim];
|
|
318 for (int i = 0; i < dim; i++)
|
|
319 {
|
|
320 (cast(void[]*)p.ptr)[i] = foo(ti.next, pdim + 1, ndims - 1);
|
|
321 }
|
|
322 }
|
|
323 return p;
|
|
324 }
|
|
325
|
|
326 size_t* pdim = cast(size_t *)q;
|
|
327 void[] arr = foo(ti, pdim, ndims);
|
|
328 result = Arra
|
|
329 debug(PRINTF) printf("result = %llx\n", result);
|
|
330
|
|
331 version (none)
|
|
332 {
|
|
333 for (int i = 0; i < ndims; i++)
|
|
334 {
|
|
335 printf("index %d: %d\n", i, va_arg!(int)(q));
|
|
336 }
|
|
337 }
|
|
338 va_end(q);
|
|
339 }
|
|
340 return result;
|
|
341 }
|
|
342
|
|
343
|
|
344 /**
|
|
345 *
|
|
346 */
|
|
347 extern (C) Array _d_newarraymiT(TypeInfo ti, int ndims, ...)
|
|
348 {
|
|
349 Array result;
|
|
350
|
|
351 debug(PRINTF) printf("_d_newarraymiT(ndims = %d)\n", ndims);
|
|
352 if (ndims == 0)
|
|
353 result = 0;
|
|
354 else
|
|
355 {
|
|
356 va_list q;
|
|
357 va_start!(int)(q, ndims);
|
|
358
|
|
359 void[] foo(TypeInfo ti, size_t* pdim, int ndims)
|
|
360 {
|
|
361 size_t dim = *pdim;
|
|
362 void[] p;
|
|
363
|
|
364 if (ndims == 1)
|
|
365 {
|
|
366 auto r = _d_newarrayiT(ti, dim);
|
|
367 p = *cast(void[]*)(&r);
|
|
368 }
|
|
369 else
|
|
370 {
|
|
371 p = gc_malloc(dim * (void[]).sizeof + 1)[0 .. dim];
|
|
372 for (int i = 0; i < dim; i++)
|
|
373 {
|
|
374 (cast(void[]*)p.ptr)[i] = foo(ti.next, pdim + 1, ndims - 1);
|
|
375 }
|
|
376 }
|
|
377 return p;
|
|
378 }
|
|
379
|
|
380 size_t* pdim = cast(size_t *)q;
|
|
381 result = cast(ulong)foo(ti, pdim, ndims);
|
|
382 debug(PRINTF) printf("result = %llx\n", result);
|
|
383
|
|
384 version (none)
|
|
385 {
|
|
386 for (int i = 0; i < ndims; i++)
|
|
387 {
|
|
388 printf("index %d: %d\n", i, va_arg!(int)(q));
|
|
389 printf("init = %d\n", va_arg!(int)(q));
|
|
390 }
|
|
391 }
|
|
392 va_end(q);
|
|
393 }
|
|
394 return result;
|
|
395 }
|
|
396
|
|
397 +/
|
|
398
|
|
399 /**
|
|
400 *
|
|
401 */
|
|
402 void* _d_allocmemory(size_t nbytes)
|
|
403 {
|
|
404 return gc_malloc(nbytes);
|
|
405 }
|
|
406
|
|
407 /+
|
|
408
|
|
409 /**
|
|
410 *
|
|
411 */
|
|
412 extern (C) void _d_delarray(Array *p)
|
|
413 {
|
|
414 if (p)
|
|
415 {
|
|
416 assert(!p.length || p.data);
|
|
417
|
|
418 if (p.data)
|
|
419 gc_free(p.data);
|
|
420 p.data = null;
|
|
421 p.length = 0;
|
|
422 }
|
|
423 }
|
|
424
|
|
425 +/
|
|
426
|
|
427 /**
|
|
428 *
|
|
429 */
|
|
430 extern (C) void _d_delmemory(void* *p)
|
|
431 {
|
|
432 if (*p)
|
|
433 {
|
|
434 gc_free(*p);
|
|
435 *p = null;
|
|
436 }
|
|
437 }
|
|
438
|
|
439
|
|
440 /**
|
|
441 *
|
|
442 */
|
|
443 extern (C) void _d_callfinalizer(void* p)
|
|
444 {
|
|
445 rt_finalize( p );
|
|
446 }
|
|
447
|
|
448
|
|
449 /**
|
|
450 *
|
|
451 */
|
|
452 extern (C) void rt_finalize(void* p, bool det = true)
|
|
453 {
|
|
454 debug(PRINTF) printf("rt_finalize(p = %p)\n", p);
|
|
455
|
|
456 if (p) // not necessary if called from gc
|
|
457 {
|
|
458 ClassInfo** pc = cast(ClassInfo**)p;
|
|
459
|
|
460 if (*pc)
|
|
461 {
|
|
462 ClassInfo c = **pc;
|
|
463
|
|
464 try
|
|
465 {
|
|
466 if (det || onCollectResource(cast(Object)p))
|
|
467 {
|
|
468 do
|
|
469 {
|
|
470 if (c.destructor)
|
|
471 {
|
|
472 fp_t fp = cast(fp_t)c.destructor;
|
|
473 (*fp)(cast(Object)p); // call destructor
|
|
474 }
|
|
475 c = c.base;
|
|
476 } while (c);
|
|
477 }
|
|
478 if ((cast(void**)p)[1]) // if monitor is not null
|
|
479 _d_monitordelete(cast(Object)p, det);
|
|
480 }
|
|
481 catch (Exception e)
|
|
482 {
|
|
483 onFinalizeError(**pc, e);
|
|
484 }
|
|
485 finally
|
|
486 {
|
|
487 *pc = null; // zero vptr
|
|
488 }
|
|
489 }
|
|
490 }
|
|
491 }
|
|
492
|
|
493 /+
|
|
494
|
|
495 /**
|
|
496 * Resize dynamic arrays with 0 initializers.
|
|
497 */
|
|
498 extern (C) byte[] _d_arraysetlengthT(TypeInfo ti, size_t newlength, Array *p)
|
|
499 in
|
|
500 {
|
|
501 assert(ti);
|
|
502 assert(!p.length || p.data);
|
|
503 }
|
|
504 body
|
|
505 {
|
|
506 byte* newdata;
|
|
507 size_t sizeelem = ti.next.tsize();
|
|
508
|
|
509 debug(PRINTF)
|
|
510 {
|
|
511 printf("_d_arraysetlengthT(p = %p, sizeelem = %d, newlength = %d)\n", p, sizeelem, newlength);
|
|
512 if (p)
|
|
513 printf("\tp.data = %p, p.length = %d\n", p.data, p.length);
|
|
514 }
|
|
515
|
|
516 if (newlength)
|
|
517 {
|
|
518 version (D_InlineAsm_X86)
|
|
519 {
|
|
520 size_t newsize = void;
|
|
521
|
|
522 asm
|
|
523 {
|
|
524 mov EAX, newlength;
|
|
525 mul EAX, sizeelem;
|
|
526 mov newsize, EAX;
|
|
527 jc Loverflow;
|
|
528 }
|
|
529 }
|
|
530 else
|
|
531 {
|
|
532 size_t newsize = sizeelem * newlength;
|
|
533
|
|
534 if (newsize / newlength != sizeelem)
|
|
535 goto Loverflow;
|
|
536 }
|
|
537
|
|
538 debug(PRINTF) printf("newsize = %x, newlength = %x\n", newsize, newlength);
|
|
539
|
|
540 if (p.data)
|
|
541 {
|
|
542 newdata = p.data;
|
|
543 if (newlength > p.length)
|
|
544 {
|
|
545 size_t size = p.length * sizeelem;
|
|
546 auto info = gc_query(p.data);
|
|
547
|
|
548 if (info.size <= newsize || info.base != p.data)
|
|
549 {
|
|
550 if (info.size >= PAGESIZE && info.base == p.data)
|
|
551 { // Try to extend in-place
|
|
552 auto u = gc_extend(p.data, (newsize + 1) - info.size, (newsize + 1) - info.size);
|
|
553 if (u)
|
|
554 {
|
|
555 goto L1;
|
|
556 }
|
|
557 }
|
|
558 newdata = cast(byte *)gc_malloc(newsize + 1, info.attr);
|
|
559 newdata[0 .. size] = p.data[0 .. size];
|
|
560 }
|
|
561 L1:
|
|
562 newdata[size .. newsize] = 0;
|
|
563 }
|
|
564 }
|
|
565 else
|
|
566 {
|
|
567 newdata = cast(byte *)gc_calloc(newsize + 1, !(ti.next.flags() & 1) ? BlkAttr.NO_SCAN : 0);
|
|
568 }
|
|
569 }
|
|
570 else
|
|
571 {
|
|
572 newdata = p.data;
|
|
573 }
|
|
574
|
|
575 p.data = newdata;
|
|
576 p.length = newlength;
|
|
577 return newdata[0 .. newlength];
|
|
578
|
|
579 Loverflow:
|
|
580 onOutOfMemoryError();
|
|
581 }
|
|
582
|
|
583
|
|
584 /**
|
|
585 * Resize arrays for non-zero initializers.
|
|
586 * p pointer to array lvalue to be updated
|
|
587 * newlength new .length property of array
|
|
588 * sizeelem size of each element of array
|
|
589 * initsize size of initializer
|
|
590 * ... initializer
|
|
591 */
|
|
592 extern (C) byte[] _d_arraysetlengthiT(TypeInfo ti, size_t newlength, Array *p)
|
|
593 in
|
|
594 {
|
|
595 assert(!p.length || p.data);
|
|
596 }
|
|
597 body
|
|
598 {
|
|
599 byte* newdata;
|
|
600 size_t sizeelem = ti.next.tsize();
|
|
601 void[] initializer = ti.next.init();
|
|
602 size_t initsize = initializer.length;
|
|
603
|
|
604 assert(sizeelem);
|
|
605 assert(initsize);
|
|
606 assert(initsize <= sizeelem);
|
|
607 assert((sizeelem / initsize) * initsize == sizeelem);
|
|
608
|
|
609 debug(PRINTF)
|
|
610 {
|
|
611 printf("_d_arraysetlengthiT(p = %p, sizeelem = %d, newlength = %d, initsize = %d)\n", p, sizeelem, newlength, initsize);
|
|
612 if (p)
|
|
613 printf("\tp.data = %p, p.length = %d\n", p.data, p.length);
|
|
614 }
|
|
615
|
|
616 if (newlength)
|
|
617 {
|
|
618 version (D_InlineAsm_X86)
|
|
619 {
|
|
620 size_t newsize = void;
|
|
621
|
|
622 asm
|
|
623 {
|
|
624 mov EAX,newlength ;
|
|
625 mul EAX,sizeelem ;
|
|
626 mov newsize,EAX ;
|
|
627 jc Loverflow ;
|
|
628 }
|
|
629 }
|
|
630 else
|
|
631 {
|
|
632 size_t newsize = sizeelem * newlength;
|
|
633
|
|
634 if (newsize / newlength != sizeelem)
|
|
635 goto Loverflow;
|
|
636 }
|
|
637 debug(PRINTF) printf("newsize = %x, newlength = %x\n", newsize, newlength);
|
|
638
|
|
639 size_t size = p.length * sizeelem;
|
|
640
|
|
641 if (p.data)
|
|
642 {
|
|
643 newdata = p.data;
|
|
644 if (newlength > p.length)
|
|
645 {
|
|
646 auto info = gc_query(p.data);
|
|
647
|
|
648 if (info.size <= newsize || info.base != p.data)
|
|
649 {
|
|
650 if (info.size >= PAGESIZE && info.base == p.data)
|
|
651 { // Try to extend in-place
|
|
652 auto u = gc_extend(p.data, (newsize + 1) - info.size, (newsize + 1) - info.size);
|
|
653 if (u)
|
|
654 {
|
|
655 goto L1;
|
|
656 }
|
|
657 }
|
|
658 newdata = cast(byte *)gc_malloc(newsize + 1, info.attr);
|
|
659 newdata[0 .. size] = p.data[0 .. size];
|
|
660 L1: ;
|
|
661 }
|
|
662 }
|
|
663 }
|
|
664 else
|
|
665 {
|
|
666 newdata = cast(byte *)gc_malloc(newsize + 1, !(ti.next.flags() & 1) ? BlkAttr.NO_SCAN : 0);
|
|
667 }
|
|
668
|
|
669 auto q = initializer.ptr; // pointer to initializer
|
|
670
|
|
671 if (newsize > size)
|
|
672 {
|
|
673 if (initsize == 1)
|
|
674 {
|
|
675 debug(PRINTF) printf("newdata = %p, size = %d, newsize = %d, *q = %d\n", newdata, size, newsize, *cast(byte*)q);
|
|
676 newdata[size .. newsize] = *(cast(byte*)q);
|
|
677 }
|
|
678 else
|
|
679 {
|
|
680 for (size_t u = size; u < newsize; u += initsize)
|
|
681 {
|
|
682 memcpy(newdata + u, q, initsize);
|
|
683 }
|
|
684 }
|
|
685 }
|
|
686 }
|
|
687 else
|
|
688 {
|
|
689 newdata = p.data;
|
|
690 }
|
|
691
|
|
692 p.data = newdata;
|
|
693 p.length = newlength;
|
|
694 return newdata[0 .. newlength];
|
|
695
|
|
696 Loverflow:
|
|
697 onOutOfMemoryError();
|
|
698 }
|
|
699
|
|
700
|
|
701 /**
|
|
702 * Append y[] to array x[].
|
|
703 * size is size of each array element.
|
|
704 */
|
|
705 extern (C) long _d_arrayappendT(TypeInfo ti, Array *px, byte[] y)
|
|
706 {
|
|
707 auto sizeelem = ti.next.tsize(); // array element size
|
|
708 auto info = gc_query(px.data);
|
|
709 auto length = px.length;
|
|
710 auto newlength = length + y.length;
|
|
711 auto newsize = newlength * sizeelem;
|
|
712
|
|
713 if (info.size < newsize || info.base != px.data)
|
|
714 { byte* newdata;
|
|
715
|
|
716 if (info.size >= PAGESIZE && info.base == px.data)
|
|
717 { // Try to extend in-place
|
|
718 auto u = gc_extend(px.data, (newsize + 1) - info.size, (newsize + 1) - info.size);
|
|
719 if (u)
|
|
720 {
|
|
721 goto L1;
|
|
722 }
|
|
723 }
|
|
724 newdata = cast(byte *)gc_malloc(newCapacity(newlength, sizeelem) + 1, info.attr);
|
|
725 memcpy(newdata, px.data, length * sizeelem);
|
|
726 px.data = newdata;
|
|
727 }
|
|
728 L1:
|
|
729 px.length = newlength;
|
|
730 memcpy(px.data + length * sizeelem, y.ptr, y.length * sizeelem);
|
|
731 return *cast(long*)px;
|
|
732 }
|
|
733
|
|
734
|
|
735 /**
|
|
736 *
|
|
737 */
|
|
738 size_t newCapacity(size_t newlength, size_t size)
|
|
739 {
|
|
740 version(none)
|
|
741 {
|
|
742 size_t newcap = newlength * size;
|
|
743 }
|
|
744 else
|
|
745 {
|
|
746 /*
|
|
747 * Better version by Dave Fladebo:
|
|
748 * This uses an inverse logorithmic algorithm to pre-allocate a bit more
|
|
749 * space for larger arrays.
|
|
750 * - Arrays smaller than PAGESIZE bytes are left as-is, so for the most
|
|
751 * common cases, memory allocation is 1 to 1. The small overhead added
|
|
752 * doesn't affect small array perf. (it's virtually the same as
|
|
753 * current).
|
|
754 * - Larger arrays have some space pre-allocated.
|
|
755 * - As the arrays grow, the relative pre-allocated space shrinks.
|
|
756 * - The logorithmic algorithm allocates relatively more space for
|
|
757 * mid-size arrays, making it very fast for medium arrays (for
|
|
758 * mid-to-large arrays, this turns out to be quite a bit faster than the
|
|
759 * equivalent realloc() code in C, on Linux at least. Small arrays are
|
|
760 * just as fast as GCC).
|
|
761 * - Perhaps most importantly, overall memory usage and stress on the GC
|
|
762 * is decreased significantly for demanding environments.
|
|
763 */
|
|
764 size_t newcap = newlength * size;
|
|
765 size_t newext = 0;
|
|
766
|
|
767 if (newcap > PAGESIZE)
|
|
768 {
|
|
769 //double mult2 = 1.0 + (size / log10(pow(newcap * 2.0,2.0)));
|
|
770
|
|
771 // redo above line using only integer math
|
|
772
|
|
773 static int log2plus1(size_t c)
|
|
774 { int i;
|
|
775
|
|
776 if (c == 0)
|
|
777 i = -1;
|
|
778 else
|
|
779 for (i = 1; c >>= 1; i++)
|
|
780 {
|
|
781 }
|
|
782 return i;
|
|
783 }
|
|
784
|
|
785 /* The following setting for mult sets how much bigger
|
|
786 * the new size will be over what is actually needed.
|
|
787 * 100 means the same size, more means proportionally more.
|
|
788 * More means faster but more memory consumption.
|
|
789 */
|
|
790 //long mult = 100 + (1000L * size) / (6 * log2plus1(newcap));
|
|
791 long mult = 100 + (1000L * size) / log2plus1(newcap);
|
|
792
|
|
793 // testing shows 1.02 for large arrays is about the point of diminishing return
|
|
794 if (mult < 102)
|
|
795 mult = 102;
|
|
796 newext = cast(size_t)((newcap * mult) / 100);
|
|
797 newext -= newext % size;
|
|
798 debug(PRINTF) printf("mult: %2.2f, alloc: %2.2f\n",mult/100.0,newext / cast(double)size);
|
|
799 }
|
|
800 newcap = newext > newcap ? newext : newcap;
|
|
801 debug(PRINTF) printf("newcap = %d, newlength = %d, size = %d\n", newcap, newlength, size);
|
|
802 }
|
|
803 return newcap;
|
|
804 }
|
|
805
|
|
806
|
|
807 /**
|
|
808 *
|
|
809 */
|
|
810 extern (C) byte[] _d_arrayappendcT(TypeInfo ti, inout byte[] x, ...)
|
|
811 {
|
|
812 auto sizeelem = ti.next.tsize(); // array element size
|
|
813 auto info = gc_query(x.ptr);
|
|
814 auto length = x.length;
|
|
815 auto newlength = length + 1;
|
|
816 auto newsize = newlength * sizeelem;
|
|
817
|
|
818 assert(info.size == 0 || length * sizeelem <= info.size);
|
|
819
|
|
820 debug(PRINTF) printf("_d_arrayappendcT(sizeelem = %d, ptr = %p, length = %d, cap = %d)\n", sizeelem, x.ptr, x.length, info.size);
|
|
821
|
|
822 if (info.size <= newsize || info.base != x.ptr)
|
|
823 { byte* newdata;
|
|
824
|
|
825 if (info.size >= PAGESIZE && info.base == x.ptr)
|
|
826 { // Try to extend in-place
|
|
827 auto u = gc_extend(x.ptr, (newsize + 1) - info.size, (newsize + 1) - info.size);
|
|
828 if (u)
|
|
829 {
|
|
830 goto L1;
|
|
831 }
|
|
832 }
|
|
833 debug(PRINTF) printf("_d_arrayappendcT(length = %d, newlength = %d, cap = %d)\n", length, newlength, info.size);
|
|
834 auto newcap = newCapacity(newlength, sizeelem);
|
|
835 assert(newcap >= newlength * sizeelem);
|
|
836 newdata = cast(byte *)gc_malloc(newcap + 1, info.attr);
|
|
837 memcpy(newdata, x.ptr, length * sizeelem);
|
|
838 (cast(void**)(&x))[1] = newdata;
|
|
839 }
|
|
840 L1:
|
|
841 byte *argp = cast(byte *)(&ti + 2);
|
|
842
|
|
843 *cast(size_t *)&x = newlength;
|
|
844 x.ptr[length * sizeelem .. newsize] = argp[0 .. sizeelem];
|
|
845 assert((cast(size_t)x.ptr & 15) == 0);
|
|
846 assert(gc_sizeOf(x.ptr) > x.length * sizeelem);
|
|
847 return x;
|
|
848 }
|
|
849
|
|
850
|
|
851 /**
|
|
852 *
|
|
853 */
|
|
854 extern (C) byte[] _d_arraycatT(TypeInfo ti, byte[] x, byte[] y)
|
|
855 out (result)
|
|
856 {
|
|
857 auto sizeelem = ti.next.tsize(); // array element size
|
|
858 debug(PRINTF) printf("_d_arraycatT(%d,%p ~ %d,%p sizeelem = %d => %d,%p)\n", x.length, x.ptr, y.length, y.ptr, sizeelem, result.length, result.ptr);
|
|
859 assert(result.length == x.length + y.length);
|
|
860 for (size_t i = 0; i < x.length * sizeelem; i++)
|
|
861 assert((cast(byte*)result)[i] == (cast(byte*)x)[i]);
|
|
862 for (size_t i = 0; i < y.length * sizeelem; i++)
|
|
863 assert((cast(byte*)result)[x.length * sizeelem + i] == (cast(byte*)y)[i]);
|
|
864
|
|
865 size_t cap = gc_sizeOf(result.ptr);
|
|
866 assert(!cap || cap > result.length * sizeelem);
|
|
867 }
|
|
868 body
|
|
869 {
|
|
870 version (none)
|
|
871 {
|
|
872 /* Cannot use this optimization because:
|
|
873 * char[] a, b;
|
|
874 * char c = 'a';
|
|
875 * b = a ~ c;
|
|
876 * c = 'b';
|
|
877 * will change the contents of b.
|
|
878 */
|
|
879 if (!y.length)
|
|
880 return x;
|
|
881 if (!x.length)
|
|
882 return y;
|
|
883 }
|
|
884
|
|
885 debug(PRINTF) printf("_d_arraycatT(%d,%p ~ %d,%p)\n", x.length, x.ptr, y.length, y.ptr);
|
|
886 auto sizeelem = ti.next.tsize(); // array element size
|
|
887 debug(PRINTF) printf("_d_arraycatT(%d,%p ~ %d,%p sizeelem = %d)\n", x.length, x.ptr, y.length, y.ptr, sizeelem);
|
|
888 size_t xlen = x.length * sizeelem;
|
|
889 size_t ylen = y.length * sizeelem;
|
|
890 size_t len = xlen + ylen;
|
|
891
|
|
892 if (!len)
|
|
893 return null;
|
|
894
|
|
895 byte* p = cast(byte*)gc_malloc(len + 1, !(ti.next.flags() & 1) ? BlkAttr.NO_SCAN : 0);
|
|
896 memcpy(p, x.ptr, xlen);
|
|
897 memcpy(p + xlen, y.ptr, ylen);
|
|
898 p[len] = 0;
|
|
899 return p[0 .. x.length + y.length];
|
|
900 }
|
|
901
|
|
902
|
|
903 /**
|
|
904 *
|
|
905 */
|
|
906 extern (C) byte[] _d_arraycatnT(TypeInfo ti, uint n, ...)
|
|
907 { void* a;
|
|
908 size_t length;
|
|
909 byte[]* p;
|
|
910 uint i;
|
|
911 byte[] b;
|
|
912 auto size = ti.next.tsize(); // array element size
|
|
913
|
|
914 p = cast(byte[]*)(&n + 1);
|
|
915
|
|
916 for (i = 0; i < n; i++)
|
|
917 {
|
|
918 b = *p++;
|
|
919 length += b.length;
|
|
920 }
|
|
921 if (!length)
|
|
922 return null;
|
|
923
|
|
924 a = gc_malloc(length * size, !(ti.next.flags() & 1) ? BlkAttr.NO_SCAN : 0);
|
|
925 p = cast(byte[]*)(&n + 1);
|
|
926
|
|
927 uint j = 0;
|
|
928 for (i = 0; i < n; i++)
|
|
929 {
|
|
930 b = *p++;
|
|
931 if (b.length)
|
|
932 {
|
|
933 memcpy(a + j, b.ptr, b.length * size);
|
|
934 j += b.length * size;
|
|
935 }
|
|
936 }
|
|
937
|
|
938 byte[] result;
|
|
939 *cast(int *)&result = length; // jam length
|
|
940 (cast(void **)&result)[1] = a; // jam ptr
|
|
941 return result;
|
|
942 }
|
|
943
|
|
944
|
|
945 /**
|
|
946 *
|
|
947 */
|
|
948 extern (C) void* _d_arrayliteralT(TypeInfo ti, size_t length, ...)
|
|
949 {
|
|
950 auto sizeelem = ti.next.tsize(); // array element size
|
|
951 void* result;
|
|
952
|
|
953 debug(PRINTF) printf("_d_arrayliteralT(sizeelem = %d, length = %d)\n", sizeelem, length);
|
|
954 if (length == 0 || sizeelem == 0)
|
|
955 result = null;
|
|
956 else
|
|
957 {
|
|
958 result = gc_malloc(length * sizeelem, !(ti.next.flags() & 1) ? BlkAttr.NO_SCAN : 0);
|
|
959
|
|
960 va_list q;
|
|
961 va_start!(size_t)(q, length);
|
|
962
|
|
963 size_t stacksize = (sizeelem + int.sizeof - 1) & ~(int.sizeof - 1);
|
|
964
|
|
965 if (stacksize == sizeelem)
|
|
966 {
|
|
967 memcpy(result, q, length * sizeelem);
|
|
968 }
|
|
969 else
|
|
970 {
|
|
971 for (size_t i = 0; i < length; i++)
|
|
972 {
|
|
973 memcpy(result + i * sizeelem, q, sizeelem);
|
|
974 q += stacksize;
|
|
975 }
|
|
976 }
|
|
977
|
|
978 va_end(q);
|
|
979 }
|
|
980 return result;
|
|
981 }
|
|
982
|
|
983 +/
|
|
984
|
|
985
|
|
986 /**
|
|
987 * Support for array.dup property.
|
|
988 */
|
|
989 struct Array2
|
|
990 {
|
|
991 size_t length;
|
|
992 void* ptr;
|
|
993 }
|
|
994
|
|
995
|
|
996 /**
|
|
997 *
|
|
998 */
|
|
999 extern (C) Array2 _adDupT(TypeInfo ti, Array2 a)
|
|
1000 out (result)
|
|
1001 {
|
|
1002 auto sizeelem = ti.next.tsize(); // array element size
|
|
1003 assert(memcmp(result.ptr, a.ptr, a.length * sizeelem) == 0);
|
|
1004 }
|
|
1005 body
|
|
1006 {
|
|
1007 Array2 r;
|
|
1008
|
|
1009 if (a.length)
|
|
1010 {
|
|
1011 auto sizeelem = ti.next.tsize(); // array element size
|
|
1012 auto size = a.length * sizeelem;
|
|
1013 r.ptr = gc_malloc(size, !(ti.next.flags() & 1) ? BlkAttr.NO_SCAN : 0);
|
|
1014 r.length = a.length;
|
|
1015 memcpy(r.ptr, a.ptr, size);
|
|
1016 }
|
|
1017 return r;
|
|
1018 }
|
|
1019
|
|
1020
|
|
1021 unittest
|
|
1022 {
|
|
1023 int[] a;
|
|
1024 int[] b;
|
|
1025 int i;
|
|
1026
|
|
1027 a = new int[3];
|
|
1028 a[0] = 1; a[1] = 2; a[2] = 3;
|
|
1029 b = a.dup;
|
|
1030 assert(b.length == 3);
|
|
1031 for (i = 0; i < 3; i++)
|
|
1032 assert(b[i] == i + 1);
|
|
1033 }
|