msg_internal.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. ** Our memory representation for parsing tables and messages themselves.
  29. ** Functions in this file are used by generated code and possibly reflection.
  30. **
  31. ** The definitions in this file are internal to upb.
  32. **/
  33. #ifndef UPB_MSG_INT_H_
  34. #define UPB_MSG_INT_H_
  35. #include <stdint.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include "upb/msg.h"
  39. #include "upb/table_internal.h"
  40. #include "upb/upb.h"
  41. /* Must be last. */
  42. #include "upb/port_def.inc"
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /** upb_msglayout *************************************************************/
  47. /* upb_msglayout represents the memory layout of a given upb_msgdef. The
  48. * members are public so generated code can initialize them, but users MUST NOT
  49. * read or write any of its members. */
  50. /* These aren't real labels according to descriptor.proto, but in the table we
  51. * use these for map/packed fields instead of UPB_LABEL_REPEATED. */
  52. enum {
  53. _UPB_LABEL_MAP = 4,
  54. _UPB_LABEL_PACKED = 7 /* Low 3 bits are common with UPB_LABEL_REPEATED. */
  55. };
  56. typedef struct {
  57. uint32_t number;
  58. uint16_t offset;
  59. int16_t presence; /* If >0, hasbit_index. If <0, ~oneof_index. */
  60. uint16_t submsg_index; /* undefined if descriptortype != MESSAGE or GROUP. */
  61. uint8_t descriptortype;
  62. uint8_t mode; /* upb_fieldmode | upb_labelflags |
  63. (upb_rep << _UPB_REP_SHIFT) */
  64. } upb_msglayout_field;
  65. typedef enum {
  66. _UPB_MODE_MAP = 0,
  67. _UPB_MODE_ARRAY = 1,
  68. _UPB_MODE_SCALAR = 2,
  69. _UPB_MODE_MASK = 3, /* Mask to isolate the mode from upb_rep. */
  70. } upb_fieldmode;
  71. /* Extra flags on the mode field. */
  72. enum upb_labelflags {
  73. _UPB_MODE_IS_PACKED = 4,
  74. _UPB_MODE_IS_EXTENSION = 8,
  75. };
  76. /* Representation in the message. Derivable from descriptortype and mode, but
  77. * fast access helps the serializer. */
  78. enum upb_rep {
  79. _UPB_REP_1BYTE = 0,
  80. _UPB_REP_4BYTE = 1,
  81. _UPB_REP_8BYTE = 2,
  82. _UPB_REP_STRVIEW = 3,
  83. #if UINTPTR_MAX == 0xffffffff
  84. _UPB_REP_PTR = _UPB_REP_4BYTE,
  85. #else
  86. _UPB_REP_PTR = _UPB_REP_8BYTE,
  87. #endif
  88. _UPB_REP_SHIFT = 6, /* Bit offset of the rep in upb_msglayout_field.mode */
  89. };
  90. UPB_INLINE upb_fieldmode _upb_getmode(const upb_msglayout_field *field) {
  91. return (upb_fieldmode)(field->mode & 3);
  92. }
  93. UPB_INLINE bool _upb_repeated_or_map(const upb_msglayout_field *field) {
  94. /* This works because upb_fieldmode has no value 3. */
  95. return !(field->mode & _UPB_MODE_SCALAR);
  96. }
  97. UPB_INLINE bool _upb_issubmsg(const upb_msglayout_field *field) {
  98. return field->descriptortype == UPB_DTYPE_MESSAGE ||
  99. field->descriptortype == UPB_DTYPE_GROUP;
  100. }
  101. struct upb_decstate;
  102. struct upb_msglayout;
  103. typedef const char *_upb_field_parser(struct upb_decstate *d, const char *ptr,
  104. upb_msg *msg, intptr_t table,
  105. uint64_t hasbits, uint64_t data);
  106. typedef struct {
  107. uint64_t field_data;
  108. _upb_field_parser *field_parser;
  109. } _upb_fasttable_entry;
  110. typedef union {
  111. const struct upb_msglayout *submsg;
  112. // TODO: const upb_enumlayout *subenum;
  113. } upb_msglayout_sub;
  114. typedef enum {
  115. _UPB_MSGEXT_NONE = 0, // Non-extendable message.
  116. _UPB_MSGEXT_EXTENDABLE = 1, // Normal extendable message.
  117. _UPB_MSGEXT_MSGSET = 2, // MessageSet message.
  118. _UPB_MSGEXT_MSGSET_ITEM = 3, // MessageSet item (temporary only, see decode.c)
  119. } upb_msgext_mode;
  120. /* MessageSet wire format is:
  121. * message MessageSet {
  122. * repeated group Item = 1 {
  123. * required int32 type_id = 2;
  124. * required string message = 3;
  125. * }
  126. * }
  127. */
  128. typedef enum {
  129. _UPB_MSGSET_ITEM = 1,
  130. _UPB_MSGSET_TYPEID = 2,
  131. _UPB_MSGSET_MESSAGE = 3,
  132. } upb_msgext_fieldnum;
  133. struct upb_msglayout {
  134. const upb_msglayout_sub *subs;
  135. const upb_msglayout_field *fields;
  136. /* Must be aligned to sizeof(void*). Doesn't include internal members like
  137. * unknown fields, extension dict, pointer to msglayout, etc. */
  138. uint16_t size;
  139. uint16_t field_count;
  140. uint8_t ext; // upb_msgext_mode, declared as uint8_t so sizeof(ext) == 1
  141. uint8_t dense_below;
  142. uint8_t table_mask;
  143. /* To constant-initialize the tables of variable length, we need a flexible
  144. * array member, and we need to compile in C99 mode. */
  145. _upb_fasttable_entry fasttable[];
  146. };
  147. typedef struct {
  148. upb_msglayout_field field;
  149. const upb_msglayout *extendee;
  150. upb_msglayout_sub sub; /* NULL unless submessage or proto2 enum */
  151. } upb_msglayout_ext;
  152. typedef struct {
  153. const upb_msglayout **msgs;
  154. const upb_msglayout_ext **exts;
  155. int msg_count;
  156. int ext_count;
  157. } upb_msglayout_file;
  158. /** upb_extreg ****************************************************************/
  159. /* Adds the given extension info for message type |l| and field number |num|
  160. * into the registry. Returns false if this message type and field number were
  161. * already in the map, or if memory allocation fails. */
  162. bool _upb_extreg_add(upb_extreg *r, const upb_msglayout_ext **e, size_t count);
  163. /* Looks up the extension (if any) defined for message type |l| and field
  164. * number |num|. If an extension was found, copies the field info into |*ext|
  165. * and returns true. Otherwise returns false. */
  166. const upb_msglayout_ext *_upb_extreg_get(const upb_extreg *r,
  167. const upb_msglayout *l, uint32_t num);
  168. /** upb_msg *******************************************************************/
  169. /* Internal members of a upb_msg that track unknown fields and/or extensions.
  170. * We can change this without breaking binary compatibility. We put these
  171. * before the user's data. The user's upb_msg* points after the
  172. * upb_msg_internal. */
  173. typedef struct {
  174. /* Total size of this structure, including the data that follows.
  175. * Must be aligned to 8, which is alignof(upb_msg_ext) */
  176. uint32_t size;
  177. /* Offsets relative to the beginning of this structure.
  178. *
  179. * Unknown data grows forward from the beginning to unknown_end.
  180. * Extension data grows backward from size to ext_begin.
  181. * When the two meet, we're out of data and have to realloc.
  182. *
  183. * If we imagine that the final member of this struct is:
  184. * char data[size - overhead]; // overhead = sizeof(upb_msg_internaldata)
  185. *
  186. * Then we have:
  187. * unknown data: data[0 .. (unknown_end - overhead)]
  188. * extensions data: data[(ext_begin - overhead) .. (size - overhead)] */
  189. uint32_t unknown_end;
  190. uint32_t ext_begin;
  191. /* Data follows, as if there were an array:
  192. * char data[size - sizeof(upb_msg_internaldata)]; */
  193. } upb_msg_internaldata;
  194. typedef struct {
  195. upb_msg_internaldata *internal;
  196. } upb_msg_internal;
  197. /* Maps upb_fieldtype_t -> memory size. */
  198. extern char _upb_fieldtype_to_size[12];
  199. UPB_INLINE size_t upb_msg_sizeof(const upb_msglayout *l) {
  200. return l->size + sizeof(upb_msg_internal);
  201. }
  202. UPB_INLINE upb_msg *_upb_msg_new_inl(const upb_msglayout *l, upb_arena *a) {
  203. size_t size = upb_msg_sizeof(l);
  204. void *mem = upb_arena_malloc(a, size);
  205. upb_msg *msg;
  206. if (UPB_UNLIKELY(!mem)) return NULL;
  207. msg = UPB_PTR_AT(mem, sizeof(upb_msg_internal), upb_msg);
  208. memset(mem, 0, size);
  209. return msg;
  210. }
  211. /* Creates a new messages with the given layout on the given arena. */
  212. upb_msg *_upb_msg_new(const upb_msglayout *l, upb_arena *a);
  213. UPB_INLINE upb_msg_internal *upb_msg_getinternal(upb_msg *msg) {
  214. ptrdiff_t size = sizeof(upb_msg_internal);
  215. return (upb_msg_internal*)((char*)msg - size);
  216. }
  217. /* Clears the given message. */
  218. void _upb_msg_clear(upb_msg *msg, const upb_msglayout *l);
  219. /* Discards the unknown fields for this message only. */
  220. void _upb_msg_discardunknown_shallow(upb_msg *msg);
  221. /* Adds unknown data (serialized protobuf data) to the given message. The data
  222. * is copied into the message instance. */
  223. bool _upb_msg_addunknown(upb_msg *msg, const char *data, size_t len,
  224. upb_arena *arena);
  225. /** upb_msg_ext ***************************************************************/
  226. /* The internal representation of an extension is self-describing: it contains
  227. * enough information that we can serialize it to binary format without needing
  228. * to look it up in a registry. */
  229. typedef struct {
  230. const upb_msglayout_ext *ext;
  231. union {
  232. upb_strview str;
  233. void *ptr;
  234. double dbl;
  235. char scalar_data[8];
  236. } data;
  237. } upb_msg_ext;
  238. /* Adds the given extension data to the given message. The returned extension will
  239. * have its "ext" member initialized according to |ext|. */
  240. upb_msg_ext *_upb_msg_getorcreateext(upb_msg *msg, const upb_msglayout_ext *ext,
  241. upb_arena *arena);
  242. /* Returns an array of extensions for this message. Note: the array is
  243. * ordered in reverse relative to the order of creation. */
  244. const upb_msg_ext *_upb_msg_getexts(const upb_msg *msg, size_t *count);
  245. /* Returns an extension for the given field number, or NULL if no extension
  246. * exists for this field number. */
  247. const upb_msg_ext *_upb_msg_getext(const upb_msg *msg,
  248. const upb_msglayout_ext *ext);
  249. void _upb_msg_clearext(upb_msg *msg, const upb_msglayout_ext *ext);
  250. /** Hasbit access *************************************************************/
  251. UPB_INLINE bool _upb_hasbit(const upb_msg *msg, size_t idx) {
  252. return (*UPB_PTR_AT(msg, idx / 8, const char) & (1 << (idx % 8))) != 0;
  253. }
  254. UPB_INLINE void _upb_sethas(const upb_msg *msg, size_t idx) {
  255. (*UPB_PTR_AT(msg, idx / 8, char)) |= (char)(1 << (idx % 8));
  256. }
  257. UPB_INLINE void _upb_clearhas(const upb_msg *msg, size_t idx) {
  258. (*UPB_PTR_AT(msg, idx / 8, char)) &= (char)(~(1 << (idx % 8)));
  259. }
  260. UPB_INLINE size_t _upb_msg_hasidx(const upb_msglayout_field *f) {
  261. UPB_ASSERT(f->presence > 0);
  262. return f->presence;
  263. }
  264. UPB_INLINE bool _upb_hasbit_field(const upb_msg *msg,
  265. const upb_msglayout_field *f) {
  266. return _upb_hasbit(msg, _upb_msg_hasidx(f));
  267. }
  268. UPB_INLINE void _upb_sethas_field(const upb_msg *msg,
  269. const upb_msglayout_field *f) {
  270. _upb_sethas(msg, _upb_msg_hasidx(f));
  271. }
  272. UPB_INLINE void _upb_clearhas_field(const upb_msg *msg,
  273. const upb_msglayout_field *f) {
  274. _upb_clearhas(msg, _upb_msg_hasidx(f));
  275. }
  276. /** Oneof case access *********************************************************/
  277. UPB_INLINE uint32_t *_upb_oneofcase(upb_msg *msg, size_t case_ofs) {
  278. return UPB_PTR_AT(msg, case_ofs, uint32_t);
  279. }
  280. UPB_INLINE uint32_t _upb_getoneofcase(const void *msg, size_t case_ofs) {
  281. return *UPB_PTR_AT(msg, case_ofs, uint32_t);
  282. }
  283. UPB_INLINE size_t _upb_oneofcase_ofs(const upb_msglayout_field *f) {
  284. UPB_ASSERT(f->presence < 0);
  285. return ~(ptrdiff_t)f->presence;
  286. }
  287. UPB_INLINE uint32_t *_upb_oneofcase_field(upb_msg *msg,
  288. const upb_msglayout_field *f) {
  289. return _upb_oneofcase(msg, _upb_oneofcase_ofs(f));
  290. }
  291. UPB_INLINE uint32_t _upb_getoneofcase_field(const upb_msg *msg,
  292. const upb_msglayout_field *f) {
  293. return _upb_getoneofcase(msg, _upb_oneofcase_ofs(f));
  294. }
  295. UPB_INLINE bool _upb_has_submsg_nohasbit(const upb_msg *msg, size_t ofs) {
  296. return *UPB_PTR_AT(msg, ofs, const upb_msg*) != NULL;
  297. }
  298. /** upb_array *****************************************************************/
  299. /* Our internal representation for repeated fields. */
  300. typedef struct {
  301. uintptr_t data; /* Tagged ptr: low 3 bits of ptr are lg2(elem size). */
  302. size_t len; /* Measured in elements. */
  303. size_t size; /* Measured in elements. */
  304. uint64_t junk;
  305. } upb_array;
  306. UPB_INLINE const void *_upb_array_constptr(const upb_array *arr) {
  307. UPB_ASSERT((arr->data & 7) <= 4);
  308. return (void*)(arr->data & ~(uintptr_t)7);
  309. }
  310. UPB_INLINE uintptr_t _upb_array_tagptr(void* ptr, int elem_size_lg2) {
  311. UPB_ASSERT(elem_size_lg2 <= 4);
  312. return (uintptr_t)ptr | elem_size_lg2;
  313. }
  314. UPB_INLINE void *_upb_array_ptr(upb_array *arr) {
  315. return (void*)_upb_array_constptr(arr);
  316. }
  317. UPB_INLINE uintptr_t _upb_tag_arrptr(void* ptr, int elem_size_lg2) {
  318. UPB_ASSERT(elem_size_lg2 <= 4);
  319. UPB_ASSERT(((uintptr_t)ptr & 7) == 0);
  320. return (uintptr_t)ptr | (unsigned)elem_size_lg2;
  321. }
  322. UPB_INLINE upb_array *_upb_array_new(upb_arena *a, size_t init_size,
  323. int elem_size_lg2) {
  324. const size_t arr_size = UPB_ALIGN_UP(sizeof(upb_array), 8);
  325. const size_t bytes = sizeof(upb_array) + (init_size << elem_size_lg2);
  326. upb_array *arr = (upb_array*)upb_arena_malloc(a, bytes);
  327. if (!arr) return NULL;
  328. arr->data = _upb_tag_arrptr(UPB_PTR_AT(arr, arr_size, void), elem_size_lg2);
  329. arr->len = 0;
  330. arr->size = init_size;
  331. return arr;
  332. }
  333. /* Resizes the capacity of the array to be at least min_size. */
  334. bool _upb_array_realloc(upb_array *arr, size_t min_size, upb_arena *arena);
  335. /* Fallback functions for when the accessors require a resize. */
  336. void *_upb_array_resize_fallback(upb_array **arr_ptr, size_t size,
  337. int elem_size_lg2, upb_arena *arena);
  338. bool _upb_array_append_fallback(upb_array **arr_ptr, const void *value,
  339. int elem_size_lg2, upb_arena *arena);
  340. UPB_INLINE bool _upb_array_reserve(upb_array *arr, size_t size,
  341. upb_arena *arena) {
  342. if (arr->size < size) return _upb_array_realloc(arr, size, arena);
  343. return true;
  344. }
  345. UPB_INLINE bool _upb_array_resize(upb_array *arr, size_t size,
  346. upb_arena *arena) {
  347. if (!_upb_array_reserve(arr, size, arena)) return false;
  348. arr->len = size;
  349. return true;
  350. }
  351. UPB_INLINE const void *_upb_array_accessor(const void *msg, size_t ofs,
  352. size_t *size) {
  353. const upb_array *arr = *UPB_PTR_AT(msg, ofs, const upb_array*);
  354. if (arr) {
  355. if (size) *size = arr->len;
  356. return _upb_array_constptr(arr);
  357. } else {
  358. if (size) *size = 0;
  359. return NULL;
  360. }
  361. }
  362. UPB_INLINE void *_upb_array_mutable_accessor(void *msg, size_t ofs,
  363. size_t *size) {
  364. upb_array *arr = *UPB_PTR_AT(msg, ofs, upb_array*);
  365. if (arr) {
  366. if (size) *size = arr->len;
  367. return _upb_array_ptr(arr);
  368. } else {
  369. if (size) *size = 0;
  370. return NULL;
  371. }
  372. }
  373. UPB_INLINE void *_upb_array_resize_accessor2(void *msg, size_t ofs, size_t size,
  374. int elem_size_lg2,
  375. upb_arena *arena) {
  376. upb_array **arr_ptr = UPB_PTR_AT(msg, ofs, upb_array *);
  377. upb_array *arr = *arr_ptr;
  378. if (!arr || arr->size < size) {
  379. return _upb_array_resize_fallback(arr_ptr, size, elem_size_lg2, arena);
  380. }
  381. arr->len = size;
  382. return _upb_array_ptr(arr);
  383. }
  384. UPB_INLINE bool _upb_array_append_accessor2(void *msg, size_t ofs,
  385. int elem_size_lg2,
  386. const void *value,
  387. upb_arena *arena) {
  388. upb_array **arr_ptr = UPB_PTR_AT(msg, ofs, upb_array *);
  389. size_t elem_size = 1 << elem_size_lg2;
  390. upb_array *arr = *arr_ptr;
  391. void *ptr;
  392. if (!arr || arr->len == arr->size) {
  393. return _upb_array_append_fallback(arr_ptr, value, elem_size_lg2, arena);
  394. }
  395. ptr = _upb_array_ptr(arr);
  396. memcpy(UPB_PTR_AT(ptr, arr->len * elem_size, char), value, elem_size);
  397. arr->len++;
  398. return true;
  399. }
  400. /* Used by old generated code, remove once all code has been regenerated. */
  401. UPB_INLINE int _upb_sizelg2(upb_fieldtype_t type) {
  402. switch (type) {
  403. case UPB_TYPE_BOOL:
  404. return 0;
  405. case UPB_TYPE_FLOAT:
  406. case UPB_TYPE_INT32:
  407. case UPB_TYPE_UINT32:
  408. case UPB_TYPE_ENUM:
  409. return 2;
  410. case UPB_TYPE_MESSAGE:
  411. return UPB_SIZE(2, 3);
  412. case UPB_TYPE_DOUBLE:
  413. case UPB_TYPE_INT64:
  414. case UPB_TYPE_UINT64:
  415. return 3;
  416. case UPB_TYPE_STRING:
  417. case UPB_TYPE_BYTES:
  418. return UPB_SIZE(3, 4);
  419. }
  420. UPB_UNREACHABLE();
  421. }
  422. UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size,
  423. upb_fieldtype_t type,
  424. upb_arena *arena) {
  425. return _upb_array_resize_accessor2(msg, ofs, size, _upb_sizelg2(type), arena);
  426. }
  427. UPB_INLINE bool _upb_array_append_accessor(void *msg, size_t ofs,
  428. size_t elem_size, upb_fieldtype_t type,
  429. const void *value,
  430. upb_arena *arena) {
  431. (void)elem_size;
  432. return _upb_array_append_accessor2(msg, ofs, _upb_sizelg2(type), value,
  433. arena);
  434. }
  435. /** upb_map *******************************************************************/
  436. /* Right now we use strmaps for everything. We'll likely want to use
  437. * integer-specific maps for integer-keyed maps.*/
  438. typedef struct {
  439. /* Size of key and val, based on the map type. Strings are represented as '0'
  440. * because they must be handled specially. */
  441. char key_size;
  442. char val_size;
  443. upb_strtable table;
  444. } upb_map;
  445. /* Map entries aren't actually stored, they are only used during parsing. For
  446. * parsing, it helps a lot if all map entry messages have the same layout.
  447. * The compiler and def.c must ensure that all map entries have this layout. */
  448. typedef struct {
  449. upb_msg_internal internal;
  450. union {
  451. upb_strview str; /* For str/bytes. */
  452. upb_value val; /* For all other types. */
  453. } k;
  454. union {
  455. upb_strview str; /* For str/bytes. */
  456. upb_value val; /* For all other types. */
  457. } v;
  458. } upb_map_entry;
  459. /* Creates a new map on the given arena with this key/value type. */
  460. upb_map *_upb_map_new(upb_arena *a, size_t key_size, size_t value_size);
  461. /* Converting between internal table representation and user values.
  462. *
  463. * _upb_map_tokey() and _upb_map_fromkey() are inverses.
  464. * _upb_map_tovalue() and _upb_map_fromvalue() are inverses.
  465. *
  466. * These functions account for the fact that strings are treated differently
  467. * from other types when stored in a map.
  468. */
  469. UPB_INLINE upb_strview _upb_map_tokey(const void *key, size_t size) {
  470. if (size == UPB_MAPTYPE_STRING) {
  471. return *(upb_strview*)key;
  472. } else {
  473. return upb_strview_make((const char*)key, size);
  474. }
  475. }
  476. UPB_INLINE void _upb_map_fromkey(upb_strview key, void* out, size_t size) {
  477. if (size == UPB_MAPTYPE_STRING) {
  478. memcpy(out, &key, sizeof(key));
  479. } else {
  480. memcpy(out, key.data, size);
  481. }
  482. }
  483. UPB_INLINE bool _upb_map_tovalue(const void *val, size_t size, upb_value *msgval,
  484. upb_arena *a) {
  485. if (size == UPB_MAPTYPE_STRING) {
  486. upb_strview *strp = (upb_strview*)upb_arena_malloc(a, sizeof(*strp));
  487. if (!strp) return false;
  488. *strp = *(upb_strview*)val;
  489. *msgval = upb_value_ptr(strp);
  490. } else {
  491. memcpy(msgval, val, size);
  492. }
  493. return true;
  494. }
  495. UPB_INLINE void _upb_map_fromvalue(upb_value val, void* out, size_t size) {
  496. if (size == UPB_MAPTYPE_STRING) {
  497. const upb_strview *strp = (const upb_strview*)upb_value_getptr(val);
  498. memcpy(out, strp, sizeof(upb_strview));
  499. } else {
  500. memcpy(out, &val, size);
  501. }
  502. }
  503. /* Map operations, shared by reflection and generated code. */
  504. UPB_INLINE size_t _upb_map_size(const upb_map *map) {
  505. return map->table.t.count;
  506. }
  507. UPB_INLINE bool _upb_map_get(const upb_map *map, const void *key,
  508. size_t key_size, void *val, size_t val_size) {
  509. upb_value tabval;
  510. upb_strview k = _upb_map_tokey(key, key_size);
  511. bool ret = upb_strtable_lookup2(&map->table, k.data, k.size, &tabval);
  512. if (ret && val) {
  513. _upb_map_fromvalue(tabval, val, val_size);
  514. }
  515. return ret;
  516. }
  517. UPB_INLINE void* _upb_map_next(const upb_map *map, size_t *iter) {
  518. upb_strtable_iter it;
  519. it.t = &map->table;
  520. it.index = *iter;
  521. upb_strtable_next(&it);
  522. *iter = it.index;
  523. if (upb_strtable_done(&it)) return NULL;
  524. return (void*)str_tabent(&it);
  525. }
  526. UPB_INLINE bool _upb_map_set(upb_map *map, const void *key, size_t key_size,
  527. void *val, size_t val_size, upb_arena *a) {
  528. upb_strview strkey = _upb_map_tokey(key, key_size);
  529. upb_value tabval = {0};
  530. if (!_upb_map_tovalue(val, val_size, &tabval, a)) return false;
  531. /* TODO(haberman): add overwrite operation to minimize number of lookups. */
  532. upb_strtable_remove(&map->table, strkey.data, strkey.size, NULL);
  533. return upb_strtable_insert(&map->table, strkey.data, strkey.size, tabval, a);
  534. }
  535. UPB_INLINE bool _upb_map_delete(upb_map *map, const void *key, size_t key_size) {
  536. upb_strview k = _upb_map_tokey(key, key_size);
  537. return upb_strtable_remove(&map->table, k.data, k.size, NULL);
  538. }
  539. UPB_INLINE void _upb_map_clear(upb_map *map) {
  540. upb_strtable_clear(&map->table);
  541. }
  542. /* Message map operations, these get the map from the message first. */
  543. UPB_INLINE size_t _upb_msg_map_size(const upb_msg *msg, size_t ofs) {
  544. upb_map *map = *UPB_PTR_AT(msg, ofs, upb_map *);
  545. return map ? _upb_map_size(map) : 0;
  546. }
  547. UPB_INLINE bool _upb_msg_map_get(const upb_msg *msg, size_t ofs,
  548. const void *key, size_t key_size, void *val,
  549. size_t val_size) {
  550. upb_map *map = *UPB_PTR_AT(msg, ofs, upb_map *);
  551. if (!map) return false;
  552. return _upb_map_get(map, key, key_size, val, val_size);
  553. }
  554. UPB_INLINE void *_upb_msg_map_next(const upb_msg *msg, size_t ofs,
  555. size_t *iter) {
  556. upb_map *map = *UPB_PTR_AT(msg, ofs, upb_map *);
  557. if (!map) return NULL;
  558. return _upb_map_next(map, iter);
  559. }
  560. UPB_INLINE bool _upb_msg_map_set(upb_msg *msg, size_t ofs, const void *key,
  561. size_t key_size, void *val, size_t val_size,
  562. upb_arena *arena) {
  563. upb_map **map = UPB_PTR_AT(msg, ofs, upb_map *);
  564. if (!*map) {
  565. *map = _upb_map_new(arena, key_size, val_size);
  566. }
  567. return _upb_map_set(*map, key, key_size, val, val_size, arena);
  568. }
  569. UPB_INLINE bool _upb_msg_map_delete(upb_msg *msg, size_t ofs, const void *key,
  570. size_t key_size) {
  571. upb_map *map = *UPB_PTR_AT(msg, ofs, upb_map *);
  572. if (!map) return false;
  573. return _upb_map_delete(map, key, key_size);
  574. }
  575. UPB_INLINE void _upb_msg_map_clear(upb_msg *msg, size_t ofs) {
  576. upb_map *map = *UPB_PTR_AT(msg, ofs, upb_map *);
  577. if (!map) return;
  578. _upb_map_clear(map);
  579. }
  580. /* Accessing map key/value from a pointer, used by generated code only. */
  581. UPB_INLINE void _upb_msg_map_key(const void* msg, void* key, size_t size) {
  582. const upb_tabent *ent = (const upb_tabent*)msg;
  583. uint32_t u32len;
  584. upb_strview k;
  585. k.data = upb_tabstr(ent->key, &u32len);
  586. k.size = u32len;
  587. _upb_map_fromkey(k, key, size);
  588. }
  589. UPB_INLINE void _upb_msg_map_value(const void* msg, void* val, size_t size) {
  590. const upb_tabent *ent = (const upb_tabent*)msg;
  591. upb_value v = {ent->val.val};
  592. _upb_map_fromvalue(v, val, size);
  593. }
  594. UPB_INLINE void _upb_msg_map_set_value(void* msg, const void* val, size_t size) {
  595. upb_tabent *ent = (upb_tabent*)msg;
  596. /* This is like _upb_map_tovalue() except the entry already exists so we can
  597. * reuse the allocated upb_strview for string fields. */
  598. if (size == UPB_MAPTYPE_STRING) {
  599. upb_strview *strp = (upb_strview*)(uintptr_t)ent->val.val;
  600. memcpy(strp, val, sizeof(*strp));
  601. } else {
  602. memcpy(&ent->val.val, val, size);
  603. }
  604. }
  605. /** _upb_mapsorter *************************************************************/
  606. /* _upb_mapsorter sorts maps and provides ordered iteration over the entries.
  607. * Since maps can be recursive (map values can be messages which contain other maps).
  608. * _upb_mapsorter can contain a stack of maps. */
  609. typedef struct {
  610. upb_tabent const**entries;
  611. int size;
  612. int cap;
  613. } _upb_mapsorter;
  614. typedef struct {
  615. int start;
  616. int pos;
  617. int end;
  618. } _upb_sortedmap;
  619. UPB_INLINE void _upb_mapsorter_init(_upb_mapsorter *s) {
  620. s->entries = NULL;
  621. s->size = 0;
  622. s->cap = 0;
  623. }
  624. UPB_INLINE void _upb_mapsorter_destroy(_upb_mapsorter *s) {
  625. if (s->entries) free(s->entries);
  626. }
  627. bool _upb_mapsorter_pushmap(_upb_mapsorter *s, upb_descriptortype_t key_type,
  628. const upb_map *map, _upb_sortedmap *sorted);
  629. UPB_INLINE void _upb_mapsorter_popmap(_upb_mapsorter *s, _upb_sortedmap *sorted) {
  630. s->size = sorted->start;
  631. }
  632. UPB_INLINE bool _upb_sortedmap_next(_upb_mapsorter *s, const upb_map *map,
  633. _upb_sortedmap *sorted,
  634. upb_map_entry *ent) {
  635. if (sorted->pos == sorted->end) return false;
  636. const upb_tabent *tabent = s->entries[sorted->pos++];
  637. upb_strview key = upb_tabstrview(tabent->key);
  638. _upb_map_fromkey(key, &ent->k, map->key_size);
  639. upb_value val = {tabent->val.val};
  640. _upb_map_fromvalue(val, &ent->v, map->val_size);
  641. return true;
  642. }
  643. #ifdef __cplusplus
  644. } /* extern "C" */
  645. #endif
  646. #include "upb/port_undef.inc"
  647. #endif /* UPB_MSG_INT_H_ */