-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collectable.h
1655 lines (1493 loc) · 48.5 KB
/
Collectable.h
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
#pragma once
#include "GCState.h"
#include "spooky.h"
#include <iostream>
//#define ENSURE_THROW(cond, exception) \
// do { int __afx_condVal=!!(cond); assert(__afx_condVal); if (!(__afx_condVal)){exception;} } while (false)
//#define ENSURE(cond) ENSURE_THROW(cond, throw std::runtime_error(#cond " failed") )
//#define ENSURE(cond)
enum _before_ { _BEFORE_, _END_ };
enum _after_ { _AFTER_, _START_ };
enum _sentinel_ { _SENTINEL_ };
class CircularDoubleList;
void merge_from_to(CircularDoubleList* source, CircularDoubleList* dest);
namespace GC {
void merge_collected();
void init_thread(bool combine_thread);
}
class CircularDoubleList
{
friend void merge_from_to(CircularDoubleList* source, CircularDoubleList* dest);
friend void GC::merge_collected();
friend void GC::init_thread(bool);
//because every collectable class has to be derived from this, give things obscure names so they don't polute the namespace for user instance variables.
CircularDoubleList* circular_double_list_prev;
CircularDoubleList* circular_double_list_next;
protected:
bool circular_double_list_is_sentinel;
public:
//doesn't conform to a standard iterator, though I suppose I could make it
//It is a bald iterator for a circular list with a sentinal.
//it's bidirectional, and it's always safe to delete at the iterator and then
//shift forward or reverse... except that you should never delete the sentinal
//execept from an already empty list.
class circular_double_list_iterator
{
CircularDoubleList* center;
CircularDoubleList* next;
CircularDoubleList* prev;
public:
circular_double_list_iterator() {}
circular_double_list_iterator(CircularDoubleList& c) :center(&c), next(c.circular_double_list_next), prev(c.circular_double_list_prev) {}
circular_double_list_iterator& remove() {
//center->fake_delete();
delete center;
return *this;
}
CircularDoubleList& operator*() { return *center; }
bool operator ++() { center = next; prev = center->circular_double_list_prev; next = center->circular_double_list_next; return !center->sentinel(); }
bool operator --() { center = prev; prev = center->circular_double_list_prev; next = center->circular_double_list_next; return !center->sentinel(); }
operator bool() { return !center->sentinel(); }
bool operator !() { return center->sentinel(); }
};
virtual void fake_delete() { disconnect(); }
circular_double_list_iterator iterate() { return circular_double_list_iterator(*this); }
bool sentinel() const { return circular_double_list_is_sentinel; }
virtual ~CircularDoubleList() { circular_double_list_next->circular_double_list_prev = circular_double_list_prev; circular_double_list_prev->circular_double_list_next = circular_double_list_next;}
void disconnect() { circular_double_list_next->circular_double_list_prev = circular_double_list_prev; circular_double_list_prev->circular_double_list_next = circular_double_list_next; }
CircularDoubleList(_sentinel_) :circular_double_list_prev(this), circular_double_list_next(this), circular_double_list_is_sentinel(true) {}
CircularDoubleList(_before_, CircularDoubleList* e) :circular_double_list_prev(e->circular_double_list_prev), circular_double_list_next(e), circular_double_list_is_sentinel(false)
{
circular_double_list_next->circular_double_list_prev = circular_double_list_prev->circular_double_list_next = this;
}
CircularDoubleList(_after_, CircularDoubleList* e) :circular_double_list_prev(e), circular_double_list_next(e->circular_double_list_next), circular_double_list_is_sentinel(false)
{
circular_double_list_next->circular_double_list_prev = circular_double_list_prev->circular_double_list_next = this;
}
CircularDoubleList(CircularDoubleList&&) = delete;
CircularDoubleList() = delete;
bool empty() { return circular_double_list_next == circular_double_list_prev; }
};
inline void merge_from_to(CircularDoubleList* source, CircularDoubleList* dest) {
assert(source->sentinel());
assert(dest->sentinel());
if (!source->empty()) {
source->circular_double_list_next->circular_double_list_prev = dest;
source->circular_double_list_prev->circular_double_list_next = dest->circular_double_list_next;
dest->circular_double_list_next->circular_double_list_prev = source->circular_double_list_prev;
dest->circular_double_list_next = source->circular_double_list_next;
source->circular_double_list_next = source->circular_double_list_prev = source;
}
}
template <typename T>
struct RootPtr;
class Collectable;
class InstancePtrBase
{
protected:
public:
GC::SnapPtr value;
Collectable* get_collectable() { return (Collectable *)GC::load_snapshot(&value); }
void mark();
};
template <typename T>
class InstancePtr : public InstancePtrBase
{
void double_ptr_store(T* v) { GC::double_ptr_store(&value, (void*)v); }
public:
T* get() const { return (T*)GC::load(&value); }
void store(T* const v) { GC::write_barrier(&value, (void*)v); }
template<typename U>
auto operator[](U i) const { return (*get())[i]; }
T& operator*() const { return *get(); }
T* operator -> () const { return get(); }
InstancePtr() { GC::double_ptr_store(&value, nullptr); }
// InstancePtr(T * const v) { GC::double_ptr_store(&value, (void*)v); }
explicit InstancePtr( T* v) { double_ptr_store( v); }
template<typename Y>
explicit InstancePtr(const InstancePtr<Y>& o) {
double_ptr_store(o.get());
}
template<typename Y>
void operator = (Y *const o) {
store(o);
}
template<typename Y>
void operator = (const InstancePtr<Y>& o) {
store(o.get());
}
template<typename Y>
explicit InstancePtr(const RootPtr<Y>& o);
template<typename Y>
void operator = (const RootPtr<Y>& o);
};
template< class T, class U >
InstancePtr<T> static_pointer_cast(const InstancePtr<U>& v) noexcept
{
return InstancePtr<T>(static_cast<T*>(v.get()));
}
template< class T, class U >
InstancePtr<T> const_pointer_cast(const InstancePtr<U>& v) noexcept
{
return InstancePtr<T>(const_cast<T*>(v.get()));
}
template< class T, class U >
InstancePtr<T> dynamic_pointer_cast(const InstancePtr<U>& v) noexcept
{
return InstancePtr<T>(dynamic_cast<T*>(v.get()));
}
template< class T, class U >
InstancePtr<T> reinterpret_pointer_cast(const InstancePtr<U>& v) noexcept
{
return InstancePtr<T>(reinterpret_cast<T*>(v.get()));
}
struct RootLetterBase;
namespace GC {
struct ScanLists
{
Collectable* collectables[3];
RootLetterBase* roots[3];
};
extern ScanLists* ScanListsByThread[MAX_COLLECTED_THREADS];
extern int ActiveIndex;
}
struct RootLetterBase : public CircularDoubleList
{
bool owned;
bool was_owned;
virtual GC::SnapPtr* double_ptr() { abort(); return nullptr; }
#ifndef NDEBUG
bool deleted;
void fake_delete() { deleted = true; disconnect(); }
void memtest() {
ENSURE(!deleted);
if (deleted) std::cout << '.';
}
RootLetterBase(_sentinel_) : CircularDoubleList(_SENTINEL_), owned(true), was_owned(true), deleted(false)
{ }
#else
RootLetterBase(_sentinel_) : CircularDoubleList(_SENTINEL_), owned(true), was_owned(true)
{ }
#endif
RootLetterBase(RootLetterBase&&) = delete;
RootLetterBase();
virtual void mark() { abort(); }
virtual ~RootLetterBase()
{
}
};
inline RootLetterBase::RootLetterBase():CircularDoubleList(_START_,GC::ScanListsByThread[GC::MyThreadNumber]->roots[GC::ActiveIndex]),owned(true),was_owned(true)
#ifndef NDEBUG
,deleted(false)
#endif
{
}
template <typename T>
struct RootLetter : public RootLetterBase
{
InstancePtr<T> value;
virtual GC::SnapPtr* double_ptr() { MEM_TEST();
return &value.value; }
virtual void mark() {
MEM_TEST();
Collectable* c = value.get_collectable();
if (c != nullptr)
c->collectable_mark(); }
RootLetter(RootLetter&&) = delete;
RootLetter() {}
RootLetter(T *v) :value(v){}
};
template <typename T>
struct RootPtr
{
RootLetter<T>* var;
void operator = ( T* const o)
{
var->value.store(o);
}
template <typename Y>
void operator = (const RootPtr<Y>& v)
{
var->value.store(v.var->value.get());
}
template <typename Y>
void operator = (const InstancePtr<Y>& v)
{
var->value.store(v.get());
}
template <typename Y>
void operator = (Y* v)
{
var->value.store(v);
}
//template <typename Y>
// void operator = (Y* const v)
// {
// var->value.store(v);
// }
template <typename U>
auto operator[](U i) { return (*get())[i]; }
template <typename U>
auto operator[](U i) const { return (*get())[i]; }
T* get() const
{
return var->value.get();
}
T& operator*() const { return *get(); }
T* operator -> () const
{
return var->value.get();
}
template <typename Y>
RootPtr(Y* const v) :var(new RootLetter<T>(v))
{
GC::log_alloc(sizeof(*var));
}
// RootPtr(RootPtr<T>&& v) = delete;
RootPtr(const InstancePtr<T>& v) :var(new RootLetter<T>(v.get())) {
#ifndef NDEBUG
v->memtest();
var->memtest();
#endif
GC::log_alloc(sizeof(*var));
}
RootPtr(const RootPtr<T>& v) :var(new RootLetter<T>(v.var->value.get())) {
#ifndef NDEBUG
v.var->memtest();
var->memtest();
#endif
GC::log_alloc(sizeof(*var));
}
template <typename Y>
RootPtr (const RootPtr<Y> &v) :var(new RootLetter<T>(v.var->value.get())){
#ifndef NDEBUG
v.var->memtest();
var->memtest();
#endif
GC::log_alloc(sizeof(*var));
}
// template <typename Y>
// RootPtr (const InstancePtr<Y> &v) :var(new RootLetter<T>(v.get())) {
//#ifdef NDEBUG
// var->memtest();
//#endif
// GC::log_alloc(sizeof(*var));
// }
RootPtr() :var(new RootLetter<T>)
{
GC::log_alloc(sizeof(*var));
}
~RootPtr() {
var->owned = false;
if (GC::ThreadState != GC::PhaseEnum::COLLECTING) var->was_owned = false;
}
};
template< class T, class U >
RootPtr<T> static_pointer_cast(const RootPtr<U>& v) noexcept
{
#ifndef NDEBUG
v->memtest();
#endif
return RootPtr<T>(static_cast<T*>(v.var->value.get()));
}
template< class T, class U >
RootPtr<T> const_pointer_cast(const RootPtr<U>& v) noexcept
{
#ifndef NDEBUG
v->memtest();
#endif
return RootPtr<T>(const_cast<T*>(v.var->value.get()));
}
template< class T, class U >
RootPtr<T> const_dynamic_cast(const RootPtr<U>& v) noexcept
{
#ifndef NDEBUG
v->memtest();
#endif
return RootPtr<T>(dynamic_cast<T*>(v.var->value.get()));
}
template< class T, class U >
RootPtr<T> const_reinterpret_cast(const RootPtr<U>& v) noexcept
{
#ifndef NDEBUG
v->memtest();
#endif
return RootPtr<T>(reinterpret_cast<T*>(v.var->value.get()));
}
template<typename T>
template<typename Y>
InstancePtr<T>::InstancePtr(const RootPtr<Y>& v) {
#ifndef NDEBUG
v->memtest();
#endif
double_ptr_store(v.var->value.get());
}
template<typename T>
template<typename Y>
void InstancePtr<T>::operator = (const RootPtr<Y>& v) {
#ifndef NDEBUG
v->memtest();
#endif
store(v.var->value.get());
}
namespace GC {
void merge_collected();
void _do_collection();
void _do_restore_snapshot();
void _end_collection_start_restore_snapshot();
void _do_finalize_snapshot();
}
enum class CollectableEqualityClass
{
by_address,
by_string,
};
#define ONE_COLLECT_THREAD
class Collectable: public CircularDoubleList {
protected:
friend void GC::merge_collected();
friend void GC::_do_collection();
friend void GC::_do_restore_snapshot();
friend void GC::_end_collection_start_restore_snapshot();
friend void GC::_do_finalize_snapshot();
//public:
// bool deleted;
protected:
//when not tracing contains self index
//when tracing points back to where we came from or 0 if that was a root
//when in a free list points to the next free element as an unbiased index into this block
Collectable* collectable_back_ptr;
unsigned int collectable_back_ptr_from_counter : 31;//came from nth snapshot ptr
#ifdef ONE_COLLECT_THREAD
bool collectable_marked : 1; //only one collection thread
#else
std::atomic_bool marked;
#endif
virtual ~Collectable()
{
}
Collectable(_sentinel_) : CircularDoubleList(_SENTINEL_), collectable_back_ptr(nullptr) , collectable_marked(false)
#ifndef NDEBUG
,deleted(false)
#endif
{
}
public:
#ifndef NDEBUG
int32_t deleted;
void memtest() const {
ENSURE(deleted != 0xfeebfdcb);
ENSURE(deleted == 0);
if (deleted == 0xfeebfdcb) std::cout << ':';
}
void fake_delete() {
if (deleted == 0xfeebfdcb) {
std::cout << '$';
return;
}
else if (deleted != 0) {
std::cout << 'C';
//return;
}
if (collectable_marked != 0 && collectable_marked != 0xbf) {
std::cout << 'c';
}
deleted = 0xfeebfdcb; disconnect();
}
#endif
//to keep from blowing the stack when marking a long chain, this is coded as a loop instead of being recursive and back pointers and context
//is stored in the objects themselves instead of on the stack.
void collectable_mark()
{
MEM_TEST();
Collectable* n=nullptr;
Collectable* c = this;
//if (deleted) std::cout << '!';
if (collectable_marked) return;
#ifdef ONE_COLLECT_THREAD
collectable_marked = true;
{
#else
bool got_it = marked.exchange(true);
if (!got_it) {
#endif
int t = total_instance_vars() - 1;
for (;;) {
if (t >= 0) {
n = c->index_into_instance_vars(t)->get_collectable();
if (n!=nullptr){
#ifndef NDEBUG
if (n->deleted) std::cout << '*';
#endif
if (!n->collectable_marked) {
#ifdef ONE_COLLECT_THREAD
n->collectable_marked = true;
{
#else
got_it = marked.exchange(true);
if (!got_it) {
#endif
n->collectable_back_ptr_from_counter = t;
n->collectable_back_ptr = c;
c = n;
t = c->total_instance_vars() - 1;
continue;
}
}
}
--t;
}
else {
if (c == this) return;
n = c;
c = c->collectable_back_ptr;
t = n->collectable_back_ptr_from_counter - 1;
}
}
}
}
//virtual int num_ptrs_in_snapshot() = 0;
//virtual GC::SnapPtr* index_into_snapshot_ptrs(int num) = 0;
//not snapshot, includes ones that could be null because they're live
virtual int total_instance_vars() const = 0;
virtual size_t my_size() const = 0;
virtual InstancePtrBase* index_into_instance_vars(int num) = 0;
virtual void clean_after_collect() {}
virtual CollectableEqualityClass equality_class() const { return CollectableEqualityClass::by_address; }
virtual bool equal(const Collectable *o)
{
CollectableEqualityClass oc = o->equality_class();
if (equality_class() != oc) return false;
return this == o;//assume CollectableEqualityClass::by_address;
}
virtual uint64_t hash() const
{
uint64_t buf = reinterpret_cast<uint64_t>(this);
return spooky_hash64((void *)&buf,8,0xe0f4deda25b832e0);
}
Collectable(Collectable&&) = delete;
Collectable() :CircularDoubleList(_START_, GC::ScanListsByThread[GC::MyThreadNumber]->collectables[GC::ActiveIndex]), collectable_back_ptr(nullptr), collectable_marked(false)
#ifndef NDEBUG
,deleted(false)
#endif
{
}
};
struct CollectableString : public Collectable
{
char* str;
CollectableString(const char* s) :str(_strdup(s)) {}
~CollectableString() { free (str); }
virtual int total_instance_vars() const { return 0; }
virtual size_t my_size() const { return sizeof(*this); }
virtual InstancePtrBase* index_into_instance_vars(int num) { return nullptr; }
virtual void clean_after_collect() {}
virtual CollectableEqualityClass equality_class() const { return CollectableEqualityClass::by_string; }
virtual bool equal(const Collectable* o)
{
CollectableEqualityClass oc = o->equality_class();
if (equality_class() != oc) return false;
return strcmp(str,((CollectableString*)o)->str)==0;
}
virtual uint64_t hash() const
{
return spooky_hash64((void*)str,strlen(str), 0xc243487c4b5ee78e);
}
};
inline std::ostream& operator<<(std::ostream& os, const RootPtr<CollectableString>& o) {
return os << o->str;
}
template<typename T>
struct CollectableBlock : public Collectable
{
InstancePtr<T> block[32];
uint8_t size;
uint8_t reserved;
size_t my_size() { return sizeof(this); }
int total_instance_vars() { return reserved; }
InstancePtrBase* index_into_instance_vars(int num) { return &block[num]; }
bool push_back(const RootPtr<T>& o) {
if (size == 32) return false;
block[size++] = o;
if (size >= reserved) reserved = size;
return true;
}
bool pop_back(const RootPtr<T>& o) {
if (size == 0) return false;
o = block[--size];
block[size] = nullptr;
return true;
}
bool pop_back(const InstancePtr<T>& o) {
if (size == 0) return false;
o = block[--size];
block[size] = nullptr;
return true;
}
CollectableBlock():size(0), reserved(0){}
InstancePtr<T>& operator [] (int i) {
return block[i & 31];
}
InstancePtr<T>& insure (int i) {
int s = i+1;
if (s > size) size = s;
if (size >= reserved) reserved = size;
return block[i & 31];
}
void clear()
{
for (int i = 0; i < size; ++i) block[i] = nullptr;
size = 0;
}
void resize(int s)
{
assert(s < 32);
while (size > s)block[--size] = nullptr;
size = s;
}
};
template<typename T>
struct Collectable2Block : public Collectable
{
InstancePtr< CollectableBlock<T> > block[32];
//uint8_t b_size;
uint8_t b_reserved;
int size;
int b_size(int i=0) { return ((size+i-1) >> 5)+1; }
size_t my_size() { return sizeof(this); }
int total_instance_vars() { return b_reserved; }
InstancePtrBase* index_into_instance_vars(int num) { return &block[num]; }
Collectable2Block() :size(0), b_reserved(0) {}
bool push_back(const RootPtr<T>& o) {
if (size == 32*32) return false;
++size;
if (b_size() > b_size(-1)) {
if (block[b_size() - 1].get() == nullptr) block[b_size() - 1] = cnew (CollectableBlock<T>);
if (b_size() > b_reserved) b_reserved = b_size();
}
return block[b_size() - 1]->push_back(o);
}
bool pop_back(RootPtr<T>& o)
{
if (size == 0) return false;
block[b_size() - 1]->pop_back(o);
--size;
//b_size = ((--size-1) >> 5)+1;
return true;
}
bool pop_back(InstancePtr<T>& o)
{
if (size == 0) return false;
block[b_size() - 1]->pop_back(o);
--size;
//b_size = ((--size-1) >> 5) + 1;
return true;
}
InstancePtr<T>& operator [] (int i) {
return block[31 & (i >> 5)]->operator[](i);
}
InstancePtr<T>& insure(int i) {
int j = 31&(i >> 5);
for (int k = b_size()-1; k <= j; ++k) {
if (block[k].get() == nullptr) {
block[k] = cnew( CollectableBlock<T>);
if (k<j) block[k]->insure((1<<6)-1);
}
}
int s = i+1;
if (s > size) size = s;
if (b_reserved<b_size()) b_reserved = b_size();
return block[j]->insure(i-(j<<5));
}
void clear()
{
GC::safe_point();
for (int i = 0; i < b_size(); ++i) block[i]->clear();
size = 0;
}
void resize(int s)
{
assert(s < 32*32);
if (s == size) return;
if (s > size) {
insure(s - 1);
}
else {
int j = (s - 1) >> 5;
block[j]->resize(s - (j << 5) + 1);
for (int k = j + 1; k < b_size(); ++k) block[k]->clear();
size = s;
}
}
};
template<typename T>
struct Collectable3Block : public Collectable
{
InstancePtr< Collectable2Block<T> > block[32];
int b_size(int i = 0) { return ((size + i - 1) >> 10) + 1; }
uint8_t b_reserved;
int size;
size_t my_size() { return sizeof(this); }
int total_instance_vars() { return b_reserved; }
Collectable3Block() :size(0), b_reserved(0) {}
InstancePtrBase* index_into_instance_vars(int num) { return &block[num]; }
bool push_back(const RootPtr<T>& o) {
if (size == 32 * 32 * 32) return false;
++size;
if (b_size() > b_size(-1)) {
if (block[b_size()-1].get() == nullptr) block[b_size()-1] = cnew (Collectable2Block<T>);
if (b_size() > b_reserved) b_reserved = b_size();
}
return block[b_size() - 1]->push_back(o);
}
bool pop_back(RootPtr<T>& o)
{
if (size == 0) return false;
block[b_size() - 1]->pop_back(o);
--size;
//b_size = ((--size-1) >> 5)+1;
return true;
}
bool pop_back(InstancePtr<T>& o)
{
if (size == 0) return false;
block[b_size() - 1]->pop_back(o);
--size;
//b_size = ((--size-1) >> 5)+1;
return true;
}
InstancePtr<T>& operator [] (int i) {
return block[31&(i >> 10)]->operator [](i);
}
InstancePtr<T>& insure(int i) {
int j = 31 & (i >> 10);
for (int k = b_size() - 1; k <= j; ++k) {
if (block[k].get() == nullptr) {
block[k] = cnew (Collectable2Block<T>);
if (k < j) block[k]->insure((1 << 11) - 1);
}
}
int s = i+1;
if (s > size) size = s;
if (b_reserved < b_size()) b_reserved = b_size();
return block[j]->insure(i-(j<<10));
}
void clear()
{
for (int i = 0; i < b_size(); ++i) {
block[i]->clear();
GC::safe_point();
}
size = 0;
}
void resize(int s)
{
assert(s < 32*32*32);
if (s == size) return;
if (s > size) {
insure(s - 1);
}
else {
int j = (s - 1) >> 10;
block[j]->resize(s - (j << 10) + 1);
for (int k = j + 1; k < b_size(); ++k) block[k]->clear();
size = s;
}
}
};
template<typename T>
struct Collectable4Block : public Collectable
{
InstancePtr< Collectable3Block<T> > block[32];
uint8_t b_reserved;
int size;
int b_size(int i = 0) { return ((size + i - 1) >> 15)+1; }
size_t my_size() { return sizeof(this); }
int total_instance_vars() { return b_reserved; }
Collectable4Block() :size(0), b_reserved(0) {}
InstancePtrBase* index_into_instance_vars(int num) { return &block[num]; }
bool push_back(const RootPtr<T>& o) {
if (size == 32 * 32 * 32) return false;
++size;
if (b_size() > b_size(-1)) {
if (block[b_size() - 1].get() == nullptr) block[b_size() - 1] = cnew (Collectable3Block<T>);
if (b_size() > b_reserved) b_reserved = b_size();
}
return block[b_size() - 1]->push_back(o);
}
bool pop_back(RootPtr<T>& o)
{
if (size == 0) return false;
block[b_size() - 1]->pop_back(o);
--size;
//b_size = ((--size-1) >> 5)+1;
return true;
}
bool pop_back(InstancePtr<T>& o)
{
if (size == 0) return false;
block[b_size() - 1]->pop_back(o);
--size;
//b_size = ((--size-1) >> 5)+1;
return true;
}
InstancePtr<T>& operator [] (int i) {
return block[31 & (i >> 15)]->operator [](i);
}
InstancePtr<T>& insure(int i) {
int j = 31 & (i >> 15);
for (int k = b_size() - 1; k <= j; ++k) {
if (block[k].get() == nullptr) {
block[k] = cnew (Collectable3Block<T>);
if (k < j) block[k]->insure((1 << 16) - 1);
}
}
int s = i+1;
if (s > size) size = s;
if (b_reserved < b_size()) b_reserved = b_size();
return block[j]->insure(i-(j<<15));
}
bool push_front(const RootPtr<T>& o) {
if (size == 32 * 32 * 32 * 32) return false;
if (size > 0) {
insure(size) = (*this)[size - 1];
for (int i = size - 1; i > 0; --i) {
if ((i&1023)==0) GC::safe_point();
(*this)[i] = (*this)[i - 1];
}
(*this)[0] = o;
}
else return push_back(o);
return true;
}
void clear()
{
for (int i = 0; i < b_size(); ++i) {
GC::safe_point();
block[i]->clear();
}
size = 0;
}
void resize(int s)
{
assert(s < 32*32*32*32);
if (s == size) return;
if (s > size) {
insure(s - 1);
}
else {
int j = (s-1) >> 15;
block[j]->resize(s - (j << 15)+1);
for (int k = j + 1; k < b_size(); ++k) block[k]->clear();
size = s;
}
}
};
template<typename T>
class VectorOfCollectable;
template<typename T>
struct CollectableInlineVector : public Collectable
{
T* data;
InstancePtrBase * * instance_counts;
int total_vars;
int size;
/*
void resize(int s) {
if (s < 0)s = 0;
else if (s > reserve) s = reserve;
if (s<size){
for (int i = size - 1; i >= s; --i) {
data[i].~T();
}
}
else if (s > size) {
for (int i = size; i < s; ++i)
new((void *)&data[i]) T();
}
size = s;
}
bool push_back(const T& s) {
if (size == reserve) return false;
data[size++].T(s);
return true;
}
*/
T* operator [](int i)
{
return &data[i];
}
CollectableInlineVector(int s) : instance_counts(nullptr),size(s){
data = new T [s];
total_vars = 0;
for (int i = 0; i < s; ++i) {
total_vars += data[i].total_instance_vars();
}
instance_counts = new InstancePtrBase * [total_vars];
int t = 0;
for (int i = 0; i < s; ++i) {
for (int j = data[i].total_instance_vars() - 1; j >= 0; --j) {
instance_counts[t++] = data[i].index_into_instance_vars(j);
}
}
}
int total_instance_vars() const
{
return total_vars;
}
size_t my_size() const
{
return sizeof(*this)+size*sizeof(T)+ total_vars*sizeof(void *);
}
InstancePtrBase* index_into_instance_vars(int num)
{
return instance_counts[num];
}
~CollectableInlineVector()
{
delete [] data;
delete[] instance_counts;
}
};
/*
template <typename T>
class CollectableInlineVector : public Collectable {
InstancePtr< CollectableInlineVectorUse<T> > data;
public:
int total_instance_vars() const { return 1; }
InstancePtrBase* index_into_instance_vars(int num) { return &data; }
size_t my_size() const { return sizeof(*this) + data->my_size(); }
bool empty() const { return data->size == 0; }
void clear() const { data->resize(0); }
int capacity() const { return data->reserve; }
void reserve(int i)
{
if (i > data->reserve) {
RootPtr< CollectableInlineVectorUse<T> > new_data = cnew( CollectableInlineVectorUse<T>(i));
for (int j = 0; j < data->size; ++j) new_data->push_back(data.data[j]);
data = new_data;
}
}
CollectableInlineVector(int i) { data = cnew (CollectableInlineVectorUse<T>(i)); data->resize(i); }
CollectableInlineVector() { data = cnew( CollectableInlineVectorUse<T> (8)); }
int size() { return data->size(); }
void resize(int i)
{
if (i > size) {
if (i > data->reserve) {
reserve(i * 2);
}
}
data->resize(i);
}
void push_back(const RootPtr<T> v)
{
if (data->size == data->reserve) resize(data->reserve << 1);
if (v == nullptr) data->resize(size() + 1);
else data->push_back(*v.get());
}
T* operator[](int i) { return (*data)[i]; }
};
*/
template<typename T>
struct CollectableVectoreUse : public Collectable
{
int size;
int scan_size;
int reserved;