Frédéric Lécaille | 4a3fef8 | 2019-05-28 14:47:17 +0200 | [diff] [blame] | 1 | #include <string.h> |
| 2 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 3 | #include <import/eb32tree.h> |
| 4 | #include <import/ebistree.h> |
Willy Tarreau | 3afc4c4 | 2020-06-03 18:23:19 +0200 | [diff] [blame] | 5 | #include <haproxy/dict.h> |
Willy Tarreau | 5b9cde4 | 2020-06-11 08:33:02 +0200 | [diff] [blame] | 6 | #include <haproxy/thread.h> |
Frédéric Lécaille | 4a3fef8 | 2019-05-28 14:47:17 +0200 | [diff] [blame] | 7 | |
| 8 | struct dict *new_dict(const char *name) |
| 9 | { |
| 10 | struct dict *dict; |
| 11 | |
| 12 | dict = malloc(sizeof *dict); |
| 13 | if (!dict) |
| 14 | return NULL; |
| 15 | |
| 16 | dict->name = name; |
| 17 | dict->values = EB_ROOT_UNIQUE; |
| 18 | HA_RWLOCK_INIT(&dict->rwlock); |
| 19 | |
| 20 | return dict; |
| 21 | } |
| 22 | |
| 23 | /* |
| 24 | * Allocate a new dictionary entry with <s> as string value which is strdup()'ed. |
| 25 | * Returns the new allocated entry if succeeded, NULL if not. |
| 26 | */ |
| 27 | static struct dict_entry *new_dict_entry(char *s) |
| 28 | { |
| 29 | struct dict_entry *de; |
| 30 | |
| 31 | de = calloc(1, sizeof *de); |
| 32 | if (!de) |
| 33 | return NULL; |
| 34 | |
| 35 | de->value.key = strdup(s); |
| 36 | if (!de->value.key) |
| 37 | goto err; |
| 38 | |
Frédéric Lécaille | 99de1d0 | 2019-06-07 10:58:20 +0200 | [diff] [blame] | 39 | de->len = strlen(s); |
Frédéric Lécaille | 4a3fef8 | 2019-05-28 14:47:17 +0200 | [diff] [blame] | 40 | de->refcount = 1; |
| 41 | |
| 42 | return de; |
| 43 | |
| 44 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 45 | ha_free(&de->value.key); |
Frédéric Lécaille | 99de1d0 | 2019-06-07 10:58:20 +0200 | [diff] [blame] | 46 | de->len = 0; |
Frédéric Lécaille | 4a3fef8 | 2019-05-28 14:47:17 +0200 | [diff] [blame] | 47 | free(de); |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * Release the memory allocated for <de> dictionary entry. |
| 53 | */ |
| 54 | static void free_dict_entry(struct dict_entry *de) |
| 55 | { |
| 56 | de->refcount = 0; |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 57 | ha_free(&de->value.key); |
Frédéric Lécaille | 4a3fef8 | 2019-05-28 14:47:17 +0200 | [diff] [blame] | 58 | free(de); |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Simple function to lookup dictionary entries with <s> as value. |
| 63 | */ |
| 64 | static struct dict_entry *__dict_lookup(struct dict *d, const char *s) |
| 65 | { |
| 66 | struct dict_entry *de; |
| 67 | struct ebpt_node *node; |
| 68 | |
| 69 | de = NULL; |
| 70 | node = ebis_lookup(&d->values, s); |
| 71 | if (node) |
| 72 | de = container_of(node, struct dict_entry, value); |
| 73 | |
| 74 | return de; |
| 75 | } |
| 76 | |
| 77 | /* |
Frédéric Lécaille | 4a3fef8 | 2019-05-28 14:47:17 +0200 | [diff] [blame] | 78 | * Insert an entry in <d> dictionary with <s> as value. * |
| 79 | */ |
| 80 | struct dict_entry *dict_insert(struct dict *d, char *s) |
| 81 | { |
| 82 | struct dict_entry *de; |
Frédéric Lécaille | b5ecf03 | 2019-06-11 08:34:26 +0200 | [diff] [blame] | 83 | struct ebpt_node *n; |
Frédéric Lécaille | 4a3fef8 | 2019-05-28 14:47:17 +0200 | [diff] [blame] | 84 | |
| 85 | HA_RWLOCK_RDLOCK(DICT_LOCK, &d->rwlock); |
| 86 | de = __dict_lookup(d, s); |
| 87 | HA_RWLOCK_RDUNLOCK(DICT_LOCK, &d->rwlock); |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 88 | if (de) { |
| 89 | HA_ATOMIC_ADD(&de->refcount, 1); |
Frédéric Lécaille | 4a3fef8 | 2019-05-28 14:47:17 +0200 | [diff] [blame] | 90 | return de; |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 91 | } |
Frédéric Lécaille | 4a3fef8 | 2019-05-28 14:47:17 +0200 | [diff] [blame] | 92 | |
| 93 | de = new_dict_entry(s); |
| 94 | if (!de) |
| 95 | return NULL; |
| 96 | |
| 97 | HA_RWLOCK_WRLOCK(DICT_LOCK, &d->rwlock); |
Frédéric Lécaille | b5ecf03 | 2019-06-11 08:34:26 +0200 | [diff] [blame] | 98 | n = ebis_insert(&d->values, &de->value); |
Frédéric Lécaille | 4a3fef8 | 2019-05-28 14:47:17 +0200 | [diff] [blame] | 99 | HA_RWLOCK_WRUNLOCK(DICT_LOCK, &d->rwlock); |
Frédéric Lécaille | b5ecf03 | 2019-06-11 08:34:26 +0200 | [diff] [blame] | 100 | if (n != &de->value) { |
| 101 | free_dict_entry(de); |
| 102 | de = container_of(n, struct dict_entry, value); |
| 103 | } |
Frédéric Lécaille | 4a3fef8 | 2019-05-28 14:47:17 +0200 | [diff] [blame] | 104 | |
| 105 | return de; |
| 106 | } |
| 107 | |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * Unreference a dict entry previously acquired with <dict_insert>. |
| 111 | * If this is the last live reference to the entry, it is |
| 112 | * removed from the dictionary. |
| 113 | */ |
| 114 | void dict_entry_unref(struct dict *d, struct dict_entry *de) |
| 115 | { |
| 116 | if (!de) |
| 117 | return; |
| 118 | |
| 119 | if (HA_ATOMIC_SUB(&de->refcount, 1) != 0) |
| 120 | return; |
| 121 | |
| 122 | HA_RWLOCK_WRLOCK(DICT_LOCK, &d->rwlock); |
| 123 | ebpt_delete(&de->value); |
| 124 | HA_RWLOCK_WRUNLOCK(DICT_LOCK, &d->rwlock); |
| 125 | |
| 126 | free_dict_entry(de); |
| 127 | } |