Blame


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