-
Notifications
You must be signed in to change notification settings - Fork 0
/
listtable.c
670 lines (480 loc) · 12.8 KB
/
listtable.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
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
/**
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
#include "listtable.h"
static listtableObj* newObject(const char* name, const void* data, size_t size);
static bool insertObject(listtable* tbl, listtableObj* obj);
static listtableObj* findObject(listtable* tbl, const char* name, listtableObj* retobj);
static bool nameMatch(listtableObj* obj, const char* name, uint32_t hash);
static bool nameCaseMatch(listtableObj* obj, const char* name, uint32_t hash);
listtable* listTable(int options) {
listtable* tbl = (listtable*) calloc(1, sizeof(listtable));
if (tbl == NULL) {
errno = ENOMEM;
return NULL;
}
// assign member methods.
tbl->put = listablePut;
tbl->putstr = listablePutAsString;
tbl->putstrf = listablePutAsStringf;
tbl->putint = listablePutAsInt;
tbl->get = listableGet;
tbl->getstr = listableGetAsString;
tbl->getint = listableGetAsInt;
tbl->getmulti = listableGetMulti;
tbl->freemulti = listableFreeMulti;
tbl->remove = listableRemove;
tbl->removeobj = listableRemoveObj;
tbl->getnext = listableGetNext;
tbl->size = listableSize;
tbl->sort = listableSort;
tbl->clear = listableClear;
tbl->save = listableSave;
tbl->load = listableLoad;
tbl->debug = listableDebug;
tbl->lock = listableLock;
tbl->unlock = listableUnlock;
tbl->free = listableFree;
// assign private methods.
tbl->namematch = nameMatch;
tbl->namecmp = strcmp;
// handle options.
if (options & LISTTABLE_THREADSAFE) {
MUTEX_NEW(tbl->qmutex, true);
if (tbl->qmutex == NULL) {
errno = ENOMEM;
free(tbl);
return NULL;
}
}
if (options & LISTTABLE_UNIQUE) {
tbl->unique = true;
}
if (options & LISTTABLE_CASEINSENSITIVE) {
tbl->namematch = nameCaseMatch;
tbl->namecmp = strcasecmp;
}
if (options & LISTTABLE_INSERTTOP) {
tbl->inserttop = true;
}
if (options & LISTTABLE_LOOKUPFORWARD) {
tbl->lookupforward = true;
}
return tbl;
}
bool listablePut(listtable* tbl, const char* name, const void* data, size_t size) {
// make new object table
listtableObj* obj = newObject(name, data, size);
if (obj == NULL) {
return false;
}
// lock table
listableLock(tbl);
// if unique flag is set, remove same key
if (tbl->unique == true) listableRemove(tbl, name);
// insert into table
if (tbl->num == 0) {
obj->prev = NULL;
obj->next = NULL;
} else {
if (tbl->inserttop == false) {
obj->prev = tbl->last;
obj->next = NULL;
} else {
obj->prev = NULL;
obj->next = tbl->first;
}
}
insertObject(tbl, obj);
// unlock table
listableUnlock(tbl);
return true;
}
bool listablePutAsString(listtable* tbl, const char* name, const char* str) {
size_t size = (str) ? (strlen(str) + 1) : 0;
return listablePut(tbl, name, (const void*)str, size);
}
bool listablePutAsStringf(listtable* tbl, const char* name, const char* format, ...) {
char* str;
DYNAMIC_VSPRINTF(str, format);
if (str == NULL) {
errno = ENOMEM;
return false;
}
bool ret = listablePutAsString(tbl, name, str);
free(str);
return ret;
}
bool qlisttbl_putint(listtable* tbl, const char* name, int64_t num) {
char str[20+1];
snprintf(str, sizeof(str), "%"PRId64, num);
return listablePutAsString(tbl, name, str);
}
void* listtableGget(listtable* tbl, const char* name, size_t* size, bool newmem) {
if (name == NULL) {
errno = EINVAL;
return NULL;
}
listableLock(tbl);
void* data = NULL;
listtableObj* obj = findObject(tbl, name, NULL);
if (obj != NULL) {
// get data
if (newmem == true) {
data = malloc(obj->size);
if (data == NULL) {
errno = ENOMEM;
listableUnlock(tbl);
return NULL;
}
memcpy(data, obj->data, obj->size);
} else {
data = obj->data;
}
// set size
if (size != NULL) *size = obj->size;
}
listableUnlock(tbl);
if (data == NULL) {
errno = ENOENT;
}
return data;
}
char* listableGetAsString(listtable* tbl, const char* name, bool newmem) {
return (char*) listableGet(tbl, name, NULL, newmem);
}
int64_t listableGetAsInt(listtable* tbl, const char* name) {
int64_t num = 0;
char* str = listableGetAsString(tbl, name, true);
if (str != NULL) {
num = atoll(str);
free(str);
}
return num;
}
listtableData* listableGetMulti(listtable* tbl, const char* name, bool newmem, size_t* numobjs) {
// objects container
listtableData* objs = NULL;
// allocated number of objs
size_t allocobjs = 0;
// number of keys found
size_t numfound = 0;
listtableObj obj;
// must be cleared before call
memset((void*)&obj, 0, sizeof(obj));
listableLock(tbl);
while (tbl->getnext(tbl, &obj, name, newmem) == true) {
numfound++;
// allocate object array.
if (numfound >= allocobjs) {
// start from 10
if (allocobjs == 0) allocobjs = 10;
else allocobjs *= 2; // double size
objs = (listtableData*) realloc(objs, sizeof(listtableData) * allocobjs);
if (objs == NULL) {
DEBUG("qlisttbl->getmulti(): Memory reallocation failure.");
errno = ENOMEM;
break;
}
}
// copy reference
listtableData* newobj = &objs[numfound - 1];
newobj->data = obj.data;
newobj->size = obj.size;
newobj->type = (newmem == false) ? 1 : 2;
// release resource
if (newmem == true) {
if (obj.name != NULL) free(obj.name);
}
// clear next block
newobj = &objs[numfound];
memset((void *)newobj, '\0', sizeof(listtableData));
newobj->type = 0; // mark, end of objects
}
listableUnlock(tbl);
// return found counter
if (numobjs != NULL) {
*numobjs = numfound;
}
if (numfound == 0) {
errno = ENOENT;
}
return objs;
}
void listableFreeMulti(listtableData* objs) {
if (objs == NULL) return;
listtableData* obj;
for (obj = &objs[0]; obj->type == 2; obj++) {
if (obj->data != NULL) free(obj->data);
}
free(objs);
}
size_t listableRemove(listtable* tbl, const char* name) {
if (name == NULL) return false;
size_t numremoved = 0;
listtableObj obj;
// must be cleared before call
memset((void*)&obj, 0, sizeof(obj));
listableLock(tbl);
while(listableGetNext(tbl, &obj, name, false) == true) {
listableRemoveObj(tbl, &obj);
numremoved++;
}
listableUnlock(tbl);
return numremoved;
}
bool listableRemoveObj(listtable* tbl, const listtableObj* obj) {
if (obj == NULL) return false;
listableLock(tbl);
// copy chains
listtableObj* prev = obj->prev;
listtableObj* next = obj->next;
// find this object
listtableObj* this = NULL;
if (prev != NULL) this = prev->next;
else if (next != NULL) this = next->prev;
else this = tbl->first; // table has only one object.
// double check
if (this == NULL) {
listableUnlock(tbl);
DEBUG("qlisttbl->removeobj(): Can't veryfy object.");
errno = ENOENT;
return false;
}
// adjust chain links
if (prev == NULL) tbl->first = next; // if the object is first one
else prev->next = next; // not the first one
if (next == NULL) tbl->last = prev; // if the object is last one
else next->prev = prev; // not the first one
// adjust counter
tbl->num--;
listableUnlock(tbl);
// free object
free(this->name);
free(this->data);
free(this);
return true;
}
bool listableGetNext(listtable* tbl, listtableObj* obj, const char* name, bool newmem) {
if (obj == NULL) return NULL;
listableLock(tbl);
listtableObj* cont = NULL;
if (obj->size == 0) { // first time call
if (name == NULL) { // full scan
cont = (tbl->lookupforward) ? tbl->first : tbl->last;
} else { // name search
cont = findobj(tbl, name, NULL);
}
} else { // next call
cont = (tbl->lookupforward) ? obj->next : obj->prev;
}
if (cont == NULL) {
errno = ENOENT;
listableUnlock(tbl);
return false;
}
uint32_t hash = (name != NULL) ? hashmurmur3_32(name, strlen(name)) : 0;
bool ret = false;
while (cont != NULL) {
if (name == NULL || tbl->namematch(cont, name, hash) == true) {
if (newmem == true) {
obj->name = strdup(cont->name);
obj->data = malloc(cont->size);
if (obj->name == NULL || obj->data == NULL) {
if (obj->data != NULL) free(obj->data);
obj->name = NULL;
if (obj->name != NULL) free(obj->name);
obj->data = NULL;
errno = ENOMEM;
break;
}
memcpy(obj->data, cont->data, cont->size);
} else {
obj->name = cont->name;
obj->data = cont->data;
}
obj->hash = cont->hash;
obj->size = cont->size;
obj->prev = cont->prev;
obj->next = cont->next;
ret = true;
break;
}
cont = (tbl->lookupforward) ? cont->next : cont->prev;
}
listableUnlock(tbl);
if (ret == false) {
errno = ENOENT;
}
return ret;
}
size_t listableSize(listtable* tbl) {
return tbl->num;
}
void listableSort(listtable* tbl) {
// run bubble sort
listableLock(tbl);
listtableObj *obj1, *obj2;
listtableObj tmpobj;
int n, n2, i;
for (n = tbl->num; n > 0;) {
n2 = 0;
for (i = 0, obj1 = tbl->first; i < (n - 1); i++, obj1 = obj1->next) {
obj2 = obj1->next; // this can't be null.
if (tbl->namecmp(obj1->name, obj2->name) > 0) {
// swapping contents is faster than adjusting links.
tmpobj = *obj1;
obj1->hash = obj2->hash;
obj1->name = obj2->name;
obj1->data = obj2->data;
obj1->size = obj2->size;
obj2->hash = tmpobj.hash;
obj2->name = tmpobj.name;
obj2->data = tmpobj.data;
obj2->size = tmpobj.size;
n2 = i + 1;
}
}
n = n2; // skip sorted tailing elements
}
listableUnlock(tbl);
}
void listableClear(listtable* tbl) {
listableLock(tbl);
listtableObj* obj;
for (obj = tbl->first; obj != NULL;) {
listtableObj* next = obj->next;
free(obj->name);
free(obj->data);
free(obj);
obj = next;
}
tbl->num = 0;
tbl->first = NULL;
tbl->last = NULL;
listableUnlock(tbl);
}
bool listableSave(qlisttbl_t *tbl, const char *filepath, char sepchar, bool encode) {
if (filepath == NULL) {
errno = EINVAL;
return false;
}
int fd;
if ((fd = open(filepath, O_CREAT|O_WRONLY|O_TRUNC, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))) < 0) {
DEBUG("qlisttbl->save(): Can't open file %s", filepath);
return false;
}
char *gmtstr = qtime_gmt_str(0);
qio_printf(fd, -1, "# %s %s\n", filepath, gmtstr);
free(gmtstr);
qlisttbl_lock(tbl);
qlisttbl_obj_t *obj;
for (obj = tbl->first; obj; obj = obj->next) {
char *encval;
if (encode == true) encval = qurl_encode(obj->data, obj->size);
else encval = obj->data;
qio_printf(fd, -1, "%s%c%s\n", obj->name, sepchar, encval);
if (encode == true) free(encval);
}
qlisttbl_unlock(tbl);
close(fd);
return true;
}
// lock must be obtained from caller
static listtableObj* newObject(const char* name, const void* data, size_t size) {
if (name == NULL || data == NULL || size <= 0) {
errno = EINVAL;
return false;
}
// make a new object
char* dup_name = strdup(name);
void* dup_data = malloc(size);
listtableObj* obj = (listtableObj*) malloc(sizeof(listtableObj));
if (dup_name == NULL || dup_data == NULL || obj == NULL) {
if (dup_name != NULL) free(dup_name);
if (dup_data != NULL) free(dup_data);
if (obj != NULL) free(obj);
errno = ENOMEM;
return NULL;
}
memcpy(dup_data, data, size);
memset((void *)obj, '\0', sizeof(listtableObj));
// obj->hash = qhashmurmur3_32(dup_name);
obj->name = dup_name;
obj->data = dup_data;
obj->size = size;
return obj;
}
// lock must be obtained from caller
static bool insertObject(listtable* tbl, listtableObj* obj) {
// update hash
obj->hash = qhashmurmur3_32(obj->name, strlen(obj->name));
listtableObj* prev = obj->prev;
listtableObj* next = obj->next;
if (prev == NULL) tbl->first = obj;
else prev->next = obj;
if (next == NULL) tbl->last = obj;
else next->prev = obj;
// increase counter
tbl->num++;
return true;
}
// lock must be obtained from caller
static listtableObj* findObject(listtable* tbl, const char* name, listtableObj* retobj) {
if (retobj != NULL) {
memset((void *)retobj, '\0', sizeof(listtableObj));
}
if (name == NULL || tbl->num == 0) {
errno = ENOENT;
return NULL;
}
uint32_t hash = hashmurmur3_32(name, strlen(name));
listtableObj* obj = (tbl->lookupforward) ? tbl->first : tbl->last;
while (obj != NULL) {
// name string will be compared only if the hash matches.
if (tbl->namematch(obj, name, hash) == true) {
if (retobj != NULL) {
*retobj = *obj;
}
return obj;
}
obj = (tbl->lookupforward)? obj->next : obj->prev;
}
// not found, set prev and next chain.
if (retobj != NULL) {
if (tbl->inserttop) {
retobj->prev = NULL;
retobj->next = tbl->first;
} else {
retobj->prev = tbl->last;
retobj->next = NULL;
}
}
errno = ENOENT;
return NULL;
}
// key comp
static bool nameMatch(listtableObj* obj, const char* name, uint32_t hash) {
if ((obj->hash == hash) && !strcmp(obj->name, name)) {
return true;
}
return false;
}
static bool nameCaseMatch(listtableObj* obj, const char* name, uint32_t hash) {
if (!strcasecmp(obj->name, name)) {
return true;
}
return false;
}