-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathImmix.cpp
More file actions
7094 lines (5849 loc) · 185 KB
/
Immix.cpp
File metadata and controls
7094 lines (5849 loc) · 185 KB
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
#include <hxcpp.h>
#include <hx/GC.h>
#include <hx/Memory.h>
#include <hx/Thread.h>
#include "../Hash.h"
#include "GcRegCapture.h"
#include <hx/Unordered.h>
#include <mutex>
#include <condition_variable>
#ifdef EMSCRIPTEN
#include <emscripten/stack.h>
#ifdef HXCPP_SINGLE_THREADED_APP
// Use provided tools to measure stack extent
#define HXCPP_EXPLICIT_STACK_EXTENT
#endif
#endif
#include <string>
#include <stdlib.h>
static bool sgIsCollecting = false;
namespace hx
{
int gByteMarkID = 0x10;
int gRememberedByteMarkID = 0x10 | HX_GC_REMEMBERED;
int gFastPath = 0;
int gSlowPath = 0;
}
using hx::gByteMarkID;
using hx::gRememberedByteMarkID;
namespace hx
{
#ifdef HXCPP_GC_DEBUG_ALWAYS_MOVE
enum { gAlwaysMove = true };
typedef hx::UnorderedSet<void *> PointerMovedSet;
PointerMovedSet sgPointerMoved;
#else
enum { gAlwaysMove = false };
#endif
}
#ifdef ANDROID
#include <android/log.h>
#endif
#ifdef HX_WINDOWS
#include <windows.h>
#endif
#include <vector>
#include <stdio.h>
#include <hx/QuickVec.h>
// #define HXCPP_GC_BIG_BLOCKS
#ifndef __has_builtin
#define __has_builtin(x) 0
#endif
namespace {
void DebuggerTrap()
{
static bool triggeredOnce = false;
if (!triggeredOnce)
{
triggeredOnce = true;
#if __has_builtin(__builtin_trap)
__builtin_trap();
#else
*(int *)0=0;
#endif
}
}
}
static bool sgAllocInit = 0;
static bool sgInternalEnable = true;
static void *sgObject_root = 0;
// With virtual inheritance, stack pointers can point to the middle of an object
#ifdef _MSC_VER
// MSVC optimizes by taking the address of an initernal data member
static int sgCheckInternalOffset = sizeof(void *)*2;
static int sgCheckInternalOffsetRows = 1;
#else
static int sgCheckInternalOffset = 0;
static int sgCheckInternalOffsetRows = 0;
#endif
int gInAlloc = false;
// This is recalculated from the other parameters
static size_t sWorkingMemorySize = 10*1024*1024;
#ifdef HXCPP_GC_MOVING
// Just not sure what this shold be
static size_t sgMaximumFreeSpace = 1024*1024*1024;
#else
static size_t sgMaximumFreeSpace = 1024*1024*1024;
#endif
// #define HXCPP_GC_DEBUG_LEVEL 1
#if HXCPP_GC_DEBUG_LEVEL>1
#define PROFILE_COLLECT
#if HXCPP_GC_DEBUG_LEVEL>2
#define SHOW_FRAGMENTATION
#if HXCPP_GC_DEBUG_LEVEL>3
#define SHOW_MEM_EVENTS
#endif
#endif
#endif
// #define SHOW_FRAGMENTATION_BLOCKS
#if defined SHOW_FRAGMENTATION_BLOCKS
#define SHOW_FRAGMENTATION
#endif
#define RECYCLE_LARGE
// HXCPP_GC_DYNAMIC_SIZE
//#define HXCPP_GC_SUMMARY
//#define PROFILE_COLLECT
//#define PROFILE_THREAD_USAGE
//#define HXCPP_GC_VERIFY
//#define HX_GC_VERIFY_ALLOC_START
//#define SHOW_MEM_EVENTS
//#define SHOW_MEM_EVENTS_VERBOSE
//#define SHOW_FRAGMENTATION
//
// Setting this can make it easier to reproduce this problem since it takes
// the timing of the zeroing out of the equation
//#define HX_GC_ZERO_EARLY
// Setting this on windows64 will mean this objects are allocated in the same place,
// which can make native debugging easier
//#define HX_GC_FIXED_BLOCKS
//
// If the problem happens on the same object, you can print info about the object
// every collect so you can see where it goes wrong (usually requires HX_GC_FIXED_BLOCKS)
//#define HX_WATCH
#if defined(HXCPP_GC_VERIFY) && defined(HXCPP_GC_GENERATIONAL)
#define HX_GC_VERIFY_GENERATIONAL
#endif
#ifdef HX_WATCH
void *hxWatchList[] = {
(void *)0x0000000100000200,
(void *)0
};
bool hxInWatchList(void *watch)
{
for(void **t = hxWatchList; *t; t++)
if (*t==watch)
return true;
return false;
}
#endif
#ifdef HX_GC_FIXED_BLOCKS
#ifdef HX_WINDOWS
#include <Memoryapi.h>
#endif
#endif
#ifdef PROFILE_COLLECT
#define HXCPP_GC_SUMMARY
#endif
#ifdef HX_GC_VERIFY_GENERATIONAL
static bool sGcVerifyGenerational = false;
#endif
#if HX_HAS_ATOMIC && (HXCPP_GC_DEBUG_LEVEL==0) && !defined(HXCPP_GC_VERIFY) && !defined(EMSCRIPTEN)
#if defined(HX_MACOS) || defined(HX_WINDOWS) || defined(HX_LINUX)
enum { MAX_GC_THREADS = 4 };
#else
enum { MAX_GC_THREADS = 2 };
#endif
// You can uncomment this for better call stacks if it crashes while collecting
#define HX_MULTI_THREAD_MARKING
#else
enum { MAX_GC_THREADS = 1 };
#endif
#ifdef PROFILE_THREAD_USAGE
static int sThreadMarkCountData[MAX_GC_THREADS+1];
static int sThreadArrayMarkCountData[MAX_GC_THREADS+1];
static int *sThreadMarkCount = sThreadMarkCountData + 1;
static int *sThreadArrayMarkCount = sThreadArrayMarkCountData + 1;
static int sThreadChunkPushCount;
static int sThreadChunkWakes;
static int sSpinCount = 0;
static int sThreadZeroWaits = 0;
static int sThreadZeroPokes = 0;
static int sThreadBlockZeroCount = 0;
static volatile int sThreadZeroMisses = 0;
#endif
enum { MARK_BYTE_MASK = 0x0f };
enum { FULL_MARK_BYTE_MASK = 0x3f };
enum
{
MEM_INFO_USAGE = 0,
MEM_INFO_RESERVED = 1,
MEM_INFO_CURRENT = 2,
MEM_INFO_LARGE = 3,
};
enum GcMode
{
gcmFull,
gcmGenerational,
};
// Start with full gc - since all objects will be new
static GcMode sGcMode = gcmFull;
#ifndef HXCPP_GC_MOVING
#ifdef HXCPP_GC_DEBUG_ALWAYS_MOVE
#define HXCPP_GC_MOVING
#endif
// Enable moving collector...
//#define HXCPP_GC_MOVING
#endif
// Allocate this many blocks at a time - this will increase memory usage % when rounding to block size must be done.
// However, a bigger number makes it harder to release blocks due to pinning
#define IMMIX_BLOCK_GROUP_BITS 5
#ifdef HXCPP_DEBUG
static hx::Object *gCollectTrace = 0;
static bool gCollectTraceDoPrint = false;
static int gCollectTraceCount = 0;
static int sgSpamCollects = 0;
#endif
#if defined(HXCPP_DEBUG) || defined(HXCPP_GC_DEBUG_ALWAYS_MOVE)
volatile int sgAllocsSinceLastSpam = 0;
#endif
#ifdef ANDROID
#define GCLOG(...) __android_log_print(ANDROID_LOG_INFO, "gclog", __VA_ARGS__)
#else
#define GCLOG printf
#endif
#ifdef PROFILE_COLLECT
#define STAMP(t) double t = __hxcpp_time_stamp();
#define MEM_STAMP(t) t = __hxcpp_time_stamp();
static double sLastCollect = __hxcpp_time_stamp();
static int sObjectMarks =0;
static int sAllocMarks =0;
#else
#define STAMP(t)
#define MEM_STAMP(t)
#endif
#if defined(HXCPP_GC_SUMMARY) || defined(HXCPP_GC_DYNAMIC_SIZE)
struct ProfileCollectSummary
{
enum { COUNT = 10 };
double timeWindow[COUNT];
int windowIdx;
double startTime;
double lastTime;
double totalCollecting;
double maxStall;
double spaceFactor;
ProfileCollectSummary()
{
startTime = __hxcpp_time_stamp();
lastTime = startTime;
totalCollecting = 0;
maxStall = 0;
windowIdx = 0;
spaceFactor = 1.0;
for(int i=0;i<COUNT;i++)
timeWindow[i] = 0.1;
}
~ProfileCollectSummary()
{
#ifdef HXCPP_GC_SUMMARY
double time = __hxcpp_time_stamp() - startTime;
GCLOG("Total time : %.2fms\n", time*1000.0);
GCLOG("Collecting time: %.2fms\n", totalCollecting*1000.0);
GCLOG("Max Stall time : %.2fms\n", maxStall*1000.0);
if (time==0) time = 1;
GCLOG(" Fraction : %.2f%%\n",totalCollecting*100.0/time);
#ifdef HXCPP_GC_DYNAMIC_SIZE
GCLOG("Space factor : %.2fx\n",spaceFactor);
#endif
#endif
}
void addTime(double inCollectStart)
{
double now = __hxcpp_time_stamp();
double dt = now-inCollectStart;
if (dt>maxStall)
maxStall = dt;
totalCollecting += dt;
#ifdef HXCPP_GC_DYNAMIC_SIZE
double wholeTime = now - lastTime;
if (wholeTime)
{
lastTime = now;
double ratio = dt/wholeTime;
windowIdx = (windowIdx +1)%COUNT;
timeWindow[windowIdx] = ratio;
double sum = 0;
for(int i=0;i<COUNT;i++)
sum += timeWindow[i];
ratio = sum/COUNT;
if (ratio<0.05)
{
if (spaceFactor>1.0)
{
spaceFactor -= 0.2;
if (spaceFactor<1.0)
spaceFactor = 1.0;
}
}
else if (ratio>0.12)
{
if (ratio>0.2)
spaceFactor += 0.5;
else
spaceFactor += 0.2;
if (spaceFactor>5.0)
spaceFactor = 5.0;
}
}
#endif
}
};
static ProfileCollectSummary profileCollectSummary;
#define PROFILE_COLLECT_SUMMARY_START double collectT0 = __hxcpp_time_stamp();
#define PROFILE_COLLECT_SUMMARY_END profileCollectSummary.addTime(collectT0);
#else
#define PROFILE_COLLECT_SUMMARY_START
#define PROFILE_COLLECT_SUMMARY_END
#endif
// TODO: Telemetry.h ?
#ifdef HXCPP_TELEMETRY
extern void __hxt_gc_realloc(void* old_obj, void* new_obj, int new_size);
extern void __hxt_gc_start();
extern void __hxt_gc_end();
extern void __hxt_gc_after_mark(int gByteMarkID, int markIdByte);
#endif
static int sgTimeToNextTableUpdate = 1;
std::recursive_mutex *gThreadStateChangeLock=nullptr;
std::mutex *gSpecialObjectLock=nullptr;
class LocalAllocator;
enum LocalAllocState { lasNew, lasRunning, lasStopped, lasWaiting, lasTerminal };
static void MarkLocalAlloc(LocalAllocator *inAlloc,hx::MarkContext *__inCtx);
#ifdef HXCPP_VISIT_ALLOCS
static void VisitLocalAlloc(LocalAllocator *inAlloc,hx::VisitContext *__inCtx);
#endif
static void WaitForSafe(LocalAllocator *inAlloc);
static void ReleaseFromSafe(LocalAllocator *inAlloc);
static void ClearPooledAlloc(LocalAllocator *inAlloc);
static void CollectFromThisThread(bool inMajor,bool inForceCompact);
namespace hx
{
int gPauseForCollect = 0x00000000;
StackContext *gMainThreadContext = 0;
unsigned int gImmixStartFlag[128];
int gMarkID = 0x10 << 24;
int gMarkIDWithContainer = (0x10 << 24) | IMMIX_ALLOC_IS_CONTAINER;
int gPrevByteMarkID = 0x2f;
unsigned int gPrevMarkIdMask = ((~0x2f000000) & 0x30000000) | HX_GC_CONST_ALLOC_BIT;
void ExitGCFreeZoneLocked();
DECLARE_FAST_TLS_DATA(StackContext, tlsStackContext);
#ifdef HXCPP_SCRIPTABLE
extern void scriptMarkStack(hx::MarkContext *);
#endif
}
//#define DEBUG_ALLOC_PTR ((char *)0xb68354)
#ifdef HX_WINDOWS
#define ZERO_MEM(ptr, n) ZeroMemory(ptr,n)
#else
#define ZERO_MEM(ptr, n) memset(ptr,0,n)
#endif
// --- Internal GC - IMMIX Implementation ------------------------------
// Some inline implementations ...
// Use macros to allow for mark/move
/*
IMMIX block size, and various masks for converting addresses
*/
#ifdef HXCPP_GC_BIG_BLOCKS
#define IMMIX_BLOCK_BITS 16
typedef unsigned int BlockIdType;
#else
#define IMMIX_BLOCK_BITS 15
typedef unsigned short BlockIdType;
#endif
#define IMMIX_BLOCK_SIZE (1<<IMMIX_BLOCK_BITS)
#define IMMIX_BLOCK_OFFSET_MASK (IMMIX_BLOCK_SIZE-1)
#define IMMIX_BLOCK_BASE_MASK (~(size_t)(IMMIX_BLOCK_OFFSET_MASK))
#define IMMIX_LINE_COUNT_BITS (IMMIX_BLOCK_BITS-IMMIX_LINE_BITS)
#define IMMIX_LINES (1<<IMMIX_LINE_COUNT_BITS)
#define IMMIX_HEADER_LINES (IMMIX_LINES>>IMMIX_LINE_BITS)
#define IMMIX_USEFUL_LINES (IMMIX_LINES - IMMIX_HEADER_LINES)
#define IMMIX_MAX_ALLOC_GROUPS_SIZE (1<<IMMIX_BLOCK_GROUP_BITS)
// Every second line used
#define MAX_HOLES (IMMIX_USEFUL_LINES>>1)
/*
IMMIX Alloc Header - 32 bits
The header is placed in the 4 bytes before the object pointer, and it is placed there using a uint32.
When addressed as the uint32, you can use the bitmasks below to extract the various data.
The "mark id" can conveniently be interpreted as a byte, however the offsets well
be different depending on whether the system is little endian or big endian.
Little endian - lsb first
7 0 15 8 23 16 31 24
-----------------------
| | | | MID | obj start here .....
-----------------------
Big endian - msb first
31 24 23 16 15 8 7 0
-----------------------
|MID | | | | obj start here .....
-----------------------
MID = HX_ENDIAN_MARK_ID_BYTE = is measured from the object pointer
HX_ENDIAN_MARK_ID_BYTE_HEADER = is measured from the header pointer (4 bytes before object)
*/
#define HX_ENDIAN_MARK_ID_BYTE_HEADER (HX_ENDIAN_MARK_ID_BYTE + 4)
// Used by strings
// HX_GC_CONST_ALLOC_BIT 0x80000000
//#define HX_GC_REMEMBERED 0x40000000
#define IMMIX_ALLOC_MARK_ID 0x3f000000
//#define IMMIX_ALLOC_IS_CONTAINER 0x00800000
//#define IMMIX_ALLOC_IS_PINNED not used at object level
//#define HX_GX_STRING_EXTENDED 0x00200000
//#define HX_GC_STRING_HASH 0x00100000
// size will shift-right IMMIX_ALLOC_SIZE_SHIFT (6). Low two bits are 0
#define IMMIX_ALLOC_SIZE_MASK 0x000fff00
#define IMMIX_ALLOC_ROW_COUNT 0x000000ff
#define IMMIX_HEADER_PRESERVE 0x00f00000
#define IMMIX_OBJECT_HAS_MOVED 0x000000fe
// Bigger than this, and they go in the large object pool
#define IMMIX_LARGE_OBJ_SIZE 4000
#ifdef allocString
#undef allocString
#endif
void CriticalGCError(const char *inMessage)
{
// Can't perfrom normal handling because it needs the GC system
#ifdef ANDROID
__android_log_print(ANDROID_LOG_ERROR, "HXCPP", "Critical Error: %s", inMessage);
#elif defined(HX_WINRT)
WINRT_LOG("HXCPP Critical Error: %s\n", inMessage);
#else
printf("Critical Error: %s\n", inMessage);
#endif
DebuggerTrap();
}
enum AllocType { allocNone, allocString, allocObject, allocMarked };
struct BlockDataInfo *gBlockStack = 0;
typedef hx::QuickVec<hx::Object *> ObjectStack;
static std::mutex sThreadPoolLock;
// For threaded marking/block reclaiming
static unsigned int sRunningThreads = 0;
static unsigned int sAllThreads = 0;
static bool sLazyThreads = false;
static bool sThreadPoolInit = false;
enum ThreadPoolJob
{
tpjNone,
tpjMark,
tpjReclaim,
tpjReclaimFull,
tpjCountRows,
tpjAsyncZero,
tpjAsyncZeroJit,
tpjGetStats,
tpjVisitBlocks,
};
int sgThreadCount = 0;
static ThreadPoolJob sgThreadPoolJob = tpjNone;
static bool sgThreadPoolAbort = false;
// Pthreads enters the sleep state while holding a mutex, so it no cost to update
// the sleeping state and thereby avoid over-signalling the condition
bool sThreadSleeping[MAX_GC_THREADS];
std::condition_variable_any* sThreadWake[MAX_GC_THREADS];
bool sThreadJobDoneSleeping = false;
std::condition_variable_any* sThreadJobDone;
static inline void SignalThreadPool(std::condition_variable_any* ioSignal, bool sThreadSleeping)
{
if (sThreadSleeping)
ioSignal->notify_one();
}
static void wakeThreadLocked(int inThreadId)
{
sRunningThreads |= (1<<inThreadId);
sLazyThreads = sRunningThreads != sAllThreads;
SignalThreadPool(sThreadWake[inThreadId],sThreadSleeping[inThreadId]);
}
union BlockData
{
// First 2/4 bytes are not needed for row markers (first 2/4 rows are for flags)
BlockIdType mId;
// First 2/4 rows contain a byte-flag-per-row
unsigned char mRowMarked[IMMIX_LINES];
// Row data as union - don't use first 2/4 rows
unsigned char mRow[IMMIX_LINES][IMMIX_LINE_LEN];
};
struct BlockDataStats
{
void clear() { ZERO_MEM(this, sizeof(BlockDataStats)); }
void add(const BlockDataStats &inOther)
{
rowsInUse += inOther.rowsInUse;
bytesInUse += inOther.bytesInUse;
emptyBlocks += inOther.emptyBlocks;
fraggedBlocks += inOther.fraggedBlocks;
fragScore += inOther.fragScore;
fraggedRows += inOther.fraggedRows;
}
int rowsInUse;
size_t bytesInUse;
int emptyBlocks;
int fragScore;
int fraggedBlocks;
int fraggedRows;
};
static BlockDataStats sThreadBlockDataStats[MAX_GC_THREADS];
#ifdef HXCPP_VISIT_ALLOCS
static hx::VisitContext *sThreadVisitContext = 0;
#endif
namespace hx { void MarkerReleaseWorkerLocked(); }
struct GroupInfo
{
int blocks;
char *alloc;
bool pinned;
bool isEmpty;
int usedBytes;
int usedSpace;
void clear()
{
pinned = false;
usedBytes = 0;
usedSpace = 0;
isEmpty = true;
}
int getMoveScore()
{
return pinned ? 0 : usedSpace-usedBytes;
}
};
hx::QuickVec<GroupInfo> gAllocGroups;
struct HoleRange
{
unsigned short start;
unsigned short length;
};
hx::QuickVec<struct BlockDataInfo *> *gBlockInfo = 0;
static int gBlockInfoEmptySlots = 0;
#define FRAG_THRESH 14
#define ZEROED_NOT 0
#define ZEROED_THREAD 1
#define ZEROED_AUTO 2
// Align padding based on block offset
#ifdef HXCPP_ALIGN_ALLOC
#define ALIGN_PADDING(x) (4-(x&4))
#else
#define ALIGN_PADDING(x) 0
#endif
struct BlockDataInfo
{
int mId;
int mGroupId;
BlockData *mPtr;
unsigned int allocStart[IMMIX_LINES];
HoleRange mRanges[MAX_HOLES];
int mHoles;
int mUsedRows;
int mMaxHoleSize;
int mMoveScore;
int mUsedBytes;
int mFraggedRows;
bool mPinned;
unsigned char mZeroed;
bool mReclaimed;
bool mOwned;
#ifdef HXCPP_GC_GENERATIONAL
bool mHasSurvivor;
#endif
volatile int mZeroLock;
BlockDataInfo(int inGid, BlockData *inData)
{
if (gBlockInfoEmptySlots)
{
for(int i=0;i<gBlockInfo->size();i++)
if ( !(*gBlockInfo)[i] )
{
gBlockInfoEmptySlots--;
mId = i;
(*gBlockInfo)[i] = this;
break;
}
}
else
{
if (gBlockInfo==0)
gBlockInfo = new hx::QuickVec<BlockDataInfo *>;
mId = gBlockInfo->size();
gBlockInfo->push( this );
}
mZeroLock = 0;
mOwned = false;
mGroupId = inGid;
mPtr = inData;
inData->mId = mId;
#ifdef SHOW_MEM_EVENTS
//GCLOG(" create block %d : %p -> %p\n", mId, this, mPtr );
#endif
clear();
}
void clear()
{
mUsedRows = 0;
mUsedBytes = 0;
mFraggedRows = 0;
mPinned = false;
ZERO_MEM(allocStart,sizeof(int)*IMMIX_LINES);
ZERO_MEM(mPtr->mRowMarked+IMMIX_HEADER_LINES, IMMIX_USEFUL_LINES);
mRanges[0].start = IMMIX_HEADER_LINES << IMMIX_LINE_BITS;
mRanges[0].length = IMMIX_USEFUL_LINES << IMMIX_LINE_BITS;
mMaxHoleSize = mRanges[0].length;
mMoveScore = 0;
mHoles = 1;
mZeroed = ZEROED_NOT;
mReclaimed = true;
mZeroLock = 0;
mOwned = false;
}
void makeFull()
{
mUsedRows = IMMIX_USEFUL_LINES;
mUsedBytes = mUsedRows<<IMMIX_LINE_BITS;
mFraggedRows = 0;
memset(mPtr->mRowMarked+IMMIX_HEADER_LINES, 1,IMMIX_USEFUL_LINES);
mRanges[0].start = 0;
mRanges[0].length = 0;
mMaxHoleSize = 0;
mMoveScore = 0;
mHoles = 0;
mZeroed = ZEROED_AUTO;
mReclaimed = true;
mZeroLock = 0;
mOwned = false;
}
void clearBlockMarks()
{
mPinned = false;
#ifdef HXCPP_GC_GENERATIONAL
mHasSurvivor = false;
#endif
}
void clearRowMarks()
{
clearBlockMarks();
ZERO_MEM((char *)mPtr+IMMIX_HEADER_LINES, IMMIX_USEFUL_LINES);
}
inline int GetFreeRows() const { return (IMMIX_USEFUL_LINES - mUsedRows); }
inline int GetFreeData() const { return (IMMIX_USEFUL_LINES - mUsedRows)<<IMMIX_LINE_BITS; }
bool zeroAndUnlock()
{
bool doZero = !mZeroed;
if (doZero)
{
if (!mReclaimed)
reclaim<false>(0);
for(int i=0;i<mHoles;i++)
ZERO_MEM( (char *)mPtr+mRanges[i].start, mRanges[i].length );
mZeroed = ZEROED_THREAD;
}
mZeroLock = 0;
return doZero;
}
bool tryZero()
{
if (mZeroed)
return false;
if (_hx_atomic_compare_exchange(&mZeroLock, 0,1) == 0)
return zeroAndUnlock();
return false;
}
void destroy()
{
#ifdef SHOW_MEM_EVENTS_VERBOSE
GCLOG(" release block %d : %p\n", mId, this );
#endif
(*gBlockInfo)[mId] = 0;
gBlockInfoEmptySlots++;
delete this;
}
bool isEmpty() const { return mUsedRows == 0; }
int getUsedRows() const { return mUsedRows; }
void getStats(BlockDataStats &outStats)
{
outStats.rowsInUse += mUsedRows;
outStats.bytesInUse += mUsedBytes;
outStats.fraggedRows += mFraggedRows;
//outStats.bytesInUse += 0;
outStats.fragScore += mPinned ? (mMoveScore>0?1:0) : mMoveScore;
if (mUsedRows==0)
outStats.emptyBlocks++;
if (mMoveScore> FRAG_THRESH )
outStats.fraggedBlocks++;
}
#ifdef HX_GC_VERIFY_ALLOC_START
void verifyAllocStart()
{
unsigned char *rowMarked = mPtr->mRowMarked;
for(int r = IMMIX_HEADER_LINES; r<IMMIX_LINES; r++)
{
if (!rowMarked[r] && allocStart[r])
{
printf("allocStart set without marking\n");
DebuggerTrap();
}
}
}
#endif
void countRows(BlockDataStats &outStats)
{
unsigned char *rowMarked = mPtr->mRowMarked;
unsigned int *rowTotals = ((unsigned int *)rowMarked) + 1;
// TODO - sse/neon
#ifdef HXCPP_GC_BIG_BLOCKS
unsigned int total = 0;
#else
unsigned int total = rowMarked[2] + rowMarked[3];
#endif
total +=
rowTotals[0] + rowTotals[1] + rowTotals[2] + rowTotals[3] + rowTotals[4] +
rowTotals[5] + rowTotals[6] + rowTotals[7] + rowTotals[8] + rowTotals[9] +
rowTotals[10] + rowTotals[11] + rowTotals[12] + rowTotals[13] + rowTotals[14] +
rowTotals[15] + rowTotals[16] + rowTotals[17] + rowTotals[18] + rowTotals[19] +
rowTotals[20] + rowTotals[21] + rowTotals[22] + rowTotals[23] + rowTotals[24] +
rowTotals[25] + rowTotals[26] + rowTotals[27] + rowTotals[28] + rowTotals[29] +
rowTotals[30] + rowTotals[31] + rowTotals[32] + rowTotals[33] + rowTotals[34] +
rowTotals[35] + rowTotals[36] + rowTotals[37] + rowTotals[38] + rowTotals[39] +
rowTotals[40] + rowTotals[41] + rowTotals[42] + rowTotals[43] + rowTotals[44] +
rowTotals[45] + rowTotals[46] + rowTotals[47] + rowTotals[48] + rowTotals[49] +
rowTotals[50] + rowTotals[51] + rowTotals[52] + rowTotals[53] + rowTotals[54] +
rowTotals[55] + rowTotals[56] + rowTotals[57] + rowTotals[58] + rowTotals[59] +
rowTotals[60] + rowTotals[61] + rowTotals[62];
#ifdef HXCPP_GC_BIG_BLOCKS
rowTotals += 63;
total +=
rowTotals[0] + rowTotals[1] + rowTotals[2] + rowTotals[3] + rowTotals[4] +
rowTotals[5] + rowTotals[6] + rowTotals[7] + rowTotals[8] + rowTotals[9] +
rowTotals[10] + rowTotals[11] + rowTotals[12] + rowTotals[13] + rowTotals[14] +
rowTotals[15] + rowTotals[16] + rowTotals[17] + rowTotals[18] + rowTotals[19] +
rowTotals[20] + rowTotals[21] + rowTotals[22] + rowTotals[23] + rowTotals[24] +
rowTotals[25] + rowTotals[26] + rowTotals[27] + rowTotals[28] + rowTotals[29] +
rowTotals[30] + rowTotals[31] + rowTotals[32] + rowTotals[33] + rowTotals[34] +
rowTotals[35] + rowTotals[36] + rowTotals[37] + rowTotals[38] + rowTotals[39] +
rowTotals[40] + rowTotals[41] + rowTotals[42] + rowTotals[43] + rowTotals[44] +
rowTotals[45] + rowTotals[46] + rowTotals[47] + rowTotals[48] + rowTotals[49] +
rowTotals[50] + rowTotals[51] + rowTotals[52] + rowTotals[53] + rowTotals[54] +
rowTotals[55] + rowTotals[56] + rowTotals[57] + rowTotals[58] + rowTotals[59] +
rowTotals[60] + rowTotals[61] + rowTotals[62] + rowTotals[63];
#endif
mUsedRows = (total & 0xff) + ((total>>8) & 0xff) + ((total>>16)&0xff) + ((total>>24)&0xff);
mUsedBytes = mUsedRows<<IMMIX_LINE_BITS;
mZeroLock = 0;
mOwned = false;
outStats.rowsInUse += mUsedRows;
outStats.bytesInUse += mUsedBytes;
outStats.fraggedRows += mFraggedRows;
mFraggedRows = 0;
mHoles = 0;
if (mUsedRows==IMMIX_USEFUL_LINES)
{
// All rows used - write the block off
mMoveScore = 0;
mZeroed = ZEROED_AUTO;
mReclaimed = true;
}
else
{
mZeroed = ZEROED_NOT;
mReclaimed = false;
}
int left = (IMMIX_USEFUL_LINES - mUsedRows) << IMMIX_LINE_BITS;
if (left<mMaxHoleSize)
mMaxHoleSize = left;
}
template<bool FULL>
void reclaim(BlockDataStats *outStats)
{
HoleRange *ranges = mRanges;
ranges[0].length = 0;
mZeroed = ZEROED_NOT;
int usedBytes = 0;
unsigned char *rowMarked = mPtr->mRowMarked;
int r = IMMIX_HEADER_LINES;
// Count unused rows ....
// start on 4-byte boundary...
#ifdef HXCPP_ALIGN_ALLOC
while(r<4 && rowMarked[r]==0)
r++;
if (!rowMarked[r])
#endif
{
while(r<(IMMIX_LINES-4) && *(int *)(rowMarked+r)==0 )
r += 4;
while(r<(IMMIX_LINES) && rowMarked[r]==0)
r++;