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
|
#include "common.hh"
#include "containers.hh"
struct TStaticStringTableBlock {
int TotalTextLength;
char Text[65536];
};
struct TDynamicStringTableBlock {
int FreeEntries;
int TotalTextLength;
bool Dirty;
uint8 EntryType[256];
uint16 StringOffset[256];
char Text[32768];
};
constexpr int StaticBlockSize = NARRAY(TStaticStringTableBlock::Text);
constexpr int DynamicBlockSize = NARRAY(TDynamicStringTableBlock::Text);
constexpr int DynamicBlockEntries = NARRAY(TDynamicStringTableBlock::EntryType);
enum : uint8 {
DYNAMIC_STRING_FREE = 0,
DYNAMIC_STRING_ALLOCATED = 1,
DYNAMIC_STRING_DELETED = 2,
};
static list<TStaticStringTableBlock> StaticStringTable;
static list<TDynamicStringTableBlock> DynamicStringTable;
const char *AddStaticString(const char *String){
int StringLen = (int)strlen(String);
if((StringLen + 1) > StaticBlockSize){
error("AddStaticString: String zu lang (%d).\n", StringLen);
return NULL;
}
if(StringLen == 0){
return "";
}
listnode<TStaticStringTableBlock> *Node = StaticStringTable.firstNode;
while(Node != NULL){
if((Node->data.TotalTextLength + StringLen + 1) <= StaticBlockSize){
break;
}
Node = Node->next;
}
if(Node == NULL){
Node = StaticStringTable.append();
memset(&Node->data, 0, sizeof(TStaticStringTableBlock));
}
TStaticStringTableBlock *Block = &Node->data;
char *Result = &Block->Text[Block->TotalTextLength];
memcpy(Result, String, StringLen + 1);
Block->TotalTextLength += StringLen + 1;
return Result;
}
uint32 AddDynamicString(const char *String){
int StringLen = (int)strlen(String);
if((StringLen + 1) > DynamicBlockSize){
error("AddDynamicString: String zu lang (%d)\n", StringLen);
return 0;
}
if(StringLen == 0){
return 0;
}
int BlockIndex = 0;
listnode<TDynamicStringTableBlock> *Node = DynamicStringTable.firstNode;
while(Node != NULL){
if(Node->data.FreeEntries > 0){
if((Node->data.TotalTextLength + StringLen + 1) <= DynamicBlockSize){
break;
}
}
Node = Node->next;
BlockIndex += 1;
}
if(Node == NULL){
Node = DynamicStringTable.append();
memset(&Node->data, 0, sizeof(TDynamicStringTableBlock));
Node->data.FreeEntries = DynamicBlockEntries;
}
TDynamicStringTableBlock *Block = &Node->data;
int EntryIndex = -1;
for(int i = 0; i < DynamicBlockEntries; i += 1){
if(Block->EntryType[i] == DYNAMIC_STRING_FREE){
EntryIndex = i;
break;
}
}
if(EntryIndex == -1){
error("AddDynamicString: Keinen freien Platz gefunden.\n");
return 0;
}
int StringOffset = Block->TotalTextLength;
Block->FreeEntries -= 1;
Block->TotalTextLength += StringLen + 1;
Block->EntryType[EntryIndex] = DYNAMIC_STRING_ALLOCATED;
Block->StringOffset[EntryIndex] = (uint16)StringOffset;
memcpy(&Block->Text[StringOffset], String, StringLen + 1);
return (uint32)(1 + BlockIndex * DynamicBlockEntries + EntryIndex);
}
const char *GetDynamicString(uint32 Number){
if(Number == 0){
return NULL;
}
int BlockIndex = (int)(Number - 1) / DynamicBlockEntries;
int EntryIndex = (int)(Number - 1) % DynamicBlockEntries;
listnode<TDynamicStringTableBlock> *Node = DynamicStringTable.firstNode;
while(BlockIndex > 0 && Node != NULL){
Node = Node->next;
BlockIndex -= 1;
}
if(Node == NULL){
error("GetDynamicString: Block für String %u existiert nicht\n", Number);
return NULL;
}
TDynamicStringTableBlock *Block = &Node->data;
if(Block->EntryType[EntryIndex] != DYNAMIC_STRING_ALLOCATED){
error("GetDynamicString: Eintrag für String %u existiert nicht\n", Number);
return NULL;
}
int StringOffset = (int)Block->StringOffset[EntryIndex];
return &Block->Text[StringOffset];
}
void DeleteDynamicString(uint32 Number){
if(Number == 0){
return;
}
int BlockIndex = (int)(Number - 1) / DynamicBlockEntries;
int EntryIndex = (int)(Number - 1) % DynamicBlockEntries;
listnode<TDynamicStringTableBlock> *Node = DynamicStringTable.firstNode;
while(BlockIndex > 0 && Node != NULL){
Node = Node->next;
BlockIndex -= 1;
}
if(Node == NULL){
error("DeleteDynamicString: Block für String %u existiert nicht\n", Number);
return;
}
TDynamicStringTableBlock *Block = &Node->data;
if(Block->EntryType[EntryIndex] != DYNAMIC_STRING_ALLOCATED){
error("DeleteDynamicString: Eintrag für String %u existiert nicht\n", Number);
return;
}
Block->EntryType[EntryIndex] = DYNAMIC_STRING_DELETED;
Block->Dirty = true;
}
void CleanupDynamicStrings(void){
// IMPORTANT(fusion): The way we manage dynamic strings also mean pointers
// returned from `GetDynamicString` aren't stable.
for(listnode<TDynamicStringTableBlock> *Node = DynamicStringTable.firstNode;
Node != NULL;
Node = Node->next){
TDynamicStringTableBlock *Block = &Node->data;
if(!Block->Dirty){
continue;
}
for(int i = 0; i < DynamicBlockEntries; i += 1){
if(Block->EntryType[i] == DYNAMIC_STRING_DELETED){
int StringOffset = Block->StringOffset[i];
char *String = &Block->Text[StringOffset];
int StringLen = (int)strlen(String);
int StringEnd = StringOffset + StringLen + 1;
Block->EntryType[i] = DYNAMIC_STRING_FREE;
if(StringEnd > DynamicBlockSize){
error("CleanupDynamicStrings: Stringende fehlt\n");
continue;
}
if(StringEnd < DynamicBlockSize){
memmove(String, String + StringEnd, DynamicBlockSize - StringEnd);
}
for(int j = 0; j < DynamicBlockEntries; j += 1){
if(Block->EntryType[j] != DYNAMIC_STRING_FREE
&& Block->StringOffset[j] > StringOffset){
Block->StringOffset[j] -= StringLen + 1;
}
}
}
}
}
}
void InitStrings(void){
// no-op
}
void ExitStrings(void){
// no-op
}
|