table_internal.h 12 KB

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