]> arthur.barton.de Git - netdata.git/blob - src/dictionary.c
build: migrate to autotools
[netdata.git] / src / dictionary.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <pthread.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include "avl.h"
9 #include "common.h"
10 #include "log.h"
11
12 #include "dictionary.h"
13
14 // ----------------------------------------------------------------------------
15 // name_value index
16
17 static int name_value_iterator(avl *a) { if(a) {}; return 0; }
18
19 static int name_value_compare(void* a, void* b) {
20         if(((NAME_VALUE *)a)->hash < ((NAME_VALUE *)b)->hash) return -1;
21         else if(((NAME_VALUE *)a)->hash > ((NAME_VALUE *)b)->hash) return 1;
22         else return strcmp(((NAME_VALUE *)a)->name, ((NAME_VALUE *)b)->name);
23 }
24
25 #define name_value_index_add(dict, cv) avl_insert(&((dict)->values_index), (avl *)(cv))
26 #define name_value_index_del(dict, cv) avl_remove(&((dict)->values_index), (avl *)(cv))
27
28 static NAME_VALUE *dictionary_name_value_index_find(DICTIONARY *dict, const char *name, uint32_t hash) {
29         NAME_VALUE *result = NULL, tmp;
30         tmp.hash = (hash)?hash:simple_hash(name);
31         tmp.name = (char *)name;
32
33         avl_search(&(dict->values_index), (avl *)&tmp, name_value_iterator, (avl **)&result);
34         return result;
35 }
36
37 // ----------------------------------------------------------------------------
38
39 static NAME_VALUE *dictionary_name_value_create(DICTIONARY *dict, const char *name, void *value, size_t value_len) {
40         debug(D_DICTIONARY, "Creating name value entry for name '%s', value '%s'.", name, value);
41
42         NAME_VALUE *nv = calloc(1, sizeof(NAME_VALUE));
43         if(!nv) fatal("Cannot allocate name_value of size %z", sizeof(NAME_VALUE));
44
45         nv->name = strdup(name);
46         if(!nv->name) fatal("Cannot allocate name_value.name of size %z", strlen(name));
47         nv->hash = simple_hash(nv->name);
48
49         nv->value = malloc(value_len);
50         if(!nv->value) fatal("Cannot allocate name_value.value of size %z", value_len);
51         memcpy(nv->value, value, value_len);
52
53         // link it
54         pthread_rwlock_wrlock(&dict->rwlock);
55         nv->next = dict->values;
56         dict->values = nv;
57         pthread_rwlock_unlock(&dict->rwlock);
58
59         // index it
60         name_value_index_add(dict, nv);
61
62         return nv;
63 }
64
65 static void dictionary_name_value_destroy(DICTIONARY *dict, NAME_VALUE *nv) {
66         debug(D_DICTIONARY, "Destroying name value entry for name '%s'.", nv->name);
67
68         pthread_rwlock_wrlock(&dict->rwlock);
69         if(dict->values == nv) dict->values = nv->next;
70         else {
71                 NAME_VALUE *n = dict->values;
72                 while(n && n->next && n->next != nv) nv = nv->next;
73                 if(!n || n->next != nv) fatal("Cannot find name_value with name '%s' in dictionary.", nv->name);
74                 n->next = nv->next;
75                 nv->next = NULL;
76         }
77         pthread_rwlock_unlock(&dict->rwlock);
78
79         free(nv->value);
80         free(nv);
81 }
82
83 // ----------------------------------------------------------------------------
84
85 DICTIONARY *dictionary_create(void) {
86         debug(D_DICTIONARY, "Creating dictionary.");
87
88         DICTIONARY *dict = calloc(1, sizeof(DICTIONARY));
89         if(!dict) fatal("Cannot allocate DICTIONARY");
90
91         dict->values_index.compar = name_value_compare;
92         pthread_rwlock_init(&dict->rwlock, NULL);
93
94         return dict;
95 }
96
97 void dictionary_destroy(DICTIONARY *dict) {
98         debug(D_DICTIONARY, "Destroying dictionary.");
99
100         pthread_rwlock_wrlock(&dict->rwlock);
101         while(dict->values) dictionary_name_value_destroy(dict, dict->values);
102         pthread_rwlock_unlock(&dict->rwlock);
103
104         free(dict);
105 }
106
107 // ----------------------------------------------------------------------------
108
109 void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t value_len) {
110         debug(D_DICTIONARY, "SET dictionary entry with name '%s'.", name);
111
112         pthread_rwlock_rdlock(&dict->rwlock);
113         NAME_VALUE *nv = dictionary_name_value_index_find(dict, name, 0);
114         pthread_rwlock_unlock(&dict->rwlock);
115         if(!nv) {
116                 debug(D_DICTIONARY, "Dictionary entry with name '%s' not found. Creating a new one.", name);
117                 nv = dictionary_name_value_create(dict, name, value, value_len);
118                 if(!nv) fatal("Cannot create name_value.");
119                 return nv->value;
120         }
121         else {
122                 debug(D_DICTIONARY, "Dictionary entry with name '%s' found. Changing its value.", name);
123                 pthread_rwlock_wrlock(&dict->rwlock);
124                 void *old = nv->value;
125                 nv->value = malloc(value_len);
126                 if(!nv->value) fatal("Cannot allocate value of size %z", value_len);
127                 memcpy(nv->value, value, value_len);
128                 pthread_rwlock_unlock(&dict->rwlock);
129                 free(old);
130         }
131
132         return nv->value;
133 }
134
135 void *dictionary_get(DICTIONARY *dict, const char *name) {
136         debug(D_DICTIONARY, "GET dictionary entry with name '%s'.", name);
137
138         pthread_rwlock_rdlock(&dict->rwlock);
139         NAME_VALUE *nv = dictionary_name_value_index_find(dict, name, 0);
140         pthread_rwlock_unlock(&dict->rwlock);
141         if(!nv) {
142                 debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
143                 return NULL;
144         }
145
146         debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
147         return nv->value;
148 }