-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathdkp.cc
1303 lines (1133 loc) · 33.6 KB
/
dkp.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file and the rest of this project are under the MIT License.
*
* This program contains toy implementations of several different
* garbage collector algorithms instrumented to produce nice
* visualizations of how the algorithms work. Many corners were cut to
* simplify the code so they are neither general purpose nor
* efficient. It's not all smoke and mirrors, but where smoke and
* mirrors worked, that's what I used.
*
* If you are trying to understand GC algorithms, it's best to read a
* good introductory text such as the Jones Lins book and only look at
* the visualizations this program generates until you understand the
* algorithm.
*
* Ken Fox
* August 2014
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cstdio>
#include <set>
#include <map>
const int HeapSize = 2000;
const int HeapSemiSize = 1000;
const int ImageWordSize = 5;
const int ImageWidthInWords = 25;
const int ImageHeight = (HeapSize / ImageWidthInWords) * ImageWordSize;
const int ImageWidth = ImageWidthInWords * ImageWordSize;
typedef signed short SWd;
typedef unsigned short UWd;
typedef unsigned short Loc;
typedef void (*VisitFn)(Loc loc);
void log_alloc_mem(Loc loc, int size);
void log_free_mem(Loc loc, int size);
void log_init_obj(void *addr, const char *type);
void log_ref_count(Loc loc, int ref_count);
void log_ref_count(void *addr, int ref_count);
void log_get_val(const void *addr);
void log_set_val(void *addr, char val);
void log_set_val(void *addr, int val);
void log_set_ref(void *addr, Loc val);
void log_copy_mem(Loc to, Loc from, int size);
void log_copy_mem(void *to, void *from, int size);
// The Obj classes hold data values stored in Mem.heap.
// A custom type tagging system is used instead of C++ virtual
// because the heap is explicitly managed to demonstrate GC.
// All fields in Obj (or sub-type) must be sizeof(SWd). Hopefully
// the compiler will then layout objects so they map directly
// to an array of Swd. (struct equivalence runs deep...)
// WARNING! None of the value classes can allocate memory!
// If a collection occurs inside a value method, the object
// may move and cause memory corruption. Allocation must
// happen in the Ref classes and those classes must handle
// being moved.
class Obj {
private:
Obj(const Obj &rhs);
Obj &operator=(const Obj &rhs);
public:
static const char *TypeName[];
enum Type { TNil=0, TForward=1, TFree=2, TNum=3, TTup=4, TVec=5, TStr=6 };
struct {
UWd ref_count : 8;
UWd mark : 1;
UWd type : 4;
} header;
void init(Type type) {
log_init_obj(&header, TypeName[type]);
header.type = type;
init_ref_count();
header.mark = 0;
}
static Obj *at(Loc loc);
Type type() const { return (Type)header.type; }
void init_ref_count() {
#if REF_COUNT_GC
header.ref_count = 1;
log_ref_count(&header, header.ref_count);
#else
header.ref_count = 0;
#endif
}
void inc_ref_count() {
#if REF_COUNT_GC
header.ref_count += 1;
log_ref_count(&header, header.ref_count);
#endif
}
bool dec_ref_count() {
#if REF_COUNT_GC
header.ref_count -= 1;
log_ref_count(&header, header.ref_count);
if (header.ref_count == 0) {
cleanup();
return true;
}
else {
return false;
}
#else
return false;
#endif
}
void traverse(VisitFn f) const;
void fixup_references();
void cleanup();
UWd size() const;
SWd to_i() const;
bool equals(const Obj *that) const;
void dump() const;
};
// The Ref classes represent pointers to data values stored in mem.
// A raw pointer must never be exposed in a place where a GC may
// happen because the C++ registers, stack and temporaries are not
// treated as roots.
// Refs never move because they are not allocated in Mem::heap,
// however, the loc value (which is a numeric offset from the start of
// the heap) in a Ref may change at any time.
class ObjRef {
private:
friend class Mem;
ObjRef &operator=(const ObjRef &rhs);
static ObjRef *root;
ObjRef *prev;
ObjRef *next;
void add_to_root_set() {
prev = 0;
if (root) {
root->prev = this;
next = root;
}
else {
next = 0;
}
root = this;
}
protected:
enum RefType { ALLOC, COPY, SHARE };
volatile Loc loc;
ObjRef(RefType type, UWd loc_or_size, UWd new_size = 0);
public:
static ObjRef *nil;
ObjRef(const ObjRef &that);
~ObjRef();
Loc share();
static ObjRef at(Loc loc) { return ObjRef(ObjRef::SHARE, loc); }
static void unshare(Loc loc);
Obj *referenced_Obj() const { return Obj::at(loc); }
Obj::Type type() const { return referenced_Obj()->type(); }
UWd size() const { return referenced_Obj()->size(); }
SWd to_i() const { return referenced_Obj()->to_i(); }
void dump() const { referenced_Obj()->dump(); }
bool equals(ObjRef that) const {
return referenced_Obj()->equals(that.referenced_Obj());
}
};
// Pluggable memory management and GC algorithms.
class FreeBlock: public Obj {
public:
UWd len;
UWd size() const { return len; }
};
class ForwardingAddress: public Obj {
public:
UWd to;
};
struct MemInfo {
static uint time;
bool is_allocated;
bool is_overhead;
uint last_write;
uint last_read;
MemInfo() {
is_allocated = false;
is_overhead = false;
last_read = 0;
last_write = 0;
}
void was_allocated() {
is_allocated = true;
is_overhead = false;
last_read = 0;
last_write = 0;
}
void was_freed() { is_allocated = false; }
void was_read() { last_read = ++time; }
void was_written() { last_write = ++time; is_overhead = false; }
void was_overhead() { last_write = ++time; is_overhead = true; }
};
class Mem {
public:
// Real GC algorithms use unused heap space for marking the live
// sets and storing forwarding addresses for moved objects.
static std::map<Loc, Loc> forwarding;
static std::set<Loc> live;
static UWd heap[HeapSize];
static MemInfo info[HeapSize]; // visualization info
static Loc top;
static Loc from_space;
static Loc to_space;
static Loc addr_to_loc(const void *addr) {
Loc loc = ((char *)(addr) - (char *)heap) / sizeof(UWd);
assert(loc < HeapSize);
return loc;
}
// TODO: implement a first-fit algorithm instead of just the bump allocator.
// free must add memory back to allocator, blocks should be coalesced
static Loc reserve(UWd size) {
Loc loc = top;
top += size;
assert(top < HeapSize);
log_alloc_mem(loc, size);
return loc;
}
static Loc reserve_with_possible_overlap(UWd size) {
Loc loc = top;
top += size;
assert(top < HeapSize);
return loc;
}
static Loc alloc(UWd size) {
Loc loc = reserve(size);
for (int i = 0; i < size; ++i) {
heap[loc + i] = 0;
}
return loc;
}
static Loc copy(Loc from, UWd new_size = 0) {
UWd size = Obj::at(from)->size();
if (new_size > 0) {
Loc to = reserve(new_size);
UWd min = (new_size < size) ? new_size : size;
for (int i = 0; i < min; ++i) {
heap[to + i] = heap[from + i];
}
for (int i = min; i < new_size; ++i) {
heap[to + i] = 0;
}
log_copy_mem(to, from, min);
return to;
}
else {
Loc to = reserve(size);
for (int i = 0; i < size; ++i) {
heap[to + i] = heap[from + i];
}
log_copy_mem(to, from, size);
return to;
}
}
static Loc move_without_forwarding(Loc from, UWd size) {
Obj *from_obj = Obj::at(from);
Loc to = reserve_with_possible_overlap(size);
for (int i = 0; i < size; ++i) {
heap[to + i] = heap[from + i];
}
log_copy_mem(to, from, size);
return to;
}
static Loc move(Loc from) {
Obj *from_obj = Obj::at(from);
UWd size = from_obj->size();
Loc to = reserve(size);
for (int i = 0; i < size; ++i) {
heap[to + i] = heap[from + i];
}
ForwardingAddress *b = (ForwardingAddress *)from_obj;
b->init(Obj::TForward);
b->to = to;
log_copy_mem(to, from, size);
return to;
}
static Loc read_barrier(Loc loc) {
return loc;
}
static void free(Loc loc, int size) {
FreeBlock *b = (FreeBlock *)Obj::at(loc);
b->init(Obj::TFree);
b->len = size;
log_free_mem(loc, size);
}
static void mark_live_loc(Loc loc) {
if (loc != 0) {
#if !COPY_GC
log_ref_count(loc, 1); // treat marking as ref count for visualization
#endif
live.insert(loc);
}
}
static void mark_live() {
ObjRef *p = ObjRef::root;
live.clear();
while (p) {
Loc loc = p->loc;
mark_live_loc(loc);
Obj::at(loc)->traverse(mark_live_loc);
p = p->next;
}
}
static void sweep_garbage() {
Loc loc = 1;
while (loc < top) {
Obj *obj = Obj::at(loc);
int size = obj->size();
if (live.count(loc) == 0) {
free(loc, size);
}
loc += size;
}
}
static void move_live() {
mark_live();
// nil is located at heap loc 0 and doesn't move
top = (top >= HeapSemiSize) ? 1 : HeapSemiSize;
std::set<Loc>::iterator it;
for (it = live.begin(); it != live.end(); ++it) {
Loc from = *it;
if (from) {
move(from);
}
}
}
static void compact_live() {
forwarding.clear();
mark_live();
Loc old_top = top;
Loc from = 1;
while (from < old_top) {
Obj *obj = Obj::at(from);
int size = obj->size();
if (live.count(from) > 0) {
if (old_top != top) {
Loc to = move_without_forwarding(from, size);
forwarding[from] = to;
}
}
else if (old_top == top) {
top = from;
}
from += size;
}
}
static Loc loc_after_move(Loc loc) {
#if COPY_GC
ForwardingAddress *b = (ForwardingAddress *)Obj::at(loc);
return (b->type() == Obj::TForward) ? b->to : loc;
#else
std::map<Loc,Loc>::iterator it = forwarding.find(loc);
return (it != forwarding.end()) ? it->second : loc;
#endif
}
static void fixup_references() {
ObjRef *p = ObjRef::root;
while (p) {
p->loc = loc_after_move(p->loc);
p = p->next;
}
#if COPY_GC
Loc loc = (top >= HeapSemiSize) ? HeapSemiSize : 1;
#else
Loc loc = 1;
#endif
while (loc < top) {
Obj *obj = Obj::at(loc);
int size = obj->size();
obj->fixup_references();
loc += size;
}
}
static void gc() {
#if MARK_SWEEP_GC
mark_live();
sweep_garbage();
#else
#if COPY_GC
move_live();
fixup_references();
if (top >= HeapSemiSize) {
log_free_mem(1, HeapSemiSize - 1);
}
else {
log_free_mem(HeapSemiSize, HeapSemiSize);
}
#else
#if MARK_COMPACT_GC
Loc old_top = top;
compact_live();
if (old_top > top) {
fixup_references();
log_free_mem(top, old_top - top);
}
#endif
#endif
#endif
}
static void add_live_loc(Loc loc) {
live.insert(loc);
}
static void log_roots(std::string msg) {
ObjRef *p = ObjRef::root;
std::cout << "['bp','" << msg << "'],\n";
std::cout << "['roots'";
live.clear();
while (p) {
Loc loc = p->loc;
std::cout << "," << loc;
live.insert(loc);
Obj::at(loc)->traverse(add_live_loc);
p = p->next;
}
std::cout << "],\n";
std::cout << "['live'";
std::set<Loc>::iterator it;
for (it = live.begin(); it != live.end(); ++it) {
std::cout << "," << *it;
}
std::cout << "],\n";
}
static char color_of_mem_loc(Loc loc) {
MemInfo &info = Mem::info[loc];
if (info.is_allocated) {
const char *color;
int age;
if (info.last_read > info.last_write) {
color = "0123456789";
age = info.time - info.last_read;
}
else {
color = "abcdefghij";
age = info.time - info.last_write;
}
if (age == info.time) { return '+'; }
if (age < 5) {
return info.is_overhead ? '#' : color[0];
}
if (age < 25) { return color[1]; }
if (age < 125) { return color[2]; }
return color[3];
}
else {
return ' ';
}
}
// Try to stay under the 2MB spin limit for the resulting animation.
static void snap() {
static int frame = 0;
char xpm_file_name[20];
sprintf(xpm_file_name, "img%08d.xpm", frame++);
std::ofstream xpm_file;
xpm_file.open(xpm_file_name);
xpm_file << "/* XPM */\n"
<< "static char * plaid[] =\n"
<< "{\n"
<< "/* width height ncolors chars_per_pixel */\n"
<< "\"" << ImageWidth << " " << ImageHeight << " 11 1\",\n"
<< "/* colors */\n"
<< "\" c black\",\n"
<< "\"+ c #888888\",\n"
<< "\"# c #ff0000\",\n"
<< "\"0 c #00ff00\",\n" // 22ee22
<< "\"1 c #22cc22\",\n"
<< "\"2 c #22aa22\",\n"
<< "\"3 c #228822\",\n"
<< "\"a c #ffff00\",\n" // eeee22
<< "\"b c #cccc22\",\n"
<< "\"c c #aaaa22\",\n"
<< "\"d c #888822\",\n"
<< "/* pixels */\n";
char row[ImageWordSize][ImageWidth + 1];
for (int py = 0; py < ImageWordSize; ++py) {
row[py][ImageWidth] = 0;
}
int loc_x = 0;
for (Loc loc = 0; loc < HeapSize; ++loc) {
char c = color_of_mem_loc(loc);
for (int py = 0; py < ImageWordSize; ++py) {
for (int px = 0; px < ImageWordSize; ++px) {
row[py][loc_x + px] = c;
}
}
#if 0
if (loc % (frame + 6) == 0) {
row[1][loc_x + 1] = '#';
}
#endif
loc_x += ImageWordSize;
if (loc_x == ImageWidth) {
for (int py = 0; py < ImageWordSize; ++py) {
xpm_file << "\"" << row[py] << "\",\n";
}
loc_x = 0;
}
}
xpm_file << "};\n";
xpm_file.close();
}
};
uint MemInfo::time = 0;
UWd Mem::heap[HeapSize];
MemInfo Mem::info[HeapSize];
Loc Mem::top = 0;
std::map<Loc,Loc> Mem::forwarding;
std::set<Loc> Mem::live;
ObjRef *ObjRef::root = 0;
ObjRef *ObjRef::nil = new ObjRef(ObjRef::SHARE, 0);
static bool log_ready = false;
void log_start() { log_ready = true; }
void log_stop() { log_ready = false; }
#define log_msg(M) if (log_ready) { std::cout << M; Mem::snap(); }
void log_alloc_mem(Loc loc, int size) {
for (int i = 0; i < size; ++i) {
Mem::info[loc + i].was_allocated();
}
log_msg("['alloc'," << loc << ',' << size << "],\n");
}
void log_free_mem(Loc loc, int size) {
for (int i = 0; i < size; ++i) {
Mem::info[loc + i].was_freed();
}
log_msg("['free'," << loc << ',' << size << "],\n");
}
void log_init_obj(void *addr, const char *type) {
if (log_ready) {
std::cout << "['init'," << Mem::addr_to_loc(addr) << ",'" << type << "'],\n";
}
}
void log_ref_count(Loc loc, int ref_count) {
Mem::info[loc].was_overhead();
log_msg("['ref_count'," << loc << "," << ref_count << "],\n");
}
void log_ref_count(void *addr, int ref_count) {
log_ref_count(Mem::addr_to_loc(addr), ref_count);
}
void log_get_val(const void *addr) {
Loc loc = Mem::addr_to_loc(addr);
Mem::info[loc].was_read();
if (log_ready) {
Mem::snap();
}
}
void log_set_val(void *addr, char val) {
Loc loc = Mem::addr_to_loc(addr);
Mem::info[loc].was_written();
log_msg("['set'," << loc << ",\"'" << val << "\"],\n");
}
void log_set_val(void *addr, int val) {
Loc loc = Mem::addr_to_loc(addr);
Mem::info[loc].was_written();
log_msg("['set'," << loc << ",'=" << val << "'],\n");
}
void log_set_ref(void *addr, Loc val) {
Loc loc = Mem::addr_to_loc(addr);
Mem::info[loc].was_written();
log_msg("['set'," << loc << "," << val << "],\n");
}
void log_copy_mem(Loc to, Loc from, int size) {
for (int i = 0; i < size; ++i) {
Mem::info[from + i].was_read();
Mem::info[to + i].was_written();
}
log_msg("['copy'," << to << ',' << from << ',' << size << "],\n");
}
void log_copy_mem(void *to, void *from, int size) {
log_copy_mem(Mem::addr_to_loc(to), Mem::addr_to_loc(from), size);
}
ObjRef::ObjRef(RefType type, UWd loc_or_size, UWd new_size) {
switch (type) {
case ALLOC:
loc = Mem::alloc(loc_or_size);
referenced_Obj()->init_ref_count();
break;
case COPY:
loc = Mem::copy(loc_or_size, new_size);
referenced_Obj()->init_ref_count();
break;
case SHARE:
loc = Mem::read_barrier(loc_or_size);
referenced_Obj()->inc_ref_count();
break;
}
add_to_root_set();
}
ObjRef::ObjRef(const ObjRef &that) {
loc = Mem::read_barrier(that.loc);
referenced_Obj()->inc_ref_count();
add_to_root_set();
}
ObjRef::~ObjRef() {
if (next) { next->prev = prev; }
if (prev) { prev->next = next; }
if (root == this) {
root = next;
}
if (referenced_Obj()->dec_ref_count()) {
Mem::free(loc, referenced_Obj()->size());
}
prev = 0;
next = 0;
loc = 0;
}
Loc ObjRef::share() {
loc = Mem::read_barrier(loc);
referenced_Obj()->inc_ref_count();
return loc;
}
void ObjRef::unshare(Loc loc) {
if (loc) {
Obj *obj = Obj::at(loc);
if (obj->dec_ref_count()) {
Mem::free(loc, obj->size());
}
}
}
// Value classes (not part of the GC system)
class Num: public Obj {
public:
SWd val;
void init(SWd _val) {
Obj::init(TNum);
val = _val;
log_set_val(&val, val);
}
void set(SWd _val) {
val = _val;
log_set_val(&val, val);
}
UWd size() const { return size_needed(); }
SWd to_i() const {
log_get_val(&val);
return val;
}
void dump() const { std::cout << val; }
static UWd size_needed() { return sizeof(Num) / sizeof(UWd); }
};
class NumRef: public ObjRef {
public:
Num *cast_Num() const { return (Num *)referenced_Obj(); }
NumRef(SWd val = 0) : ObjRef(ALLOC, Num::size_needed()) {
cast_Num()->init(val);
}
void set(SWd val) { cast_Num()->set(val); }
};
class Tup: public Obj {
public:
UWd len;
Loc val[];
void init(int _len) {
Obj::init(TTup);
len = _len;
log_set_val(&len, len);
// due to the shallow copy constructor, there may be initial
// values in this tuple which need their ref counts bumped.
for (int i = 0; i < len; ++i) {
if (val[i]) {
Obj::at(val[i])->inc_ref_count();
}
}
}
static Tup *at(Loc loc) { return (Tup *)Obj::at(loc); }
ObjRef get(int i) const {
assert(i < len);
log_get_val(&val[i]);
return ObjRef::at(val[i]);
}
void set(int i, ObjRef obj) {
assert(i < len);
// always increment the ref count before decrementing
// otherwise self-assignment will fail.
Loc tmp = obj.share();
ObjRef::unshare(val[i]);
val[i] = tmp;
log_set_ref(val + i, val[i]);
}
void traverse(VisitFn f) const {
for (int i = 0; i < len; ++i) {
log_get_val(&val[i]);
f(val[i]);
Obj::at(val[i])->traverse(f);
}
}
void fixup_references() {
for (int i = 0; i < len; ++i) {
val[i] = Mem::loc_after_move(val[i]);
}
}
void cleanup() {
for (int i = 0; i < len; ++i) {
ObjRef::unshare(val[i]);
val[i] = 0;
}
}
UWd size() const { return size_needed(len); }
void dump_up_to(int max) const {
std::cout << '[';
for (int i = 0; i < max; ++i) {
if (i > 0) {
std::cout << ',';
}
Obj::at(val[i])->dump();
}
std::cout << ']';
}
void dump() const { dump_up_to(len); }
static UWd size_needed(int len) { return sizeof(Tup) / sizeof(UWd) + len; }
};
class TupRef: public ObjRef {
public:
Tup *cast_Tup() const { return (Tup *)referenced_Obj(); }
TupRef(int len = 2) : ObjRef(ALLOC, Tup::size_needed(len)) {
cast_Tup()->init(len);
}
// FIXME busted if a GC happens during a copy
TupRef(Loc src, int len) : ObjRef(COPY, src, Tup::size_needed(len)) {
cast_Tup()->init(len);
}
int length() const { return cast_Tup()->len; }
ObjRef get(int i) const { return cast_Tup()->get(i); }
void set(int i, ObjRef obj) { cast_Tup()->set(i, obj); }
};
class Vec: public Obj {
public:
UWd len;
Loc tup;
void init(Loc _tup) {
Obj::init(TVec);
len = 0;
tup = _tup; // caller already incremented ref count
log_set_val(&len, len);
log_set_ref(&tup, tup);
}
ObjRef get(int i) const {
assert(i < len);
log_get_val(&tup);
return Tup::at(tup)->get(i);
}
ObjRef get(int i, int j) const {
ObjRef inner = get(i);
assert(inner.type() == TTup || inner.type() == TVec);
if (inner.type() == TTup) {
Tup *inner_tup = (Tup *)inner.referenced_Obj();
return inner_tup->get(j);
}
else {
Vec *inner_vec = (Vec *)inner.referenced_Obj();
return inner_vec->get(j);
}
}
void set(int i, ObjRef obj) {
assert(i < len);
log_get_val(&tup);
Tup::at(tup)->set(i, obj);
}
void traverse(VisitFn f) const {
log_get_val(&tup);
f(tup);
Tup::at(tup)->traverse(f);
}
void fixup_references() {
tup = Mem::loc_after_move(tup);
}
void cleanup() {
ObjRef::unshare(tup);
tup = 0;
}
UWd size() const { return size_needed(len); }
void dump() const { Tup::at(tup)->dump_up_to(len); }
static UWd size_needed(int len) { return sizeof(Vec) / sizeof(UWd); }
};
class VecRef: public ObjRef {
public:
Vec *cast_Vec() const { return (Vec *)referenced_Obj(); }
VecRef(int size = 1) : ObjRef(ALLOC, Vec::size_needed(size)) {
Loc tup = TupRef(size).share();
cast_Vec()->init(tup);
}
VecRef(ObjRef that) : ObjRef(that) {
assert(that.type() == Obj::TVec);
}
int length() const { return cast_Vec()->len; }
ObjRef get(int i) const { return cast_Vec()->get(i); }
ObjRef get(int i, int j) const { return cast_Vec()->get(i, j); }
void set(int i, ObjRef obj) { cast_Vec()->set(i, obj); }
void push(ObjRef obj) {
std::cout << "// push "; obj.dump(); std::cout << '\n';
Vec *vec = cast_Vec();
Tup *tup = Tup::at(vec->tup);
if (tup->len == vec->len) {
Loc new_tup = TupRef(vec->tup, 2 * vec->len).share();
vec = cast_Vec();
ObjRef::unshare(vec->tup);
vec->tup = new_tup;
tup = Tup::at(vec->tup);
log_set_ref(&vec->tup, vec->tup);
}
tup->set(vec->len, obj);
vec->len += 1;
log_set_val(&vec->len, vec->len);
}
bool contains(int j, const ObjRef &obj) {
Vec *vec = cast_Vec();
for (int i = 0; i < vec->len; ++i) {
ObjRef other = vec->get(i, j);
if (obj.equals(other)) {
return true;
}
}
return false;
}
};
class Str: public Obj {
public:
UWd len;
UWd val[]; // array of char
void init(std::string data) {
Obj::init(TStr);
len = data.length();
log_set_val(&len, len);
for (int i = 0; i < len; ++i) {
val[i] = data[i];
log_set_val(val + i, data[i]);
}
}
void init(int _len) {
Obj::init(TStr);
len = _len;
log_set_val(&len, len);
}
int split(char sep, int begin[], int end[]) {
int found = 0;
int last = 0;
for (int i = 0; i < len; ++i) {
log_get_val(&val[i]);
if (val[i] == sep) {
begin[found] = last;
end[found] = i;
last = i + 1;
++found;
}
}
begin[found] = last;
end[found] = len;
return found + 1;
}
void copy(int begin, int end, Str *dest) {
for (int i = 0; i < end - begin; ++i) {
dest->val[i] = val[begin + i];
}
log_copy_mem(dest->val, val + begin, end - begin);
}
UWd size() {
return size_needed(len);
}
SWd to_i() {
SWd n = 0;
int sign = 1;
int i = 0;
while (i < len) {
log_get_val(&val[i]);
if (val[i] == '-') {
sign = -sign;
++i;
}
else {
break;
}
}
while (i < len) {
log_get_val(&val[i]);
if ('0' <= val[i] && val[i] <= '9') {