-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.cpp
More file actions
206 lines (168 loc) · 3.94 KB
/
strings.cpp
File metadata and controls
206 lines (168 loc) · 3.94 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
//
// strings and string heap
//
#include "doctest.h"
#include "consVM.h"
static const int N_BYTES = 1000 * 1000;
static char space[N_BYTES];
static String* frontier;
static int n_free;
static int required_space(const char* s);
static int required_space(String* s);
static int required_space(int len);
static String* next_string(String* s);
static String* find_string(const char* p);
void init_strings()
{
frontier = (String*) space;
n_free = N_BYTES;
}
void print(String* s)
{
std::cout.write(s->body, s->length);
}
String* intern_string(const char* s)
{
String* p = find_string(s);
if (p != NULL) {
return p;
}
int len = std::strlen(s);
int n = required_space(len);
if (n > n_free) {
gc("string space exhausted");
if (n > n_free) {
LispError("String space exhausted", true);
}
}
p = frontier;
p->forward = p;
p->length = len;
p->flags = 0;
std::strcpy(p->body, s);
frontier = next_string(frontier);
n_free -= n;
return p;
}
static int required_space(const char* s)
{
return required_space(std::strlen(s) + 1); // Account for '\0 terminator
}
static int required_space(String* s)
{
return required_space(s->length);
}
static int required_space(int len)
{
int padded_len = sizeof(String) * (len + sizeof(String) - 1) / sizeof(String);
return sizeof(String) + padded_len;
}
static String* next_string(String* s)
{
return (String*) ((char*) s + required_space(s));
}
static String* find_string(const char* s)
{
String* p = (String*) space;
while (p < frontier)
{
if (std::strcmp(s, p->body) == 0) {
return p;
}
p = next_string(p);
}
return NULL;
}
int sweep_strings()
{
// Find the first not-marked string,
// For marked strings, set forward = self (i.e., not moved)
String* p = (String*) space;
while (p < frontier) {
if (not_marked(p)) {
break;
}
p->forward = p;
p = next_string(p);
}
// If all strings are marked, done
if (p == frontier) {
return 0;
}
// freep points to non-marked string
char* freep = (char*) p;
int nBytesRecovered = 0;
// For all strings up to frontier,
// if marked, update forward = freep
// if not marked, freep += size of string
while (p < frontier) {
if (is_marked(p)) {
p->forward = (String*) freep;
freep += required_space(p);
} else {
nBytesRecovered += required_space(p);
}
p = next_string(p);
}
return nBytesRecovered;
}
void compactify_strings() {
String* p = (String*) space;
// Find the first not-marked string;
// that is, the first avaiable free space
while (p < frontier) {
if (not_marked(p)) {
break;
}
clear_mark(p);
p = next_string(p);
}
String* q = p;
p = next_string(p);
// Relocate all subsequent marked strings
while (p < frontier) {
if (is_marked(p)) {
clear_mark(p);
if (p != p->forward) {
// Sanity check
if (p->forward != q) {
LispError("compactify_strings: inconsistent forwarding address");
}
std::memcpy((char*) q, (char*) p, required_space(p));
}
q = next_string(q);
}
p = next_string(p);
}
frontier = q;
}
// -------------------------------------
void audit_strings()
{
String* p = (String*) space;
int nStrings = 0;
while (p < frontier) {
if (p->type != Tag::STRING_TAG) {
LispError("audit_strings: bad tag");
}
if (p->length != std::strlen(p->body)) {
LispError("audit_strings: bad length");
}
++nStrings;
p = next_string(p);
}
std::cout << "audit_strings: Verified " << nStrings << " strings" << std::endl;
}
// -------------------------------------
TEST_CASE("Strings are correctly stored") {
const char* s = "test case";
String* p = intern_string(s);
REQUIRE(std::strcmp(p->body, s) == 0);
}
TEST_CASE("Strings are unique") {
String* p = intern_string("foo");
String* q = intern_string("fum");
String* r = intern_string("foo");
REQUIRE(p != q);
REQUIRE(p == r);
}