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 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. #endif
  55. /* upb_value ******************************************************************/
  56. typedef struct {
  57. uint64_t val;
  58. } upb_value;
  59. /* Variant that works with a length-delimited rather than NULL-delimited string,
  60. * as supported by strtable. */
  61. char* upb_strdup2(const char* s, size_t len, upb_Arena* a);
  62. UPB_INLINE void _upb_value_setval(upb_value* v, uint64_t val) { v->val = val; }
  63. /* For each value ctype, define the following set of functions:
  64. *
  65. * // Get/set an int32 from a upb_value.
  66. * int32_t upb_value_getint32(upb_value val);
  67. * void upb_value_setint32(upb_value *val, int32_t cval);
  68. *
  69. * // Construct a new upb_value from an int32.
  70. * upb_value upb_value_int32(int32_t val); */
  71. #define FUNCS(name, membername, type_t, converter, proto_type) \
  72. UPB_INLINE void upb_value_set##name(upb_value* val, type_t cval) { \
  73. val->val = (converter)cval; \
  74. } \
  75. UPB_INLINE upb_value upb_value_##name(type_t val) { \
  76. upb_value ret; \
  77. upb_value_set##name(&ret, val); \
  78. return ret; \
  79. } \
  80. UPB_INLINE type_t upb_value_get##name(upb_value val) { \
  81. return (type_t)(converter)val.val; \
  82. }
  83. FUNCS(int32, int32, int32_t, int32_t, UPB_CTYPE_INT32)
  84. FUNCS(int64, int64, int64_t, int64_t, UPB_CTYPE_INT64)
  85. FUNCS(uint32, uint32, uint32_t, uint32_t, UPB_CTYPE_UINT32)
  86. FUNCS(uint64, uint64, uint64_t, uint64_t, UPB_CTYPE_UINT64)
  87. FUNCS(bool, _bool, bool, bool, UPB_CTYPE_BOOL)
  88. FUNCS(cstr, cstr, char*, uintptr_t, UPB_CTYPE_CSTR)
  89. FUNCS(ptr, ptr, void*, uintptr_t, UPB_CTYPE_PTR)
  90. FUNCS(constptr, constptr, const void*, uintptr_t, UPB_CTYPE_CONSTPTR)
  91. #undef FUNCS
  92. UPB_INLINE void upb_value_setfloat(upb_value* val, float cval) {
  93. memcpy(&val->val, &cval, sizeof(cval));
  94. }
  95. UPB_INLINE void upb_value_setdouble(upb_value* val, double cval) {
  96. memcpy(&val->val, &cval, sizeof(cval));
  97. }
  98. UPB_INLINE upb_value upb_value_float(float cval) {
  99. upb_value ret;
  100. upb_value_setfloat(&ret, cval);
  101. return ret;
  102. }
  103. UPB_INLINE upb_value upb_value_double(double cval) {
  104. upb_value ret;
  105. upb_value_setdouble(&ret, cval);
  106. return ret;
  107. }
  108. #undef SET_TYPE
  109. /* upb_tabkey *****************************************************************/
  110. /* Either:
  111. * 1. an actual integer key, or
  112. * 2. a pointer to a string prefixed by its uint32_t length, owned by us.
  113. *
  114. * ...depending on whether this is a string table or an int table. We would
  115. * make this a union of those two types, but C89 doesn't support statically
  116. * initializing a non-first union member. */
  117. typedef uintptr_t upb_tabkey;
  118. UPB_INLINE char* upb_tabstr(upb_tabkey key, uint32_t* len) {
  119. char* mem = (char*)key;
  120. if (len) memcpy(len, mem, sizeof(*len));
  121. return mem + sizeof(*len);
  122. }
  123. UPB_INLINE upb_StringView upb_tabstrview(upb_tabkey key) {
  124. upb_StringView ret;
  125. uint32_t len;
  126. ret.data = upb_tabstr(key, &len);
  127. ret.size = len;
  128. return ret;
  129. }
  130. /* upb_tabval *****************************************************************/
  131. typedef struct upb_tabval {
  132. uint64_t val;
  133. } upb_tabval;
  134. #define UPB_TABVALUE_EMPTY_INIT \
  135. { -1 }
  136. /* upb_table ******************************************************************/
  137. typedef struct _upb_tabent {
  138. upb_tabkey key;
  139. upb_tabval val;
  140. /* Internal chaining. This is const so we can create static initializers for
  141. * tables. We cast away const sometimes, but *only* when the containing
  142. * upb_table is known to be non-const. This requires a bit of care, but
  143. * the subtlety is confined to table.c. */
  144. const struct _upb_tabent* next;
  145. } upb_tabent;
  146. typedef struct {
  147. size_t count; /* Number of entries in the hash part. */
  148. uint32_t mask; /* Mask to turn hash value -> bucket. */
  149. uint32_t max_count; /* Max count before we hit our load limit. */
  150. uint8_t size_lg2; /* Size of the hashtable part is 2^size_lg2 entries. */
  151. upb_tabent* entries;
  152. } upb_table;
  153. typedef struct {
  154. upb_table t;
  155. } upb_strtable;
  156. typedef struct {
  157. upb_table t; /* For entries that don't fit in the array part. */
  158. const upb_tabval* array; /* Array part of the table. See const note above. */
  159. size_t array_size; /* Array part size. */
  160. size_t array_count; /* Array part number of elements. */
  161. } upb_inttable;
  162. UPB_INLINE size_t upb_table_size(const upb_table* t) {
  163. if (t->size_lg2 == 0)
  164. return 0;
  165. else
  166. return 1 << t->size_lg2;
  167. }
  168. /* Internal-only functions, in .h file only out of necessity. */
  169. UPB_INLINE bool upb_tabent_isempty(const upb_tabent* e) { return e->key == 0; }
  170. /* Initialize and uninitialize a table, respectively. If memory allocation
  171. * failed, false is returned that the table is uninitialized. */
  172. bool upb_inttable_init(upb_inttable* table, upb_Arena* a);
  173. bool upb_strtable_init(upb_strtable* table, size_t expected_size, upb_Arena* a);
  174. /* Returns the number of values in the table. */
  175. size_t upb_inttable_count(const upb_inttable* t);
  176. UPB_INLINE size_t upb_strtable_count(const upb_strtable* t) {
  177. return t->t.count;
  178. }
  179. void upb_strtable_clear(upb_strtable* t);
  180. /* Inserts the given key into the hashtable with the given value. The key must
  181. * not already exist in the hash table. For strtables, the key is not required
  182. * to be NULL-terminated, and the table will make an internal copy of the key.
  183. * Inttables must not insert a value of UINTPTR_MAX.
  184. *
  185. * If a table resize was required but memory allocation failed, false is
  186. * returned and the table is unchanged. */
  187. bool upb_inttable_insert(upb_inttable* t, uintptr_t key, upb_value val,
  188. upb_Arena* a);
  189. bool upb_strtable_insert(upb_strtable* t, const char* key, size_t len,
  190. upb_value val, upb_Arena* a);
  191. /* Looks up key in this table, returning "true" if the key was found.
  192. * If v is non-NULL, copies the value for this key into *v. */
  193. bool upb_inttable_lookup(const upb_inttable* t, uintptr_t key, upb_value* v);
  194. bool upb_strtable_lookup2(const upb_strtable* t, const char* key, size_t len,
  195. upb_value* v);
  196. /* For NULL-terminated strings. */
  197. UPB_INLINE bool upb_strtable_lookup(const upb_strtable* t, const char* key,
  198. upb_value* v) {
  199. return upb_strtable_lookup2(t, key, strlen(key), v);
  200. }
  201. /* Removes an item from the table. Returns true if the remove was successful,
  202. * and stores the removed item in *val if non-NULL. */
  203. bool upb_inttable_remove(upb_inttable* t, uintptr_t key, upb_value* val);
  204. bool upb_strtable_remove2(upb_strtable* t, const char* key, size_t len,
  205. upb_value* val);
  206. UPB_INLINE bool upb_strtable_remove(upb_strtable* t, const char* key,
  207. upb_value* v) {
  208. return upb_strtable_remove2(t, key, strlen(key), v);
  209. }
  210. /* Updates an existing entry in an inttable. If the entry does not exist,
  211. * returns false and does nothing. Unlike insert/remove, this does not
  212. * invalidate iterators. */
  213. bool upb_inttable_replace(upb_inttable* t, uintptr_t key, upb_value val);
  214. /* Optimizes the table for the current set of entries, for both memory use and
  215. * lookup time. Client should call this after all entries have been inserted;
  216. * inserting more entries is legal, but will likely require a table resize. */
  217. void upb_inttable_compact(upb_inttable* t, upb_Arena* a);
  218. /* Exposed for testing only. */
  219. bool upb_strtable_resize(upb_strtable* t, size_t size_lg2, upb_Arena* a);
  220. /* Iterators ******************************************************************/
  221. /* Iteration over inttable.
  222. *
  223. * intptr_t iter = UPB_INTTABLE_BEGIN;
  224. * uintptr_t key;
  225. * upb_value val;
  226. * while (upb_inttable_next2(t, &key, &val, &iter)) {
  227. * // ...
  228. * }
  229. */
  230. #define UPB_INTTABLE_BEGIN -1
  231. bool upb_inttable_next2(const upb_inttable* t, uintptr_t* key, upb_value* val,
  232. intptr_t* iter);
  233. void upb_inttable_removeiter(upb_inttable* t, intptr_t* iter);
  234. /* Iteration over strtable.
  235. *
  236. * intptr_t iter = UPB_INTTABLE_BEGIN;
  237. * upb_StringView key;
  238. * upb_value val;
  239. * while (upb_strtable_next2(t, &key, &val, &iter)) {
  240. * // ...
  241. * }
  242. */
  243. #define UPB_STRTABLE_BEGIN -1
  244. bool upb_strtable_next2(const upb_strtable* t, upb_StringView* key,
  245. upb_value* val, intptr_t* iter);
  246. void upb_strtable_removeiter(upb_strtable* t, intptr_t* iter);
  247. /* DEPRECATED iterators, slated for removal.
  248. *
  249. * Iterators for int and string tables. We are subject to some kind of unusual
  250. * design constraints:
  251. *
  252. * For high-level languages:
  253. * - we must be able to guarantee that we don't crash or corrupt memory even if
  254. * the program accesses an invalidated iterator.
  255. *
  256. * For C++11 range-based for:
  257. * - iterators must be copyable
  258. * - iterators must be comparable
  259. * - it must be possible to construct an "end" value.
  260. *
  261. * Iteration order is undefined.
  262. *
  263. * Modifying the table invalidates iterators. upb_{str,int}table_done() is
  264. * guaranteed to work even on an invalidated iterator, as long as the table it
  265. * is iterating over has not been freed. Calling next() or accessing data from
  266. * an invalidated iterator yields unspecified elements from the table, but it is
  267. * guaranteed not to crash and to return real table elements (except when done()
  268. * is true). */
  269. /* upb_strtable_iter **********************************************************/
  270. /* upb_strtable_iter i;
  271. * upb_strtable_begin(&i, t);
  272. * for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
  273. * const char *key = upb_strtable_iter_key(&i);
  274. * const upb_value val = upb_strtable_iter_value(&i);
  275. * // ...
  276. * }
  277. */
  278. typedef struct {
  279. const upb_strtable* t;
  280. size_t index;
  281. } upb_strtable_iter;
  282. void upb_strtable_begin(upb_strtable_iter* i, const upb_strtable* t);
  283. void upb_strtable_next(upb_strtable_iter* i);
  284. bool upb_strtable_done(const upb_strtable_iter* i);
  285. upb_StringView upb_strtable_iter_key(const upb_strtable_iter* i);
  286. upb_value upb_strtable_iter_value(const upb_strtable_iter* i);
  287. void upb_strtable_iter_setdone(upb_strtable_iter* i);
  288. bool upb_strtable_iter_isequal(const upb_strtable_iter* i1,
  289. const upb_strtable_iter* i2);
  290. /* upb_inttable_iter **********************************************************/
  291. /* upb_inttable_iter i;
  292. * upb_inttable_begin(&i, t);
  293. * for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  294. * uintptr_t key = upb_inttable_iter_key(&i);
  295. * upb_value val = upb_inttable_iter_value(&i);
  296. * // ...
  297. * }
  298. */
  299. typedef struct {
  300. const upb_inttable* t;
  301. size_t index;
  302. bool array_part;
  303. } upb_inttable_iter;
  304. UPB_INLINE const upb_tabent* str_tabent(const upb_strtable_iter* i) {
  305. return &i->t->t.entries[i->index];
  306. }
  307. void upb_inttable_begin(upb_inttable_iter* i, const upb_inttable* t);
  308. void upb_inttable_next(upb_inttable_iter* i);
  309. bool upb_inttable_done(const upb_inttable_iter* i);
  310. uintptr_t upb_inttable_iter_key(const upb_inttable_iter* i);
  311. upb_value upb_inttable_iter_value(const upb_inttable_iter* i);
  312. void upb_inttable_iter_setdone(upb_inttable_iter* i);
  313. bool upb_inttable_iter_isequal(const upb_inttable_iter* i1,
  314. const upb_inttable_iter* i2);
  315. uint32_t _upb_Hash(const void* p, size_t n, uint64_t seed);
  316. #ifdef __cplusplus
  317. } /* extern "C" */
  318. #endif
  319. #include "upb/port_undef.inc"
  320. #endif /* UPB_INTERNAL_TABLE_H_ */