-
Notifications
You must be signed in to change notification settings - Fork 1
/
atom.c
376 lines (332 loc) · 8.72 KB
/
atom.c
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
/*
Copyright (c) 2003-2006 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "polipo.h"
/* Atoms are interned, read-only reference-counted strings.
Interned means that equality of atoms is equivalent to structural
equality -- you don't need to strcmp, you just compare the AtomPtrs.
This property is used throughout Polipo, e.g. to speed up the HTTP
parser.
Polipo's atoms may contain NUL bytes -- you can use internAtomN to
store any random binary data within an atom. However, Polipo always
terminates your data, so if you store textual data in an atom, you
may use the result of atomString as though it were a (read-only)
C string.
*/
static AtomPtr *atomHashTable;
int used_atoms;
void
initAtoms()
{
atomHashTable = calloc((1 << LOG2_ATOM_HASH_TABLE_SIZE),
sizeof(AtomPtr));
if(atomHashTable == NULL) {
do_log(L_ERROR, "Couldn't allocate atom hash table.\n");
exit(1);
}
used_atoms = 0;
}
AtomPtr
internAtomN(const char *string, int n)
{
AtomPtr atom;
int h;
if(n < 0 || n >= (1 << (8 * sizeof(unsigned short))))
return NULL;
h = hash(0, string, n, LOG2_ATOM_HASH_TABLE_SIZE);
atom = atomHashTable[h];
while(atom) {
if(atom->length == n &&
(n == 0 || memcmp(atom->string, string, n) == 0))
break;
atom = atom->next;
}
if(!atom) {
atom = malloc(sizeof(AtomRec) - 1 + n + 1);
if(atom == NULL) {
return NULL;
}
atom->refcount = 0;
atom->length = n;
/* Atoms are used both for binary data and strings. To make
their use as strings more convenient, atoms are always
NUL-terminated. */
memcpy(atom->string, string, n);
atom->string[n] = '\0';
atom->next = atomHashTable[h];
atomHashTable[h] = atom;
used_atoms++;
}
do_log(D_ATOM_REFCOUNT, "A 0x%lx %d++\n",
(unsigned long)atom, atom->refcount);
atom->refcount++;
return atom;
}
AtomPtr
internAtom(const char *string)
{
return internAtomN(string, strlen(string));
}
AtomPtr
atomCat(AtomPtr atom, const char *string)
{
char buf[128];
char *s = buf;
AtomPtr newAtom;
int n = strlen(string);
if(atom->length + n > 128) {
s = malloc(atom->length + n + 1);
if(s == NULL)
return NULL;
}
memcpy(s, atom->string, atom->length);
memcpy(s + atom->length, string, n);
newAtom = internAtomN(s, atom->length + n);
if(s != buf) free(s);
return newAtom;
}
int
atomSplit(AtomPtr atom, char c, AtomPtr *return1, AtomPtr *return2)
{
char *p;
AtomPtr atom1, atom2;
p = memchr(atom->string, c, atom->length);
if(p == NULL)
return 0;
atom1 = internAtomN(atom->string, p - atom->string);
if(atom1 == NULL)
return -ENOMEM;
atom2 = internAtomN(p + 1, atom->length - (p + 1 - atom->string));
if(atom2 == NULL) {
releaseAtom(atom1);
return -ENOMEM;
}
*return1 = atom1;
*return2 = atom2;
return 1;
}
AtomPtr
internAtomLowerN(const char *string, int n)
{
char *s;
char buf[100];
AtomPtr atom;
if(n < 0 || n >= 50000)
return NULL;
if(n < 100) {
s = buf;
} else {
s = malloc(n);
if(s == NULL)
return NULL;
}
lwrcpy(s, string, n);
atom = internAtomN(s, n);
if(s != buf) free(s);
return atom;
}
AtomPtr
retainAtom(AtomPtr atom)
{
if(atom == NULL)
return NULL;
do_log(D_ATOM_REFCOUNT, "A 0x%lx %d++\n",
(unsigned long)atom, atom->refcount);
assert(atom->refcount >= 1 && atom->refcount < LARGE_ATOM_REFCOUNT);
atom->refcount++;
return atom;
}
void
releaseAtom(AtomPtr atom)
{
if(atom == NULL)
return;
do_log(D_ATOM_REFCOUNT, "A 0x%lx %d--\n",
(unsigned long)atom, atom->refcount);
assert(atom->refcount >= 1 && atom->refcount < LARGE_ATOM_REFCOUNT);
atom->refcount--;
if(atom->refcount == 0) {
int h = hash(0, atom->string, atom->length, LOG2_ATOM_HASH_TABLE_SIZE);
assert(atomHashTable[h] != NULL);
if(atom == atomHashTable[h]) {
atomHashTable[h] = atom->next;
free(atom);
} else {
AtomPtr previous = atomHashTable[h];
while(previous->next) {
if(previous->next == atom)
break;
previous = previous->next;
}
assert(previous->next != NULL);
previous->next = atom->next;
free(atom);
}
used_atoms--;
}
}
AtomPtr
internAtomF(const char *format, ...)
{
char *s;
char buf[150];
int n;
va_list args;
AtomPtr atom = NULL;
va_start(args, format);
n = vsnprintf(buf, 150, format, args);
va_end(args);
if(n >= 0 && n < 150) {
atom = internAtomN(buf, n);
} else {
va_start(args, format);
s = vsprintf_a(format, args);
va_end(args);
if(s != NULL) {
atom = internAtom(s);
free(s);
}
}
return atom;
}
static AtomPtr
internAtomErrorV(int e, const char *f, va_list args)
{
char *es = pstrerror(e);
AtomPtr atom;
char *s1, *s2;
int n, rc;
if(f) {
s1 = vsprintf_a(f, args);
if(s1 == NULL)
return NULL;
n = strlen(s1);
} else {
s1 = NULL;
n = 0;
}
s2 = malloc(n + 70);
if(s2 == NULL) {
free(s1);
return NULL;
}
if(s1) {
strcpy(s2, s1);
free(s1);
}
rc = snprintf(s2 + n, 69, f ? ": %s" : "%s", es);
if(rc < 0 || rc >= 69) {
free(s2);
return NULL;
}
atom = internAtomN(s2, n + rc);
free(s2);
return atom;
}
AtomPtr
internAtomError(int e, const char *f, ...)
{
AtomPtr atom;
va_list args;
va_start(args, f);
atom = internAtomErrorV(e, f, args);
va_end(args);
return atom;
}
char *
atomString(AtomPtr atom)
{
if(atom)
return atom->string;
else
return "(null)";
}
AtomListPtr
makeAtomList(AtomPtr *atoms, int n)
{
AtomListPtr list;
list = malloc(sizeof(AtomListRec));
if(list == NULL) return NULL;
list->length = 0;
list->size = 0;
list->list = NULL;
if(n > 0) {
int i;
list->list = malloc(n * sizeof(AtomPtr));
if(list->list == NULL) {
free(list);
return NULL;
}
list->size = n;
for(i = 0; i < n; i++)
list->list[i] = atoms[i];
list->length = n;
}
return list;
}
void
destroyAtomList(AtomListPtr list)
{
int i;
if(list->list) {
for(i = 0; i < list->length; i++)
releaseAtom(list->list[i]);
list->length = 0;
free(list->list);
list->list = NULL;
list->size = 0;
}
assert(list->size == 0);
free(list);
}
int
atomListMember(AtomPtr atom, AtomListPtr list)
{
int i;
for(i = 0; i < list->length; i++) {
if(atom == list->list[i])
return 1;
}
return 0;
}
void
atomListCons(AtomPtr atom, AtomListPtr list)
{
if(list->list == NULL) {
assert(list->size == 0);
list->list = malloc(5 * sizeof(AtomPtr));
if(list->list == NULL) {
do_log(L_ERROR, "Couldn't allocate AtomList\n");
return;
}
list->size = 5;
}
if(list->size <= list->length) {
AtomPtr *new_list;
int n = (2 * list->length + 1);
new_list = realloc(list->list, n * sizeof(AtomPtr));
if(new_list == NULL) {
do_log(L_ERROR, "Couldn't realloc AtomList\n");
return;
}
list->list = new_list;
list->size = n;
}
list->list[list->length] = atom;
list->length++;
}