Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Generic hashtable | ||
3 | * Copyright (C) 2025 Emma Worley <emma@emma.gg> | ||
4 | * | ||
5 | * This file is part of FFmpeg. | ||
6 | * | ||
7 | * FFmpeg is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU Lesser General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2.1 of the License, or (at your option) any later version. | ||
11 | * | ||
12 | * FFmpeg is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with FFmpeg; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | #include <stdint.h> | ||
23 | #include <string.h> | ||
24 | |||
25 | #include "libavutil/attributes.h" | ||
26 | #include "libavutil/crc.h" | ||
27 | #include "libavutil/error.h" | ||
28 | #include "libavutil/macros.h" | ||
29 | #include "libavutil/mem.h" | ||
30 | #include "hashtable.h" | ||
31 | |||
32 | #define ALIGN _Alignof(size_t) | ||
33 | |||
34 | struct FFHashtableContext { | ||
35 | size_t key_size; | ||
36 | size_t val_size; | ||
37 | size_t entry_size; | ||
38 | size_t max_entries; | ||
39 | size_t nb_entries; | ||
40 | const AVCRC *crc; | ||
41 | uint8_t *table; | ||
42 | uint8_t swapbuf[]; | ||
43 | }; | ||
44 | |||
45 | /* | ||
46 | * Hash table entries are comprised of a probe sequence length (PSL), key, and | ||
47 | * value. When the PSL of an entry is zero, it means it is not occupied by a | ||
48 | * key/value pair. When the PSL is non-zero, it represents the "distance" of | ||
49 | * the entry from its "home" location plus one, where the "home" location is | ||
50 | * hash(key) % max_entries. | ||
51 | */ | ||
52 | |||
53 | #define ENTRY_PSL_VAL(entry) (*(size_t*)(entry)) | ||
54 | #define ENTRY_KEY_PTR(entry) ((entry) + sizeof(size_t)) | ||
55 | #define ENTRY_VAL_PTR(entry) (ENTRY_KEY_PTR(entry) + ctx->key_size) | ||
56 | |||
57 | #define KEYS_EQUAL(k1, k2) (!memcmp((k1), (k2), ctx->key_size)) | ||
58 | |||
59 | 29 | av_cold int ff_hashtable_alloc(FFHashtableContext **ctx, size_t key_size, | |
60 | size_t val_size, size_t max_entries) | ||
61 | { | ||
62 | 29 | const size_t keyval_size = key_size + val_size; | |
63 | |||
64 |
3/4✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
|
29 | if (keyval_size < key_size || // did (unsigned,defined) wraparound happen? |
65 | keyval_size > FFMIN(SIZE_MAX - sizeof(size_t) - (ALIGN - 1), | ||
66 | (SIZE_MAX - sizeof(FFHashtableContext)) / 2)) | ||
67 | 1 | return AVERROR(ERANGE); | |
68 | |||
69 | 28 | FFHashtableContext *res = av_mallocz(sizeof(*res) + 2 * keyval_size); | |
70 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (!res) |
71 | ✗ | return AVERROR(ENOMEM); | |
72 | 28 | res->key_size = key_size; | |
73 | 28 | res->val_size = val_size; | |
74 | 28 | res->entry_size = FFALIGN(sizeof(size_t) + keyval_size, ALIGN); | |
75 | 28 | res->max_entries = max_entries; | |
76 | 28 | res->nb_entries = 0; | |
77 | 28 | res->crc = av_crc_get_table(AV_CRC_32_IEEE); | |
78 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (!res->crc) { |
79 | ✗ | ff_hashtable_freep(&res); | |
80 | ✗ | return AVERROR_BUG; | |
81 | } | ||
82 | 28 | res->table = av_calloc(res->max_entries, res->entry_size); | |
83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (!res->table) { |
84 | ✗ | ff_hashtable_freep(&res); | |
85 | ✗ | return AVERROR(ENOMEM); | |
86 | } | ||
87 | |||
88 | 28 | *ctx = res; | |
89 | 28 | return 0; | |
90 | } | ||
91 | |||
92 | 731034 | static size_t hash_key(const struct FFHashtableContext *ctx, const void *key) | |
93 | { | ||
94 | 731034 | return av_crc(ctx->crc, 0, key, ctx->key_size) % ctx->max_entries; | |
95 | } | ||
96 | |||
97 | 337209 | int ff_hashtable_get(const struct FFHashtableContext *ctx, const void *key, void *val) | |
98 | { | ||
99 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 337208 times.
|
337209 | if (!ctx->nb_entries) |
100 | 1 | return 0; | |
101 | |||
102 | 337208 | size_t hash = hash_key(ctx, key); | |
103 | |||
104 |
1/2✓ Branch 0 taken 337436 times.
✗ Branch 1 not taken.
|
337436 | for (size_t psl = 1; psl <= ctx->max_entries; psl++) { |
105 | 337436 | size_t wrapped_index = (hash + psl) % ctx->max_entries; | |
106 | 337436 | uint8_t *entry = ctx->table + wrapped_index * ctx->entry_size; | |
107 |
2/2✓ Branch 0 taken 12054 times.
✓ Branch 1 taken 325382 times.
|
337436 | if (ENTRY_PSL_VAL(entry) < psl) |
108 | // When PSL stops increasing it means there are no further entries | ||
109 | // with the same key hash. | ||
110 | 12054 | return 0; | |
111 |
2/2✓ Branch 0 taken 325154 times.
✓ Branch 1 taken 228 times.
|
325382 | if (KEYS_EQUAL(ENTRY_KEY_PTR(entry), key)) { |
112 | 325154 | memcpy(val, ENTRY_VAL_PTR(entry), ctx->val_size); | |
113 | 325154 | return 1; | |
114 | } | ||
115 | } | ||
116 | ✗ | return 0; | |
117 | } | ||
118 | |||
119 | 391686 | int ff_hashtable_set(struct FFHashtableContext *ctx, const void *key, const void *val) | |
120 | { | ||
121 | 391686 | int swapping = 0; | |
122 | 391686 | size_t psl = 1; | |
123 | 391686 | size_t hash = hash_key(ctx, key); | |
124 | 391686 | size_t wrapped_index = hash % ctx->max_entries; | |
125 | 391686 | uint8_t *set = ctx->swapbuf; | |
126 | 391686 | uint8_t *tmp = ctx->swapbuf + ctx->key_size + ctx->val_size; | |
127 | |||
128 | 391686 | memcpy(set, key, ctx->key_size); | |
129 | 391686 | memcpy(set + ctx->key_size, val, ctx->val_size); | |
130 | |||
131 |
1/2✓ Branch 0 taken 391919 times.
✗ Branch 1 not taken.
|
391919 | for (size_t i = 0; i < ctx->max_entries; i++) { |
132 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 391916 times.
|
391919 | if (++wrapped_index == ctx->max_entries) |
133 | 3 | wrapped_index = 0; | |
134 | 391919 | uint8_t *entry = ctx->table + wrapped_index * ctx->entry_size; | |
135 |
5/6✓ Branch 0 taken 379860 times.
✓ Branch 1 taken 12059 times.
✓ Branch 2 taken 379860 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 379626 times.
✓ Branch 5 taken 234 times.
|
391919 | if (!ENTRY_PSL_VAL(entry) || (!swapping && KEYS_EQUAL(ENTRY_KEY_PTR(entry), set))) { |
136 |
2/2✓ Branch 0 taken 12059 times.
✓ Branch 1 taken 379626 times.
|
391685 | if (!ENTRY_PSL_VAL(entry)) |
137 | 12059 | ctx->nb_entries++; | |
138 | 391685 | ENTRY_PSL_VAL(entry) = psl; | |
139 | 391685 | memcpy(ENTRY_KEY_PTR(entry), set, ctx->key_size + ctx->val_size); | |
140 | 391685 | return 1; | |
141 | } | ||
142 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 231 times.
|
234 | if (ENTRY_PSL_VAL(entry) < psl) { |
143 | // When PSL stops increasing it means there are no further entries | ||
144 | // with the same key hash. We can only hope to find an unoccupied | ||
145 | // entry. | ||
146 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (ctx->nb_entries == ctx->max_entries) |
147 | // The table is full so inserts are impossible. | ||
148 | 1 | return 0; | |
149 | // Robin Hood hash tables "steal from the rich" by minimizing the | ||
150 | // PSL of the inserted entry. | ||
151 | 2 | swapping = 1; | |
152 | // set needs to swap with entry | ||
153 | 2 | memcpy(tmp, ENTRY_KEY_PTR(entry), ctx->key_size + ctx->val_size); | |
154 | 2 | memcpy(ENTRY_KEY_PTR(entry), set, ctx->key_size + ctx->val_size); | |
155 | 2 | FFSWAP(uint8_t*, set, tmp); | |
156 | 2 | FFSWAP(size_t, psl, ENTRY_PSL_VAL(entry)); | |
157 | } | ||
158 | 233 | psl++; | |
159 | } | ||
160 | ✗ | return 0; | |
161 | } | ||
162 | |||
163 | 2141 | int ff_hashtable_delete(struct FFHashtableContext *ctx, const void *key) | |
164 | { | ||
165 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2140 times.
|
2141 | if (!ctx->nb_entries) |
166 | 1 | return 0; | |
167 | |||
168 | uint8_t *next_entry; | ||
169 | 2140 | size_t hash = hash_key(ctx, key); | |
170 | 2140 | size_t wrapped_index = hash % ctx->max_entries; | |
171 | |||
172 |
1/2✓ Branch 0 taken 2140 times.
✗ Branch 1 not taken.
|
2140 | for (size_t psl = 1; psl <= ctx->max_entries; psl++) { |
173 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2140 times.
|
2140 | if (++wrapped_index == ctx->max_entries) |
174 | ✗ | wrapped_index = 0; | |
175 | 2140 | uint8_t *entry = ctx->table + wrapped_index * ctx->entry_size; | |
176 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2140 times.
|
2140 | if (ENTRY_PSL_VAL(entry) < psl) |
177 | // When PSL stops increasing it means there are no further entries | ||
178 | // with the same key hash. | ||
179 | ✗ | return 0; | |
180 |
1/2✓ Branch 0 taken 2140 times.
✗ Branch 1 not taken.
|
2140 | if (KEYS_EQUAL(ENTRY_KEY_PTR(entry), key)) { |
181 | 2140 | ENTRY_PSL_VAL(entry) = 0; | |
182 | // Shift each following entry that will benefit from a reduced PSL. | ||
183 |
1/2✓ Branch 0 taken 2182 times.
✗ Branch 1 not taken.
|
2182 | for (psl++; psl <= ctx->max_entries; psl++) { |
184 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2181 times.
|
2182 | if (++wrapped_index == ctx->max_entries) |
185 | 1 | wrapped_index = 0; | |
186 | 2182 | next_entry = ctx->table + wrapped_index * ctx->entry_size; | |
187 |
2/2✓ Branch 0 taken 2140 times.
✓ Branch 1 taken 42 times.
|
2182 | if (ENTRY_PSL_VAL(next_entry) <= 1) { |
188 | 2140 | ctx->nb_entries--; | |
189 | 2140 | return 1; | |
190 | } | ||
191 | 42 | memcpy(entry, next_entry, ctx->entry_size); | |
192 | 42 | ENTRY_PSL_VAL(entry)--; | |
193 | 42 | ENTRY_PSL_VAL(next_entry) = 0; | |
194 | 42 | entry = next_entry; | |
195 | } | ||
196 | } | ||
197 | } | ||
198 | ✗ | return 0; | |
199 | } | ||
200 | |||
201 | 3 | void ff_hashtable_clear(struct FFHashtableContext *ctx) | |
202 | { | ||
203 | 3 | memset(ctx->table, 0, ctx->entry_size * ctx->max_entries); | |
204 | 3 | } | |
205 | |||
206 | 28 | av_cold void ff_hashtable_freep(FFHashtableContext **ctx) | |
207 | { | ||
208 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | if (*ctx) { |
209 | 28 | av_freep(&(*ctx)->table); | |
210 | 28 | av_freep(ctx); | |
211 | } | ||
212 | 28 | } | |
213 |