2 8d1b399b 2021-07-22 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 8d1b399b 2021-07-22 op * Permission to use, copy, modify, and distribute this software for any
5 8d1b399b 2021-07-22 op * purpose with or without fee is hereby granted, provided that the above
6 8d1b399b 2021-07-22 op * copyright notice and this permission notice appear in all copies.
8 8d1b399b 2021-07-22 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 8d1b399b 2021-07-22 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 8d1b399b 2021-07-22 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 8d1b399b 2021-07-22 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 8d1b399b 2021-07-22 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 8d1b399b 2021-07-22 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 8d1b399b 2021-07-22 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 8d1b399b 2021-07-22 op #include "compat.h"
19 8d1b399b 2021-07-22 op #include <ohash.h>
20 8d1b399b 2021-07-22 op #include <stdlib.h>
22 8d1b399b 2021-07-22 op #include "kamid.h"
23 8d1b399b 2021-07-22 op #include "utils.h"
25 8d1b399b 2021-07-22 op static void *hash_alloc(size_t, void *);
26 8d1b399b 2021-07-22 op static void *hash_calloc(size_t, size_t, void *);
27 8d1b399b 2021-07-22 op static void hash_free(void *, void *);
29 8d1b399b 2021-07-22 op static int table_static_open(struct table *);
30 8d1b399b 2021-07-22 op static int table_static_add(struct table *, const char *, const char *);
31 8d1b399b 2021-07-22 op static int table_static_lookup(struct table *, const char *, char **);
32 8d1b399b 2021-07-22 op static void table_static_close(struct table *);
34 8d1b399b 2021-07-22 op struct table_backend table_static = {
36 8d1b399b 2021-07-22 op table_static_open,
37 8d1b399b 2021-07-22 op table_static_add,
38 8d1b399b 2021-07-22 op table_static_lookup,
39 8d1b399b 2021-07-22 op table_static_close,
48 8d1b399b 2021-07-22 op hash_alloc(size_t len, void *d)
50 8d1b399b 2021-07-22 op return xmalloc(len);
54 8d1b399b 2021-07-22 op hash_calloc(size_t nmemb, size_t size, void *d)
56 8d1b399b 2021-07-22 op return xcalloc(nmemb, size);
60 8d1b399b 2021-07-22 op hash_free(void *ptr, void *d)
66 8d1b399b 2021-07-22 op table_static_open(struct table *t)
68 8d1b399b 2021-07-22 op struct ohash_info info = {
69 8d1b399b 2021-07-22 op .key_offset = offsetof(struct kp, key),
70 8d1b399b 2021-07-22 op .calloc = hash_calloc,
71 8d1b399b 2021-07-22 op .free = hash_free,
72 8d1b399b 2021-07-22 op .alloc = hash_alloc,
75 8d1b399b 2021-07-22 op t->t_handle = xmalloc(sizeof(struct ohash));
76 8d1b399b 2021-07-22 op ohash_init(t->t_handle, 5, &info);
81 8d1b399b 2021-07-22 op table_static_add(struct table *t, const char *key, const char *val)
83 8d1b399b 2021-07-22 op struct kp *kp;
84 8d1b399b 2021-07-22 op unsigned int slot;
86 8d1b399b 2021-07-22 op kp = xcalloc(1, sizeof(*kp));
87 8d1b399b 2021-07-22 op kp->key = xstrdup(key);
88 8d1b399b 2021-07-22 op if (val != NULL)
89 8d1b399b 2021-07-22 op kp->val = xstrdup(val);
91 8d1b399b 2021-07-22 op slot = ohash_qlookup(t->t_handle, kp->key);
92 8d1b399b 2021-07-22 op ohash_insert(t->t_handle, slot, kp);
98 8d1b399b 2021-07-22 op table_static_lookup(struct table *t, const char *key, char **ret_val)
100 8d1b399b 2021-07-22 op struct kp *kp;
101 8d1b399b 2021-07-22 op unsigned int slot;
103 8d1b399b 2021-07-22 op slot = ohash_qlookup(t->t_handle, key);
104 8d1b399b 2021-07-22 op if ((kp = ohash_find(t->t_handle, slot)) == NULL)
107 8d1b399b 2021-07-22 op *ret_val = xstrdup(kp->val);
112 8d1b399b 2021-07-22 op table_static_close(struct table *t)
114 8d1b399b 2021-07-22 op struct kp *kp;
115 8d1b399b 2021-07-22 op unsigned int i;
117 8d1b399b 2021-07-22 op for (kp = ohash_first(t->t_handle, &i);
119 8d1b399b 2021-07-22 op kp = ohash_next(t->t_handle, &i)) {
120 8d1b399b 2021-07-22 op ohash_remove(t->t_handle, i);
121 8d1b399b 2021-07-22 op free(kp->key);
122 8d1b399b 2021-07-22 op free(kp->val);
126 8d1b399b 2021-07-22 op free(t->t_handle);