]> arthur.barton.de Git - netdata.git/blob - src/registry_url.c
removed over-optimization at the registry to simplify its logic and remove race condi...
[netdata.git] / src / registry_url.c
1 #include "registry_internals.h"
2
3 // ----------------------------------------------------------------------------
4 // REGISTRY_URL
5
6 int registry_url_compare(void *a, void *b) {
7     if(((REGISTRY_URL *)a)->hash < ((REGISTRY_URL *)b)->hash) return -1;
8     else if(((REGISTRY_URL *)a)->hash > ((REGISTRY_URL *)b)->hash) return 1;
9     else return strcmp(((REGISTRY_URL *)a)->url, ((REGISTRY_URL *)b)->url);
10 }
11
12 #define registry_url_index_add(rc) (REGISTRY_URL *)avl_insert(&(registry.registry_urls_root_index), (avl *)(rc))
13 #define registry_url_index_del(rc) (REGISTRY_URL *)avl_remove(&(registry.registry_urls_root_index), (avl *)(rc))
14
15 REGISTRY_URL *registry_url_get(const char *url, size_t urllen) {
16     // protection from too big URLs
17     if(urllen > registry.max_url_length)
18         urllen = registry.max_url_length;
19
20     debug(D_REGISTRY, "Registry: registry_url_get('%s', %zu)", url, urllen);
21
22     char buf[sizeof(REGISTRY_URL) + urllen]; // no need for +1, 1 is already in REGISTRY_URL
23     REGISTRY_URL *n = (REGISTRY_URL *)&buf[0];
24     n->len = (uint16_t)urllen;
25     strncpyz(n->url, url, n->len);
26     n->hash = simple_hash(n->url);
27
28     REGISTRY_URL *u = (REGISTRY_URL *)avl_search(&(registry.registry_urls_root_index), (avl *)n);
29     if(!u) {
30         debug(D_REGISTRY, "Registry: registry_url_get('%s', %zu): allocating %zu bytes", url, urllen, sizeof(REGISTRY_URL) + urllen);
31         u = callocz(1, sizeof(REGISTRY_URL) + urllen); // no need for +1, 1 is already in REGISTRY_URL
32
33         // a simple strcpy() should do the job
34         // but I prefer to be safe, since the caller specified urllen
35         u->len = (uint16_t)urllen;
36         strncpyz(u->url, url, u->len);
37         u->links = 0;
38         u->hash = simple_hash(u->url);
39
40         registry.urls_memory += sizeof(REGISTRY_URL) + urllen; // no need for +1, 1 is already in REGISTRY_URL
41
42         debug(D_REGISTRY, "Registry: registry_url_get('%s'): indexing it", url);
43         n = registry_url_index_add(u);
44         if(n != u) {
45             error("INTERNAL ERROR: registry_url_get(): url '%s' already exists in the registry as '%s'", u->url, n->url);
46             free(u);
47             u = n;
48         }
49         else
50             registry.urls_count++;
51     }
52
53     return u;
54 }
55
56 void registry_url_link(REGISTRY_URL *u) {
57     u->links++;
58     debug(D_REGISTRY, "Registry: registry_url_link('%s'): URL has now %u links", u->url, u->links);
59 }
60
61 void registry_url_unlink(REGISTRY_URL *u) {
62     u->links--;
63     if(!u->links) {
64         debug(D_REGISTRY, "Registry: registry_url_unlink('%s'): No more links for this URL", u->url);
65         REGISTRY_URL *n = registry_url_index_del(u);
66         if(n != u)
67             error("INTERNAL ERROR: registry_url_unlink(): request to delete url '%s', deleted url '%s'", u->url, n->url);
68
69         registry.urls_memory -= sizeof(REGISTRY_URL) + n->len; // no need for +1, 1 is already in REGISTRY_URL
70         freez(n);
71     }
72     else
73         debug(D_REGISTRY, "Registry: registry_url_unlink('%s'): URL has %u links left", u->url, u->links);
74 }