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

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