SDL_hashtable.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. typedef struct SDL_HashItem
  20. {
  21. // TODO: Splitting off values into a separate array might be more cache-friendly
  22. const void *key;
  23. const void *value;
  24. Uint32 hash;
  25. Uint32 probe_len : 31;
  26. Uint32 live : 1;
  27. } SDL_HashItem;
  28. // Must be a power of 2 >= sizeof(SDL_HashItem)
  29. #define MAX_HASHITEM_SIZEOF 32u
  30. SDL_COMPILE_TIME_ASSERT(sizeof_SDL_HashItem, sizeof(SDL_HashItem) <= MAX_HASHITEM_SIZEOF);
  31. // Anything larger than this will cause integer overflows
  32. #define MAX_HASHTABLE_SIZE (0x80000000u / (MAX_HASHITEM_SIZEOF))
  33. struct SDL_HashTable
  34. {
  35. SDL_RWLock *lock; // NULL if not created threadsafe
  36. SDL_HashItem *table;
  37. SDL_HashCallback hash;
  38. SDL_HashKeyMatchCallback keymatch;
  39. SDL_HashDestroyCallback destroy;
  40. void *userdata;
  41. Uint32 hash_mask;
  42. Uint32 max_probe_len;
  43. Uint32 num_occupied_slots;
  44. };
  45. static Uint32 CalculateHashBucketsFromEstimate(int estimated_capacity)
  46. {
  47. if (estimated_capacity <= 0) {
  48. return 4; // start small, grow as necessary.
  49. }
  50. const Uint32 estimated32 = (Uint32) estimated_capacity;
  51. Uint32 buckets = ((Uint32) 1) << SDL_MostSignificantBitIndex32(estimated32);
  52. if (!SDL_HasExactlyOneBitSet32(estimated32)) {
  53. buckets <<= 1; // need next power of two up to fit overflow capacity bits.
  54. }
  55. return SDL_min(buckets, MAX_HASHTABLE_SIZE);
  56. }
  57. SDL_HashTable *SDL_CreateHashTable(int estimated_capacity, bool threadsafe, SDL_HashCallback hash,
  58. SDL_HashKeyMatchCallback keymatch,
  59. SDL_HashDestroyCallback destroy, void *userdata)
  60. {
  61. const Uint32 num_buckets = CalculateHashBucketsFromEstimate(estimated_capacity);
  62. SDL_HashTable *table = (SDL_HashTable *)SDL_calloc(1, sizeof(SDL_HashTable));
  63. if (!table) {
  64. return NULL;
  65. }
  66. if (threadsafe) {
  67. table->lock = SDL_CreateRWLock();
  68. if (!table->lock) {
  69. SDL_DestroyHashTable(table);
  70. return NULL;
  71. }
  72. }
  73. table->table = (SDL_HashItem *)SDL_calloc(num_buckets, sizeof(SDL_HashItem));
  74. if (!table->table) {
  75. SDL_DestroyHashTable(table);
  76. return NULL;
  77. }
  78. table->hash_mask = num_buckets - 1;
  79. table->userdata = userdata;
  80. table->hash = hash;
  81. table->keymatch = keymatch;
  82. table->destroy = destroy;
  83. return table;
  84. }
  85. static SDL_INLINE Uint32 calc_hash(const SDL_HashTable *table, const void *key)
  86. {
  87. const Uint32 BitMixer = 0x9E3779B1u;
  88. return table->hash(table->userdata, key) * BitMixer;
  89. }
  90. static SDL_INLINE Uint32 get_probe_length(Uint32 zero_idx, Uint32 actual_idx, Uint32 num_buckets)
  91. {
  92. // returns the probe sequence length from zero_idx to actual_idx
  93. if (actual_idx < zero_idx) {
  94. return num_buckets - zero_idx + actual_idx;
  95. }
  96. return actual_idx - zero_idx;
  97. }
  98. static SDL_HashItem *find_item(const SDL_HashTable *ht, const void *key, Uint32 hash, Uint32 *i, Uint32 *probe_len)
  99. {
  100. Uint32 hash_mask = ht->hash_mask;
  101. Uint32 max_probe_len = ht->max_probe_len;
  102. SDL_HashItem *table = ht->table;
  103. while (true) {
  104. SDL_HashItem *item = table + *i;
  105. Uint32 item_hash = item->hash;
  106. if (!item->live) {
  107. return NULL;
  108. }
  109. if (item_hash == hash && ht->keymatch(ht->userdata, item->key, key)) {
  110. return item;
  111. }
  112. Uint32 item_probe_len = item->probe_len;
  113. SDL_assert(item_probe_len == get_probe_length(item_hash & hash_mask, (Uint32)(item - table), hash_mask + 1));
  114. if (*probe_len > item_probe_len) {
  115. return NULL;
  116. }
  117. if (++*probe_len > max_probe_len) {
  118. return NULL;
  119. }
  120. *i = (*i + 1) & hash_mask;
  121. }
  122. }
  123. static SDL_HashItem *find_first_item(const SDL_HashTable *ht, const void *key, Uint32 hash)
  124. {
  125. Uint32 i = hash & ht->hash_mask;
  126. Uint32 probe_len = 0;
  127. return find_item(ht, key, hash, &i, &probe_len);
  128. }
  129. static SDL_HashItem *insert_item(SDL_HashItem *item_to_insert, SDL_HashItem *table, Uint32 hash_mask, Uint32 *max_probe_len_ptr)
  130. {
  131. const Uint32 num_buckets = hash_mask + 1;
  132. Uint32 idx = item_to_insert->hash & hash_mask;
  133. SDL_HashItem *target = NULL;
  134. SDL_HashItem temp_item;
  135. while (true) {
  136. SDL_HashItem *candidate = table + idx;
  137. if (!candidate->live) {
  138. // Found an empty slot. Put it here and we're done.
  139. *candidate = *item_to_insert;
  140. if (target == NULL) {
  141. target = candidate;
  142. }
  143. const Uint32 probe_len = get_probe_length(candidate->hash & hash_mask, idx, num_buckets);
  144. candidate->probe_len = probe_len;
  145. if (*max_probe_len_ptr < probe_len) {
  146. *max_probe_len_ptr = probe_len;
  147. }
  148. break;
  149. }
  150. const Uint32 candidate_probe_len = candidate->probe_len;
  151. SDL_assert(candidate_probe_len == get_probe_length(candidate->hash & hash_mask, idx, num_buckets));
  152. const Uint32 new_probe_len = get_probe_length(item_to_insert->hash & hash_mask, idx, num_buckets);
  153. if (candidate_probe_len < new_probe_len) {
  154. // Robin Hood hashing: the item at idx has a better probe length than our item would at this position.
  155. // Evict it and put our item in its place, then continue looking for a new spot for the displaced item.
  156. // This algorithm significantly reduces clustering in the table, making lookups take very few probes.
  157. temp_item = *candidate;
  158. *candidate = *item_to_insert;
  159. if (target == NULL) {
  160. target = candidate;
  161. }
  162. *item_to_insert = temp_item;
  163. SDL_assert(new_probe_len == get_probe_length(candidate->hash & hash_mask, idx, num_buckets));
  164. candidate->probe_len = new_probe_len;
  165. if (*max_probe_len_ptr < new_probe_len) {
  166. *max_probe_len_ptr = new_probe_len;
  167. }
  168. }
  169. idx = (idx + 1) & hash_mask;
  170. }
  171. return target;
  172. }
  173. static void delete_item(SDL_HashTable *ht, SDL_HashItem *item)
  174. {
  175. const Uint32 hash_mask = ht->hash_mask;
  176. SDL_HashItem *table = ht->table;
  177. if (ht->destroy) {
  178. ht->destroy(ht->userdata, item->key, item->value);
  179. }
  180. SDL_assert(ht->num_occupied_slots > 0);
  181. ht->num_occupied_slots--;
  182. Uint32 idx = (Uint32)(item - ht->table);
  183. while (true) {
  184. idx = (idx + 1) & hash_mask;
  185. SDL_HashItem *next_item = table + idx;
  186. if (next_item->probe_len < 1) {
  187. SDL_zerop(item);
  188. return;
  189. }
  190. *item = *next_item;
  191. item->probe_len -= 1;
  192. SDL_assert(item->probe_len < ht->max_probe_len);
  193. item = next_item;
  194. }
  195. }
  196. static bool resize(SDL_HashTable *ht, Uint32 new_size)
  197. {
  198. const Uint32 new_hash_mask = new_size - 1;
  199. SDL_HashItem *new_table = SDL_calloc(new_size, sizeof(*new_table));
  200. if (!new_table) {
  201. return false;
  202. }
  203. SDL_HashItem *old_table = ht->table;
  204. const Uint32 old_size = ht->hash_mask + 1;
  205. ht->max_probe_len = 0;
  206. ht->hash_mask = new_hash_mask;
  207. ht->table = new_table;
  208. for (Uint32 i = 0; i < old_size; ++i) {
  209. SDL_HashItem *item = old_table + i;
  210. if (item->live) {
  211. insert_item(item, new_table, new_hash_mask, &ht->max_probe_len);
  212. }
  213. }
  214. SDL_free(old_table);
  215. return true;
  216. }
  217. static bool maybe_resize(SDL_HashTable *ht)
  218. {
  219. const Uint32 capacity = ht->hash_mask + 1;
  220. if (capacity >= MAX_HASHTABLE_SIZE) {
  221. return false;
  222. }
  223. const Uint32 max_load_factor = 217; // range: 0-255; 217 is roughly 85%
  224. const Uint32 resize_threshold = (Uint32)((max_load_factor * (Uint64)capacity) >> 8);
  225. if (ht->num_occupied_slots > resize_threshold) {
  226. return resize(ht, capacity * 2);
  227. }
  228. return true;
  229. }
  230. bool SDL_InsertIntoHashTable(SDL_HashTable *table, const void *key, const void *value, bool replace)
  231. {
  232. if (!table) {
  233. return SDL_InvalidParamError("table");
  234. }
  235. bool result = false;
  236. SDL_LockRWLockForWriting(table->lock);
  237. const Uint32 hash = calc_hash(table, key);
  238. SDL_HashItem *item = find_first_item(table, key, hash);
  239. bool do_insert = true;
  240. if (item) {
  241. if (replace) {
  242. delete_item(table, item);
  243. } else {
  244. SDL_SetError("key already exists and replace is disabled");
  245. do_insert = false;
  246. }
  247. }
  248. if (do_insert) {
  249. SDL_HashItem new_item;
  250. new_item.key = key;
  251. new_item.value = value;
  252. new_item.hash = hash;
  253. new_item.live = true;
  254. new_item.probe_len = 0;
  255. table->num_occupied_slots++;
  256. if (!maybe_resize(table)) {
  257. table->num_occupied_slots--;
  258. } else {
  259. // This never returns NULL
  260. insert_item(&new_item, table->table, table->hash_mask, &table->max_probe_len);
  261. result = true;
  262. }
  263. }
  264. SDL_UnlockRWLock(table->lock);
  265. return result;
  266. }
  267. bool SDL_FindInHashTable(const SDL_HashTable *table, const void *key, const void **value)
  268. {
  269. if (!table) {
  270. if (value) {
  271. *value = NULL;
  272. }
  273. return SDL_InvalidParamError("table");
  274. }
  275. SDL_LockRWLockForReading(table->lock);
  276. bool result = false;
  277. const Uint32 hash = calc_hash(table, key);
  278. SDL_HashItem *i = find_first_item(table, key, hash);
  279. if (i) {
  280. if (value) {
  281. *value = i->value;
  282. }
  283. result = true;
  284. }
  285. SDL_UnlockRWLock(table->lock);
  286. return result;
  287. }
  288. bool SDL_RemoveFromHashTable(SDL_HashTable *table, const void *key)
  289. {
  290. if (!table) {
  291. return SDL_InvalidParamError("table");
  292. }
  293. SDL_LockRWLockForWriting(table->lock);
  294. bool result = false;
  295. const Uint32 hash = calc_hash(table, key);
  296. SDL_HashItem *item = find_first_item(table, key, hash);
  297. if (item) {
  298. delete_item(table, item);
  299. result = true;
  300. }
  301. SDL_UnlockRWLock(table->lock);
  302. return result;
  303. }
  304. bool SDL_IterateHashTable(const SDL_HashTable *table, SDL_HashTableIterateCallback callback, void *userdata)
  305. {
  306. if (!table) {
  307. return SDL_InvalidParamError("table");
  308. } else if (!callback) {
  309. return SDL_InvalidParamError("callback");
  310. }
  311. SDL_LockRWLockForReading(table->lock);
  312. SDL_HashItem *end = table->table + (table->hash_mask + 1);
  313. Uint32 num_iterated = 0;
  314. for (SDL_HashItem *item = table->table; item < end; item++) {
  315. if (item->live) {
  316. if (!callback(userdata, table, item->key, item->value)) {
  317. break; // callback requested iteration stop.
  318. } else if (++num_iterated >= table->num_occupied_slots) {
  319. break; // we can drop out early because we've seen all the live items.
  320. }
  321. }
  322. }
  323. SDL_UnlockRWLock(table->lock);
  324. return true;
  325. }
  326. bool SDL_HashTableEmpty(SDL_HashTable *table)
  327. {
  328. if (!table) {
  329. return SDL_InvalidParamError("table");
  330. }
  331. SDL_LockRWLockForReading(table->lock);
  332. const bool retval = (table->num_occupied_slots == 0);
  333. SDL_UnlockRWLock(table->lock);
  334. return retval;
  335. }
  336. static void destroy_all(SDL_HashTable *table)
  337. {
  338. SDL_HashDestroyCallback destroy = table->destroy;
  339. if (destroy) {
  340. void *userdata = table->userdata;
  341. SDL_HashItem *end = table->table + (table->hash_mask + 1);
  342. for (SDL_HashItem *i = table->table; i < end; ++i) {
  343. if (i->live) {
  344. i->live = false;
  345. destroy(userdata, i->key, i->value);
  346. }
  347. }
  348. }
  349. }
  350. void SDL_ClearHashTable(SDL_HashTable *table)
  351. {
  352. if (table) {
  353. SDL_LockRWLockForWriting(table->lock);
  354. {
  355. destroy_all(table);
  356. SDL_memset(table->table, 0, sizeof(*table->table) * (table->hash_mask + 1));
  357. table->num_occupied_slots = 0;
  358. }
  359. SDL_UnlockRWLock(table->lock);
  360. }
  361. }
  362. void SDL_DestroyHashTable(SDL_HashTable *table)
  363. {
  364. if (table) {
  365. destroy_all(table);
  366. if (table->lock) {
  367. SDL_DestroyRWLock(table->lock);
  368. }
  369. SDL_free(table->table);
  370. SDL_free(table);
  371. }
  372. }
  373. // this is djb's xor hashing function.
  374. static SDL_INLINE Uint32 hash_string_djbxor(const char *str, size_t len)
  375. {
  376. Uint32 hash = 5381;
  377. while (len--) {
  378. hash = ((hash << 5) + hash) ^ *(str++);
  379. }
  380. return hash;
  381. }
  382. Uint32 SDL_HashPointer(void *unused, const void *key)
  383. {
  384. (void)unused;
  385. return SDL_murmur3_32(&key, sizeof(key), 0);
  386. }
  387. bool SDL_KeyMatchPointer(void *unused, const void *a, const void *b)
  388. {
  389. (void)unused;
  390. return (a == b);
  391. }
  392. Uint32 SDL_HashString(void *unused, const void *key)
  393. {
  394. (void)unused;
  395. const char *str = (const char *)key;
  396. return hash_string_djbxor(str, SDL_strlen(str));
  397. }
  398. bool SDL_KeyMatchString(void *unused, const void *a, const void *b)
  399. {
  400. const char *a_string = (const char *)a;
  401. const char *b_string = (const char *)b;
  402. (void)unused;
  403. if (a == b) {
  404. return true; // same pointer, must match.
  405. } else if (!a || !b) {
  406. return false; // one pointer is NULL (and first test shows they aren't the same pointer), must not match.
  407. } else if (a_string[0] != b_string[0]) {
  408. return false; // we know they don't match
  409. }
  410. return (SDL_strcmp(a_string, b_string) == 0); // Check against actual string contents.
  411. }
  412. // We assume we can fit the ID in the key directly
  413. SDL_COMPILE_TIME_ASSERT(SDL_HashID_KeySize, sizeof(Uint32) <= sizeof(const void *));
  414. Uint32 SDL_HashID(void *unused, const void *key)
  415. {
  416. (void)unused;
  417. return (Uint32)(uintptr_t)key;
  418. }
  419. bool SDL_KeyMatchID(void *unused, const void *a, const void *b)
  420. {
  421. (void)unused;
  422. return (a == b);
  423. }
  424. void SDL_DestroyHashKeyAndValue(void *unused, const void *key, const void *value)
  425. {
  426. (void)unused;
  427. SDL_free((void *)key);
  428. SDL_free((void *)value);
  429. }
  430. void SDL_DestroyHashKey(void *unused, const void *key, const void *value)
  431. {
  432. (void)value;
  433. (void)unused;
  434. SDL_free((void *)key);
  435. }
  436. void SDL_DestroyHashValue(void *unused, const void *key, const void *value)
  437. {
  438. (void)key;
  439. (void)unused;
  440. SDL_free((void *)value);
  441. }