|
1 | 1 | /** |
2 | 2 | * @file hashmap.h |
| 3 | + * @brief Open-addressed hashmap (Robin Hood), with allocator udata support. |
| 4 | + * |
| 5 | + * Updated by Craig Edwards, September 2025, to allow `udata` to be passed to |
| 6 | + * the allocator functions. This enables contextual/arena allocators (e.g., per |
| 7 | + * BASIC interpreter context) without altering external call sites. |
| 8 | + * |
| 9 | + * Based on tidwall hashmap, copyright 2020 Joshua J Baker. All rights |
| 10 | + * reserved. Use of this source code is governed by an MIT-style licence that |
| 11 | + * can be found in the LICENSE file. https://github.com/tidwall/hashmap.c |
3 | 12 | */ |
4 | | -// Copyright 2020 Joshua J Baker. All rights reserved. |
5 | | -// Use of this source code is governed by an MIT-style |
6 | | -// license that can be found in the LICENSE file. |
7 | | - |
8 | | -// https://github.com/tidwall/hashmap.c |
9 | | - |
10 | | -#ifndef HASHMAP_H |
11 | | -#define HASHMAP_H |
| 13 | +#pragma once |
12 | 14 |
|
13 | 15 | #include "kernel.h" |
14 | 16 | #include <stdbool.h> |
|
17 | 19 |
|
18 | 20 | struct hashmap; |
19 | 21 |
|
20 | | -struct hashmap *hashmap_new(size_t elsize, size_t cap, |
21 | | - uint64_t seed0, uint64_t seed1, |
22 | | - uint64_t (*hash)(const void *item, |
23 | | - uint64_t seed0, uint64_t seed1), |
24 | | - int (*compare)(const void *a, const void *b, |
25 | | - void *udata), |
26 | | - void (*elfree)(const void *item), |
27 | | - void *udata); |
28 | | -struct hashmap *hashmap_new_with_allocator( |
29 | | - void *(*malloc)(size_t), |
30 | | - void *(*realloc)(void *, size_t), |
31 | | - void (*free)(const void*), |
32 | | - size_t elsize, size_t cap, |
33 | | - uint64_t seed0, uint64_t seed1, |
34 | | - uint64_t (*hash)(const void *item, |
35 | | - uint64_t seed0, uint64_t seed1), |
36 | | - int (*compare)(const void *a, const void *b, |
37 | | - void *udata), |
38 | | - void (*elfree)(const void *item), |
39 | | - void *udata); |
| 22 | +/* -------------------------------------------------------------------------- |
| 23 | + * Callback typedefs |
| 24 | + * -------------------------------------------------------------------------- */ |
| 25 | + |
| 26 | +/** |
| 27 | + * @brief Allocate a new block. |
| 28 | + * @param size Bytes requested. |
| 29 | + * @param udata User data pointer supplied at map construction. |
| 30 | + * @return Pointer to allocated block, or NULL on failure. |
| 31 | + */ |
| 32 | +typedef void *(*hashmap_allocator)(size_t size, void *udata); |
| 33 | + |
| 34 | +/** |
| 35 | + * @brief Resize an existing block. |
| 36 | + * @param ptr Existing allocation (or NULL to behave like malloc). |
| 37 | + * @param size New size in bytes. |
| 38 | + * @param udata User data pointer supplied at map construction. |
| 39 | + * @return Pointer to (possibly moved) block, or NULL on failure. |
| 40 | + */ |
| 41 | +typedef void *(*hashmap_reallocator)(void *ptr, size_t size, void *udata); |
| 42 | + |
| 43 | +/** |
| 44 | + * @brief Release an allocated block. |
| 45 | + * @param ptr Block to free (may be NULL). |
| 46 | + * @param udata User data pointer supplied at map construction. |
| 47 | + */ |
| 48 | +typedef void (*hashmap_releaser)(const void *ptr, void *udata); |
| 49 | + |
| 50 | +/** |
| 51 | + * @brief Hash an item. |
| 52 | + * @param item Pointer to key/item. |
| 53 | + * @param seed0 First 64-bit seed. |
| 54 | + * @param seed1 Second 64-bit seed. |
| 55 | + * @return 64-bit hash value. |
| 56 | + */ |
| 57 | +typedef uint64_t (*hashmap_hash_fn)(const void *item, uint64_t seed0, uint64_t seed1); |
| 58 | + |
| 59 | +/** |
| 60 | + * @brief Compare two items. |
| 61 | + * @param a First item. |
| 62 | + * @param b Second item. |
| 63 | + * @param udata User data pointer supplied at map construction. |
| 64 | + * @return <0 if a<b, 0 if equal, >0 if a>b. |
| 65 | + */ |
| 66 | +typedef int (*hashmap_compare_fn)(const void *a, const void *b, void *udata); |
| 67 | + |
| 68 | +/** |
| 69 | + * @brief Optional element destructor for items stored by value. |
| 70 | + * @param item Element to dispose. |
| 71 | + * |
| 72 | + * Note: This is only for element-internal resources. The map storage itself is |
| 73 | + * freed by the map’s allocator callbacks. |
| 74 | + */ |
| 75 | +typedef void (*hashmap_elfree_fn)(const void *item, void *udata); |
| 76 | + |
| 77 | +/** |
| 78 | + * @brief Iterator callback for scanning all items. |
| 79 | + * @param item Current element. |
| 80 | + * @param udata User data pointer supplied at call site. |
| 81 | + * @return true to continue, false to stop. |
| 82 | + */ |
| 83 | +typedef bool (*hashmap_iter_fn)(const void *item, void *udata); |
| 84 | + |
| 85 | + |
| 86 | +/* -------------------------------------------------------------------------- |
| 87 | + * Creation / destruction |
| 88 | + * -------------------------------------------------------------------------- */ |
| 89 | + |
| 90 | +/** |
| 91 | + * @brief Create a new hashmap with standard kernel allocators. |
| 92 | + * |
| 93 | + * @param elsize Size in bytes of each element stored by the map. |
| 94 | + * @param cap Initial capacity hint (0 defaults to 16). |
| 95 | + * @param seed0 First 64-bit hash seed. |
| 96 | + * @param seed1 Second 64-bit hash seed. |
| 97 | + * @param hash Hash function for items. |
| 98 | + * @param compare Comparison function for items. |
| 99 | + * @param elfree Optional element destructor (may be NULL). |
| 100 | + * @param udata User data passed to `compare` (and may be used internally). |
| 101 | + * @return Handle to a new hashmap, or NULL on OOM. |
| 102 | + */ |
| 103 | +struct hashmap *hashmap_new(size_t elsize, size_t cap, uint64_t seed0, uint64_t seed1, hashmap_hash_fn hash, hashmap_compare_fn compare, hashmap_elfree_fn elfree, void *udata); |
| 104 | + |
| 105 | +/** |
| 106 | + * @brief Create a new hashmap with custom allocators. |
| 107 | + * |
| 108 | + * @param _malloc Allocator callback (required). |
| 109 | + * @param _realloc Reallocator callback (required). |
| 110 | + * @param _free Releaser callback (required). |
| 111 | + * @param elsize Size in bytes of each element stored by the map. |
| 112 | + * @param cap Initial capacity hint (0 defaults to 16). |
| 113 | + * @param seed0 First 64-bit hash seed. |
| 114 | + * @param seed1 Second 64-bit hash seed. |
| 115 | + * @param hash Hash function for items. |
| 116 | + * @param compare Comparison function for items. |
| 117 | + * @param elfree Optional element destructor (may be NULL). |
| 118 | + * @param udata User data passed to allocator callbacks and `compare`. |
| 119 | + * @return Handle to a new hashmap, or NULL on OOM. |
| 120 | + */ |
| 121 | +struct hashmap *hashmap_new_with_allocator(hashmap_allocator _malloc, hashmap_reallocator _realloc, hashmap_releaser _free, size_t elsize, size_t cap, uint64_t seed0, uint64_t seed1, hashmap_hash_fn hash, hashmap_compare_fn compare, hashmap_elfree_fn elfree, void *udata); |
| 122 | + |
| 123 | +/** |
| 124 | + * @brief Free a hashmap and its storage. |
| 125 | + * |
| 126 | + * Calls `elfree` for each element if provided, then releases internal buffers |
| 127 | + * via the map’s allocator callbacks. |
| 128 | + * |
| 129 | + * @param map Hashmap handle (may be NULL). |
| 130 | + */ |
40 | 131 | void hashmap_free(struct hashmap *map); |
| 132 | + |
| 133 | +/** |
| 134 | + * @brief Clear all items from the map. |
| 135 | + * |
| 136 | + * Each element is passed to `elfree` (if provided). If `update_cap` is true, |
| 137 | + * internal capacity is trimmed to the current number of buckets to avoid new |
| 138 | + * allocations on the clear. |
| 139 | + * |
| 140 | + * @param map Hashmap handle. |
| 141 | + * @param update_cap When true, shrink capacity to current bucket count. |
| 142 | + */ |
41 | 143 | void hashmap_clear(struct hashmap *map, bool update_cap); |
| 144 | + |
| 145 | +/** |
| 146 | + * @brief Get the number of stored items. |
| 147 | + * @param map Hashmap handle. |
| 148 | + * @return Item count. |
| 149 | + */ |
42 | 150 | size_t hashmap_count(struct hashmap *map); |
| 151 | + |
| 152 | +/** |
| 153 | + * @brief Test whether the previous `hashmap_set` failed due to OOM. |
| 154 | + * @param map Hashmap handle. |
| 155 | + * @return true if the last set operation ran out of memory. |
| 156 | + */ |
43 | 157 | bool hashmap_oom(struct hashmap *map); |
| 158 | + |
| 159 | +/** |
| 160 | + * @brief Look up an item by key. |
| 161 | + * @param map Hashmap handle. |
| 162 | + * @param item Key to search for. |
| 163 | + * @return Pointer to stored item, or NULL if not found. |
| 164 | + */ |
44 | 165 | void *hashmap_get(struct hashmap *map, const void *item); |
| 166 | + |
| 167 | +/** |
| 168 | + * @brief Insert or replace an item. |
| 169 | + * @param map Hashmap handle. |
| 170 | + * @param item Item to insert (by value). |
| 171 | + * @return Pointer to the replaced item if it existed, otherwise NULL. |
| 172 | + * |
| 173 | + * On allocation failure returns NULL and sets the sticky OOM flag |
| 174 | + * (see `hashmap_oom`). |
| 175 | + */ |
45 | 176 | void *hashmap_set(struct hashmap *map, const void *item); |
| 177 | + |
| 178 | +/** |
| 179 | + * @brief Remove an item by key. |
| 180 | + * @param map Hashmap handle. |
| 181 | + * @param item Key to delete. |
| 182 | + * @return Pointer to the removed item, or NULL if not found. |
| 183 | + */ |
46 | 184 | void *hashmap_delete(struct hashmap *map, void *item); |
| 185 | + |
| 186 | +/** |
| 187 | + * @brief Probe a raw bucket position. |
| 188 | + * @param map Hashmap handle. |
| 189 | + * @param position Bucket index (will be reduced modulo bucket count). |
| 190 | + * @return Pointer to stored item at that bucket, or NULL. |
| 191 | + */ |
47 | 192 | void *hashmap_probe(struct hashmap *map, uint64_t position); |
48 | | -bool hashmap_scan(struct hashmap *map, |
49 | | - bool (*iter)(const void *item, void *udata), void *udata); |
50 | | -bool hashmap_iter(struct hashmap *map, size_t *i, void **item); |
51 | 193 |
|
52 | | -uint64_t hashmap_sip(const void *data, size_t len, |
53 | | - uint64_t seed0, uint64_t seed1); |
54 | | -uint64_t hashmap_murmur(const void *data, size_t len, |
55 | | - uint64_t seed0, uint64_t seed1); |
| 194 | +/** |
| 195 | + * @brief Iterate over every item in the map. |
| 196 | + * @param map Hashmap handle. |
| 197 | + * @param iter Callback invoked for each item; return false to stop early. |
| 198 | + * @param udata User data passed to `iter`. |
| 199 | + * @return false if iteration was stopped early, true otherwise. |
| 200 | + */ |
| 201 | +bool hashmap_scan(struct hashmap *map, hashmap_iter_fn iter, void *udata); |
56 | 202 |
|
| 203 | +/** |
| 204 | + * @brief Cursor-based iterator over items. |
| 205 | + * @param map Hashmap handle. |
| 206 | + * @param i Cursor (initialise to 0; updated on each call). |
| 207 | + * @param item Out: pointer to the current stored item. |
| 208 | + * @return true if an item was produced; false if iteration is complete. |
| 209 | + * |
| 210 | + * Note: If `hashmap_delete` is called during iteration, the bucket layout may |
| 211 | + * change. Reset the cursor to 0 to restart iteration safely. |
| 212 | + */ |
| 213 | +bool hashmap_iter(struct hashmap *map, size_t *i, void **item); |
57 | 214 |
|
58 | | -// DEPRECATED: use `hashmap_new_with_allocator` |
59 | | -void hashmap_set_allocator(void *(*malloc)(size_t), void (*free)(const void*)); |
| 215 | +/** |
| 216 | + * @brief SipHash-2-4. |
| 217 | + * @param data Input buffer. |
| 218 | + * @param len Length in bytes. |
| 219 | + * @param seed0 First 64-bit seed. |
| 220 | + * @param seed1 Second 64-bit seed. |
| 221 | + * @return 64-bit hash value. |
| 222 | + */ |
| 223 | +uint64_t hashmap_sip(const void *data, size_t len, uint64_t seed0, uint64_t seed1); |
60 | 224 |
|
61 | | -#endif |
| 225 | +/** |
| 226 | + * @brief Murmur3_86_128 (folded to 64 bits). |
| 227 | + * @param data Input buffer. |
| 228 | + * @param len Length in bytes. |
| 229 | + * @param seed0 First 64-bit seed. |
| 230 | + * @param seed1 Second 64-bit seed. |
| 231 | + * @return 64-bit hash value. |
| 232 | + */ |
| 233 | +uint64_t hashmap_murmur(const void *data, size_t len, uint64_t seed0, uint64_t seed1); |
0 commit comments