1 #include "adaptive_resortable_list.h"
3 // the default processor() of the ARL
4 // can be overwritten at arl_create()
5 static inline void arl_callback_str2ull(const char *name, uint32_t hash, const char *value, void *dst) {
9 register unsigned long long *d = dst;
11 // fprintf(stderr, "name '%s' with hash %u and value '%s' is %llu\n", name, hash, value, *d);
15 ARL_BASE *arl_create(void (*processor)(const char *, uint32_t, const char *, void *), size_t rechecks) {
16 ARL_BASE *base = callocz(1, sizeof(ARL_BASE));
19 base->processor = arl_callback_str2ull;
21 base->processor = processor;
23 base->rechecks = rechecks;
28 void arl_free(ARL_BASE *arl_base) {
29 if(unlikely(!arl_base))
32 while(arl_base->head) {
33 ARL_ENTRY *e = arl_base->head;
34 arl_base->head = e->next;
37 #ifdef NETDATA_INTERNAL_CHECKS
38 memset(e, 0, sizeof(ARL_ENTRY));
43 #ifdef NETDATA_INTERNAL_CHECKS
44 memset(arl_base, 0, sizeof(ARL_BASE));
50 void arl_begin(ARL_BASE *base) {
54 info("iteration %zu, expected %zu, wanted %zu, allocated %zu, fred %zu, relinkings %zu, found %zu, added %zu, fast %zu, slow %zu"
66 for(e = base->head; e ; e = e->next) fprintf(stderr, "%s ", e->name);
67 fprintf(stderr, "\n");
70 if(unlikely(base->added || base->iteration % base->rechecks) == 1) {
73 for(e = base->head; e ; e = e->next) {
74 if(e->flags & ARL_ENTRY_FLAG_FOUND) {
76 // remove the found flag
77 e->flags &= ~ARL_ENTRY_FLAG_FOUND;
80 if(e->flags & ARL_ENTRY_FLAG_EXPECTED)
83 else if(e->flags & ARL_ENTRY_FLAG_DYNAMIC) {
84 // we can remove this entry
85 // it is not found, and it was created because
86 // it was found in the source file
87 if(e->next) e->next->prev = e->prev;
88 if(e->prev) e->prev->next = e->next;
89 if(base->head == e) base->head = e->next;
99 base->next_keyword = base->head;
103 // register an expected keyword to the ARL
104 // together with its destination ( i.e. the output of the processor() )
105 ARL_ENTRY *arl_expect(ARL_BASE *base, const char *keyword, void *dst) {
106 ARL_ENTRY *e = callocz(1, sizeof(ARL_ENTRY));
107 e->name = strdupz(keyword);
108 e->hash = simple_hash(e->name);
110 e->flags = ARL_ENTRY_FLAG_EXPECTED;
112 e->next = base->head;
114 if(base->head) base->head->prev = e;
115 else base->next_keyword = e;
121 base->wanted = base->expected;
126 int arl_find_or_create_and_relink(ARL_BASE *base, const char *s, uint32_t hash, const char *value) {
129 // find if it already exists in the data
130 for(e = base->head; e ; e = e->next)
131 if(e->hash == hash && !strsame(e->name, s))
134 #ifdef NETDATA_INTERNAL_CHECKS
135 if(unlikely(e == base->next_keyword))
136 fatal("Internal Error: e == base->last");
140 // found it in the keywords
143 // run the processor for it
144 if(unlikely(e->dst)) {
145 base->processor(e->name, hash, value, e->dst);
149 // unlink it - we will relink it below
150 if(e->next) e->next->prev = e->prev;
151 if(e->prev) e->prev->next = e->next;
153 // make sure the head is properly linked
155 base->head = e->next;
161 e = callocz(1, sizeof(ARL_ENTRY));
162 e->name = strdupz(s);
164 e->flags = ARL_ENTRY_FLAG_DYNAMIC;
170 e->flags |= ARL_ENTRY_FLAG_FOUND;
173 e->next = base->next_keyword;
174 if(base->next_keyword) {
175 e->prev = base->next_keyword->prev;
176 base->next_keyword->prev = e;
181 if(base->head == base->next_keyword)
187 base->next_keyword = e->next;
188 if(unlikely(!base->next_keyword))
189 base->next_keyword = base->head;
191 if(unlikely(base->found == base->wanted))