This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
/
lifetime.d
2373 lines (2122 loc) · 68.9 KB
/
lifetime.d
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* This module contains all functions related to an object's lifetime:
* allocation, resizing, deallocation, and finalization.
*
* Copyright: Copyright Digital Mars 2000 - 2012.
* License: Distributed under the
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
* (See accompanying file LICENSE)
* Authors: Walter Bright, Sean Kelly, Steven Schveighoffer
* Source: $(DRUNTIMESRC src/rt/_lifetime.d)
*/
module rt.lifetime;
private
{
import core.stdc.stdlib;
import core.stdc.string;
import core.stdc.stdarg;
import core.bitop;
debug(PRINTF) import core.stdc.stdio;
static import rt.tlsgc;
}
private
{
enum BlkAttr : uint
{
FINALIZE = 0b0000_0001,
NO_SCAN = 0b0000_0010,
NO_MOVE = 0b0000_0100,
APPENDABLE = 0b0000_1000,
ALL_BITS = 0b1111_1111
}
struct BlkInfo
{
void* base;
size_t size;
uint attr;
}
extern (C) uint gc_getAttr( in void* p );
extern (C) uint gc_isCollecting( in void* p );
extern (C) uint gc_setAttr( in void* p, uint a );
extern (C) uint gc_clrAttr( in void* p, uint a );
extern (C) void* gc_malloc( size_t sz, uint ba = 0 );
extern (C) BlkInfo gc_qalloc( size_t sz, uint ba = 0 );
extern (C) void* gc_calloc( size_t sz, uint ba = 0 );
extern (C) size_t gc_extend( void* p, size_t mx, size_t sz );
extern (C) void gc_free( void* p );
extern (C) void* gc_addrOf( in void* p );
extern (C) size_t gc_sizeOf( in void* p );
extern (C) BlkInfo gc_query( in void* p );
extern (C) void onFinalizeError( ClassInfo c, Throwable e );
extern (C) void onOutOfMemoryError();
extern (C) void _d_monitordelete(Object h, bool det = true);
enum
{
PAGESIZE = 4096
}
alias bool function(Object) CollectHandler;
__gshared CollectHandler collectHandler = null;
enum : size_t
{
BIGLENGTHMASK = ~(cast(size_t)PAGESIZE - 1),
SMALLPAD = 1,
MEDPAD = ushort.sizeof,
LARGEPREFIX = 16, // 16 bytes padding at the front of the array
LARGEPAD = LARGEPREFIX + 1,
MAXSMALLSIZE = 256-SMALLPAD,
MAXMEDSIZE = (PAGESIZE / 2) - MEDPAD
}
}
/**
*
*/
extern (C) void* _d_allocmemory(size_t sz)
{
return gc_malloc(sz);
}
/**
*
*/
extern (C) Object _d_newclass(const ClassInfo ci)
{
void* p;
debug(PRINTF) printf("_d_newclass(ci = %p, %s)\n", ci, cast(char *)ci.name);
if (ci.m_flags & 1) // if COM object
{ /* COM objects are not garbage collected, they are reference counted
* using AddRef() and Release(). They get free'd by C's free()
* function called by Release() when Release()'s reference count goes
* to zero.
*/
p = malloc(ci.init.length);
if (!p)
onOutOfMemoryError();
}
else
{
// TODO: should this be + 1 to avoid having pointers to the next block?
p = gc_malloc(ci.init.length,
BlkAttr.FINALIZE | (ci.m_flags & 2 ? BlkAttr.NO_SCAN : 0));
debug(PRINTF) printf(" p = %p\n", p);
}
debug(PRINTF)
{
printf("p = %p\n", p);
printf("ci = %p, ci.init = %p, len = %d\n", ci, ci.init, ci.init.length);
printf("vptr = %p\n", *cast(void**) ci.init);
printf("vtbl[0] = %p\n", (*cast(void***) ci.init)[0]);
printf("vtbl[1] = %p\n", (*cast(void***) ci.init)[1]);
printf("init[0] = %x\n", (cast(uint*) ci.init)[0]);
printf("init[1] = %x\n", (cast(uint*) ci.init)[1]);
printf("init[2] = %x\n", (cast(uint*) ci.init)[2]);
printf("init[3] = %x\n", (cast(uint*) ci.init)[3]);
printf("init[4] = %x\n", (cast(uint*) ci.init)[4]);
}
// initialize it
(cast(byte*) p)[0 .. ci.init.length] = ci.init[];
debug(PRINTF) printf("initialization done\n");
return cast(Object) p;
}
/**
*
*/
extern (C) void _d_delinterface(void** p)
{
if (*p)
{
Interface* pi = **cast(Interface ***)*p;
Object o = cast(Object)(*p - pi.offset);
_d_delclass(&o);
*p = null;
}
}
// used for deletion
private extern (D) alias void function (Object) fp_t;
/**
*
*/
extern (C) void _d_delclass(Object* p)
{
if (*p)
{
debug(PRINTF) printf("_d_delclass(%p)\n", *p);
ClassInfo **pc = cast(ClassInfo **)*p;
if (*pc)
{
ClassInfo c = **pc;
rt_finalize(cast(void*) *p);
if (c.deallocator)
{
fp_t fp = cast(fp_t)c.deallocator;
(*fp)(*p); // call deallocator
*p = null;
return;
}
}
else
{
rt_finalize(cast(void*) *p);
}
gc_free(cast(void*) *p);
*p = null;
}
}
/** dummy class used to lock for shared array appending */
private class ArrayAllocLengthLock
{}
/**
Set the allocated length of the array block. This is called
any time an array is appended to or its length is set.
The allocated block looks like this for blocks < PAGESIZE:
|elem0|elem1|elem2|...|elemN-1|emptyspace|N*elemsize|
The size of the allocated length at the end depends on the block size:
a block of 16 to 256 bytes has an 8-bit length.
a block with 512 to pagesize/2 bytes has a 16-bit length.
For blocks >= pagesize, the length is a size_t and is at the beginning of the
block. The reason we have to do this is because the block can extend into
more pages, so we cannot trust the block length if it sits at the end of the
block, because it might have just been extended. If we can prove in the
future that the block is unshared, we may be able to change this, but I'm not
sure it's important.
In order to do put the length at the front, we have to provide 16 bytes
buffer space in case the block has to be aligned properly. In x86, certain
SSE instructions will only work if the data is 16-byte aligned. In addition,
we need the sentinel byte to prevent accidental pointers to the next block.
Because of the extra overhead, we only do this for page size and above, where
the overhead is minimal compared to the block size.
So for those blocks, it looks like:
|N*elemsize|padding|elem0|elem1|...|elemN-1|emptyspace|sentinelbyte|
where elem0 starts 16 bytes after the first byte.
*/
bool __setArrayAllocLength(ref BlkInfo info, size_t newlength, bool isshared, size_t oldlength = ~0)
{
if(info.size <= 256)
{
if(newlength + SMALLPAD > info.size)
// new size does not fit inside block
return false;
auto length = cast(ubyte *)(info.base + info.size - SMALLPAD);
if(oldlength != ~0)
{
if(isshared)
{
synchronized(typeid(ArrayAllocLengthLock))
{
if(*length == cast(ubyte)oldlength)
*length = cast(ubyte)newlength;
else
return false;
}
}
else
{
if(*length == cast(ubyte)oldlength)
*length = cast(ubyte)newlength;
else
return false;
}
}
else
{
// setting the initial length, no lock needed
*length = cast(ubyte)newlength;
}
}
else if(info.size < PAGESIZE)
{
if(newlength + MEDPAD > info.size)
// new size does not fit inside block
return false;
auto length = cast(ushort *)(info.base + info.size - MEDPAD);
if(oldlength != ~0)
{
if(isshared)
{
synchronized(typeid(ArrayAllocLengthLock))
{
if(*length == oldlength)
*length = cast(ushort)newlength;
else
return false;
}
}
else
{
if(*length == oldlength)
*length = cast(ushort)newlength;
else
return false;
}
}
else
{
// setting the initial length, no lock needed
*length = cast(ushort)newlength;
}
}
else
{
if(newlength + LARGEPAD > info.size)
// new size does not fit inside block
return false;
auto length = cast(size_t *)(info.base);
if(oldlength != ~0)
{
if(isshared)
{
synchronized(typeid(ArrayAllocLengthLock))
{
if(*length == oldlength)
*length = newlength;
else
return false;
}
}
else
{
if(*length == oldlength)
*length = newlength;
else
return false;
}
}
else
{
// setting the initial length, no lock needed
*length = newlength;
}
}
return true; // resize succeeded
}
/**
get the start of the array for the given block
*/
void *__arrayStart(BlkInfo info)
{
return info.base + ((info.size & BIGLENGTHMASK) ? LARGEPREFIX : 0);
}
/**
get the padding required to allocate size bytes. Note that the padding is
NOT included in the passed in size. Therefore, do NOT call this function
with the size of an allocated block.
*/
size_t __arrayPad(size_t size)
{
return size > MAXMEDSIZE ? LARGEPAD : (size > MAXSMALLSIZE ? MEDPAD : SMALLPAD);
}
/**
cache for the lookup of the block info
*/
enum N_CACHE_BLOCKS=8;
// note this is TLS, so no need to sync.
BlkInfo *__blkcache_storage;
static if(N_CACHE_BLOCKS==1)
{
version=single_cache;
}
else
{
//version=simple_cache; // uncomment to test simple cache strategy
//version=random_cache; // uncomment to test random cache strategy
// ensure N_CACHE_BLOCKS is power of 2.
static assert(!((N_CACHE_BLOCKS - 1) & N_CACHE_BLOCKS));
version(random_cache)
{
int __nextRndNum = 0;
}
int __nextBlkIdx;
}
@property BlkInfo *__blkcache()
{
if(!__blkcache_storage)
{
// allocate the block cache for the first time
immutable size = BlkInfo.sizeof * N_CACHE_BLOCKS;
__blkcache_storage = cast(BlkInfo *)malloc(size);
memset(__blkcache_storage, 0, size);
}
return __blkcache_storage;
}
// called when thread is exiting.
static ~this()
{
// free the blkcache
if(__blkcache_storage)
{
free(__blkcache_storage);
__blkcache_storage = null;
}
}
// we expect this to be called with the lock in place
void processGCMarks(BlkInfo* cache, scope rt.tlsgc.IsMarkedDg isMarked)
{
// called after the mark routine to eliminate block cache data when it
// might be ready to sweep
debug(PRINTF) printf("processing GC Marks, %x\n", cache);
if(cache)
{
debug(PRINTF) foreach(i; 0 .. N_CACHE_BLOCKS)
{
printf("cache entry %d has base ptr %x\tsize %d\tflags %x\n", i, cache[i].base, cache[i].size, cache[i].attr);
}
auto cache_end = cache + N_CACHE_BLOCKS;
for(;cache < cache_end; ++cache)
{
if(cache.base != null && !isMarked(cache.base))
{
debug(PRINTF) printf("clearing cache entry at %x\n", cache.base);
cache.base = null; // clear that data.
}
}
}
}
/**
Get the cached block info of an interior pointer. Returns null if the
interior pointer's block is not cached.
NOTE: The base ptr in this struct can be cleared asynchronously by the GC,
so any use of the returned BlkInfo should copy it and then check the
base ptr of the copy before actually using it.
TODO: Change this function so the caller doesn't have to be aware of this
issue. Either return by value and expect the caller to always check
the base ptr as an indication of whether the struct is valid, or set
the BlkInfo as a side-effect and return a bool to indicate success.
*/
BlkInfo *__getBlkInfo(void *interior)
{
BlkInfo *ptr = __blkcache;
version(single_cache)
{
if(ptr.base && ptr.base <= interior && (interior - ptr.base) < ptr.size)
return ptr;
return null; // not in cache.
}
else version(simple_cache)
{
foreach(i; 0..N_CACHE_BLOCKS)
{
if(ptr.base && ptr.base <= interior && (interior - ptr.base) < ptr.size)
return ptr;
ptr++;
}
}
else
{
// try to do a smart lookup, using __nextBlkIdx as the "head"
auto curi = ptr + __nextBlkIdx;
for(auto i = curi; i >= ptr; --i)
{
if(i.base && i.base <= interior && (interior - i.base) < i.size)
return i;
}
for(auto i = ptr + N_CACHE_BLOCKS - 1; i > curi; --i)
{
if(i.base && i.base <= interior && (interior - i.base) < i.size)
return i;
}
}
return null; // not in cache.
}
void __insertBlkInfoCache(BlkInfo bi, BlkInfo *curpos)
{
version(single_cache)
{
*__blkcache = bi;
}
else
{
version(simple_cache)
{
if(curpos)
*curpos = bi;
else
{
// note, this is a super-simple algorithm that does not care about
// most recently used. It simply uses a round-robin technique to
// cache block info. This means that the ordering of the cache
// doesn't mean anything. Certain patterns of allocation may
// render the cache near-useless.
__blkcache[__nextBlkIdx] = bi;
__nextBlkIdx = (__nextBlkIdx+1) & (N_CACHE_BLOCKS - 1);
}
}
else version(random_cache)
{
// strategy: if the block currently is in the cache, move the
// current block index to the a random element and evict that
// element.
auto cache = __blkcache;
if(!curpos)
{
__nextBlkIdx = (__nextRndNum = 1664525 * __nextRndNum + 1013904223) & (N_CACHE_BLOCKS - 1);
curpos = cache + __nextBlkIdx;
}
else
{
__nextBlkIdx = curpos - cache;
}
*curpos = bi;
}
else
{
//
// strategy: If the block currently is in the cache, swap it with
// the head element. Otherwise, move the head element up by one,
// and insert it there.
//
auto cache = __blkcache;
if(!curpos)
{
__nextBlkIdx = (__nextBlkIdx+1) & (N_CACHE_BLOCKS - 1);
curpos = cache + __nextBlkIdx;
}
else if(curpos !is cache + __nextBlkIdx)
{
*curpos = cache[__nextBlkIdx];
curpos = cache + __nextBlkIdx;
}
*curpos = bi;
}
}
}
/**
* Shrink the "allocated" length of an array to be the exact size of the array.
* It doesn't matter what the current allocated length of the array is, the
* user is telling the runtime that he knows what he is doing.
*/
extern(C) void _d_arrayshrinkfit(const TypeInfo ti, void[] arr)
{
// note, we do not care about shared. We are setting the length no matter
// what, so no lock is required.
debug(PRINTF) printf("_d_arrayshrinkfit, elemsize = %d, arr.ptr = x%x arr.length = %d\n", ti.next.tsize, arr.ptr, arr.length);
auto size = ti.next.tsize; // array element size
auto cursize = arr.length * size;
auto bic = __getBlkInfo(arr.ptr);
auto info = bic ? *bic : gc_query(arr.ptr);
if(info.base && (info.attr & BlkAttr.APPENDABLE))
{
if(info.size >= PAGESIZE)
// remove prefix from the current stored size
cursize -= LARGEPREFIX;
debug(PRINTF) printf("setting allocated size to %d\n", (arr.ptr - info.base) + cursize);
__setArrayAllocLength(info, (arr.ptr - info.base) + cursize, false);
}
}
void __doPostblit(void *ptr, size_t len, const TypeInfo ti)
{
// optimize out any type info that does not need postblit.
//if((&ti.postblit).funcptr is &TypeInfo.postblit) // compiler doesn't like this
auto fptr = &ti.postblit;
if(fptr.funcptr is &TypeInfo.postblit)
// postblit has not been overridden, no point in looping.
return;
if(auto tis = cast(TypeInfo_Struct)ti)
{
// this is a struct, check the xpostblit member
auto pblit = tis.xpostblit;
if(!pblit)
// postblit not specified, no point in looping.
return;
// optimized for struct, call xpostblit directly for each element
immutable size = ti.tsize;
const eptr = ptr + len;
for(;ptr < eptr;ptr += size)
pblit(ptr);
}
else
{
// generic case, call the typeinfo's postblit function
immutable size = ti.tsize;
const eptr = ptr + len;
for(;ptr < eptr;ptr += size)
ti.postblit(ptr);
}
}
/**
* set the array capacity. If the array capacity isn't currently large enough
* to hold the requested capacity (in number of elements), then the array is
* resized/reallocated to the appropriate size. Pass in a requested capacity
* of 0 to get the current capacity. Returns the number of elements that can
* actually be stored once the resizing is done.
*/
extern(C) size_t _d_arraysetcapacity(const TypeInfo ti, size_t newcapacity, void[]* p)
in
{
assert(ti);
assert(!(*p).length || (*p).ptr);
}
body
{
// step 1, get the block
auto isshared = ti.classinfo is TypeInfo_Shared.classinfo;
auto bic = !isshared ? __getBlkInfo((*p).ptr) : null;
auto info = bic ? *bic : gc_query((*p).ptr);
auto size = ti.next.tsize;
version (D_InlineAsm_X86)
{
size_t reqsize = void;
asm
{
mov EAX, newcapacity;
mul EAX, size;
mov reqsize, EAX;
jc Loverflow;
}
}
else
{
size_t reqsize = size * newcapacity;
if (newcapacity > 0 && reqsize / newcapacity != size)
goto Loverflow;
}
// step 2, get the actual "allocated" size. If the allocated size does not
// match what we expect, then we will need to reallocate anyways.
// TODO: this probably isn't correct for shared arrays
size_t curallocsize = void;
size_t curcapacity = void;
size_t offset = void;
size_t arraypad = void;
if(info.base && (info.attr & BlkAttr.APPENDABLE))
{
if(info.size <= 256)
{
curallocsize = *(cast(ubyte *)(info.base + info.size - SMALLPAD));
arraypad = SMALLPAD;
}
else if(info.size < PAGESIZE)
{
curallocsize = *(cast(ushort *)(info.base + info.size - MEDPAD));
arraypad = MEDPAD;
}
else
{
curallocsize = *(cast(size_t *)(info.base));
arraypad = LARGEPAD;
}
offset = (*p).ptr - __arrayStart(info);
if(offset + (*p).length * size != curallocsize)
{
curcapacity = 0;
}
else
{
// figure out the current capacity of the block from the point
// of view of the array.
curcapacity = info.size - offset - arraypad;
}
}
else
{
curallocsize = curcapacity = offset = 0;
}
debug(PRINTF) printf("_d_arraysetcapacity, p = x%d,%d, newcapacity=%d, info.size=%d, reqsize=%d, curallocsize=%d, curcapacity=%d, offset=%d\n", (*p).ptr, (*p).length, newcapacity, info.size, reqsize, curallocsize, curcapacity, offset);
if(curcapacity >= reqsize)
{
// no problems, the current allocated size is large enough.
return curcapacity / size;
}
// step 3, try to extend the array in place.
if(info.size >= PAGESIZE && curcapacity != 0)
{
auto extendsize = reqsize + offset + LARGEPAD - info.size;
auto u = gc_extend((*p).ptr, extendsize, extendsize);
if(u)
{
// extend worked, save the new current allocated size
curcapacity = u - offset - LARGEPAD;
return curcapacity / size;
}
}
// step 4, if extending doesn't work, allocate a new array with at least the requested allocated size.
auto datasize = (*p).length * size;
reqsize += __arrayPad(reqsize);
// copy attributes from original block, or from the typeinfo if the
// original block doesn't exist.
info = gc_qalloc(reqsize, (info.base ? info.attr : (!(ti.next.flags & 1) ? BlkAttr.NO_SCAN : 0)) | BlkAttr.APPENDABLE);
if(info.base is null)
goto Loverflow;
// copy the data over.
// note that malloc will have initialized the data we did not request to 0.
auto tgt = __arrayStart(info);
memcpy(tgt, (*p).ptr, datasize);
// handle postblit
__doPostblit(tgt, datasize, ti.next);
if(!(info.attr & BlkAttr.NO_SCAN))
{
// need to memset the newly requested data, except for the data that
// malloc returned that we didn't request.
void *endptr = info.base + reqsize;
void *begptr = tgt + datasize;
// sanity check
assert(endptr >= begptr);
memset(begptr, 0, endptr - begptr);
}
// set up the correct length
__setArrayAllocLength(info, datasize, isshared);
if(!isshared)
__insertBlkInfoCache(info, bic);
*p = (cast(void*)tgt)[0 .. (*p).length];
// determine the padding. This has to be done manually because __arrayPad
// assumes you are not counting the pad size, and info.size does include
// the pad.
if(info.size <= 256)
arraypad = SMALLPAD;
else if(info.size < PAGESIZE)
arraypad = MEDPAD;
else
arraypad = LARGEPAD;
curcapacity = info.size - arraypad;
return curcapacity / size;
Loverflow:
onOutOfMemoryError();
assert(0);
}
/**
* Allocate a new array of length elements.
* ti is the type of the resulting array, or pointer to element.
* (For when the array is initialized to 0)
*/
extern (C) void[] _d_newarrayT(const TypeInfo ti, size_t length)
{
void[] result;
auto size = ti.next.tsize; // array element size
debug(PRINTF) printf("_d_newarrayT(length = x%x, size = %d)\n", length, size);
if (length == 0 || size == 0)
result = null;
else
{
version (D_InlineAsm_X86)
{
asm
{
mov EAX,size ;
mul EAX,length ;
mov size,EAX ;
jc Loverflow ;
}
}
else version(D_InlineAsm_X86_64)
{
asm
{
mov RAX,size ;
mul RAX,length ;
mov size,RAX ;
jc Loverflow ;
}
}
else
{
auto newsize = size * length;
if (newsize / length != size)
goto Loverflow;
size = newsize;
}
// increase the size by the array pad.
auto info = gc_qalloc(size + __arrayPad(size), !(ti.next.flags & 1) ? BlkAttr.NO_SCAN | BlkAttr.APPENDABLE : BlkAttr.APPENDABLE);
debug(PRINTF) printf(" p = %p\n", info.base);
// update the length of the array
auto arrstart = __arrayStart(info);
memset(arrstart, 0, size);
auto isshared = ti.classinfo is TypeInfo_Shared.classinfo;
__setArrayAllocLength(info, size, isshared);
result = arrstart[0..length];
}
return result;
Loverflow:
onOutOfMemoryError();
assert(0);
}
/**
* For when the array has a non-zero initializer.
*/
extern (C) void[] _d_newarrayiT(const TypeInfo ti, size_t length)
{
void[] result;
auto size = ti.next.tsize; // array element size
debug(PRINTF) printf("_d_newarrayiT(length = %d, size = %d)\n", length, size);
if (length == 0 || size == 0)
result = null;
else
{
auto initializer = ti.next.init();
auto isize = initializer.length;
auto q = initializer.ptr;
version (D_InlineAsm_X86)
{
asm
{
mov EAX,size ;
mul EAX,length ;
mov size,EAX ;
jc Loverflow ;
}
}
else version (D_InlineAsm_X86_64)
{
asm
{
mov RAX,size ;
mul RAX,length ;
mov size,RAX ;
jc Loverflow ;
}
}
else
{
auto newsize = size * length;
if (newsize / length != size)
goto Loverflow;
size = newsize;
}
auto info = gc_qalloc(size + __arrayPad(size), !(ti.next.flags & 1) ? BlkAttr.NO_SCAN | BlkAttr.APPENDABLE : BlkAttr.APPENDABLE);
debug(PRINTF) printf(" p = %p\n", info.base);
auto arrstart = __arrayStart(info);
if (isize == 1)
memset(arrstart, *cast(ubyte*)q, size);
else if (isize == int.sizeof)
{
int init = *cast(int*)q;
auto len = size / int.sizeof;
for (size_t u = 0; u < len; u++)
{
(cast(int*)arrstart)[u] = init;
}
}
else
{
for (size_t u = 0; u < size; u += isize)
{
memcpy(arrstart + u, q, isize);
}
}
auto isshared = ti.classinfo is TypeInfo_Shared.classinfo;
__setArrayAllocLength(info, size, isshared);
result = arrstart[0..length];
}
return result;
Loverflow:
onOutOfMemoryError();
assert(0);
}
/**
*
*/
void[] _d_newarrayOpT(alias op)(const TypeInfo ti, size_t ndims, va_list q)
{
debug(PRINTF) printf("_d_newarrayOpT(ndims = %d)\n", ndims);
if (ndims == 0)
return null;
else
{
void[] foo(const TypeInfo ti, va_list ap, size_t ndims)
{
size_t dim;
va_arg(ap, dim);
debug(PRINTF) printf("foo(ti = %p, ti.next = %p, dim = %d, ndims = %d\n", ti, ti.next, dim, ndims);
if (ndims == 1)
{
auto r = op(ti, dim);
return *cast(void[]*)(&r);
}
else
{
auto allocsize = (void[]).sizeof * dim;
auto info = gc_qalloc(allocsize + __arrayPad(allocsize));
auto isshared = ti.classinfo is TypeInfo_Shared.classinfo;
__setArrayAllocLength(info, allocsize, isshared);
auto p = __arrayStart(info)[0 .. dim];
version(X86)
{
va_list ap2;
va_copy(ap2, ap);
}
else version(Win64)
{
va_list ap2;
va_copy(ap2, ap);
}
for (size_t i = 0; i < dim; i++)
{
version (Win64)
{
}
else version(X86_64)
{
__va_list argsave = *cast(__va_list*)ap;
va_list ap2 = &argsave;
}
(cast(void[]*)p.ptr)[i] = foo(ti.next, ap2, ndims - 1);
}
return p;
}
}
version (none)
{
va_list q2;
va_copy(q2, q);
for (size_t i = 0; i < ndims; i++)
{
printf("index %d: %ul\n", i, va_arg!(size_t)(q2));
}
va_end(q2);
}
auto result = foo(ti, q, ndims);
debug(PRINTF) printf("result = %llx\n", result);
va_end(q);
return result;
}
}
/**
*
*/
extern (C) void[] _d_newarraymT(const TypeInfo ti, size_t ndims, ...)
{
debug(PRINTF) printf("_d_newarraymT(ndims = %d)\n", ndims);
if (ndims == 0)
return null;
else
{
va_list q;
version(X86)
va_start(q, ndims);
else version(Win64)
va_start(q, ndims);
else version(X86_64)
va_start(q, __va_argsave);
else
static assert(false, "platform not supported");
return _d_newarrayOpT!(_d_newarrayT)(ti, ndims, q);
}
}
/**
*
*/
extern (C) void[] _d_newarraymiT(const TypeInfo ti, size_t ndims, ...)