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