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.

msg_internal.h 31 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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/extension_registry.h"
  39. #include "upb/internal/table.h"
  40. #include "upb/msg.h"
  41. #include "upb/upb.h"
  42. /* Must be last. */
  43. #include "upb/port_def.inc"
  44. #ifdef __cplusplus
  45. extern "C"
  46. {
  47. #endif
  48. /** upb_*Int* conversion routines ********************************************/
  49. UPB_INLINE int32_t _upb_Int32_FromI(int v)
  50. {
  51. return (int32_t)v;
  52. }
  53. UPB_INLINE int64_t _upb_Int64_FromLL(long long v)
  54. {
  55. return (int64_t)v;
  56. }
  57. UPB_INLINE uint32_t _upb_UInt32_FromU(unsigned v)
  58. {
  59. return (uint32_t)v;
  60. }
  61. UPB_INLINE uint64_t _upb_UInt64_FromULL(unsigned long long v)
  62. {
  63. return (uint64_t)v;
  64. }
  65. /** upb_MiniTable *************************************************************/
  66. /* upb_MiniTable represents the memory layout of a given upb_MessageDef. The
  67. * members are public so generated code can initialize them, but users MUST NOT
  68. * read or write any of its members. */
  69. typedef struct
  70. {
  71. uint32_t number;
  72. uint16_t offset;
  73. int16_t presence; // If >0, hasbit_index. If <0, ~oneof_index
  74. uint16_t submsg_index; // kUpb_NoSub if descriptortype != MESSAGE/GROUP/ENUM
  75. uint8_t descriptortype;
  76. uint8_t mode; /* upb_FieldMode | upb_LabelFlags |
  77. (upb_FieldRep << kUpb_FieldRep_Shift) */
  78. } upb_MiniTable_Field;
  79. #define kUpb_NoSub ((uint16_t)-1)
  80. typedef enum
  81. {
  82. kUpb_FieldMode_Map = 0,
  83. kUpb_FieldMode_Array = 1,
  84. kUpb_FieldMode_Scalar = 2,
  85. } upb_FieldMode;
  86. // Mask to isolate the upb_FieldMode from field.mode.
  87. #define kUpb_FieldMode_Mask 3
  88. /* Extra flags on the mode field. */
  89. typedef enum
  90. {
  91. kUpb_LabelFlags_IsPacked = 4,
  92. kUpb_LabelFlags_IsExtension = 8,
  93. } upb_LabelFlags;
  94. // Note: we sort by this number when calculating layout order.
  95. typedef enum
  96. {
  97. kUpb_FieldRep_1Byte = 0,
  98. kUpb_FieldRep_4Byte = 1,
  99. kUpb_FieldRep_StringView = 2,
  100. kUpb_FieldRep_Pointer = 3,
  101. kUpb_FieldRep_8Byte = 4,
  102. kUpb_FieldRep_Shift = 5, // Bit offset of the rep in upb_MiniTable_Field.mode
  103. kUpb_FieldRep_Max = kUpb_FieldRep_8Byte,
  104. } upb_FieldRep;
  105. UPB_INLINE upb_FieldMode upb_FieldMode_Get(const upb_MiniTable_Field* field)
  106. {
  107. return (upb_FieldMode)(field->mode & 3);
  108. }
  109. UPB_INLINE bool upb_IsRepeatedOrMap(const upb_MiniTable_Field* field)
  110. {
  111. /* This works because upb_FieldMode has no value 3. */
  112. return !(field->mode & kUpb_FieldMode_Scalar);
  113. }
  114. UPB_INLINE bool upb_IsSubMessage(const upb_MiniTable_Field* field)
  115. {
  116. return field->descriptortype == kUpb_FieldType_Message ||
  117. field->descriptortype == kUpb_FieldType_Group;
  118. }
  119. struct upb_Decoder;
  120. struct upb_MiniTable;
  121. typedef const char* _upb_FieldParser(struct upb_Decoder* d, const char* ptr, upb_Message* msg, intptr_t table, uint64_t hasbits, uint64_t data);
  122. typedef struct
  123. {
  124. uint64_t field_data;
  125. _upb_FieldParser* field_parser;
  126. } _upb_FastTable_Entry;
  127. typedef struct
  128. {
  129. const int32_t* values; // List of values <0 or >63
  130. uint64_t mask; // Bits are set for acceptable value 0 <= x < 64
  131. int value_count;
  132. } upb_MiniTable_Enum;
  133. typedef union
  134. {
  135. const struct upb_MiniTable* submsg;
  136. const upb_MiniTable_Enum* subenum;
  137. } upb_MiniTable_Sub;
  138. typedef enum
  139. {
  140. kUpb_ExtMode_NonExtendable = 0, // Non-extendable message.
  141. kUpb_ExtMode_Extendable = 1, // Normal extendable message.
  142. kUpb_ExtMode_IsMessageSet = 2, // MessageSet message.
  143. kUpb_ExtMode_IsMessageSet_ITEM =
  144. 3, // MessageSet item (temporary only, see decode.c)
  145. // During table building we steal a bit to indicate that the message is a map
  146. // entry. *Only* used during table building!
  147. kUpb_ExtMode_IsMapEntry = 4,
  148. } upb_ExtMode;
  149. /* MessageSet wire format is:
  150. * message MessageSet {
  151. * repeated group Item = 1 {
  152. * required int32 type_id = 2;
  153. * required bytes message = 3;
  154. * }
  155. * }
  156. */
  157. typedef enum
  158. {
  159. _UPB_MSGSET_ITEM = 1,
  160. _UPB_MSGSET_TYPEID = 2,
  161. _UPB_MSGSET_MESSAGE = 3,
  162. } upb_msgext_fieldnum;
  163. struct upb_MiniTable
  164. {
  165. const upb_MiniTable_Sub* subs;
  166. const upb_MiniTable_Field* fields;
  167. /* Must be aligned to sizeof(void*). Doesn't include internal members like
  168. * unknown fields, extension dict, pointer to msglayout, etc. */
  169. uint16_t size;
  170. uint16_t field_count;
  171. uint8_t ext; // upb_ExtMode, declared as uint8_t so sizeof(ext) == 1
  172. uint8_t dense_below;
  173. uint8_t table_mask;
  174. uint8_t required_count; // Required fields have the lowest hasbits.
  175. /* To statically initialize the tables of variable length, we need a flexible
  176. * array member, and we need to compile in gnu99 mode (constant initialization
  177. * of flexible array members is a GNU extension, not in C99 unfortunately. */
  178. _upb_FastTable_Entry fasttable[];
  179. };
  180. typedef struct
  181. {
  182. upb_MiniTable_Field field;
  183. const upb_MiniTable* extendee;
  184. upb_MiniTable_Sub sub; /* NULL unless submessage or proto2 enum */
  185. } upb_MiniTable_Extension;
  186. typedef struct
  187. {
  188. const upb_MiniTable** msgs;
  189. const upb_MiniTable_Enum** enums;
  190. const upb_MiniTable_Extension** exts;
  191. int msg_count;
  192. int enum_count;
  193. int ext_count;
  194. } upb_MiniTable_File;
  195. // Computes a bitmask in which the |l->required_count| lowest bits are set,
  196. // except that we skip the lowest bit (because upb never uses hasbit 0).
  197. //
  198. // Sample output:
  199. // requiredmask(1) => 0b10 (0x2)
  200. // requiredmask(5) => 0b111110 (0x3e)
  201. UPB_INLINE uint64_t upb_MiniTable_requiredmask(const upb_MiniTable* l)
  202. {
  203. int n = l->required_count;
  204. assert(0 < n && n <= 63);
  205. return ((1ULL << n) - 1) << 1;
  206. }
  207. /** upb_ExtensionRegistry *****************************************************/
  208. /* Adds the given extension info for message type |l| and field number |num|
  209. * into the registry. Returns false if this message type and field number were
  210. * already in the map, or if memory allocation fails. */
  211. bool _upb_extreg_add(upb_ExtensionRegistry* r, const upb_MiniTable_Extension** e, size_t count);
  212. /* Looks up the extension (if any) defined for message type |l| and field
  213. * number |num|. If an extension was found, copies the field info into |*ext|
  214. * and returns true. Otherwise returns false. */
  215. const upb_MiniTable_Extension* _upb_extreg_get(const upb_ExtensionRegistry* r, const upb_MiniTable* l, uint32_t num);
  216. /** upb_Message ***************************************************************/
  217. /* Internal members of a upb_Message that track unknown fields and/or
  218. * extensions. We can change this without breaking binary compatibility. We put
  219. * these before the user's data. The user's upb_Message* points after the
  220. * upb_Message_Internal. */
  221. typedef struct
  222. {
  223. /* Total size of this structure, including the data that follows.
  224. * Must be aligned to 8, which is alignof(upb_Message_Extension) */
  225. uint32_t size;
  226. /* Offsets relative to the beginning of this structure.
  227. *
  228. * Unknown data grows forward from the beginning to unknown_end.
  229. * Extension data grows backward from size to ext_begin.
  230. * When the two meet, we're out of data and have to realloc.
  231. *
  232. * If we imagine that the final member of this struct is:
  233. * char data[size - overhead]; // overhead =
  234. * sizeof(upb_Message_InternalData)
  235. *
  236. * Then we have:
  237. * unknown data: data[0 .. (unknown_end - overhead)]
  238. * extensions data: data[(ext_begin - overhead) .. (size - overhead)] */
  239. uint32_t unknown_end;
  240. uint32_t ext_begin;
  241. /* Data follows, as if there were an array:
  242. * char data[size - sizeof(upb_Message_InternalData)]; */
  243. } upb_Message_InternalData;
  244. typedef struct
  245. {
  246. upb_Message_InternalData* internal;
  247. /* Message data follows. */
  248. } upb_Message_Internal;
  249. /* Maps upb_CType -> memory size. */
  250. extern char _upb_CTypeo_size[12];
  251. UPB_INLINE size_t upb_msg_sizeof(const upb_MiniTable* l)
  252. {
  253. return l->size + sizeof(upb_Message_Internal);
  254. }
  255. UPB_INLINE upb_Message* _upb_Message_New_inl(const upb_MiniTable* l, upb_Arena* a)
  256. {
  257. size_t size = upb_msg_sizeof(l);
  258. void* mem = upb_Arena_Malloc(a, size + sizeof(upb_Message_Internal));
  259. upb_Message* msg;
  260. if (UPB_UNLIKELY(!mem))
  261. return NULL;
  262. msg = UPB_PTR_AT(mem, sizeof(upb_Message_Internal), upb_Message);
  263. memset(mem, 0, size);
  264. return msg;
  265. }
  266. /* Creates a new messages with the given layout on the given arena. */
  267. upb_Message* _upb_Message_New(const upb_MiniTable* l, upb_Arena* a);
  268. UPB_INLINE upb_Message_Internal* upb_Message_Getinternal(upb_Message* msg)
  269. {
  270. ptrdiff_t size = sizeof(upb_Message_Internal);
  271. return (upb_Message_Internal*)((char*)msg - size);
  272. }
  273. /* Clears the given message. */
  274. void _upb_Message_Clear(upb_Message* msg, const upb_MiniTable* l);
  275. /* Discards the unknown fields for this message only. */
  276. void _upb_Message_DiscardUnknown_shallow(upb_Message* msg);
  277. /* Adds unknown data (serialized protobuf data) to the given message. The data
  278. * is copied into the message instance. */
  279. bool _upb_Message_AddUnknown(upb_Message* msg, const char* data, size_t len, upb_Arena* arena);
  280. /** upb_Message_Extension *****************************************************/
  281. /* The internal representation of an extension is self-describing: it contains
  282. * enough information that we can serialize it to binary format without needing
  283. * to look it up in a upb_ExtensionRegistry.
  284. *
  285. * This representation allocates 16 bytes to data on 64-bit platforms. This is
  286. * rather wasteful for scalars (in the extreme case of bool, it wastes 15
  287. * bytes). We accept this because we expect messages to be the most common
  288. * extension type. */
  289. typedef struct
  290. {
  291. const upb_MiniTable_Extension* ext;
  292. union
  293. {
  294. upb_StringView str;
  295. void* ptr;
  296. char scalar_data[8];
  297. } data;
  298. } upb_Message_Extension;
  299. /* Adds the given extension data to the given message. |ext| is copied into the
  300. * message instance. This logically replaces any previously-added extension with
  301. * this number */
  302. upb_Message_Extension* _upb_Message_GetOrCreateExtension(
  303. upb_Message* msg, const upb_MiniTable_Extension* ext, upb_Arena* arena
  304. );
  305. /* Returns an array of extensions for this message. Note: the array is
  306. * ordered in reverse relative to the order of creation. */
  307. const upb_Message_Extension* _upb_Message_Getexts(const upb_Message* msg, size_t* count);
  308. /* Returns an extension for the given field number, or NULL if no extension
  309. * exists for this field number. */
  310. const upb_Message_Extension* _upb_Message_Getext(
  311. const upb_Message* msg, const upb_MiniTable_Extension* ext
  312. );
  313. void _upb_Message_Clearext(upb_Message* msg, const upb_MiniTable_Extension* ext);
  314. void _upb_Message_Clearext(upb_Message* msg, const upb_MiniTable_Extension* ext);
  315. /** Hasbit access *************************************************************/
  316. UPB_INLINE bool _upb_hasbit(const upb_Message* msg, size_t idx)
  317. {
  318. return (*UPB_PTR_AT(msg, idx / 8, const char) & (1 << (idx % 8))) != 0;
  319. }
  320. UPB_INLINE void _upb_sethas(const upb_Message* msg, size_t idx)
  321. {
  322. (*UPB_PTR_AT(msg, idx / 8, char)) |= (char)(1 << (idx % 8));
  323. }
  324. UPB_INLINE void _upb_clearhas(const upb_Message* msg, size_t idx)
  325. {
  326. (*UPB_PTR_AT(msg, idx / 8, char)) &= (char)(~(1 << (idx % 8)));
  327. }
  328. UPB_INLINE size_t _upb_Message_Hasidx(const upb_MiniTable_Field* f)
  329. {
  330. UPB_ASSERT(f->presence > 0);
  331. return f->presence;
  332. }
  333. UPB_INLINE bool _upb_hasbit_field(const upb_Message* msg, const upb_MiniTable_Field* f)
  334. {
  335. return _upb_hasbit(msg, _upb_Message_Hasidx(f));
  336. }
  337. UPB_INLINE void _upb_sethas_field(const upb_Message* msg, const upb_MiniTable_Field* f)
  338. {
  339. _upb_sethas(msg, _upb_Message_Hasidx(f));
  340. }
  341. UPB_INLINE void _upb_clearhas_field(const upb_Message* msg, const upb_MiniTable_Field* f)
  342. {
  343. _upb_clearhas(msg, _upb_Message_Hasidx(f));
  344. }
  345. /** Oneof case access *********************************************************/
  346. UPB_INLINE uint32_t* _upb_oneofcase(upb_Message* msg, size_t case_ofs)
  347. {
  348. return UPB_PTR_AT(msg, case_ofs, uint32_t);
  349. }
  350. UPB_INLINE uint32_t _upb_getoneofcase(const void* msg, size_t case_ofs)
  351. {
  352. return *UPB_PTR_AT(msg, case_ofs, uint32_t);
  353. }
  354. UPB_INLINE size_t _upb_oneofcase_ofs(const upb_MiniTable_Field* f)
  355. {
  356. UPB_ASSERT(f->presence < 0);
  357. return ~(ptrdiff_t)f->presence;
  358. }
  359. UPB_INLINE uint32_t* _upb_oneofcase_field(upb_Message* msg, const upb_MiniTable_Field* f)
  360. {
  361. return _upb_oneofcase(msg, _upb_oneofcase_ofs(f));
  362. }
  363. UPB_INLINE uint32_t _upb_getoneofcase_field(const upb_Message* msg, const upb_MiniTable_Field* f)
  364. {
  365. return _upb_getoneofcase(msg, _upb_oneofcase_ofs(f));
  366. }
  367. UPB_INLINE bool _upb_has_submsg_nohasbit(const upb_Message* msg, size_t ofs)
  368. {
  369. return *UPB_PTR_AT(msg, ofs, const upb_Message*) != NULL;
  370. }
  371. /** upb_Array *****************************************************************/
  372. /* Our internal representation for repeated fields. */
  373. typedef struct
  374. {
  375. uintptr_t data; /* Tagged ptr: low 3 bits of ptr are lg2(elem size). */
  376. size_t len; /* Measured in elements. */
  377. size_t size; /* Measured in elements. */
  378. uint64_t junk;
  379. } upb_Array;
  380. UPB_INLINE const void* _upb_array_constptr(const upb_Array* arr)
  381. {
  382. UPB_ASSERT((arr->data & 7) <= 4);
  383. return (void*)(arr->data & ~(uintptr_t)7);
  384. }
  385. UPB_INLINE uintptr_t _upb_array_tagptr(void* ptr, int elem_size_lg2)
  386. {
  387. UPB_ASSERT(elem_size_lg2 <= 4);
  388. return (uintptr_t)ptr | elem_size_lg2;
  389. }
  390. UPB_INLINE void* _upb_array_ptr(upb_Array* arr)
  391. {
  392. return (void*)_upb_array_constptr(arr);
  393. }
  394. UPB_INLINE uintptr_t _upb_tag_arrptr(void* ptr, int elem_size_lg2)
  395. {
  396. UPB_ASSERT(elem_size_lg2 <= 4);
  397. UPB_ASSERT(((uintptr_t)ptr & 7) == 0);
  398. return (uintptr_t)ptr | (unsigned)elem_size_lg2;
  399. }
  400. UPB_INLINE upb_Array* _upb_Array_New(upb_Arena* a, size_t init_size, int elem_size_lg2)
  401. {
  402. const size_t arr_size = UPB_ALIGN_UP(sizeof(upb_Array), 8);
  403. const size_t bytes = sizeof(upb_Array) + (init_size << elem_size_lg2);
  404. upb_Array* arr = (upb_Array*)upb_Arena_Malloc(a, bytes);
  405. if (!arr)
  406. return NULL;
  407. arr->data = _upb_tag_arrptr(UPB_PTR_AT(arr, arr_size, void), elem_size_lg2);
  408. arr->len = 0;
  409. arr->size = init_size;
  410. return arr;
  411. }
  412. /* Resizes the capacity of the array to be at least min_size. */
  413. bool _upb_array_realloc(upb_Array* arr, size_t min_size, upb_Arena* arena);
  414. /* Fallback functions for when the accessors require a resize. */
  415. void* _upb_Array_Resize_fallback(upb_Array** arr_ptr, size_t size, int elem_size_lg2, upb_Arena* arena);
  416. bool _upb_Array_Append_fallback(upb_Array** arr_ptr, const void* value, int elem_size_lg2, upb_Arena* arena);
  417. UPB_INLINE bool _upb_array_reserve(upb_Array* arr, size_t size, upb_Arena* arena)
  418. {
  419. if (arr->size < size)
  420. return _upb_array_realloc(arr, size, arena);
  421. return true;
  422. }
  423. UPB_INLINE bool _upb_Array_Resize(upb_Array* arr, size_t size, upb_Arena* arena)
  424. {
  425. if (!_upb_array_reserve(arr, size, arena))
  426. return false;
  427. arr->len = size;
  428. return true;
  429. }
  430. UPB_INLINE void _upb_array_detach(const void* msg, size_t ofs)
  431. {
  432. *UPB_PTR_AT(msg, ofs, upb_Array*) = NULL;
  433. }
  434. UPB_INLINE const void* _upb_array_accessor(const void* msg, size_t ofs, size_t* size)
  435. {
  436. const upb_Array* arr = *UPB_PTR_AT(msg, ofs, const upb_Array*);
  437. if (arr)
  438. {
  439. if (size)
  440. *size = arr->len;
  441. return _upb_array_constptr(arr);
  442. }
  443. else
  444. {
  445. if (size)
  446. *size = 0;
  447. return NULL;
  448. }
  449. }
  450. UPB_INLINE void* _upb_array_mutable_accessor(void* msg, size_t ofs, size_t* size)
  451. {
  452. upb_Array* arr = *UPB_PTR_AT(msg, ofs, upb_Array*);
  453. if (arr)
  454. {
  455. if (size)
  456. *size = arr->len;
  457. return _upb_array_ptr(arr);
  458. }
  459. else
  460. {
  461. if (size)
  462. *size = 0;
  463. return NULL;
  464. }
  465. }
  466. UPB_INLINE void* _upb_Array_Resize_accessor2(void* msg, size_t ofs, size_t size, int elem_size_lg2, upb_Arena* arena)
  467. {
  468. upb_Array** arr_ptr = UPB_PTR_AT(msg, ofs, upb_Array*);
  469. upb_Array* arr = *arr_ptr;
  470. if (!arr || arr->size < size)
  471. {
  472. return _upb_Array_Resize_fallback(arr_ptr, size, elem_size_lg2, arena);
  473. }
  474. arr->len = size;
  475. return _upb_array_ptr(arr);
  476. }
  477. UPB_INLINE bool _upb_Array_Append_accessor2(void* msg, size_t ofs, int elem_size_lg2, const void* value, upb_Arena* arena)
  478. {
  479. upb_Array** arr_ptr = UPB_PTR_AT(msg, ofs, upb_Array*);
  480. size_t elem_size = 1 << elem_size_lg2;
  481. upb_Array* arr = *arr_ptr;
  482. void* ptr;
  483. if (!arr || arr->len == arr->size)
  484. {
  485. return _upb_Array_Append_fallback(arr_ptr, value, elem_size_lg2, arena);
  486. }
  487. ptr = _upb_array_ptr(arr);
  488. memcpy(UPB_PTR_AT(ptr, arr->len * elem_size, char), value, elem_size);
  489. arr->len++;
  490. return true;
  491. }
  492. /* Used by old generated code, remove once all code has been regenerated. */
  493. UPB_INLINE int _upb_sizelg2(upb_CType type)
  494. {
  495. switch (type)
  496. {
  497. case kUpb_CType_Bool:
  498. return 0;
  499. case kUpb_CType_Float:
  500. case kUpb_CType_Int32:
  501. case kUpb_CType_UInt32:
  502. case kUpb_CType_Enum:
  503. return 2;
  504. case kUpb_CType_Message:
  505. return UPB_SIZE(2, 3);
  506. case kUpb_CType_Double:
  507. case kUpb_CType_Int64:
  508. case kUpb_CType_UInt64:
  509. return 3;
  510. case kUpb_CType_String:
  511. case kUpb_CType_Bytes:
  512. return UPB_SIZE(3, 4);
  513. }
  514. UPB_UNREACHABLE();
  515. }
  516. UPB_INLINE void* _upb_Array_Resize_accessor(void* msg, size_t ofs, size_t size, upb_CType type, upb_Arena* arena)
  517. {
  518. return _upb_Array_Resize_accessor2(msg, ofs, size, _upb_sizelg2(type), arena);
  519. }
  520. UPB_INLINE bool _upb_Array_Append_accessor(void* msg, size_t ofs, size_t elem_size, upb_CType type, const void* value, upb_Arena* arena)
  521. {
  522. (void)elem_size;
  523. return _upb_Array_Append_accessor2(msg, ofs, _upb_sizelg2(type), value, arena);
  524. }
  525. /** upb_Map *******************************************************************/
  526. /* Right now we use strmaps for everything. We'll likely want to use
  527. * integer-specific maps for integer-keyed maps.*/
  528. typedef struct
  529. {
  530. /* Size of key and val, based on the map type. Strings are represented as '0'
  531. * because they must be handled specially. */
  532. char key_size;
  533. char val_size;
  534. upb_strtable table;
  535. } upb_Map;
  536. /* Map entries aren't actually stored, they are only used during parsing. For
  537. * parsing, it helps a lot if all map entry messages have the same layout.
  538. * The compiler and def.c must ensure that all map entries have this layout. */
  539. typedef struct
  540. {
  541. upb_Message_Internal internal;
  542. union
  543. {
  544. upb_StringView str; /* For str/bytes. */
  545. upb_value val; /* For all other types. */
  546. } k;
  547. union
  548. {
  549. upb_StringView str; /* For str/bytes. */
  550. upb_value val; /* For all other types. */
  551. } v;
  552. } upb_MapEntry;
  553. /* Creates a new map on the given arena with this key/value type. */
  554. upb_Map* _upb_Map_New(upb_Arena* a, size_t key_size, size_t value_size);
  555. /* Converting between internal table representation and user values.
  556. *
  557. * _upb_map_tokey() and _upb_map_fromkey() are inverses.
  558. * _upb_map_tovalue() and _upb_map_fromvalue() are inverses.
  559. *
  560. * These functions account for the fact that strings are treated differently
  561. * from other types when stored in a map.
  562. */
  563. UPB_INLINE upb_StringView _upb_map_tokey(const void* key, size_t size)
  564. {
  565. if (size == UPB_MAPTYPE_STRING)
  566. {
  567. return *(upb_StringView*)key;
  568. }
  569. else
  570. {
  571. return upb_StringView_FromDataAndSize((const char*)key, size);
  572. }
  573. }
  574. UPB_INLINE void _upb_map_fromkey(upb_StringView key, void* out, size_t size)
  575. {
  576. if (size == UPB_MAPTYPE_STRING)
  577. {
  578. memcpy(out, &key, sizeof(key));
  579. }
  580. else
  581. {
  582. memcpy(out, key.data, size);
  583. }
  584. }
  585. UPB_INLINE bool _upb_map_tovalue(const void* val, size_t size, upb_value* msgval, upb_Arena* a)
  586. {
  587. if (size == UPB_MAPTYPE_STRING)
  588. {
  589. upb_StringView* strp = (upb_StringView*)upb_Arena_Malloc(a, sizeof(*strp));
  590. if (!strp)
  591. return false;
  592. *strp = *(upb_StringView*)val;
  593. *msgval = upb_value_ptr(strp);
  594. }
  595. else
  596. {
  597. memcpy(msgval, val, size);
  598. }
  599. return true;
  600. }
  601. UPB_INLINE void _upb_map_fromvalue(upb_value val, void* out, size_t size)
  602. {
  603. if (size == UPB_MAPTYPE_STRING)
  604. {
  605. const upb_StringView* strp = (const upb_StringView*)upb_value_getptr(val);
  606. memcpy(out, strp, sizeof(upb_StringView));
  607. }
  608. else
  609. {
  610. memcpy(out, &val, size);
  611. }
  612. }
  613. /* Map operations, shared by reflection and generated code. */
  614. UPB_INLINE size_t _upb_Map_Size(const upb_Map* map)
  615. {
  616. return map->table.t.count;
  617. }
  618. UPB_INLINE bool _upb_Map_Get(const upb_Map* map, const void* key, size_t key_size, void* val, size_t val_size)
  619. {
  620. upb_value tabval;
  621. upb_StringView k = _upb_map_tokey(key, key_size);
  622. bool ret = upb_strtable_lookup2(&map->table, k.data, k.size, &tabval);
  623. if (ret && val)
  624. {
  625. _upb_map_fromvalue(tabval, val, val_size);
  626. }
  627. return ret;
  628. }
  629. UPB_INLINE void* _upb_map_next(const upb_Map* map, size_t* iter)
  630. {
  631. upb_strtable_iter it;
  632. it.t = &map->table;
  633. it.index = *iter;
  634. upb_strtable_next(&it);
  635. *iter = it.index;
  636. if (upb_strtable_done(&it))
  637. return NULL;
  638. return (void*)str_tabent(&it);
  639. }
  640. typedef enum
  641. {
  642. // LINT.IfChange
  643. _kUpb_MapInsertStatus_Inserted = 0,
  644. _kUpb_MapInsertStatus_Replaced = 1,
  645. _kUpb_MapInsertStatus_OutOfMemory = 2,
  646. // LINT.ThenChange(//depot/google3/third_party/upb/upb/map.h)
  647. } _upb_MapInsertStatus;
  648. UPB_INLINE _upb_MapInsertStatus _upb_Map_Insert(upb_Map* map, const void* key, size_t key_size, void* val, size_t val_size, upb_Arena* a)
  649. {
  650. upb_StringView strkey = _upb_map_tokey(key, key_size);
  651. upb_value tabval = {0};
  652. if (!_upb_map_tovalue(val, val_size, &tabval, a))
  653. {
  654. return _kUpb_MapInsertStatus_OutOfMemory;
  655. }
  656. /* TODO(haberman): add overwrite operation to minimize number of lookups. */
  657. bool removed =
  658. upb_strtable_remove2(&map->table, strkey.data, strkey.size, NULL);
  659. if (!upb_strtable_insert(&map->table, strkey.data, strkey.size, tabval, a))
  660. {
  661. return _kUpb_MapInsertStatus_OutOfMemory;
  662. }
  663. return removed ? _kUpb_MapInsertStatus_Replaced : _kUpb_MapInsertStatus_Inserted;
  664. }
  665. UPB_INLINE bool _upb_Map_Delete(upb_Map* map, const void* key, size_t key_size)
  666. {
  667. upb_StringView k = _upb_map_tokey(key, key_size);
  668. return upb_strtable_remove2(&map->table, k.data, k.size, NULL);
  669. }
  670. UPB_INLINE void _upb_Map_Clear(upb_Map* map)
  671. {
  672. upb_strtable_clear(&map->table);
  673. }
  674. /* Message map operations, these get the map from the message first. */
  675. UPB_INLINE size_t _upb_msg_map_size(const upb_Message* msg, size_t ofs)
  676. {
  677. upb_Map* map = *UPB_PTR_AT(msg, ofs, upb_Map*);
  678. return map ? _upb_Map_Size(map) : 0;
  679. }
  680. UPB_INLINE bool _upb_msg_map_get(const upb_Message* msg, size_t ofs, const void* key, size_t key_size, void* val, size_t val_size)
  681. {
  682. upb_Map* map = *UPB_PTR_AT(msg, ofs, upb_Map*);
  683. if (!map)
  684. return false;
  685. return _upb_Map_Get(map, key, key_size, val, val_size);
  686. }
  687. UPB_INLINE void* _upb_msg_map_next(const upb_Message* msg, size_t ofs, size_t* iter)
  688. {
  689. upb_Map* map = *UPB_PTR_AT(msg, ofs, upb_Map*);
  690. if (!map)
  691. return NULL;
  692. return _upb_map_next(map, iter);
  693. }
  694. UPB_INLINE bool _upb_msg_map_set(upb_Message* msg, size_t ofs, const void* key, size_t key_size, void* val, size_t val_size, upb_Arena* arena)
  695. {
  696. upb_Map** map = UPB_PTR_AT(msg, ofs, upb_Map*);
  697. if (!*map)
  698. {
  699. *map = _upb_Map_New(arena, key_size, val_size);
  700. }
  701. return _upb_Map_Insert(*map, key, key_size, val, val_size, arena) !=
  702. _kUpb_MapInsertStatus_OutOfMemory;
  703. }
  704. UPB_INLINE bool _upb_msg_map_delete(upb_Message* msg, size_t ofs, const void* key, size_t key_size)
  705. {
  706. upb_Map* map = *UPB_PTR_AT(msg, ofs, upb_Map*);
  707. if (!map)
  708. return false;
  709. return _upb_Map_Delete(map, key, key_size);
  710. }
  711. UPB_INLINE void _upb_msg_map_clear(upb_Message* msg, size_t ofs)
  712. {
  713. upb_Map* map = *UPB_PTR_AT(msg, ofs, upb_Map*);
  714. if (!map)
  715. return;
  716. _upb_Map_Clear(map);
  717. }
  718. /* Accessing map key/value from a pointer, used by generated code only. */
  719. UPB_INLINE void _upb_msg_map_key(const void* msg, void* key, size_t size)
  720. {
  721. const upb_tabent* ent = (const upb_tabent*)msg;
  722. uint32_t u32len;
  723. upb_StringView k;
  724. k.data = upb_tabstr(ent->key, &u32len);
  725. k.size = u32len;
  726. _upb_map_fromkey(k, key, size);
  727. }
  728. UPB_INLINE void _upb_msg_map_value(const void* msg, void* val, size_t size)
  729. {
  730. const upb_tabent* ent = (const upb_tabent*)msg;
  731. upb_value v = {ent->val.val};
  732. _upb_map_fromvalue(v, val, size);
  733. }
  734. UPB_INLINE void _upb_msg_map_set_value(void* msg, const void* val, size_t size)
  735. {
  736. upb_tabent* ent = (upb_tabent*)msg;
  737. /* This is like _upb_map_tovalue() except the entry already exists so we can
  738. * reuse the allocated upb_StringView for string fields. */
  739. if (size == UPB_MAPTYPE_STRING)
  740. {
  741. upb_StringView* strp = (upb_StringView*)(uintptr_t)ent->val.val;
  742. memcpy(strp, val, sizeof(*strp));
  743. }
  744. else
  745. {
  746. memcpy(&ent->val.val, val, size);
  747. }
  748. }
  749. /** _upb_mapsorter ************************************************************/
  750. /* _upb_mapsorter sorts maps and provides ordered iteration over the entries.
  751. * Since maps can be recursive (map values can be messages which contain other
  752. * maps). _upb_mapsorter can contain a stack of maps. */
  753. typedef struct
  754. {
  755. upb_tabent const** entries;
  756. int size;
  757. int cap;
  758. } _upb_mapsorter;
  759. typedef struct
  760. {
  761. int start;
  762. int pos;
  763. int end;
  764. } _upb_sortedmap;
  765. UPB_INLINE void _upb_mapsorter_init(_upb_mapsorter* s)
  766. {
  767. s->entries = NULL;
  768. s->size = 0;
  769. s->cap = 0;
  770. }
  771. UPB_INLINE void _upb_mapsorter_destroy(_upb_mapsorter* s)
  772. {
  773. if (s->entries)
  774. free(s->entries);
  775. }
  776. bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type, const upb_Map* map, _upb_sortedmap* sorted);
  777. UPB_INLINE void _upb_mapsorter_popmap(_upb_mapsorter* s, _upb_sortedmap* sorted)
  778. {
  779. s->size = sorted->start;
  780. }
  781. UPB_INLINE bool _upb_sortedmap_next(_upb_mapsorter* s, const upb_Map* map, _upb_sortedmap* sorted, upb_MapEntry* ent)
  782. {
  783. if (sorted->pos == sorted->end)
  784. return false;
  785. const upb_tabent* tabent = s->entries[sorted->pos++];
  786. upb_StringView key = upb_tabstrview(tabent->key);
  787. _upb_map_fromkey(key, &ent->k, map->key_size);
  788. upb_value val = {tabent->val.val};
  789. _upb_map_fromvalue(val, &ent->v, map->val_size);
  790. return true;
  791. }
  792. #ifdef __cplusplus
  793. } /* extern "C" */
  794. #endif
  795. #include "upb/port_undef.inc"
  796. #endif /* UPB_MSG_INT_H_ */