1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* include/k5-hash.h - hash table interface definitions */
4 * Copyright (C) 2018 by the Massachusetts Institute of Technology.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
34 * This file contains declarations for a simple hash table using siphash. Some
35 * limitations which might need to be addressed in the future:
37 * - The table does not manage caller memory. This limitation could be
38 * addressed by adding an optional free callback to k5_hashtab_create(), to
39 * be called by k5_hashtab_free() and k5_hashtab_remove().
41 * - There is no way to iterate over a hash table.
43 * - k5_hashtab_add() does not check for duplicate entries.
49 #define K5_HASH_SEED_LEN 16
54 * Create a new hash table in *ht_out. seed must point to random bytes if keys
55 * might be under the control of an attacker; otherwise it may be NULL.
56 * initial_buckets controls the initial allocation of hash buckets; pass zero
57 * to use a default value. The number of hash buckets will be doubled as the
58 * number of entries increases. Return 0 on success, ENOMEM on failure.
60 int k5_hashtab_create(const uint8_t seed[K5_HASH_SEED_LEN],
61 size_t initial_buckets, struct k5_hashtab **ht_out);
63 /* Release the memory used by a hash table. Keys and values are the caller's
65 void k5_hashtab_free(struct k5_hashtab *ht);
67 /* Add an entry to a hash table. key and val must remain valid until the entry
68 * is removed or the hash table is freed. The caller must avoid duplicates. */
69 int k5_hashtab_add(struct k5_hashtab *ht, const void *key, size_t klen,
72 /* Remove an entry from a hash table by key. Does not free key or the
73 * associated value. Return 1 if the key was found and removed, 0 if not. */
74 int k5_hashtab_remove(struct k5_hashtab *ht, const void *key, size_t klen);
76 /* Retrieve a value from a hash table by key. */
77 void *k5_hashtab_get(struct k5_hashtab *ht, const void *key, size_t klen);
79 #endif /* K5_HASH_H */