Repository navigation
Expand file tree
/
Copy pathdictionary.c
More file actions
209 lines (148 loc) · 5.86 KB
/
Copy pathdictionary.c
File metadata and controls
209 lines (148 loc) · 5.86 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
207
208
209
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include "avl.h"
#include "common.h"
#include "log.h"
#include "dictionary.h"
// ----------------------------------------------------------------------------
// name_value index
static int name_value_iterator(avl *a) { if(a) {}; return 0; }
static int name_value_compare(void* a, void* b) {
if(((NAME_VALUE *)a)->hash < ((NAME_VALUE *)b)->hash) return -1;
else if(((NAME_VALUE *)a)->hash > ((NAME_VALUE *)b)->hash) return 1;
else return strcmp(((NAME_VALUE *)a)->name, ((NAME_VALUE *)b)->name);
}
#define dictionary_name_value_index_add_nolock(dict, nv) do { (dict)->inserts++; avl_insert(&((dict)->values_index), (avl *)(nv)); } while(0)
#define dictionary_name_value_index_del_nolock(dict, nv) do { (dict)->deletes++; avl_remove(&(dict->values_index), (avl *)(nv)); } while(0)
static inline NAME_VALUE *dictionary_name_value_index_find_nolock(DICTIONARY *dict, const char *name, uint32_t hash) {
NAME_VALUE *result = NULL, tmp;
tmp.hash = (hash)?hash:simple_hash(name);
tmp.name = (char *)name;
dict->searches++;
avl_search(&(dict->values_index), (avl *)&tmp, name_value_iterator, (avl **)&result);
return result;
}
static void dictionary_read_lock(DICTIONARY *dict) {
if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED)))
pthread_rwlock_rdlock(&dict->rwlock);
}
static void dictionary_write_lock(DICTIONARY *dict) {
if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED)))
pthread_rwlock_wrlock(&dict->rwlock);
}
static void dictionary_unlock(DICTIONARY *dict) {
if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED)))
pthread_rwlock_unlock(&dict->rwlock);
}
// ----------------------------------------------------------------------------
static NAME_VALUE *dictionary_name_value_create_nolock(DICTIONARY *dict, const char *name, void *value, size_t value_len, uint32_t hash) {
debug(D_DICTIONARY, "Creating name value entry for name '%s', value '%s'.", name, value);
NAME_VALUE *nv = calloc(1, sizeof(NAME_VALUE));
if(unlikely(!nv)) fatal("Cannot allocate name_value of size %z", sizeof(NAME_VALUE));
nv->name = strdup(name);
if(unlikely(!nv->name))
fatal("Cannot allocate name_value.name of size %z", strlen(name));
nv->hash = (hash)?hash:simple_hash(nv->name);
if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)
nv->value = value;
else {
nv->value = malloc(value_len);
if (unlikely(!nv->value))
fatal("Cannot allocate name_value.value of size %z", value_len);
memcpy(nv->value, value, value_len);
}
dictionary_write_lock(dict);
// index it
dictionary_name_value_index_add_nolock(dict, nv);
dict->entries++;
dictionary_unlock(dict);
return nv;
}
static void dictionary_name_value_destroy_nolock(DICTIONARY *dict, NAME_VALUE *nv) {
debug(D_DICTIONARY, "Destroying name value entry for name '%s'.", nv->name);
dictionary_name_value_index_del_nolock(dict, nv);
dict->entries--;
free(nv->name);
if(!(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE))
free(nv->value);
free(nv);
}
// ----------------------------------------------------------------------------
DICTIONARY *dictionary_create(uint32_t flags) {
debug(D_DICTIONARY, "Creating dictionary.");
DICTIONARY *dict = calloc(1, sizeof(DICTIONARY));
if(unlikely(!dict)) fatal("Cannot allocate DICTIONARY");
avl_init(&dict->values_index, name_value_compare);
pthread_rwlock_init(&dict->rwlock, NULL);
dict->flags = flags;
return dict;
}
void dictionary_destroy(DICTIONARY *dict) {
debug(D_DICTIONARY, "Destroying dictionary.");
dictionary_write_lock(dict);
while(dict->values_index.root)
dictionary_name_value_destroy_nolock(dict, (NAME_VALUE *)dict->values_index.root);
dictionary_unlock(dict);
free(dict);
}
// ----------------------------------------------------------------------------
void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t value_len) {
debug(D_DICTIONARY, "SET dictionary entry with name '%s'.", name);
uint32_t hash = simple_hash(name);
dictionary_read_lock(dict);
NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, hash);
dictionary_unlock(dict);
if(unlikely(!nv)) {
debug(D_DICTIONARY, "Dictionary entry with name '%s' not found. Creating a new one.", name);
pthread_rwlock_wrlock(&dict->rwlock);
nv = dictionary_name_value_create_nolock(dict, name, value, value_len, hash);
if(unlikely(!nv))
fatal("Cannot create name_value.");
dictionary_unlock(dict);
}
else {
debug(D_DICTIONARY, "Dictionary entry with name '%s' found. Changing its value.", name);
if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)
nv->value = value;
else {
void *value = malloc(value_len),
*old = nv->value;
if(unlikely(!nv->value))
fatal("Cannot allocate value of size %z", value_len);
memcpy(value, value, value_len);
nv->value = value;
free(old);
}
}
return nv->value;
}
void *dictionary_get(DICTIONARY *dict, const char *name) {
debug(D_DICTIONARY, "GET dictionary entry with name '%s'.", name);
dictionary_read_lock(dict);
NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
dictionary_unlock(dict);
if(unlikely(!nv)) {
debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
return NULL;
}
debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
return nv->value;
}
void dictionary_del(DICTIONARY *dict, const char *name) {
debug(D_DICTIONARY, "DEL dictionary entry with name '%s'.", name);
dictionary_read_lock(dict);
NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
dictionary_unlock(dict);
if(unlikely(!nv)) {
debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
return;
}
debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
pthread_rwlock_wrlock(&dict->rwlock);
dictionary_name_value_destroy_nolock(dict, nv);
dictionary_unlock(dict);
}