Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "telescope.h"
19 #include <stdlib.h>
21 static void *hash_alloc(size_t, void*);
22 static void *hash_calloc(size_t, size_t, void*);
23 static void hash_free(void*, void*);
25 static void *
26 hash_alloc(size_t len, void *d)
27 {
28 if ((d = malloc(len)) == NULL)
29 abort();
30 return d;
31 }
33 static void *
34 hash_calloc(size_t nmemb, size_t size, void *d)
35 {
36 if ((d = calloc(nmemb, size)) == NULL)
37 abort();
38 return d;
39 }
41 static void
42 hash_free(void *ptr, void *d)
43 {
44 free(ptr);
45 }
47 void
48 telescope_ohash_init(struct ohash *h, unsigned int sz, ptrdiff_t ko)
49 {
50 struct ohash_info info = {
51 .key_offset = ko,
52 .calloc = hash_calloc,
53 .free = hash_free,
54 .alloc = hash_alloc,
55 };
57 ohash_init(h, sz, &info);
58 }
60 struct tofu_entry *
61 telescope_lookup_tofu(struct ohash *h, const char *domain)
62 {
63 unsigned int slot;
65 slot = ohash_qlookup(h, domain);
66 return ohash_find(h, slot);
67 }
69 void
70 telescope_ohash_insert(struct ohash *h, struct tofu_entry *e)
71 {
72 unsigned int slot;
74 slot = ohash_qlookup(h, e->domain);
75 ohash_insert(h, slot, e);
76 }