forked from dlang/druntime
-
Notifications
You must be signed in to change notification settings - Fork 3
/
allocator.d
1062 lines (970 loc) · 28.1 KB
/
allocator.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
module core.allocator;
import core.stdc.stdlib;
import core.stdc.stdio;
import core.sync.mutex;
import core.stdc.string; //for memcpy
import core.hashmap;
import core.refcounted;
import core.traits;
version(DUMA)
{
extern(C)
{
//void * _duma_allocate(size_t alignment, size_t userSize, int protectBelow, int fillByte, int protectAllocList, enum _DUMA_Allocator allocator, enum _DUMA_FailReturn fail, const char * filename, int lineno);
//void _duma_deallocate(void * baseAdr, int protectAllocList, enum _DUMA_Allocator allocator, const char * filename, int lineno);
void * _duma_malloc(size_t size, const char * filename, int lineno);
void * _duma_calloc(size_t elemCount, size_t elemSize, const char * filename, int lineno);
void _duma_free(void * baseAdr, const char * filename, int lineno);
void * _duma_memalign(size_t alignment, size_t userSize, const char * filename, int lineno);
int _duma_posix_memalign(void **memptr, size_t alignment, size_t userSize, const char * filename, int lineno);
void * _duma_realloc(void * baseAdr, size_t newSize, const char * filename, int lineno);
void * _duma_valloc(size_t size, const char * filename, int lineno);
char * _duma_strdup(const char *str, const char * filename, int lineno);
void * _duma_memcpy(void *dest, const void *src, size_t size, const char * filename, int lineno);
char * _duma_strcpy(char *dest, const char *src, const char * filename, int lineno);
char * _duma_strncpy(char *dest, const char *src, size_t size, const char * filename, int lineno);
char * _duma_strcat(char *dest, const char *src, const char * filename, int lineno);
char * _duma_strncat(char *dest, const char *src, size_t size, const char * filename, int lineno);
}
}
extern (C) void rt_finalize2(void* p, bool det = true, bool resetMemory = true);
version(Windows)
{
import core.sys.windows.stacktrace;
}
enum InitializeMemoryWith
{
NOTHING,
NULL,
INIT
}
struct PointerHashPolicy
{
static uint Hash(void* ptr)
{
//Usually pointers are at least 4 byte aligned if they come out of a allocator
static if(size_t.sizeof <= uint.sizeof)
return (cast(size_t)ptr) / 4;
else
return cast(uint)((cast(size_t)ptr / 8) % uint.max);
}
static bool equals(void* lhs, void* rhs)
{
return (lhs is rhs);
}
}
private {
__gshared bool g_isStdAlloactorInit = false;
__gshared bool g_allowMemoryTracking = true;
extern(C) void _initStdAllocator(bool allowMemoryTracking)
{
if(g_isStdAlloactorInit)
return;
g_stdAllocatorMem[] = typeid(StdAllocator).init[];
g_stdAllocator = cast(StdAllocator)g_stdAllocatorMem.ptr;
static if(is(typeof(g_stdAllocator.__ctor())))
{
g_stdAllocator.__ctor();
}
g_isStdAlloactorInit = true;
g_allowMemoryTracking = allowMemoryTracking;
}
extern(C) void _initMemoryTracking()
{
version(MEMORY_TRACKING)
{
if(g_allowMemoryTracking)
StdAllocator.globalInstance.InitMemoryTracking();
}
}
extern(C) void _deinitMemoryTracking()
{
version(MEMORY_TRACKING)
{
if(g_allowMemoryTracking)
StdAllocator.globalInstance.DeinitMemoryTracking();
}
Destruct(g_stdAllocator);
}
}
interface IAllocator
{
/**
* allocates a block of memory
* Params:
* size = the size in bytes to allocate
* Returns: the allocated block of memory or a empty array if allocation failed
*/
void[] AllocateMemory(size_t size);
/**
* Frees a block of memory
* Params:
* mem = pointer to the start of the memory block to free
*/
void FreeMemory(void* mem);
}
interface IAdvancedAllocator : IAllocator
{
/**
* tries to resize a block of memory, if not possible allocates a new block and copies all data to it
* Params:
* mem = pointer to the block of memory which should be resized
* size = new size the block should have in bytes
*/
void[] ReallocateMemory(void* mem, size_t size);
}
private class TrackingAllocator : IAdvancedAllocator
{
final void[] AllocateMemory(size_t size)
{
version(DUMA)
return _duma_memalign(size_t.sizeof,size,__FILE__,__LINE__)[0..size];
else
return malloc(size)[0..size];
}
final void[] ReallocateMemory(void* mem, size_t size)
{
version(DUMA)
{
void* temp = _duma_realloc(mem,size,__FILE__,__LINE__);
assert(cast(size_t)temp / size_t.sizeof == 0,"alignment changed");
return temp[0..size];
}
else
return realloc(mem,size)[0..size];
}
final void FreeMemory(void* mem)
{
version(DUMA)
return _duma_free(mem,__FILE__,__LINE__);
else
free(mem);
}
}
private TrackingAllocator g_trackingAllocator;
class StdAllocator : IAdvancedAllocator
{
alias g_stdAllocator globalInstance;
static if(size_t.sizeof == 4)
{
enum size_t alignment = 4; // default alignment 4 byte (32 bit)
}
else
{
enum size_t alignment = 16; // default alignment 16 byte (64 bit)
}
enum BlockFlags
{
IsClass = 0x01
}
static struct MemoryBlockInfo
{
size_t size;
size_t[14] backtrace;
byte backtraceSize;
ubyte flags; //combination of BlockFlags
this(size_t size)
{
this.size = size;
}
uint Hash()
{
return hashOf(backtrace.ptr, typeof(backtrace[0]).sizeof * backtraceSize);
}
bool opEquals(ref const(MemoryBlockInfo) rh)
{
if(backtraceSize != rh.backtraceSize)
return false;
for(int i=0; i<backtraceSize; i++)
{
if(backtrace[i] != rh.backtrace[i])
return false;
}
return true;
}
}
version(MEMORY_TRACKING)
{
public static struct TraceInfo
{
char[][14] trace;
size_t count;
}
}
//this should return NULL when the standard allocator should be used
//if this does not return NULL the returned memory is used instead
//Needs to be thread safe !
alias void* delegate(size_t size, size_t alignment) OnAllocateMemoryDelegate;
//This should return true if it frees the memory, otherwise the standard allocator will free it
//Needs to be thread safe !
alias bool delegate(void* ptr) OnFreeMemoryDelegate;
//This should return NULL if it did not reallocate the memory
//otherwise the standard allocator will reallocate the memory
//Needs to be thread safe !
alias void* delegate(void* ptr, size_t newSize) OnReallocateMemoryDelegate;
//The following two members are only needed for the memory tracking version
//We leave them always in to not change the memory layout beetween the two versions
private Mutex m_allocMutex;
private Hashmap!(void*, MemoryBlockInfo, PointerHashPolicy, TrackingAllocator) m_memoryMap;
OnAllocateMemoryDelegate OnAllocateMemoryCallback;
OnFreeMemoryDelegate OnFreeMemoryCallback;
OnReallocateMemoryDelegate OnReallocateMemoryCallback;
version(MEMORY_TRACKING)
{
final void InitMemoryTracking()
{
printf("initializing memory tracking\n");
initializeStackTracing();
g_trackingAllocator = New!TrackingAllocator();
m_memoryMap = AllocatorNew!(typeof(m_memoryMap), TrackingAllocator)(g_trackingAllocator, g_trackingAllocator);
m_allocMutex = New!Mutex();
}
final void DeinitMemoryTracking()
{
//Set the allocation mutex to null so that any allocations that happen during
//resolving don't get added to the hashmap to prevent recursion and changing of
//the hashmap while it is in use
auto temp = m_allocMutex;
m_allocMutex = null;
auto traceCache = AllocatorNew!(Hashmap!(MemoryBlockInfo, TraceInfo, StdHashPolicy, TrackingAllocator))(g_trackingAllocator, g_trackingAllocator);
printf("deinitializing memory tracking\n");
printf("Found %d memory leaks",m_memoryMap.count);
FILE* log = null;
void* min = null;
void* max = null;
size_t leakSum = 0;
if(m_memoryMap.count > 0)
log = fopen("memoryleaks.log","wb");
foreach(ref void* addr, ref leak; m_memoryMap)
{
leakSum += leak.size;
if( !traceCache.exists(leak) )
{
ulong[14] backtrace;
for(int i=0; i<leak.backtraceSize; i++)
{
backtrace[i] = leak.backtrace[i];
}
rcstring lines[14];
auto trace = StackTrace.resolve(backtrace[0..leak.backtraceSize], lines);
TraceInfo traceinfo;
traceinfo.count = 1;
int i=0;
foreach(ref line; trace)
{
line ~= "\0";
if(i < traceinfo.trace.length)
{
traceinfo.trace[i] = AllocatorNewArray!char(g_trackingAllocator, line.length);
traceinfo.trace[i][] = line[];
i++;
}
}
traceCache[leak] = traceinfo;
}
else
{
traceCache[leak].count++;
}
if(min == null)
{
min = addr;
max = addr;
}
else
{
min = (min > addr) ? addr : min;
max = (max < addr) ? addr : max;
}
}
foreach(ref leak, ref info; traceCache)
{
printf("---------------------------------\n");
if(log !is null) fprintf(log,"---------------------------------\n");
printf("memory leak %d bytes %d times\n",leak.size, info.count);
if(log !is null) fprintf(log,"memory leak %d bytes %d times\n",leak.size, info.count);
for(int i=0; i<leak.backtraceSize; i++)
{
printf("%s\n",info.trace[i].ptr);
if(log !is null) fprintf(log, "%s\n",info.trace[i].ptr);
}
}
if( m_memoryMap.count > 0)
{
//find root leaks
auto nonRootMap = AllocatorNew!(Hashmap!(void*, bool, PointerHashPolicy, TrackingAllocator), TrackingAllocator)(g_trackingAllocator, g_trackingAllocator);
foreach(void* addr, ref leak; m_memoryMap)
{
void*[] ptrs = (cast(void**)addr)[0..(leak.size / (void*).sizeof)];
foreach(ptr; ptrs)
{
if(ptr >= min && ptr <= max && cast(size_t)ptr % 4 == 0)
{
if(m_memoryMap.exists(ptr))
{
nonRootMap[ptr] = true;
}
}
}
}
if(m_memoryMap.count > nonRootMap.count)
{
printf("---------------------------------\n");
printf("ROOTS\n");
if(log !is null)
{
fprintf(log, "---------------------------------\n");
fprintf(log, "ROOTS\n");
}
foreach(void* addr, ref leak; m_memoryMap)
{
if(nonRootMap.exists(addr)) //skip non roots
continue;
printf("---------------------------------\n");
if(log !is null) fprintf(log,"---------------------------------\n");
printf("memory leak %d bytes\n",leak.size);
if(log !is null) fprintf(log,"memory leak %d bytes\n",leak.size);
if( traceCache.exists(leak) )
{
auto info = &traceCache[leak];
for(int i=0; i<leak.backtraceSize; i++)
{
printf("%s\n",info.trace[i].ptr);
if(log !is null) fprintf(log, "%s\n",info.trace[i].ptr);
}
}
if( leak.flags & BlockFlags.IsClass )
{
Object obj = cast(Object)addr;
TypeInfo t = obj.classinfo;
if(t is null)
{
printf("Already destroyed object\n");
if(log !is null) fprintf(log, "Already destroyed object\n");
}
else if(t.classinfo is null)
{
printf("Invalid classinfo\n");
if(log !is null) fprintf(log, "Invalid classinfo\n");
}
else
{
string name = t.GetName();
printf("Object of type '");
if(log !is null) fprintf(log, "Object of type '");
for(int i=0; i<name.length; i++)
{
printf("%c", name[i]);
if(log !is null) fprintf(log, "%c", name[i]);
}
printf("'\n");
if(log !is null) fprintf(log, "'\n");
}
RefCounted refCounted = cast(RefCounted)obj;
if(refCounted !is null)
{
printf("Reference counted object: ref-count = %d\n", refCounted.refcount);
if(log !is null) fprintf(log, "Reference counted object: ref-count = %d\n", refCounted.refcount);
}
}
}
}
printf("\ntotal leaked memory %d bytes\nTotal leaks %d\n", leakSum, m_memoryMap.count);
if(log !is null) fprintf(log, "\ntotal leaked memory %d bytes\nTotal leaks %d\n", leakSum, m_memoryMap.count);
Delete(nonRootMap);
//close logfile
if(log !is null) fclose(log);
debug
{
version(GNU)
asm { "int $0x3"; }
else
asm { int 3; } //if you ended up here, you have memory leaks, see memoryleaks.log for details
}
}
AllocatorDelete(g_trackingAllocator, traceCache);
Delete(temp);
Delete(m_memoryMap);
Delete(g_trackingAllocator);
}
}
public void SetIsClass(void* mem)
{
if(m_memoryMap !is null && m_memoryMap.exists(mem))
{
m_memoryMap[mem].flags |= BlockFlags.IsClass;
Object obj = cast(Object)mem;
TypeInfo t = obj.classinfo;
if(t is null || t.classinfo is null)
{
version(D_InlineAsm_X86)
asm { int 3; }
else version(GNU)
asm { "int $0x3"; }
}
}
}
final void[] AllocateMemory(size_t size)
{
void* mem = (OnAllocateMemoryCallback is null) ? null : OnAllocateMemoryCallback(size,alignment);
if(mem is null)
{
version(DUMA)
mem = _duma_memalign(alignment, size, __FILE__, __LINE__);
else
mem = malloc(size);
}
version(MEMORY_TRACKING)
{
if(m_allocMutex !is null)
{
synchronized(m_allocMutex)
{
//printf("adding %x (%d)to memory map\n",mem,size);
auto info = MemoryBlockInfo(size);
// TODO implement for linux / osx
version(Windows)
{
ulong backtrace[14];
info.backtraceSize = cast(byte)StackTrace.trace(backtrace, 3).length;
for(int i=0; i<info.backtraceSize; i++)
{
info.backtrace[i] = cast(size_t)backtrace[i];
}
}
auto map = m_memoryMap;
map[mem] = info;
}
}
}
return mem[0..size];
}
final void[] ReallocateMemory(void* ptr, size_t size)
{
version(MEMORY_TRACKING)
{
if( m_allocMutex !is null)
{
synchronized( m_allocMutex )
{
size_t oldSize = 0;
if(ptr !is null)
{
assert( m_memoryMap.exists(ptr), "trying to realloc already freed memory");
oldSize = m_memoryMap[ptr].size;
assert( oldSize < size, "trying to realloc smaller size");
}
void *mem = (OnReallocateMemoryCallback is null) ? null : OnReallocateMemoryCallback(ptr,size);
if(mem is null)
{
version(DUMA)
{
mem = _duma_memalign(size_t.sizeof,size,__FILE__,__LINE__);
if( ptr !is null)
{
_duma_memcpy(mem,ptr,oldSize,__FILE__,__LINE__);
_duma_free(ptr,__FILE__,__LINE__);
}
}
else
{
mem = realloc(ptr,size);
}
}
if(mem != ptr)
{
if(ptr !is null)
{
//printf("realloc removing %x\n",ptr);
m_memoryMap.remove(ptr);
}
auto info = MemoryBlockInfo(size);
// TODO implement for linux / osx
version(Windows)
{
ulong backtrace[14];
info.backtraceSize = cast(byte)StackTrace.trace(backtrace, 3).length;
for(int i=0; i<info.backtraceSize; i++)
{
info.backtrace[i] = cast(size_t)backtrace[i];
}
}
//printf("realloc adding %x(%d)\n",mem,size);
m_memoryMap[mem] = info;
}
else
{
//printf("changeing size of %x to %d\n",mem,size);
m_memoryMap[mem].size = size;
}
return mem[0..size];
}
}
}
void *mem = (OnReallocateMemoryCallback is null) ? null : OnReallocateMemoryCallback(ptr,size);
if(mem is null)
{
version(DUMA)
{
throw New!Error("Nested duma reallocate");
}
else
{
mem = realloc(ptr,size);
}
}
return mem[0..size];
}
final void FreeMemory(void* ptr)
{
version(MEMORY_TRACKING)
{
if( ptr !is null)
{
if( m_allocMutex !is null)
{
synchronized( m_allocMutex )
{
//printf("removing %x from memory map\n",ptr);
assert( m_memoryMap.exists(ptr), "double free");
m_memoryMap.remove(ptr);
}
}
}
}
if(OnFreeMemoryCallback is null || !OnFreeMemoryCallback(ptr))
{
version(DUMA)
_duma_free(ptr,__FILE__,__LINE__);
else
free(ptr);
}
}
}
__gshared StdAllocator g_stdAllocator;
__gshared void[__traits(classInstanceSize, StdAllocator)] g_stdAllocatorMem = void;
auto New(T,ARGS...)(ARGS args)
{
return AllocatorNew!(T,StdAllocator,ARGS)(StdAllocator.globalInstance, args);
}
string ListAvailableCtors(T)()
{
string result = "";
foreach(t; __traits(getOverloads, T, "__ctor"))
result ~= typeof(t).stringof ~ "\n";
return result;
}
auto AllocatorNew(T,AT,ARGS...)(AT allocator, ARGS args)
{
static if(is(T == class))
{
size_t memSize = __traits(classInstanceSize,T);
static assert(!__traits(compiles, { T temp; bool test = temp.outer !is null; }), "inner classes are not implemented yet");
}
else {
size_t memSize = T.sizeof;
}
void[] mem = allocator.AllocateMemory(memSize);
debug {
assert(mem.ptr !is null,"Out of memory");
auto address = cast(size_t)mem.ptr;
auto alignment = T.alignof;
assert(address % alignment == 0,"Missaligned memory");
}
//initialize
static if(is(T == class))
{
auto ti = typeid(StripModifier!T);
assert(memSize == ti.init.length,"classInstanceSize and typeid(T).init.length do not match");
mem[] = (cast(void[])ti.init)[];
auto result = (cast(T)mem.ptr);
static if(is(typeof(result.__ctor(args))))
{
scope(failure)
{
AllocatorDelete(allocator, result);
}
result.__ctor(args);
}
else
{
static assert(args.length == 0 && !is(typeof(&T.__ctor)),
"Don't know how to initialize an object of type "
~ T.stringof ~ " with arguments:\n" ~ ARGS.stringof ~ "\nAvailable ctors:\n" ~ ListAvailableCtors!T() );
}
static if(is(AT == StdAllocator))
{
allocator.SetIsClass(mem.ptr);
}
static if(__traits(hasMember, T, "SetAllocator"))
{
result.SetAllocator(allocator);
}
static if(is(T : RefCounted))
{
return ReturnRefCounted!T(result);
}
else
{
return result;
}
}
else
{
auto ti = typeid(StripModifier!T);
assert(memSize == ti.init().length, "T.sizeof and typeid(T).init.length do not match");
if(ti.init().ptr is null)
memset(mem.ptr, 0, mem.length);
else
mem[] = (cast(void[])ti.init())[];
auto result = (cast(T*)mem);
static if(ARGS.length > 0 && is(typeof(result.__ctor(args))))
{
result.__ctor(args);
}
else static if(ARGS.length > 0)
{
static assert(args.length == 0 && !is(typeof(&T.__ctor)),
"Don't know how to initialize an object of type "
~ T.stringof ~ " with arguments " ~ args.stringof ~ "\nAvailable ctors:\n" ~ ListAvailableCtors!T() );
}
return result;
}
}
/**
* Deletes an object / array and destroys it
*/
void Delete(T)(T mem)
{
AllocatorDelete!(T,StdAllocator)(StdAllocator.globalInstance, mem);
}
/**
* Frees the memory pointed at by the object / array
*/
void Free(T)(T mem)
{
AlloactorFree!(T,StdAllocator)(StdAllocator.globalInstance, mem);
}
void Destruct(Object obj)
{
rt_finalize2(cast(void*)obj, false, false);
}
struct DefaultCtor {}; //call default ctor type
enum defaultCtor = DefaultCtor();
struct composite(T)
{
static if(size_t.sizeof == 4)
{
align(4):
}
else
{
align(16):
}
static assert(is(T == class),"can only composite classes");
void[__traits(classInstanceSize, T)] _classMemory = void;
bool m_destructed = false;
debug {
T _instance;
}
else
{
@property T _instance()
{
return cast(T)_classMemory.ptr;
}
@property const(T) _instance() const
{
return cast(const(T))_classMemory.ptr;
}
}
alias _instance this;
@disable this();
@disable this(this); //prevent evil stuff from happening
this(DefaultCtor c){
};
void construct(ARGS...)(ARGS args) //TODO fix: workaround because constructor can not be a template BUG 4749
{
_classMemory[] = typeid(T).init[];
T result = (cast(T)_classMemory.ptr);
static if(is(typeof(result.__ctor(args))))
{
result.__ctor(args);
}
else
{
static assert(args.length == 0 && !is(typeof(T.__ctor)),
"Don't know how to initialize an object of type "
~ T.stringof ~ " with arguments:\n" ~ ARGS.stringof ~ "\nAvailable ctors:\n" ~ ListAvailableCtors!T() );
}
debug _instance = result;
}
void destruct()
{
assert(!m_destructed);
Destruct(_instance);
debug _instance = null;
m_destructed = true;
}
~this()
{
if(!m_destructed)
{
Destruct(_instance);
m_destructed = true;
}
}
}
//TODO should work with dmd 2.063
//static assert((composite!Object).alignof == StdAllocator.alignment);
void AllocatorDelete(T,AT)(AT allocator, T obj)
{
static assert(!is(T U == composite!U), "can not delete composited instance");
static if(is(T : RefCounted))
{
assert(obj.refcount == 0, "trying to delete reference counted object which is still referenced");
}
static if(is(T == class))
{
if(obj is null)
return;
rt_finalize2(cast(void*)obj, false, false);
allocator.FreeMemory(cast(void*)obj);
}
else static if(is(T == interface))
{
if(obj is null)
return;
Object realObj = cast(Object)obj;
if(realObj is null)
return;
rt_finalize2(cast(void*)realObj, false, false);
allocator.FreeMemory(cast(void*)realObj);
}
else static if(is(T P == U*, U))
{
if(obj is null)
return;
callDtor(obj);
allocator.FreeMemory(cast(void*)obj);
}
else static if(is(T P == U[], U))
{
if(!obj)
return;
callDtor(obj);
allocator.FreeMemory(cast(void*)obj.ptr);
}
else
{
static assert(0, "Can not delete " ~ T.stringof);
}
}
void AllocatorFree(T,AT)(AT allocator, T obj)
{
static assert(!is(T U == composite!U), "can not free composited instance");
static if(is(T == class))
{
if(obj is null)
return;
allocator.FreeMemory(cast(void*)obj);
}
else static if(is(T P == U*, U))
{
if(obj is null)
return;
allocator.FreeMemory(cast(void*)obj);
}
else static if(is(T P == U[], U))
{
if(!obj)
return;
allocator.FreeMemory(cast(void*)obj.ptr);
}
}
auto NewArray(T)(size_t size, InitializeMemoryWith init = InitializeMemoryWith.INIT){
return AllocatorNewArray!(T,StdAllocator)(StdAllocator.globalInstance, size,init);
}
auto AllocatorNewArray(T,AT)(AT allocator, size_t size, InitializeMemoryWith init = InitializeMemoryWith.INIT)
{
if(size == 0)
return cast(T[])[];
size_t memSize = T.sizeof * size;
void* mem = allocator.AllocateMemory(memSize).ptr;
T[] data = (cast(T*)mem)[0..size];
final switch(init)
{
case InitializeMemoryWith.NOTHING:
break;
case InitializeMemoryWith.INIT:
static if(is(T == struct))
{
const(void[]) initMem = typeid(T).init();
if(initMem.ptr is null)
{
memset(data.ptr, 0, data.length * T.sizeof);
}
else
{
foreach(ref e;data)
{
memcpy(&e,initMem.ptr,initMem.length);
}
}
}
else static if(!is(T == void))
{
foreach(ref e; data)
{
e = T.init;
}
}
break;
case InitializeMemoryWith.NULL:
memset(mem,0,memSize);
break;
}
return data;
}
void callPostBlit(T)(T subject)
{
static if(is(T U == V[], V))
{
static if(is(V == struct))
{
static if(is(typeof(subject[0].__postblit)))
{
foreach(ref el; subject)
{
el.__postblit();
}
}
else
{
TypeInfo_Struct tt = typeid(V);
if(tt.xpostblit !is null)
{
foreach(ref el; subject)
{
(*(tt.xpostblit))(&el);
}
}
}
}
}
else static if(is(T P == U*, U))
{
static if(is(U == struct))
{
static if(is(typeof(subject.__postblit)))
{
subject.__postblit();
}
else
{
TypeInfo_Struct tt = typeid(U);
if(tt.xpostblit !is null)
{
(*(tt.xpostblit))(subject);
}
}
}
}
else
{
static if(is(T == struct))
{
static assert(0, "can not call postblit on copy");
}
}
}
void callDtor(T)(T subject)
{
static if(is(T U == V[], V))
{
static if(is(V == struct))
{
auto typeinfo = typeid(V);
if(typeinfo.xdtor !is null)
{
foreach(ref el; subject)
{
typeinfo.xdtor(&el);
}
}
//TODO: structs are currently only destroyable over a typeinfo object, fix
/*static if(is(typeof(subject[0].__fieldDtor)))
{
foreach(ref el; subject)
el.__fieldDtor();
}
else static if(is(typeof(subject[0].__dtor)))
{
foreach(ref el; subject)
el.__dtor();
}*/
}
}
else static if(is(T P == U*, U))
{
static if(is(U == struct))
{
auto typeinfo = typeid(U);
if(typeinfo.xdtor !is null)
{