hashmap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. *
  3. * Embedded Linux library
  4. *
  5. * Copyright (C) 2011-2014 Intel Corporation. All rights reserved.
  6. *
  7. * This library 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. * This library 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 this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include "hashmap.h"
  26. #include "private.h"
  27. #include "useful.h"
  28. /**
  29. * SECTION:hashmap
  30. * @short_description: Hash table support
  31. *
  32. * Hash table support
  33. */
  34. #define NBUCKETS 127
  35. struct entry {
  36. void *key;
  37. void *value;
  38. struct entry *next;
  39. unsigned int hash;
  40. };
  41. /**
  42. * l_hashmap:
  43. *
  44. * Opague object representing the hash table.
  45. */
  46. struct l_hashmap {
  47. l_hashmap_hash_func_t hash_func;
  48. l_hashmap_compare_func_t compare_func;
  49. l_hashmap_key_new_func_t key_new_func;
  50. l_hashmap_key_free_func_t key_free_func;
  51. unsigned int entries;
  52. struct entry buckets[NBUCKETS];
  53. };
  54. static inline void *get_key_new(const struct l_hashmap *hashmap,
  55. const void *key)
  56. {
  57. if (hashmap->key_new_func)
  58. return hashmap->key_new_func(key);
  59. return (void *)key;
  60. }
  61. static inline void free_key(const struct l_hashmap *hashmap, void *key)
  62. {
  63. if (hashmap->key_free_func)
  64. hashmap->key_free_func(key);
  65. }
  66. static inline unsigned int hash_superfast(const uint8_t *key, unsigned int len)
  67. {
  68. /*
  69. * Paul Hsieh (http://www.azillionmonkeys.com/qed/hash.html)
  70. * used by WebCore (http://webkit.org/blog/8/hashtables-part-2/),
  71. * EFL's eina, kmod and possible others.
  72. */
  73. unsigned int tmp, hash = len, rem = len & 3;
  74. len /= 4;
  75. /* Main loop */
  76. for (; len > 0; len--) {
  77. hash += l_get_u16(key);
  78. tmp = (l_get_u16(key + 2) << 11) ^ hash;
  79. hash = (hash << 16) ^ tmp;
  80. key += 4;
  81. hash += hash >> 11;
  82. }
  83. /* Handle end cases */
  84. switch (rem) {
  85. case 3:
  86. hash += l_get_u16(key);
  87. hash ^= hash << 16;
  88. hash ^= key[2] << 18;
  89. hash += hash >> 11;
  90. break;
  91. case 2:
  92. hash += l_get_u16(key);
  93. hash ^= hash << 11;
  94. hash += hash >> 17;
  95. break;
  96. case 1:
  97. hash += *key;
  98. hash ^= hash << 10;
  99. hash += hash >> 1;
  100. break;
  101. }
  102. /* Force "avalanching" of final 127 bits */
  103. hash ^= hash << 3;
  104. hash += hash >> 5;
  105. hash ^= hash << 4;
  106. hash += hash >> 17;
  107. hash ^= hash << 25;
  108. hash += hash >> 6;
  109. return hash;
  110. }
  111. static unsigned int direct_hash_func(const void *p)
  112. {
  113. return L_PTR_TO_UINT(p);
  114. }
  115. static int direct_compare_func(const void *a, const void *b)
  116. {
  117. return a < b ? -1 : (a > b ? 1 : 0);
  118. }
  119. /**
  120. * l_hashmap_new:
  121. *
  122. * Create a new hash table. The keys are managed as pointers, that is,
  123. * the pointer value is hashed and looked up.
  124. *
  125. * No error handling is needed since. In case of real memory allocation
  126. * problems abort() will be called.
  127. *
  128. * See also l_hashmap_string_new().
  129. *
  130. * Returns: a newly allocated #l_hashmap object
  131. **/
  132. LIB_EXPORT struct l_hashmap *l_hashmap_new(void)
  133. {
  134. struct l_hashmap *hashmap;
  135. hashmap = l_new(struct l_hashmap, 1);
  136. hashmap->hash_func = direct_hash_func;
  137. hashmap->compare_func = direct_compare_func;
  138. hashmap->entries = 0;
  139. return hashmap;
  140. }
  141. LIB_EXPORT unsigned int l_str_hash(const void *p)
  142. {
  143. const char *s = p;
  144. size_t len = strlen(s);
  145. return hash_superfast((const uint8_t *)s, len);
  146. }
  147. /**
  148. * l_hashmap_string_new:
  149. *
  150. * Create a new hash table. The keys are considered strings and are
  151. * copied.
  152. *
  153. * No error handling is needed since. In case of real memory allocation
  154. * problems abort() will be called.
  155. *
  156. * See also l_hashmap_new().
  157. *
  158. * Returns: a newly allocated #l_hashmap object
  159. **/
  160. LIB_EXPORT struct l_hashmap *l_hashmap_string_new(void)
  161. {
  162. struct l_hashmap *hashmap;
  163. hashmap = l_new(struct l_hashmap, 1);
  164. hashmap->hash_func = l_str_hash;
  165. hashmap->compare_func = (l_hashmap_compare_func_t) strcmp;
  166. hashmap->key_new_func = (l_hashmap_key_new_func_t) l_strdup;
  167. hashmap->key_free_func = l_free;
  168. hashmap->entries = 0;
  169. return hashmap;
  170. }
  171. /**
  172. * l_hashmap_set_hash_function:
  173. * @hashmap: hash table object
  174. * @func: Key hashing function
  175. *
  176. * Sets the hashing function to be used by this object.
  177. *
  178. * This function can only be called when the @hashmap is empty.
  179. *
  180. * Returns: #true when the hashing function could be updated successfully,
  181. * and #false otherwise.
  182. **/
  183. LIB_EXPORT bool l_hashmap_set_hash_function(struct l_hashmap *hashmap,
  184. l_hashmap_hash_func_t func)
  185. {
  186. if (unlikely(!hashmap))
  187. return false;
  188. if (hashmap->entries != 0)
  189. return false;
  190. hashmap->hash_func = func;
  191. return true;
  192. }
  193. /**
  194. * l_hashmap_set_compare_function:
  195. * @hashmap: hash table object
  196. * @func: Key compare function
  197. *
  198. * Sets the key comparison function to be used by this object.
  199. *
  200. * This function can only be called when the @hashmap is empty.
  201. *
  202. * Returns: #true when the comparison function could be updated successfully,
  203. * and #false otherwise.
  204. **/
  205. LIB_EXPORT bool l_hashmap_set_compare_function(struct l_hashmap *hashmap,
  206. l_hashmap_compare_func_t func)
  207. {
  208. if (unlikely(!hashmap))
  209. return false;
  210. if (hashmap->entries != 0)
  211. return false;
  212. hashmap->compare_func = func;
  213. return true;
  214. }
  215. /**
  216. * l_hashmap_set_key_copy_function:
  217. * @hashmap: hash table object
  218. * @func: Key duplication function
  219. *
  220. * Sets the key duplication function to be used by this object. If the
  221. * function is NULL, then the keys are assigned directly.
  222. *
  223. * This function can only be called when the @hashmap is empty.
  224. *
  225. * Returns: #true when the key copy function could be updated successfully,
  226. * and #false otherwise.
  227. **/
  228. LIB_EXPORT bool l_hashmap_set_key_copy_function(struct l_hashmap *hashmap,
  229. l_hashmap_key_new_func_t func)
  230. {
  231. if (unlikely(!hashmap))
  232. return false;
  233. if (hashmap->entries != 0)
  234. return false;
  235. hashmap->key_new_func = func;
  236. return true;
  237. }
  238. /**
  239. * l_hashmap_set_key_free_function:
  240. * @hashmap: hash table object
  241. * @func: Key destructor function
  242. *
  243. * Sets the key destructor function to be used by this object. This function
  244. * should undo the result of the function specified in
  245. * l_hashmap_set_key_copy_function(). This function can be NULL, in which
  246. * case no destructor is called.
  247. *
  248. * This function can only be called when the @hashmap is empty.
  249. *
  250. * Returns: #true when the key free function could be updated successfully,
  251. * and #false otherwise.
  252. **/
  253. LIB_EXPORT bool l_hashmap_set_key_free_function(struct l_hashmap *hashmap,
  254. l_hashmap_key_free_func_t func)
  255. {
  256. if (unlikely(!hashmap))
  257. return false;
  258. if (hashmap->entries != 0)
  259. return false;
  260. hashmap->key_free_func = func;
  261. return true;
  262. }
  263. /**
  264. * l_hashmap_destroy:
  265. * @hashmap: hash table object
  266. * @destroy: destroy function
  267. *
  268. * Free hash table and call @destory on all remaining entries.
  269. *
  270. * NOTE: While the destroy is in progress, the hashmap is assumed to be
  271. * invariant. The behavior of adding or removing entries while a destroy
  272. * operation is in progress is undefined.
  273. **/
  274. LIB_EXPORT void l_hashmap_destroy(struct l_hashmap *hashmap,
  275. l_hashmap_destroy_func_t destroy)
  276. {
  277. unsigned int i;
  278. if (unlikely(!hashmap))
  279. return;
  280. for (i = 0; i < NBUCKETS; i++) {
  281. struct entry *entry, *next, *head = &hashmap->buckets[i];
  282. if (!head->next)
  283. continue;
  284. for (entry = head;; entry = next) {
  285. if (destroy)
  286. destroy(entry->value);
  287. free_key(hashmap, entry->key);
  288. next = entry->next;
  289. if (entry != head)
  290. l_free(entry);
  291. if (next == head)
  292. break;
  293. }
  294. }
  295. l_free(hashmap);
  296. }
  297. /**
  298. * l_hashmap_insert:
  299. * @hashmap: hash table object
  300. * @key: key pointer
  301. * @value: value pointer
  302. *
  303. * Insert new @value entry with @key. Note that entries with a duplicate key
  304. * are allowed. If a duplicate entry in inserted, it will be added in order
  305. * of insertion. @l_hashmap_lookup and @l_hashmap_remove will remove the
  306. * first matching entry.
  307. *
  308. * Returns: #true when value has been added and #false in case of failure
  309. **/
  310. LIB_EXPORT bool l_hashmap_insert(struct l_hashmap *hashmap,
  311. const void *key, void *value)
  312. {
  313. struct entry *entry, *head;
  314. unsigned int hash;
  315. void *key_new;
  316. if (unlikely(!hashmap))
  317. return false;
  318. key_new = get_key_new(hashmap, key);
  319. hash = hashmap->hash_func(key_new);
  320. head = &hashmap->buckets[hash % NBUCKETS];
  321. if (!head->next) {
  322. head->key = key_new;
  323. head->value = value;
  324. head->hash = hash;
  325. head->next = head;
  326. goto done;
  327. }
  328. entry = l_new(struct entry, 1);
  329. entry->key = key_new;
  330. entry->value = value;
  331. entry->hash = hash;
  332. entry->next = head;
  333. while (head->next != entry->next)
  334. head = head->next;
  335. head->next = entry;
  336. done:
  337. hashmap->entries++;
  338. return true;
  339. }
  340. /**
  341. * l_hashmap_replace:
  342. * @hashmap: hash table object
  343. * @key: key pointer
  344. * @value: value pointer
  345. * @old_value: old value that has been replaced.
  346. *
  347. * Replace the first entry with @key by @value or insert a new @value entry
  348. * with @key. If the entry was replaced, then the old value is returned
  349. * in @old_value, otherwise @old_value is assigned a #NULL.
  350. *
  351. * Returns: #true when value has been added and #false in case of failure
  352. **/
  353. LIB_EXPORT bool l_hashmap_replace(struct l_hashmap *hashmap,
  354. const void *key, void *value,
  355. void **old_value)
  356. {
  357. struct entry *entry;
  358. struct entry *head;
  359. unsigned int hash;
  360. void *key_new;
  361. if (unlikely(!hashmap))
  362. return false;
  363. key_new = get_key_new(hashmap, key);
  364. hash = hashmap->hash_func(key_new);
  365. head = &hashmap->buckets[hash % NBUCKETS];
  366. if (!head->next) {
  367. head->key = key_new;
  368. head->value = value;
  369. head->hash = hash;
  370. head->next = head;
  371. goto done;
  372. }
  373. for (entry = head;; entry = entry->next) {
  374. if (entry->hash != hash)
  375. goto next;
  376. if (hashmap->compare_func(key, entry->key))
  377. goto next;
  378. if (old_value)
  379. *old_value = entry->value;
  380. entry->value = value;
  381. free_key(hashmap, key_new);
  382. return true;
  383. next:
  384. if (entry->next == head)
  385. break;
  386. }
  387. entry = l_new(struct entry, 1);
  388. entry->key = key_new;
  389. entry->value = value;
  390. entry->hash = hash;
  391. entry->next = head;
  392. while (head->next != entry->next)
  393. head = head->next;
  394. head->next = entry;
  395. done:
  396. if (old_value)
  397. *old_value = NULL;
  398. hashmap->entries++;
  399. return true;
  400. }
  401. /**
  402. * l_hashmap_remove:
  403. * @hashmap: hash table object
  404. * @key: key pointer
  405. *
  406. * Remove entry for @key.
  407. *
  408. * Returns: value pointer of the removed entry or #NULL in case of failure
  409. **/
  410. LIB_EXPORT void *l_hashmap_remove(struct l_hashmap *hashmap, const void *key)
  411. {
  412. struct entry *entry, *head, *prev;
  413. unsigned int hash;
  414. if (unlikely(!hashmap))
  415. return NULL;
  416. hash = hashmap->hash_func(key);
  417. head = &hashmap->buckets[hash % NBUCKETS];
  418. if (!head->next)
  419. return NULL;
  420. for (entry = head, prev = NULL;; prev = entry, entry = entry->next) {
  421. void *value;
  422. if (entry->hash != hash)
  423. goto next;
  424. if (hashmap->compare_func(key, entry->key))
  425. goto next;
  426. value = entry->value;
  427. if (entry == head) {
  428. if (entry->next == head) {
  429. free_key(hashmap, entry->key);
  430. head->key = NULL;
  431. head->value = NULL;
  432. head->hash = 0;
  433. head->next = NULL;
  434. } else {
  435. entry = entry->next;
  436. free_key(hashmap, head->key);
  437. head->key = entry->key;
  438. head->value = entry->value;
  439. head->hash = entry->hash;
  440. head->next = entry->next;
  441. l_free(entry);
  442. }
  443. } else {
  444. prev->next = entry->next;
  445. free_key(hashmap, entry->key);
  446. l_free(entry);
  447. }
  448. hashmap->entries--;
  449. return value;
  450. next:
  451. if (entry->next == head)
  452. break;
  453. }
  454. return NULL;
  455. }
  456. /**
  457. * l_hashmap_lookup:
  458. * @hashmap: hash table object
  459. * @key: key pointer
  460. *
  461. * Lookup entry for @key.
  462. *
  463. * Returns: value pointer for @key or #NULL in case of failure
  464. **/
  465. LIB_EXPORT void *l_hashmap_lookup(struct l_hashmap *hashmap, const void *key)
  466. {
  467. struct entry *entry, *head;
  468. unsigned int hash;
  469. if (unlikely(!hashmap))
  470. return NULL;
  471. hash = hashmap->hash_func(key);
  472. head = &hashmap->buckets[hash % NBUCKETS];
  473. if (!head->next)
  474. return NULL;
  475. for (entry = head;; entry = entry->next) {
  476. if (entry->hash == hash &&
  477. !hashmap->compare_func(key, entry->key))
  478. return entry->value;
  479. if (entry->next == head)
  480. break;
  481. }
  482. return NULL;
  483. }
  484. /**
  485. * l_hashmap_foreach:
  486. * @hashmap: hash table object
  487. * @function: callback function
  488. * @user_data: user data given to callback function
  489. *
  490. * Call @function for every entry in @hashmap.
  491. *
  492. * NOTE: While the foreach is in progress, the hashmap is assumed to be
  493. * invariant. The behavior of adding or removing entries while a foreach
  494. * operation is in progress is undefined.
  495. **/
  496. LIB_EXPORT void l_hashmap_foreach(struct l_hashmap *hashmap,
  497. l_hashmap_foreach_func_t function, void *user_data)
  498. {
  499. unsigned int i;
  500. if (unlikely(!hashmap || !function))
  501. return;
  502. for (i = 0; i < NBUCKETS; i++) {
  503. struct entry *entry, *head = &hashmap->buckets[i];
  504. if (!head->next)
  505. continue;
  506. for (entry = head;; entry = entry->next) {
  507. function(entry->key, entry->value, user_data);
  508. if (entry->next == head)
  509. break;
  510. }
  511. }
  512. }
  513. /**
  514. * l_hashmap_foreach_remove:
  515. * @hashmap: hash table object
  516. * @function: callback function
  517. * @user_data: user data given to callback function
  518. *
  519. * Call @function for every entry in @hashmap. If the @function returns
  520. * true, then the object will be removed from the hashmap.
  521. *
  522. * NOTE: While the foreach is in progress, the hashmap is assumed to be
  523. * invariant. The behavior of adding or removing entries while a foreach
  524. * operation is in progress is undefined.
  525. *
  526. * Returns: Number of entries removed.
  527. **/
  528. LIB_EXPORT unsigned int l_hashmap_foreach_remove(struct l_hashmap *hashmap,
  529. l_hashmap_remove_func_t function,
  530. void *user_data)
  531. {
  532. unsigned int i;
  533. unsigned int nremoved = 0;
  534. if (unlikely(!hashmap || !function))
  535. return 0;
  536. for (i = 0; i < NBUCKETS; i++) {
  537. struct entry *head = &hashmap->buckets[i];
  538. struct entry *entry;
  539. struct entry *prev;
  540. bool remove;
  541. if (head->next == NULL)
  542. continue;
  543. entry = head;
  544. prev = NULL;
  545. while (true) {
  546. remove = function(entry->key, entry->value, user_data);
  547. if (!remove)
  548. goto next;
  549. nremoved += 1;
  550. hashmap->entries -= 1;
  551. if (entry == head) {
  552. if (entry->next == head) {
  553. free_key(hashmap, entry->key);
  554. head->key = NULL;
  555. head->value = NULL;
  556. head->hash = 0;
  557. head->next = NULL;
  558. break;
  559. } else {
  560. entry = entry->next;
  561. free_key(hashmap, head->key);
  562. head->key = entry->key;
  563. head->value = entry->value;
  564. head->hash = entry->hash;
  565. head->next = entry->next;
  566. l_free(entry);
  567. entry = head;
  568. continue;
  569. }
  570. } else {
  571. prev->next = entry->next;
  572. free_key(hashmap, entry->key);
  573. l_free(entry);
  574. entry = prev->next;
  575. if (entry == head)
  576. break;
  577. continue;
  578. }
  579. next:
  580. if (entry->next == head)
  581. break;
  582. prev = entry;
  583. entry = entry->next;
  584. }
  585. }
  586. return nremoved;
  587. }
  588. /**
  589. * l_hashmap_size:
  590. * @hashmap: hash table object
  591. *
  592. * Returns: entries in the hash table
  593. **/
  594. LIB_EXPORT unsigned int l_hashmap_size(struct l_hashmap *hashmap)
  595. {
  596. if (unlikely(!hashmap))
  597. return 0;
  598. return hashmap->entries;
  599. }
  600. /**
  601. * l_hashmap_isempty:
  602. * @hashmap: hash table object
  603. *
  604. * Returns: #true if hash table is empty and #false if not
  605. **/
  606. LIB_EXPORT bool l_hashmap_isempty(struct l_hashmap *hashmap)
  607. {
  608. if (unlikely(!hashmap))
  609. return true;
  610. return hashmap->entries == 0;
  611. }