13 #include "dictionary.h"
15 // ----------------------------------------------------------------------------
18 static inline void dictionary_read_lock(DICTIONARY *dict) {
19 if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
20 // debug(D_DICTIONARY, "Dictionary READ lock");
21 pthread_rwlock_rdlock(&dict->rwlock);
25 static inline void dictionary_write_lock(DICTIONARY *dict) {
26 if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
27 // debug(D_DICTIONARY, "Dictionary WRITE lock");
28 pthread_rwlock_wrlock(&dict->rwlock);
32 static inline void dictionary_unlock(DICTIONARY *dict) {
33 if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
34 // debug(D_DICTIONARY, "Dictionary UNLOCK lock");
35 pthread_rwlock_unlock(&dict->rwlock);
40 // ----------------------------------------------------------------------------
43 static int name_value_iterator(avl *a) { if(a) {}; return 0; }
45 static int name_value_compare(void* a, void* b) {
46 if(((NAME_VALUE *)a)->hash < ((NAME_VALUE *)b)->hash) return -1;
47 else if(((NAME_VALUE *)a)->hash > ((NAME_VALUE *)b)->hash) return 1;
48 else return strcmp(((NAME_VALUE *)a)->name, ((NAME_VALUE *)b)->name);
51 #define dictionary_name_value_index_add_nolock(dict, nv) do { (dict)->inserts++; avl_insert(&((dict)->values_index), (avl *)(nv)); } while(0)
52 #define dictionary_name_value_index_del_nolock(dict, nv) do { (dict)->deletes++; avl_remove(&(dict->values_index), (avl *)(nv)); } while(0)
54 static inline NAME_VALUE *dictionary_name_value_index_find_nolock(DICTIONARY *dict, const char *name, uint32_t hash) {
55 NAME_VALUE *result = NULL, tmp;
56 tmp.hash = (hash)?hash:simple_hash(name);
57 tmp.name = (char *)name;
60 avl_search(&(dict->values_index), (avl *) &tmp, name_value_iterator, (avl **) &result);
65 // ----------------------------------------------------------------------------
68 static NAME_VALUE *dictionary_name_value_create_nolock(DICTIONARY *dict, const char *name, void *value, size_t value_len, uint32_t hash) {
69 debug(D_DICTIONARY, "Creating name value entry for name '%s'.", name);
71 NAME_VALUE *nv = calloc(1, sizeof(NAME_VALUE));
72 if(unlikely(!nv)) fatal("Cannot allocate name_value of size %z", sizeof(NAME_VALUE));
74 if(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)
75 nv->name = (char *)name;
77 nv->name = strdup(name);
78 if (unlikely(!nv->name))
79 fatal("Cannot allocate name_value.name of size %z", strlen(name));
82 nv->hash = (hash)?hash:simple_hash(nv->name);
84 if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)
87 nv->value = malloc(value_len);
88 if (unlikely(!nv->value))
89 fatal("Cannot allocate name_value.value of size %z", value_len);
91 memcpy(nv->value, value, value_len);
95 dictionary_name_value_index_add_nolock(dict, nv);
101 static void dictionary_name_value_destroy_nolock(DICTIONARY *dict, NAME_VALUE *nv) {
102 debug(D_DICTIONARY, "Destroying name value entry for name '%s'.", nv->name);
104 dictionary_name_value_index_del_nolock(dict, nv);
108 if(!(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)) {
109 debug(D_REGISTRY, "Dictionary freeing value of '%s'", nv->name);
113 if(!(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)) {
114 debug(D_REGISTRY, "Dictionary freeing name '%s'", nv->name);
121 // ----------------------------------------------------------------------------
122 // API - basic methods
124 DICTIONARY *dictionary_create(uint32_t flags) {
125 debug(D_DICTIONARY, "Creating dictionary.");
127 DICTIONARY *dict = calloc(1, sizeof(DICTIONARY));
128 if(unlikely(!dict)) fatal("Cannot allocate DICTIONARY");
130 avl_init(&dict->values_index, name_value_compare);
131 pthread_rwlock_init(&dict->rwlock, NULL);
138 void dictionary_destroy(DICTIONARY *dict) {
139 debug(D_DICTIONARY, "Destroying dictionary.");
141 dictionary_write_lock(dict);
143 while(dict->values_index.root)
144 dictionary_name_value_destroy_nolock(dict, (NAME_VALUE *)dict->values_index.root);
146 dictionary_unlock(dict);
151 // ----------------------------------------------------------------------------
153 void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t value_len) {
154 debug(D_DICTIONARY, "SET dictionary entry with name '%s'.", name);
156 uint32_t hash = simple_hash(name);
158 dictionary_write_lock(dict);
160 NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, hash);
162 debug(D_DICTIONARY, "Dictionary entry with name '%s' not found. Creating a new one.", name);
164 nv = dictionary_name_value_create_nolock(dict, name, value, value_len, hash);
166 fatal("Cannot create name_value.");
169 debug(D_DICTIONARY, "Dictionary entry with name '%s' found. Changing its value.", name);
171 if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE) {
172 debug(D_REGISTRY, "Dictionary: linking value to '%s'", name);
176 debug(D_REGISTRY, "Dictionary: cloning value to '%s'", name);
178 void *value = malloc(value_len),
181 if(unlikely(!nv->value))
182 fatal("Cannot allocate value of size %z", value_len);
184 memcpy(value, value, value_len);
187 debug(D_REGISTRY, "Dictionary: freeing old value of '%s'", name);
192 dictionary_unlock(dict);
197 void *dictionary_get(DICTIONARY *dict, const char *name) {
198 debug(D_DICTIONARY, "GET dictionary entry with name '%s'.", name);
200 dictionary_read_lock(dict);
201 NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
202 dictionary_unlock(dict);
205 debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
209 debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
213 int dictionary_del(DICTIONARY *dict, const char *name) {
216 debug(D_DICTIONARY, "DEL dictionary entry with name '%s'.", name);
218 dictionary_write_lock(dict);
220 NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
222 debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
226 debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
227 dictionary_name_value_destroy_nolock(dict, nv);
231 dictionary_unlock(dict);
237 // ----------------------------------------------------------------------------
238 // API - walk through the dictionary
239 // the dictionary is locked for reading while this happens
240 // do not user other dictionary calls while walking the dictionary - deadlock!
242 static int dictionary_walker(avl *a, int (*callback)(void *entry, void *data), void *data) {
243 int total = 0, ret = 0;
246 ret = dictionary_walker(a->right, callback, data);
247 if(ret < 0) return ret;
251 ret = callback(((NAME_VALUE *)a)->value, data);
252 if(ret < 0) return ret;
256 dictionary_walker(a->left, callback, data);
257 if (ret < 0) return ret;
264 int dictionary_get_all(DICTIONARY *dict, int (*callback)(void *entry, void *data), void *data) {
267 dictionary_read_lock(dict);
269 if(likely(dict->values_index.root))
270 ret = dictionary_walker(dict->values_index.root, callback, data);
272 dictionary_unlock(dict);