You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

linkhash.h 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * $Id: linkhash.h,v 1.6 2006/01/30 23:07:57 mclark Exp $
  3. *
  4. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  5. * Michael Clark <michael@metaparadigm.com>
  6. * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
  7. *
  8. * This library is free software; you can redistribute it and/or modify
  9. * it under the terms of the MIT license. See COPYING for details.
  10. *
  11. */
  12. /**
  13. * @file
  14. * @brief Internal methods for working with json_type_object objects. Although
  15. * this is exposed by the json_object_get_object() function and within the
  16. * json_object_iter type, it is not recommended for direct use.
  17. */
  18. #ifndef _json_c_linkhash_h_
  19. #define _json_c_linkhash_h_
  20. #include "json_object.h"
  21. #include <stdio.h>
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /**
  26. * golden prime used in hash functions
  27. */
  28. #define LH_PRIME 0x9e370001UL
  29. /**
  30. * The fraction of filled hash buckets until an insert will cause the table
  31. * to be resized.
  32. * This can range from just above 0 up to 1.0.
  33. */
  34. #define LH_LOAD_FACTOR 0.66
  35. /**
  36. * sentinel pointer value for empty slots
  37. */
  38. #define LH_EMPTY (void *)-1
  39. /**
  40. * sentinel pointer value for freed slots
  41. */
  42. #define LH_FREED (void *)-2
  43. /**
  44. * default string hash function
  45. */
  46. #define JSON_C_STR_HASH_DFLT 0
  47. /**
  48. * perl-like string hash function
  49. */
  50. #define JSON_C_STR_HASH_PERLLIKE 1
  51. /**
  52. * This function sets the hash function to be used for strings.
  53. * Must be one of the JSON_C_STR_HASH_* values.
  54. * @returns 0 - ok, -1 if parameter was invalid
  55. */
  56. int json_global_set_string_hash(const int h);
  57. struct lh_entry;
  58. /**
  59. * callback function prototypes
  60. */
  61. typedef void(lh_entry_free_fn)(struct lh_entry *e);
  62. /**
  63. * callback function prototypes
  64. */
  65. typedef unsigned long(lh_hash_fn)(const void *k);
  66. /**
  67. * callback function prototypes
  68. */
  69. typedef int(lh_equal_fn)(const void *k1, const void *k2);
  70. /**
  71. * @brief A buffer of characters that may contain null charaters in the middle
  72. *
  73. * A buffer of data that can hold a normal null-terminated string
  74. * (in which case `length` should just be equal to `strlen`)
  75. * or a string with embedded null characters (in which case `length` reflects
  76. * all the characters that make up the "string").
  77. * Either way, this struct can be treated as if it contains null characters,
  78. * since the `length` member should always be equal to the proper size of the
  79. * buffer and the terminating null character wouldn't be included
  80. * (it wouldn't be counted by strlen).
  81. */
  82. struct lh_string
  83. {
  84. /**
  85. * @brief Stores the length of the buffer
  86. *
  87. * If the length is positive, then `pdata` should be used.
  88. * Otherwise, idata should be used.
  89. */
  90. ssize_t length;
  91. union
  92. {
  93. const char *pdata;
  94. const char idata[0];
  95. } str;
  96. };
  97. /**
  98. * An entry in the hash table
  99. */
  100. struct lh_entry
  101. {
  102. /**
  103. * The key. Use lh_entry_k() instead of accessing this directly.
  104. */
  105. const void *k;
  106. /**
  107. * A flag for users of linkhash to know whether or not they
  108. * need to free k.
  109. */
  110. int k_is_constant;
  111. /**
  112. * The value. Use lh_entry_v() instead of accessing this directly.
  113. */
  114. const void *v;
  115. /**
  116. * The next entry
  117. */
  118. struct lh_entry *next;
  119. /**
  120. * The previous entry.
  121. */
  122. struct lh_entry *prev;
  123. };
  124. /**
  125. * The hash table structure.
  126. */
  127. struct lh_table
  128. {
  129. /**
  130. * Size of our hash.
  131. */
  132. int size;
  133. /**
  134. * Numbers of entries.
  135. */
  136. int count;
  137. /**
  138. * The first entry.
  139. */
  140. struct lh_entry *head;
  141. /**
  142. * The last entry.
  143. */
  144. struct lh_entry *tail;
  145. struct lh_entry *table;
  146. /**
  147. * A pointer onto the function responsible for freeing an entry.
  148. */
  149. lh_entry_free_fn *free_fn;
  150. /**
  151. * A function that is capable of hashing entries in this table
  152. */
  153. lh_hash_fn *hash_fn;
  154. /**
  155. * A function that is capable of determining if entries in this table
  156. * are equal
  157. */
  158. lh_equal_fn *equal_fn;
  159. };
  160. typedef struct lh_table lh_table;
  161. /**
  162. * Convenience list iterator.
  163. */
  164. #define lh_foreach(table, entry) for (entry = table->head; entry; entry = entry->next)
  165. /**
  166. * lh_foreach_safe allows calling of deletion routine while iterating.
  167. *
  168. * @param table a struct lh_table * to iterate over
  169. * @param entry a struct lh_entry * variable to hold each element
  170. * @param tmp a struct lh_entry * variable to hold a temporary pointer to the next element
  171. */
  172. #define lh_foreach_safe(table, entry, tmp) \
  173. for (entry = table->head; entry && ((tmp = entry->next) || 1); entry = tmp)
  174. /**
  175. * @brief Get the data from a `struct lh_string *`
  176. *
  177. * @param str value to retrieve the data from
  178. */
  179. extern const char *lh_string_data(const struct lh_string *str);
  180. /**
  181. * @brief Get the length of the data stored in a `struct lh_string *`
  182. *
  183. * @param str value to retrieve the length of
  184. */
  185. extern size_t lh_string_size(const struct lh_string *str);
  186. /**
  187. * @brief Print a `struct lh_string` to a given stream
  188. *
  189. * @param str value to print
  190. * @param stream Stream to write data to
  191. */
  192. extern size_t lh_string_print(const struct lh_string *str, FILE *stream);
  193. /**
  194. * @brief Creates a new `struct lh_string` using the `pdata` field.
  195. *
  196. * This avoids unneeded copying, for cases wher ethe key is in an area of
  197. * memory that will not be modified or freed until after this object is freed.
  198. *
  199. * @param length The length of the data located at @p data
  200. * @param data The data to include
  201. * @return `NULL` on error
  202. */
  203. extern struct lh_string *lh_string_new_ptr(const size_t length, const char *data);
  204. /**
  205. * @brief Creates a new `struct lh_string` using the `idata` field.
  206. *
  207. * @param length The length of the data to copy into the returned value
  208. * @param data The data to include and copy into the returned value
  209. * @return `NULL` on error
  210. */
  211. extern struct lh_string *lh_string_new_imm(const size_t length, const char *data);
  212. /**
  213. * Create a new linkhash table.
  214. *
  215. * @param size initial table size. The table is automatically resized
  216. * although this incurs a performance penalty.
  217. * @param free_fn callback function used to free memory for entries
  218. * when lh_table_free or lh_table_delete is called.
  219. * If NULL is provided, then memory for keys and values
  220. * must be freed by the caller.
  221. * @param hash_fn function used to hash keys. 2 standard ones are defined:
  222. * lh_ptr_hash and lh_char_hash for hashing pointer values
  223. * and C strings respectively.
  224. * @param equal_fn comparison function to compare keys. 2 standard ones defined:
  225. * lh_ptr_hash and lh_char_hash for comparing pointer values
  226. * and C strings respectively.
  227. * @return On success, a pointer to the new linkhash table is returned.
  228. * On error, a null pointer is returned.
  229. */
  230. extern struct lh_table *lh_table_new(int size, lh_entry_free_fn *free_fn, lh_hash_fn *hash_fn,
  231. lh_equal_fn *equal_fn);
  232. /**
  233. * Convenience function to create a new linkhash table with char keys.
  234. *
  235. * @param size initial table size.
  236. * @param free_fn callback function used to free memory for entries.
  237. * @return On success, a pointer to the new linkhash table is returned.
  238. * On error, a null pointer is returned.
  239. */
  240. extern struct lh_table *lh_kchar_table_new(int size, lh_entry_free_fn *free_fn);
  241. /**
  242. * Convenience function to create a new linkhash table with ptr keys.
  243. *
  244. * @param size initial table size.
  245. * @param free_fn callback function used to free memory for entries.
  246. * @return On success, a pointer to the new linkhash table is returned.
  247. * On error, a null pointer is returned.
  248. */
  249. extern struct lh_table *lh_kptr_table_new(int size, lh_entry_free_fn *free_fn);
  250. /**
  251. * Free a linkhash table.
  252. *
  253. * If a lh_entry_free_fn callback free function was provided then it is
  254. * called for all entries in the table.
  255. *
  256. * @param t table to free.
  257. */
  258. extern void lh_table_free(struct lh_table *t);
  259. /**
  260. * Insert a record into the table.
  261. *
  262. * @param t the table to insert into.
  263. * @param k a pointer to the key to insert.
  264. * @param v a pointer to the value to insert.
  265. *
  266. * @return On success, <code>0</code> is returned.
  267. * On error, a negative value is returned.
  268. */
  269. extern int lh_table_insert(struct lh_table *t, const void *k, const void *v);
  270. /**
  271. * Insert a record into the table using a precalculated key hash.
  272. *
  273. * The hash h, which should be calculated with lh_get_hash() on k, is provided by
  274. * the caller, to allow for optimization when multiple operations with the same
  275. * key are known to be needed.
  276. *
  277. * @param t the table to insert into.
  278. * @param k a pointer to the key to insert.
  279. * @param v a pointer to the value to insert.
  280. * @param h hash value of the key to insert
  281. * @param opts if set to JSON_C_OBJECT_KEY_IS_CONSTANT, sets lh_entry.k_is_constant
  282. * so t's free function knows to avoid freeing the key.
  283. */
  284. extern int lh_table_insert_w_hash(struct lh_table *t, const void *k, const void *v,
  285. const unsigned long h, const unsigned opts);
  286. /**
  287. * Lookup a record in the table.
  288. *
  289. * @param t the table to lookup
  290. * @param k a pointer to the key to lookup
  291. * @return a pointer to the record structure of the value or NULL if it does not exist.
  292. */
  293. extern struct lh_entry *lh_table_lookup_entry(struct lh_table *t, const void *k);
  294. /**
  295. * Lookup a record in the table using a precalculated key hash.
  296. *
  297. * The hash h, which should be calculated with lh_get_hash() on k, is provided by
  298. * the caller, to allow for optimization when multiple operations with the same
  299. * key are known to be needed.
  300. *
  301. * @param t the table to lookup
  302. * @param k a pointer to the key to lookup
  303. * @param h hash value of the key to lookup
  304. * @return a pointer to the record structure of the value or NULL if it does not exist.
  305. */
  306. extern struct lh_entry *lh_table_lookup_entry_w_hash(struct lh_table *t, const void *k,
  307. const unsigned long h);
  308. /**
  309. * Lookup a record in the table.
  310. *
  311. * @param t the table to lookup
  312. * @param k a pointer to the key to lookup
  313. * @param v a pointer to a where to store the found value (set to NULL if it doesn't exist).
  314. * @return whether or not the key was found
  315. */
  316. extern json_bool lh_table_lookup_ex(struct lh_table *t, const void *k, void **v);
  317. /**
  318. * Delete a record from the table.
  319. *
  320. * If a callback free function is provided then it is called for the
  321. * for the item being deleted.
  322. * @param t the table to delete from.
  323. * @param e a pointer to the entry to delete.
  324. * @return 0 if the item was deleted.
  325. * @return -1 if it was not found.
  326. */
  327. extern int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e);
  328. /**
  329. * Delete a record from the table.
  330. *
  331. * If a callback free function is provided then it is called for the
  332. * for the item being deleted.
  333. * @param t the table to delete from.
  334. * @param k a pointer to the key to delete.
  335. * @return 0 if the item was deleted.
  336. * @return -1 if it was not found.
  337. */
  338. extern int lh_table_delete(struct lh_table *t, const void *k);
  339. extern int lh_table_length(struct lh_table *t);
  340. /**
  341. * Resizes the specified table.
  342. *
  343. * @param t Pointer to table to resize.
  344. * @param new_size New table size. Must be positive.
  345. *
  346. * @return On success, <code>0</code> is returned.
  347. * On error, a negative value is returned.
  348. */
  349. int lh_table_resize(struct lh_table *t, int new_size);
  350. /**
  351. * @deprecated Don't use this outside of linkhash.h:
  352. */
  353. #if (defined(AIX_CC) || (defined(_MSC_VER) && (_MSC_VER <= 1800)))
  354. /* VS2010 can't handle inline funcs, so skip it there */
  355. #define _LH_INLINE
  356. #else
  357. #define _LH_INLINE inline
  358. #endif
  359. /**
  360. * Calculate the hash of a key for a given table.
  361. *
  362. * This is an exension to support functions that need to calculate
  363. * the hash several times and allows them to do it just once and then pass
  364. * in the hash to all utility functions. Depending on use case, this can be a
  365. * considerable performance improvement.
  366. * @param t the table (used to obtain hash function)
  367. * @param k a pointer to the key to lookup
  368. * @return the key's hash
  369. */
  370. static _LH_INLINE unsigned long lh_get_hash(const struct lh_table *t, const void *k)
  371. {
  372. return t->hash_fn(k);
  373. }
  374. #undef _LH_INLINE
  375. /**
  376. * @deprecated Don't use this outside of linkhash.h:
  377. */
  378. #ifdef __UNCONST
  379. #define _LH_UNCONST(a) __UNCONST(a)
  380. #else
  381. #define _LH_UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
  382. #endif
  383. /**
  384. * Return a non-const version of lh_entry.k.
  385. *
  386. * lh_entry.k is const to indicate and help ensure that linkhash itself doesn't modify
  387. * it, but callers are allowed to do what they want with it.
  388. * See also lh_entry.k_is_constant
  389. */
  390. #define lh_entry_k(entry) _LH_UNCONST((entry)->k)
  391. /**
  392. * Return a non-const version of lh_entry.v.
  393. *
  394. * v is const to indicate and help ensure that linkhash itself doesn't modify
  395. * it, but callers are allowed to do what they want with it.
  396. */
  397. #define lh_entry_v(entry) _LH_UNCONST((entry)->v)
  398. #ifdef __cplusplus
  399. }
  400. #endif
  401. #endif