forked from brianuuuu/atomdict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atomdict.c
411 lines (367 loc) · 8.16 KB
/
atomdict.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
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
#include "atomdict.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#define ATOMDICT_NUMBER 0
#define ATOMDICT_STRING 1
#define MAX_COL 32
#define AVERAGE_STRING 64
#define DIRTY_WRITE 1
#define DIRTY_STRING 2
#if 0
static int g_count = 0;
static void *
my_malloc(size_t sz) {
++g_count;
return malloc(sz);
}
static void
my_free(void *p) {
--g_count;
free(p);
}
#define DUMP_MEM printf("memory count = %d\n",g_count);
#define malloc my_malloc
#define free my_free
#else
#define DUMP_MEM
#endif
union atomvalue {
int s;
float n;
} ;
struct atomcol {
union atomvalue *v;
char * stringbuffer;
int sb_size;
int dirty;
int ref;
};
#define COL_STRING(col, n) (col->stringbuffer + col->v[n].s)
struct atomkey {
const char *key;
int type;
};
struct atomdict {
int current;
int property_count;
int stringbuffer;
char * typename;
struct atomkey * key;
struct atomcol col[MAX_COL];
};
struct atomdict *
atomdict_new(void) {
struct atomdict * ad = malloc(sizeof(struct atomdict));
memset(ad, 0 , sizeof(*ad));
return ad;
}
static int
_init_key(struct atomdict *ad, struct atomslot *slot, const char * typename) {
int keybuffer_size = 0;
int n=0;
while (slot[n].key) {
keybuffer_size += strlen(slot[n].key) + 1;
++n;
}
if (typename) {
keybuffer_size += strlen(typename) + 1;
}
assert(n>0);
ad->property_count = n;
ad->key = malloc( n * sizeof(struct atomkey) + keybuffer_size);
char * keybuffer = (char *)(ad->key + n);
if (typename) {
int len = strlen(typename) + 1;
memcpy(keybuffer, typename , len);
ad->typename = keybuffer;
keybuffer += len;
}
int i;
int stringkey = 0;
for (i=0;i<n;i++) {
int len = strlen(slot[i].key) + 1;
memcpy(keybuffer, slot[i].key, len);
ad->key[i].key = keybuffer;
keybuffer += len;
if (slot[i].vs == NULL) {
ad->key[i].type = ATOMDICT_NUMBER;
} else {
++stringkey;
ad->key[i].type = ATOMDICT_STRING;
}
}
return stringkey;
}
static void
_init_col(struct atomdict *ad, int n) {
ad->stringbuffer = n * AVERAGE_STRING;
int i;
int pn = ad->property_count;
for (i=0;i<MAX_COL;i++) {
ad->col[i].v = malloc(pn * sizeof(union atomvalue));
memset(ad->col[i].v, 0 , pn * sizeof(union atomvalue));
}
if (n > 0) {
for (i=0;i<MAX_COL;i++) {
ad->col[i].stringbuffer = malloc(ad->stringbuffer);
ad->col[i].stringbuffer[0] = '\0';
}
}
}
static int
_insert_string(struct atomcol * col, const char *key, int cap) {
int len = strlen(key) + 1;
if (col->sb_size + len > cap) {
return -1;
}
int ret = col->sb_size;
memcpy(col->stringbuffer + ret , key, len);
col->sb_size += len;
return ret;
}
static int
_init_value(struct atomdict *ad, struct atomslot *slot) {
int pn = ad->property_count;
int i;
struct atomcol *c = &(ad->col[0]);
for (i=0;i<pn;i++) {
if (slot[i].vs) {
int index = _insert_string(c, slot[i].vs , ad->stringbuffer);
if (index < 0) {
return -1;
}
c->v[i].s = index;
} else {
c->v[i].n = slot[i].vn;
}
}
return 0;
}
int
atomdict_init(struct atomdict *ad, struct atomslot *slot, const char *typename) {
int stringkey = _init_key(ad, slot, typename);
_init_col(ad, stringkey);
return _init_value(ad, slot);
}
const char *
atomdict_key(struct atomdict *ad, int n, struct atomslot *slot) {
if (n>=ad->property_count) {
return NULL;
}
const char * key = ad->key[n].key;
if (slot) {
slot->key = key;
switch (ad->key[n].type) {
case ATOMDICT_NUMBER:
slot->vs = NULL;
slot->vn = ad->col[ad->current].v[n].n;
break;
case ATOMDICT_STRING:
slot->vs = COL_STRING(ad->col, n);
slot->vn = 0;
break;
}
}
return key;
}
const char *
atomdict_typename(struct atomdict *ad) {
return ad->typename;
}
void
atomdict_delete(struct atomdict *ad) {
if (ad == NULL)
return;
free(ad->key);
int i;
for (i=0;i<MAX_COL;i++) {
struct atomcol *c = &(ad->col[i]);
free(c->v);
free(c->stringbuffer);
}
free(ad);
}
static void
_dump_col(struct atomdict *ad, int n, int current) {
struct atomcol *c = &(ad->col[n]);
struct atomkey *key = ad->key;
if (n == current) {
printf(" *");
} else {
if (c->ref == 0 && current >=0) {
return;
} else {
printf(" ");
}
}
printf("[%d] ref=%d dirty=%d string_size=%d\n",n,c->ref, c->dirty,c->sb_size);
int i;
for (i=0;i<ad->property_count;i++) {
printf(" %s = ",key[i].key);
switch(key[i].type) {
case ATOMDICT_NUMBER:
printf("%f",c->v[i].n);
break;
case ATOMDICT_STRING:
printf("'%s'",COL_STRING(c,i));
break;
}
printf("\n");
}
}
void
atomdict_dump(struct atomdict * ad, int col) {
printf("ATOM DICT %p [%s]\n",ad, ad->typename);
if (col >= 0) {
printf(" current = %d\n",ad->current);
if (col >= ad->property_count) {
printf(" property_count = %d , request = %d \n",ad->property_count, col);
return;
}
_dump_col(ad, col, -1);
} else {
col = atomdict_grab(ad);
printf(" current = %d\n",col);
printf(" property_count = %d\n",ad->property_count);
printf(" stringbuffer_cap = %d\n",ad->stringbuffer);
int i;
for (i=0;i<MAX_COL;i++) {
_dump_col(ad, i, col);
}
atomdict_commit(ad,col);
}
}
int
atomdict_grab(struct atomdict * ad) {
for (;;) {
int current = ad->current;
__sync_add_and_fetch(&(ad->col[current].ref), 1);
if (current != ad->current) {
__sync_sub_and_fetch(&(ad->col[current].ref), 1);
} else {
return current;
}
}
}
static int
_get_free_col(struct atomdict * ad) {
int i,j;
for (j=0;j<4;j++) {
for (i=0;i<MAX_COL;i++) {
int ref = ad->col[i].ref;
if (ref > 0)
continue;
if (__sync_bool_compare_and_swap(&(ad->col[i].ref), 0 , 1)) {
return i;
}
}
}
return -1;
}
static int
_atomdict_dup(struct atomdict * ad, int copy) {
int pn = ad->property_count;
int c = _get_free_col(ad);
if (c<0) {
return -1;
}
struct atomcol * from = &(ad->col[copy]);
struct atomcol * to = &(ad->col[c]);
memcpy(to->v,from->v,pn * sizeof(union atomvalue));
if (to->stringbuffer) {
to->dirty = 0;
to->sb_size = from->sb_size;
memcpy(to->stringbuffer, from->stringbuffer, from->sb_size);
}
return c;
}
int
atomdict_set_string(struct atomdict * ad, int col, int idx, const char * v) {
assert(idx < ad->property_count && col < MAX_COL);
struct atomcol *c = &(ad->col[col]);
if (c->dirty == 0) {
int old = col;
col = _atomdict_dup(ad, old);
if (col < 0) {
return -2;
}
c = &(ad->col[col]);
assert(c->ref == 1);
c->dirty = DIRTY_WRITE | DIRTY_STRING;
int ref = __sync_sub_and_fetch(&(ad->col[old].ref), 1);
assert(ref >= 0);
} else {
c->dirty |= DIRTY_STRING;
}
int index = _insert_string(c, v, ad->stringbuffer);
if (index < 0) {
return -2;
}
c->v[idx].s = index;
return col;
}
int
atomdict_set_number(struct atomdict * ad, int col, int idx, float v) {
assert(idx < ad->property_count && col < MAX_COL);
struct atomcol *c = &(ad->col[col]);
if (c->v[idx].n == v) {
return col;
}
if (c->dirty == 0) {
int old = col;
col = _atomdict_dup(ad, old);
c = &(ad->col[col]);
assert(c->ref == 1);
c->v[idx].n = v;
c->dirty = DIRTY_WRITE;
int ref = __sync_sub_and_fetch(&(ad->col[old].ref), 1);
assert(ref >= 0);
} else {
c->v[idx].n = v;
}
// printf("set col=%d idx=%d %f\n",col,idx,v);
return col;
}
const char *
atomdict_get_string(struct atomdict * ad, int col, int idx) {
assert(idx < ad->property_count && col < MAX_COL);
struct atomcol *c = &(ad->col[col]);
return COL_STRING(c, idx);
}
float
atomdict_get_number(struct atomdict * ad, int col, int idx) {
assert(idx < ad->property_count && col < MAX_COL);
struct atomcol *c = &(ad->col[col]);
// printf("get col=%d idx=%d %f\n",col,idx,c->v[idx].n);
return c->v[idx].n;
}
void
atomdict_commit(struct atomdict * ad, int col) {
struct atomcol *c = &(ad->col[col]);
if (c->dirty == 0) {
int ref = __sync_sub_and_fetch(&(ad->col[col].ref), 1);
assert(ref >= 0);
return;
}
assert(c->ref == 1);
if (c->dirty | DIRTY_STRING) {
// arrange string buffer
char tmp[c->sb_size];
int pn = ad->property_count;
int i;
int cap = ad->stringbuffer;
memcpy(tmp, c->stringbuffer, c->sb_size);
c->sb_size = 0;
for (i=0;i<pn;i++) {
if (ad->key[i].type == ATOMDICT_STRING) {
c->v[i].s = _insert_string(c, tmp+c->v[i].s , cap);
}
}
c->dirty = 0;
}
__sync_synchronize();
c->ref = 0;
ad->current = col;
}