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.

table.h 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (c) 2009-2021, Google LLC
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of Google LLC nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
  20. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*
  28. * upb_table
  29. *
  30. * This header is INTERNAL-ONLY! Its interfaces are not public or stable!
  31. * This file defines very fast int->upb_value (inttable) and string->upb_value
  32. * (strtable) hash tables.
  33. *
  34. * The table uses chained scatter with Brent's variation (inspired by the Lua
  35. * implementation of hash tables). The hash function for strings is Austin
  36. * Appleby's "MurmurHash."
  37. *
  38. * The inttable uses uintptr_t as its key, which guarantees it can be used to
  39. * store pointers or integers of at least 32 bits (upb isn't really useful on
  40. * systems where sizeof(void*) < 4).
  41. *
  42. * The table must be homogeneous (all values of the same type). In debug
  43. * mode, we check this on insert and lookup.
  44. */
  45. #ifndef UPB_INTERNAL_TABLE_H_
  46. #define UPB_INTERNAL_TABLE_H_
  47. #include <stdint.h>
  48. #include <string.h>
  49. #include "upb/upb.h"
  50. // Must be last.
  51. #include "upb/port_def.inc"
  52. #ifdef __cplusplus
  53. extern "C"
  54. {
  55. #endif
  56. /* upb_value ******************************************************************/
  57. typedef struct
  58. {
  59. uint64_t val;
  60. } upb_value;
  61. /* Variant that works with a length-delimited rather than NULL-delimited string,
  62. * as supported by strtable. */
  63. char* upb_strdup2(const char* s, size_t len, upb_Arena* a);
  64. UPB_INLINE void _upb_value_setval(upb_value* v, uint64_t val)
  65. {
  66. v->val = val;
  67. }
  68. /* For each value ctype, define the following set of functions:
  69. *
  70. * // Get/set an int32 from a upb_value.
  71. * int32_t upb_value_getint32(upb_value val);
  72. * void upb_value_setint32(upb_value *val, int32_t cval);
  73. *
  74. * // Construct a new upb_value from an int32.
  75. * upb_value upb_value_int32(int32_t val); */
  76. #define FUNCS(name, membername, type_t, converter, proto_type) \
  77. UPB_INLINE void upb_value_set##name(upb_value* val, type_t cval) \
  78. { \
  79. val->val = (converter)cval; \
  80. } \
  81. UPB_INLINE upb_value upb_value_##name(type_t val) \
  82. { \
  83. upb_value ret; \
  84. upb_value_set##name(&ret, val); \
  85. return ret; \
  86. } \
  87. UPB_INLINE type_t upb_value_get##name(upb_value val) \
  88. { \
  89. return (type_t)(converter)val.val; \
  90. }
  91. FUNCS(int32, int32, int32_t, int32_t, UPB_CTYPE_INT32)
  92. FUNCS(int64, int64, int64_t, int64_t, UPB_CTYPE_INT64)
  93. FUNCS(uint32, uint32, uint32_t, uint32_t, UPB_CTYPE_UINT32)
  94. FUNCS(uint64, uint64, uint64_t, uint64_t, UPB_CTYPE_UINT64)
  95. FUNCS(bool, _bool, bool, bool, UPB_CTYPE_BOOL)
  96. FUNCS(cstr, cstr, char*, uintptr_t, UPB_CTYPE_CSTR)
  97. FUNCS(ptr, ptr, void*, uintptr_t, UPB_CTYPE_PTR)
  98. FUNCS(constptr, constptr, const void*, uintptr_t, UPB_CTYPE_CONSTPTR)
  99. #undef FUNCS
  100. UPB_INLINE void upb_value_setfloat(upb_value* val, float cval)
  101. {
  102. memcpy(&val->val, &cval, sizeof(cval));
  103. }
  104. UPB_INLINE void upb_value_setdouble(upb_value* val, double cval)
  105. {
  106. memcpy(&val->val, &cval, sizeof(cval));
  107. }
  108. UPB_INLINE upb_value upb_value_float(float cval)
  109. {
  110. upb_value ret;
  111. upb_value_setfloat(&ret, cval);
  112. return ret;
  113. }
  114. UPB_INLINE upb_value upb_value_double(double cval)
  115. {
  116. upb_value ret;
  117. upb_value_setdouble(&ret, cval);
  118. return ret;
  119. }
  120. #undef SET_TYPE
  121. /* upb_tabkey *****************************************************************/
  122. /* Either:
  123. * 1. an actual integer key, or
  124. * 2. a pointer to a string prefixed by its uint32_t length, owned by us.
  125. *
  126. * ...depending on whether this is a string table or an int table. We would
  127. * make this a union of those two types, but C89 doesn't support statically
  128. * initializing a non-first union member. */
  129. typedef uintptr_t upb_tabkey;
  130. UPB_INLINE char* upb_tabstr(upb_tabkey key, uint32_t* len)
  131. {
  132. char* mem = (char*)key;
  133. if (len)
  134. memcpy(len, mem, sizeof(*len));
  135. return mem + sizeof(*len);
  136. }
  137. UPB_INLINE upb_StringView upb_tabstrview(upb_tabkey key)
  138. {
  139. upb_StringView ret;
  140. uint32_t len;
  141. ret.data = upb_tabstr(key, &len);
  142. ret.size = len;
  143. return ret;
  144. }
  145. /* upb_tabval *****************************************************************/
  146. typedef struct upb_tabval
  147. {
  148. uint64_t val;
  149. } upb_tabval;
  150. #define UPB_TABVALUE_EMPTY_INIT \
  151. { \
  152. -1 \
  153. }
  154. /* upb_table ******************************************************************/
  155. typedef struct _upb_tabent
  156. {
  157. upb_tabkey key;
  158. upb_tabval val;
  159. /* Internal chaining. This is const so we can create static initializers for
  160. * tables. We cast away const sometimes, but *only* when the containing
  161. * upb_table is known to be non-const. This requires a bit of care, but
  162. * the subtlety is confined to table.c. */
  163. const struct _upb_tabent* next;
  164. } upb_tabent;
  165. typedef struct
  166. {
  167. size_t count; /* Number of entries in the hash part. */
  168. uint32_t mask; /* Mask to turn hash value -> bucket. */
  169. uint32_t max_count; /* Max count before we hit our load limit. */
  170. uint8_t size_lg2; /* Size of the hashtable part is 2^size_lg2 entries. */
  171. upb_tabent* entries;
  172. } upb_table;
  173. typedef struct
  174. {
  175. upb_table t;
  176. } upb_strtable;
  177. typedef struct
  178. {
  179. upb_table t; /* For entries that don't fit in the array part. */
  180. const upb_tabval* array; /* Array part of the table. See const note above. */
  181. size_t array_size; /* Array part size. */
  182. size_t array_count; /* Array part number of elements. */
  183. } upb_inttable;
  184. UPB_INLINE size_t upb_table_size(const upb_table* t)
  185. {
  186. if (t->size_lg2 == 0)
  187. return 0;
  188. else
  189. return 1 << t->size_lg2;
  190. }
  191. /* Internal-only functions, in .h file only out of necessity. */
  192. UPB_INLINE bool upb_tabent_isempty(const upb_tabent* e)
  193. {
  194. return e->key == 0;
  195. }
  196. /* Initialize and uninitialize a table, respectively. If memory allocation
  197. * failed, false is returned that the table is uninitialized. */
  198. bool upb_inttable_init(upb_inttable* table, upb_Arena* a);
  199. bool upb_strtable_init(upb_strtable* table, size_t expected_size, upb_Arena* a);
  200. /* Returns the number of values in the table. */
  201. size_t upb_inttable_count(const upb_inttable* t);
  202. UPB_INLINE size_t upb_strtable_count(const upb_strtable* t)
  203. {
  204. return t->t.count;
  205. }
  206. void upb_strtable_clear(upb_strtable* t);
  207. /* Inserts the given key into the hashtable with the given value. The key must
  208. * not already exist in the hash table. For strtables, the key is not required
  209. * to be NULL-terminated, and the table will make an internal copy of the key.
  210. * Inttables must not insert a value of UINTPTR_MAX.
  211. *
  212. * If a table resize was required but memory allocation failed, false is
  213. * returned and the table is unchanged. */
  214. bool upb_inttable_insert(upb_inttable* t, uintptr_t key, upb_value val, upb_Arena* a);
  215. bool upb_strtable_insert(upb_strtable* t, const char* key, size_t len, upb_value val, upb_Arena* a);
  216. /* Looks up key in this table, returning "true" if the key was found.
  217. * If v is non-NULL, copies the value for this key into *v. */
  218. bool upb_inttable_lookup(const upb_inttable* t, uintptr_t key, upb_value* v);
  219. bool upb_strtable_lookup2(const upb_strtable* t, const char* key, size_t len, upb_value* v);
  220. /* For NULL-terminated strings. */
  221. UPB_INLINE bool upb_strtable_lookup(const upb_strtable* t, const char* key, upb_value* v)
  222. {
  223. return upb_strtable_lookup2(t, key, strlen(key), v);
  224. }
  225. /* Removes an item from the table. Returns true if the remove was successful,
  226. * and stores the removed item in *val if non-NULL. */
  227. bool upb_inttable_remove(upb_inttable* t, uintptr_t key, upb_value* val);
  228. bool upb_strtable_remove2(upb_strtable* t, const char* key, size_t len, upb_value* val);
  229. UPB_INLINE bool upb_strtable_remove(upb_strtable* t, const char* key, upb_value* v)
  230. {
  231. return upb_strtable_remove2(t, key, strlen(key), v);
  232. }
  233. /* Updates an existing entry in an inttable. If the entry does not exist,
  234. * returns false and does nothing. Unlike insert/remove, this does not
  235. * invalidate iterators. */
  236. bool upb_inttable_replace(upb_inttable* t, uintptr_t key, upb_value val);
  237. /* Optimizes the table for the current set of entries, for both memory use and
  238. * lookup time. Client should call this after all entries have been inserted;
  239. * inserting more entries is legal, but will likely require a table resize. */
  240. void upb_inttable_compact(upb_inttable* t, upb_Arena* a);
  241. /* Exposed for testing only. */
  242. bool upb_strtable_resize(upb_strtable* t, size_t size_lg2, upb_Arena* a);
  243. /* Iterators ******************************************************************/
  244. /* Iteration over inttable.
  245. *
  246. * intptr_t iter = UPB_INTTABLE_BEGIN;
  247. * uintptr_t key;
  248. * upb_value val;
  249. * while (upb_inttable_next2(t, &key, &val, &iter)) {
  250. * // ...
  251. * }
  252. */
  253. #define UPB_INTTABLE_BEGIN -1
  254. bool upb_inttable_next2(const upb_inttable* t, uintptr_t* key, upb_value* val, intptr_t* iter);
  255. void upb_inttable_removeiter(upb_inttable* t, intptr_t* iter);
  256. /* Iteration over strtable.
  257. *
  258. * intptr_t iter = UPB_INTTABLE_BEGIN;
  259. * upb_StringView key;
  260. * upb_value val;
  261. * while (upb_strtable_next2(t, &key, &val, &iter)) {
  262. * // ...
  263. * }
  264. */
  265. #define UPB_STRTABLE_BEGIN -1
  266. bool upb_strtable_next2(const upb_strtable* t, upb_StringView* key, upb_value* val, intptr_t* iter);
  267. void upb_strtable_removeiter(upb_strtable* t, intptr_t* iter);
  268. /* DEPRECATED iterators, slated for removal.
  269. *
  270. * Iterators for int and string tables. We are subject to some kind of unusual
  271. * design constraints:
  272. *
  273. * For high-level languages:
  274. * - we must be able to guarantee that we don't crash or corrupt memory even if
  275. * the program accesses an invalidated iterator.
  276. *
  277. * For C++11 range-based for:
  278. * - iterators must be copyable
  279. * - iterators must be comparable
  280. * - it must be possible to construct an "end" value.
  281. *
  282. * Iteration order is undefined.
  283. *
  284. * Modifying the table invalidates iterators. upb_{str,int}table_done() is
  285. * guaranteed to work even on an invalidated iterator, as long as the table it
  286. * is iterating over has not been freed. Calling next() or accessing data from
  287. * an invalidated iterator yields unspecified elements from the table, but it is
  288. * guaranteed not to crash and to return real table elements (except when done()
  289. * is true). */
  290. /* upb_strtable_iter **********************************************************/
  291. /* upb_strtable_iter i;
  292. * upb_strtable_begin(&i, t);
  293. * for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
  294. * const char *key = upb_strtable_iter_key(&i);
  295. * const upb_value val = upb_strtable_iter_value(&i);
  296. * // ...
  297. * }
  298. */
  299. typedef struct
  300. {
  301. const upb_strtable* t;
  302. size_t index;
  303. } upb_strtable_iter;
  304. void upb_strtable_begin(upb_strtable_iter* i, const upb_strtable* t);
  305. void upb_strtable_next(upb_strtable_iter* i);
  306. bool upb_strtable_done(const upb_strtable_iter* i);
  307. upb_StringView upb_strtable_iter_key(const upb_strtable_iter* i);
  308. upb_value upb_strtable_iter_value(const upb_strtable_iter* i);
  309. void upb_strtable_iter_setdone(upb_strtable_iter* i);
  310. bool upb_strtable_iter_isequal(const upb_strtable_iter* i1, const upb_strtable_iter* i2);
  311. /* upb_inttable_iter **********************************************************/
  312. /* upb_inttable_iter i;
  313. * upb_inttable_begin(&i, t);
  314. * for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  315. * uintptr_t key = upb_inttable_iter_key(&i);
  316. * upb_value val = upb_inttable_iter_value(&i);
  317. * // ...
  318. * }
  319. */
  320. typedef struct
  321. {
  322. const upb_inttable* t;
  323. size_t index;
  324. bool array_part;
  325. } upb_inttable_iter;
  326. UPB_INLINE const upb_tabent* str_tabent(const upb_strtable_iter* i)
  327. {
  328. return &i->t->t.entries[i->index];
  329. }
  330. void upb_inttable_begin(upb_inttable_iter* i, const upb_inttable* t);
  331. void upb_inttable_next(upb_inttable_iter* i);
  332. bool upb_inttable_done(const upb_inttable_iter* i);
  333. uintptr_t upb_inttable_iter_key(const upb_inttable_iter* i);
  334. upb_value upb_inttable_iter_value(const upb_inttable_iter* i);
  335. void upb_inttable_iter_setdone(upb_inttable_iter* i);
  336. bool upb_inttable_iter_isequal(const upb_inttable_iter* i1, const upb_inttable_iter* i2);
  337. uint32_t _upb_Hash(const void* p, size_t n, uint64_t seed);
  338. #ifdef __cplusplus
  339. } /* extern "C" */
  340. #endif
  341. #include "upb/port_undef.inc"
  342. #endif /* UPB_INTERNAL_TABLE_H_ */