/* * Copyright (c) 2021 Omar Polo * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include "telescope.h" #include static void *hash_alloc(size_t, void*); static void *hash_calloc(size_t, size_t, void*); static void hash_free(void*, void*); static void * hash_alloc(size_t len, void *d) { if ((d = malloc(len)) == NULL) abort(); return d; } static void * hash_calloc(size_t nmemb, size_t size, void *d) { if ((d = calloc(nmemb, size)) == NULL) abort(); return d; } static void hash_free(void *ptr, void *d) { free(ptr); } void telescope_ohash_init(struct ohash *h, unsigned int sz, ptrdiff_t ko) { struct ohash_info info = { .key_offset = ko, .calloc = hash_calloc, .free = hash_free, .alloc = hash_alloc, }; ohash_init(h, sz, &info); } struct tofu_entry * telescope_lookup_tofu(struct ohash *h, const char *domain) { unsigned int slot; slot = ohash_qlookup(h, domain); return ohash_find(h, slot); } void telescope_ohash_insert(struct ohash *h, struct tofu_entry *e) { unsigned int slot; slot = ohash_qlookup(h, e->domain); ohash_insert(h, slot, e); }