This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 419
/
Copy pathgc.d
4560 lines (3873 loc) · 133 KB
/
gc.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
/**
* Contains the garbage collector implementation.
*
* Copyright: Copyright Digital Mars 2001 - 2016.
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: Walter Bright, David Friedman, Sean Kelly
*/
/* Copyright Digital Mars 2005 - 2016.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
module gc.impl.conservative.gc;
// D Programming Language Garbage Collector implementation
/************** Debugging ***************************/
//debug = PRINTF; // turn on printf's
//debug = PARALLEL_PRINTF; // turn on printf's
//debug = COLLECT_PRINTF; // turn on printf's
//debug = MARK_PRINTF; // turn on printf's
//debug = PRINTF_TO_FILE; // redirect printf's ouptut to file "gcx.log"
//debug = LOGGING; // log allocations / frees
//debug = MEMSTOMP; // stomp on memory
//debug = SENTINEL; // add underrun/overrrun protection
// NOTE: this needs to be enabled globally in the makefiles
// (-debug=SENTINEL) to pass druntime's unittests.
//debug = PTRCHECK; // more pointer checking
//debug = PTRCHECK2; // thorough but slow pointer checking
//debug = INVARIANT; // enable invariants
//debug = PROFILE_API; // profile API calls for config.profile > 1
/***************************************************/
version = COLLECT_PARALLEL; // parallel scanning
import gc.bits;
import gc.os;
import core.gc.config;
import core.gc.gcinterface;
import rt.util.container.treap;
import cstdlib = core.stdc.stdlib : calloc, free, malloc, realloc;
import core.stdc.string : memcpy, memset, memmove;
import core.bitop;
import core.thread;
static import core.memory;
version (GNU) import gcc.builtins;
debug (PRINTF_TO_FILE) import core.stdc.stdio : sprintf, fprintf, fopen, fflush, FILE;
else import core.stdc.stdio : sprintf, printf; // needed to output profiling results
import core.time;
alias currTime = MonoTime.currTime;
// Track total time spent preparing for GC,
// marking, sweeping and recovering pages.
__gshared Duration prepTime;
__gshared Duration markTime;
__gshared Duration sweepTime;
__gshared Duration pauseTime;
__gshared Duration maxPauseTime;
__gshared Duration maxCollectionTime;
__gshared size_t numCollections;
__gshared size_t maxPoolMemory;
__gshared long numMallocs;
__gshared long numFrees;
__gshared long numReallocs;
__gshared long numExtends;
__gshared long numOthers;
__gshared long mallocTime; // using ticks instead of MonoTime for better performance
__gshared long freeTime;
__gshared long reallocTime;
__gshared long extendTime;
__gshared long otherTime;
__gshared long lockTime;
ulong bytesAllocated; // thread local counter
private
{
extern (C)
{
// to allow compilation of this module without access to the rt package,
// make these functions available from rt.lifetime
void rt_finalizeFromGC(void* p, size_t size, uint attr) nothrow;
int rt_hasFinalizerInSegment(void* p, size_t size, uint attr, const scope void[] segment) nothrow;
// Declared as an extern instead of importing core.exception
// to avoid inlining - see issue 13725.
void onInvalidMemoryOperationError(void* pretend_sideffect = null) @trusted pure nothrow @nogc;
void onOutOfMemoryErrorNoGC() @trusted nothrow @nogc;
}
enum
{
OPFAIL = ~cast(size_t)0
}
}
alias GC gc_t;
/* ============================ GC =============================== */
// register GC in C constructor (_STI_)
extern(C) pragma(crt_constructor) void _d_register_conservative_gc()
{
import core.gc.registry;
registerGCFactory("conservative", &initialize);
}
extern(C) pragma(crt_constructor) void _d_register_precise_gc()
{
import core.gc.registry;
registerGCFactory("precise", &initialize_precise);
}
private GC initialize()
{
import core.stdc.string: memcpy;
auto p = cstdlib.malloc(__traits(classInstanceSize, ConservativeGC));
if (!p)
onOutOfMemoryErrorNoGC();
auto init = typeid(ConservativeGC).initializer();
assert(init.length == __traits(classInstanceSize, ConservativeGC));
auto instance = cast(ConservativeGC) memcpy(p, init.ptr, init.length);
instance.__ctor();
return instance;
}
private GC initialize_precise()
{
ConservativeGC.isPrecise = true;
return initialize();
}
class ConservativeGC : GC
{
// For passing to debug code (not thread safe)
__gshared size_t line;
__gshared char* file;
Gcx *gcx; // implementation
import core.internal.spinlock;
static gcLock = shared(AlignedSpinLock)(SpinLock.Contention.lengthy);
static bool _inFinalizer;
__gshared bool isPrecise = false;
// lock GC, throw InvalidMemoryOperationError on recursive locking during finalization
static void lockNR() @nogc nothrow
{
if (_inFinalizer)
onInvalidMemoryOperationError();
gcLock.lock();
}
this()
{
//config is assumed to have already been initialized
gcx = cast(Gcx*)cstdlib.calloc(1, Gcx.sizeof);
if (!gcx)
onOutOfMemoryErrorNoGC();
gcx.initialize();
if (config.initReserve)
gcx.reserve(config.initReserve << 20);
if (config.disable)
gcx.disabled++;
}
~this()
{
version (linux)
{
//debug(PRINTF) printf("Thread %x ", pthread_self());
//debug(PRINTF) printf("GC.Dtor()\n");
}
if (gcx)
{
gcx.Dtor();
cstdlib.free(gcx);
gcx = null;
}
// TODO: cannot free as memory is overwritten and
// the monitor is still read in rt_finalize (called by destroy)
// cstdlib.free(cast(void*) this);
}
void enable()
{
static void go(Gcx* gcx) nothrow
{
assert(gcx.disabled > 0);
gcx.disabled--;
}
runLocked!(go, otherTime, numOthers)(gcx);
}
void disable()
{
static void go(Gcx* gcx) nothrow
{
gcx.disabled++;
}
runLocked!(go, otherTime, numOthers)(gcx);
}
auto runLocked(alias func, Args...)(auto ref Args args)
{
debug(PROFILE_API) immutable tm = (config.profile > 1 ? currTime.ticks : 0);
lockNR();
scope (failure) gcLock.unlock();
debug(PROFILE_API) immutable tm2 = (config.profile > 1 ? currTime.ticks : 0);
static if (is(typeof(func(args)) == void))
func(args);
else
auto res = func(args);
debug(PROFILE_API) if (config.profile > 1)
lockTime += tm2 - tm;
gcLock.unlock();
static if (!is(typeof(func(args)) == void))
return res;
}
auto runLocked(alias func, alias time, alias count, Args...)(auto ref Args args)
{
debug(PROFILE_API) immutable tm = (config.profile > 1 ? currTime.ticks : 0);
lockNR();
scope (failure) gcLock.unlock();
debug(PROFILE_API) immutable tm2 = (config.profile > 1 ? currTime.ticks : 0);
static if (is(typeof(func(args)) == void))
func(args);
else
auto res = func(args);
debug(PROFILE_API) if (config.profile > 1)
{
count++;
immutable now = currTime.ticks;
lockTime += tm2 - tm;
time += now - tm2;
}
gcLock.unlock();
static if (!is(typeof(func(args)) == void))
return res;
}
uint getAttr(void* p) nothrow
{
if (!p)
{
return 0;
}
static uint go(Gcx* gcx, void* p) nothrow
{
Pool* pool = gcx.findPool(p);
uint oldb = 0;
if (pool)
{
p = sentinel_sub(p);
if (p != pool.findBase(p))
return 0;
auto biti = cast(size_t)(p - pool.baseAddr) >> pool.shiftBy;
oldb = pool.getBits(biti);
}
return oldb;
}
return runLocked!(go, otherTime, numOthers)(gcx, p);
}
uint setAttr(void* p, uint mask) nothrow
{
if (!p)
{
return 0;
}
static uint go(Gcx* gcx, void* p, uint mask) nothrow
{
Pool* pool = gcx.findPool(p);
uint oldb = 0;
if (pool)
{
p = sentinel_sub(p);
if (p != pool.findBase(p))
return 0;
auto biti = cast(size_t)(p - pool.baseAddr) >> pool.shiftBy;
oldb = pool.getBits(biti);
pool.setBits(biti, mask);
}
return oldb;
}
return runLocked!(go, otherTime, numOthers)(gcx, p, mask);
}
uint clrAttr(void* p, uint mask) nothrow
{
if (!p)
{
return 0;
}
static uint go(Gcx* gcx, void* p, uint mask) nothrow
{
Pool* pool = gcx.findPool(p);
uint oldb = 0;
if (pool)
{
p = sentinel_sub(p);
if (p != pool.findBase(p))
return 0;
auto biti = cast(size_t)(p - pool.baseAddr) >> pool.shiftBy;
oldb = pool.getBits(biti);
pool.clrBits(biti, mask);
}
return oldb;
}
return runLocked!(go, otherTime, numOthers)(gcx, p, mask);
}
void *malloc(size_t size, uint bits, const TypeInfo ti) nothrow
{
if (!size)
{
return null;
}
size_t localAllocSize = void;
auto p = runLocked!(mallocNoSync, mallocTime, numMallocs)(size, bits, localAllocSize, ti);
if (!(bits & BlkAttr.NO_SCAN))
{
memset(p + size, 0, localAllocSize - size);
}
return p;
}
//
//
//
private void *mallocNoSync(size_t size, uint bits, ref size_t alloc_size, const TypeInfo ti = null) nothrow
{
assert(size != 0);
debug(PRINTF)
printf("GC::malloc(gcx = %p, size = %d bits = %x, ti = %s)\n", gcx, size, bits, debugTypeName(ti).ptr);
assert(gcx);
//debug(PRINTF) printf("gcx.self = %x, pthread_self() = %x\n", gcx.self, pthread_self());
auto p = gcx.alloc(size + SENTINEL_EXTRA, alloc_size, bits, ti);
if (!p)
onOutOfMemoryErrorNoGC();
debug (SENTINEL)
{
p = sentinel_add(p);
sentinel_init(p, size);
alloc_size = size;
}
gcx.leakDetector.log_malloc(p, size);
bytesAllocated += alloc_size;
debug(PRINTF) printf(" => p = %p\n", p);
return p;
}
BlkInfo qalloc( size_t size, uint bits, const TypeInfo ti) nothrow
{
if (!size)
{
return BlkInfo.init;
}
BlkInfo retval;
retval.base = runLocked!(mallocNoSync, mallocTime, numMallocs)(size, bits, retval.size, ti);
if (!(bits & BlkAttr.NO_SCAN))
{
memset(retval.base + size, 0, retval.size - size);
}
retval.attr = bits;
return retval;
}
void *calloc(size_t size, uint bits, const TypeInfo ti) nothrow
{
if (!size)
{
return null;
}
size_t localAllocSize = void;
auto p = runLocked!(mallocNoSync, mallocTime, numMallocs)(size, bits, localAllocSize, ti);
memset(p, 0, size);
if (!(bits & BlkAttr.NO_SCAN))
{
memset(p + size, 0, localAllocSize - size);
}
return p;
}
void *realloc(void *p, size_t size, uint bits, const TypeInfo ti) nothrow
{
size_t localAllocSize = void;
auto oldp = p;
p = runLocked!(reallocNoSync, mallocTime, numMallocs)(p, size, bits, localAllocSize, ti);
if (p && !(bits & BlkAttr.NO_SCAN))
{
memset(p + size, 0, localAllocSize - size);
}
return p;
}
//
// bits will be set to the resulting bits of the new block
//
private void *reallocNoSync(void *p, size_t size, ref uint bits, ref size_t alloc_size, const TypeInfo ti = null) nothrow
{
if (!size)
{
if (p)
freeNoSync(p);
alloc_size = 0;
return null;
}
if (!p)
return mallocNoSync(size, bits, alloc_size, ti);
debug(PRINTF) printf("GC::realloc(p = %p, size = %llu)\n", p, cast(ulong)size);
Pool *pool = gcx.findPool(p);
if (!pool)
return null;
size_t psize;
size_t biti;
debug(SENTINEL)
{
void* q = p;
p = sentinel_sub(p);
bool alwaysMalloc = true;
}
else
{
alias q = p;
enum alwaysMalloc = false;
}
void* doMalloc()
{
if (!bits)
bits = pool.getBits(biti);
void* p2 = mallocNoSync(size, bits, alloc_size, ti);
debug (SENTINEL)
psize = sentinel_size(q, psize);
if (psize < size)
size = psize;
//debug(PRINTF) printf("\tcopying %d bytes\n",size);
memcpy(p2, q, size);
freeNoSync(q);
return p2;
}
if (pool.isLargeObject)
{
auto lpool = cast(LargeObjectPool*) pool;
auto psz = lpool.getPages(p); // get allocated size
if (psz == 0)
return null; // interior pointer
psize = psz * PAGESIZE;
alias pagenum = biti; // happens to be the same, but rename for clarity
pagenum = lpool.pagenumOf(p);
if (size <= PAGESIZE / 2 || alwaysMalloc)
return doMalloc(); // switching from large object pool to small object pool
auto newsz = lpool.numPages(size);
if (newsz == psz)
{
// nothing to do
}
else if (newsz < psz)
{
// Shrink in place
debug (MEMSTOMP) memset(p + size, 0xF2, psize - size);
lpool.freePages(pagenum + newsz, psz - newsz);
lpool.mergeFreePageOffsets!(false, true)(pagenum + newsz, psz - newsz);
lpool.bPageOffsets[pagenum] = cast(uint) newsz;
}
else if (pagenum + newsz <= pool.npages)
{
// Attempt to expand in place (TODO: merge with extend)
if (lpool.pagetable[pagenum + psz] != B_FREE)
return doMalloc();
auto newPages = newsz - psz;
auto freesz = lpool.bPageOffsets[pagenum + psz];
if (freesz < newPages)
return doMalloc(); // free range too small
debug (MEMSTOMP) memset(p + psize, 0xF0, size - psize);
debug (PRINTF) printFreeInfo(pool);
memset(&lpool.pagetable[pagenum + psz], B_PAGEPLUS, newPages);
lpool.bPageOffsets[pagenum] = cast(uint) newsz;
for (auto offset = psz; offset < newsz; offset++)
lpool.bPageOffsets[pagenum + offset] = cast(uint) offset;
if (freesz > newPages)
lpool.setFreePageOffsets(pagenum + newsz, freesz - newPages);
gcx.usedLargePages += newPages;
lpool.freepages -= newPages;
debug (PRINTF) printFreeInfo(pool);
}
else
return doMalloc(); // does not fit into current pool
alloc_size = newsz * PAGESIZE;
}
else
{
psize = (cast(SmallObjectPool*) pool).getSize(p); // get allocated bin size
if (psize == 0)
return null; // interior pointer
biti = cast(size_t)(p - pool.baseAddr) >> Pool.ShiftBy.Small;
if (pool.freebits.test (biti))
return null;
// allocate if new size is bigger or less than half
if (psize < size || psize > size * 2 || alwaysMalloc)
return doMalloc();
alloc_size = psize;
if (isPrecise)
pool.setPointerBitmapSmall(p, size, psize, bits, ti);
}
if (bits)
{
pool.clrBits(biti, ~BlkAttr.NONE);
pool.setBits(biti, bits);
}
return p;
}
size_t extend(void* p, size_t minsize, size_t maxsize, const TypeInfo ti) nothrow
{
return runLocked!(extendNoSync, extendTime, numExtends)(p, minsize, maxsize, ti);
}
//
//
//
private size_t extendNoSync(void* p, size_t minsize, size_t maxsize, const TypeInfo ti = null) nothrow
in
{
assert(minsize <= maxsize);
}
do
{
debug(PRINTF) printf("GC::extend(p = %p, minsize = %zu, maxsize = %zu)\n", p, minsize, maxsize);
debug (SENTINEL)
{
return 0;
}
else
{
auto pool = gcx.findPool(p);
if (!pool || !pool.isLargeObject)
return 0;
auto lpool = cast(LargeObjectPool*) pool;
size_t pagenum = lpool.pagenumOf(p);
if (lpool.pagetable[pagenum] != B_PAGE)
return 0;
size_t psz = lpool.bPageOffsets[pagenum];
assert(psz > 0);
auto minsz = lpool.numPages(minsize);
auto maxsz = lpool.numPages(maxsize);
if (pagenum + psz >= lpool.npages)
return 0;
if (lpool.pagetable[pagenum + psz] != B_FREE)
return 0;
size_t freesz = lpool.bPageOffsets[pagenum + psz];
if (freesz < minsz)
return 0;
size_t sz = freesz > maxsz ? maxsz : freesz;
debug (MEMSTOMP) memset(pool.baseAddr + (pagenum + psz) * PAGESIZE, 0xF0, sz * PAGESIZE);
memset(lpool.pagetable + pagenum + psz, B_PAGEPLUS, sz);
lpool.bPageOffsets[pagenum] = cast(uint) (psz + sz);
for (auto offset = psz; offset < psz + sz; offset++)
lpool.bPageOffsets[pagenum + offset] = cast(uint) offset;
if (freesz > sz)
lpool.setFreePageOffsets(pagenum + psz + sz, freesz - sz);
lpool.freepages -= sz;
gcx.usedLargePages += sz;
return (psz + sz) * PAGESIZE;
}
}
size_t reserve(size_t size) nothrow
{
if (!size)
{
return 0;
}
return runLocked!(reserveNoSync, otherTime, numOthers)(size);
}
//
//
//
private size_t reserveNoSync(size_t size) nothrow
{
assert(size != 0);
assert(gcx);
return gcx.reserve(size);
}
void free(void *p) nothrow @nogc
{
if (!p || _inFinalizer)
{
return;
}
return runLocked!(freeNoSync, freeTime, numFrees)(p);
}
//
//
//
private void freeNoSync(void *p) nothrow @nogc
{
debug(PRINTF) printf("Freeing %p\n", cast(size_t) p);
assert (p);
Pool* pool;
size_t pagenum;
Bins bin;
size_t biti;
// Find which page it is in
pool = gcx.findPool(p);
if (!pool) // if not one of ours
return; // ignore
pagenum = pool.pagenumOf(p);
debug(PRINTF) printf("pool base = %p, PAGENUM = %d of %d, bin = %d\n", pool.baseAddr, pagenum, pool.npages, pool.pagetable[pagenum]);
debug(PRINTF) if (pool.isLargeObject) printf("Block size = %d\n", pool.bPageOffsets[pagenum]);
bin = cast(Bins)pool.pagetable[pagenum];
// Verify that the pointer is at the beginning of a block,
// no action should be taken if p is an interior pointer
if (bin > B_PAGE) // B_PAGEPLUS or B_FREE
return;
size_t off = (sentinel_sub(p) - pool.baseAddr);
size_t base = baseOffset(off, bin);
if (off != base)
return;
sentinel_Invariant(p);
p = sentinel_sub(p);
if (pool.isLargeObject) // if large alloc
{
biti = cast(size_t)(p - pool.baseAddr) >> pool.ShiftBy.Large;
assert(bin == B_PAGE);
auto lpool = cast(LargeObjectPool*) pool;
// Free pages
size_t npages = lpool.bPageOffsets[pagenum];
debug (MEMSTOMP) memset(p, 0xF2, npages * PAGESIZE);
lpool.freePages(pagenum, npages);
lpool.mergeFreePageOffsets!(true, true)(pagenum, npages);
}
else
{
biti = cast(size_t)(p - pool.baseAddr) >> pool.ShiftBy.Small;
if (pool.freebits.test (biti))
return;
// Add to free list
List *list = cast(List*)p;
debug (MEMSTOMP) memset(p, 0xF2, binsize[bin]);
// in case the page hasn't been recovered yet, don't add the object to the free list
if (!gcx.recoverPool[bin] || pool.binPageChain[pagenum] == Pool.PageRecovered)
{
list.next = gcx.bucket[bin];
list.pool = pool;
gcx.bucket[bin] = list;
}
pool.freebits.set(biti);
}
pool.clrBits(biti, ~BlkAttr.NONE);
gcx.leakDetector.log_free(sentinel_add(p));
}
void* addrOf(void *p) nothrow @nogc
{
if (!p)
{
return null;
}
return runLocked!(addrOfNoSync, otherTime, numOthers)(p);
}
//
//
//
void* addrOfNoSync(void *p) nothrow @nogc
{
if (!p)
{
return null;
}
auto q = gcx.findBase(p);
if (q)
q = sentinel_add(q);
return q;
}
size_t sizeOf(void *p) nothrow @nogc
{
if (!p)
{
return 0;
}
return runLocked!(sizeOfNoSync, otherTime, numOthers)(p);
}
//
//
//
private size_t sizeOfNoSync(void *p) nothrow @nogc
{
assert (p);
debug (SENTINEL)
{
p = sentinel_sub(p);
size_t size = gcx.findSize(p);
return size ? size - SENTINEL_EXTRA : 0;
}
else
{
size_t size = gcx.findSize(p);
return size;
}
}
BlkInfo query(void *p) nothrow
{
if (!p)
{
BlkInfo i;
return i;
}
return runLocked!(queryNoSync, otherTime, numOthers)(p);
}
//
//
//
BlkInfo queryNoSync(void *p) nothrow
{
assert(p);
BlkInfo info = gcx.getInfo(p);
debug(SENTINEL)
{
if (info.base)
{
info.base = sentinel_add(info.base);
info.size = *sentinel_psize(info.base);
}
}
return info;
}
/**
* Verify that pointer p:
* 1) belongs to this memory pool
* 2) points to the start of an allocated piece of memory
* 3) is not on a free list
*/
void check(void *p) nothrow
{
if (!p)
{
return;
}
return runLocked!(checkNoSync, otherTime, numOthers)(p);
}
//
//
//
private void checkNoSync(void *p) nothrow
{
assert(p);
sentinel_Invariant(p);
debug (PTRCHECK)
{
Pool* pool;
size_t pagenum;
Bins bin;
p = sentinel_sub(p);
pool = gcx.findPool(p);
assert(pool);
pagenum = pool.pagenumOf(p);
bin = cast(Bins)pool.pagetable[pagenum];
assert(bin <= B_PAGE);
assert(p == cast(void*)baseOffset(cast(size_t)p, bin));
debug (PTRCHECK2)
{
if (bin < B_PAGE)
{
// Check that p is not on a free list
List *list;
for (list = gcx.bucket[bin]; list; list = list.next)
{
assert(cast(void*)list != p);
}
}
}
}
}
void addRoot(void *p) nothrow @nogc
{
if (!p)
{
return;
}
gcx.addRoot(p);
}
void removeRoot(void *p) nothrow @nogc
{
if (!p)
{
return;
}
gcx.removeRoot(p);
}
@property RootIterator rootIter() @nogc
{
return &gcx.rootsApply;
}
void addRange(void *p, size_t sz, const TypeInfo ti = null) nothrow @nogc
{
if (!p || !sz)
{
return;
}
gcx.addRange(p, p + sz, ti);
}
void removeRange(void *p) nothrow @nogc
{
if (!p)
{
return;
}
gcx.removeRange(p);
}
@property RangeIterator rangeIter() @nogc
{
return &gcx.rangesApply;
}
void runFinalizers(const scope void[] segment) nothrow
{
static void go(Gcx* gcx, const scope void[] segment) nothrow
{
gcx.runFinalizers(segment);
}
return runLocked!(go, otherTime, numOthers)(gcx, segment);
}
bool inFinalizer() nothrow @nogc
{
return _inFinalizer;
}
void collect() nothrow
{
fullCollect();
}
void collectNoStack() nothrow
{
fullCollectNoStack();
}
/**