google-protobuf 4.36.2-java → 4.37.0.rc.1-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -83,6 +83,19 @@
83
83
  #define UPB_HAS_BUILTIN(x) 0
84
84
  #endif
85
85
 
86
+ // UPB_MEMCPY_INLINE(dst, src, n): memcpy() of a compile-time constant number
87
+ // of bytes `n`, which the compiler never turns into a call to the library
88
+ // memcpy(). A plain memcpy() of a constant size is usually inlined as loads and
89
+ // stores too, but the compiler can merge such copies of different sizes (e.g.
90
+ // the cases of a switch on the size) into one copy of a variable size, which
91
+ // it then can't inline; the merge can even happen before the compiler uses the
92
+ // UPB_ASSUME()s about the size.
93
+ #if UPB_HAS_BUILTIN(__builtin_memcpy_inline)
94
+ #define UPB_MEMCPY_INLINE(dst, src, n) __builtin_memcpy_inline(dst, src, n)
95
+ #else
96
+ #define UPB_MEMCPY_INLINE(dst, src, n) memcpy(dst, src, n)
97
+ #endif
98
+
86
99
  #ifdef __has_extension
87
100
  #define UPB_HAS_EXTENSION(x) __has_extension(x)
88
101
  #else
@@ -128,6 +141,14 @@ Error, UINTPTR_MAX is undefined
128
141
  (((SIZE_MAX - offsetof(type, member[0])) / \
129
142
  (offsetof(type, member[1]) - offsetof(type, member[0]))) < (size_t)count)
130
143
 
144
+ // Inverse of UPB_SIZEOF_FLEX; given the size in memory, how many elements can
145
+ // the flexible array member store?
146
+ #define UPB_FLEX_CAPACITY(type, member, size) \
147
+ ((size) < sizeof(type) \
148
+ ? (size_t)0 \
149
+ : ((size) - offsetof(type, member[0])) / \
150
+ (offsetof(type, member[1]) - offsetof(type, member[0])))
151
+
131
152
  #define UPB_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
132
153
 
133
154
  #define UPB_MAPTYPE_STRING 0
@@ -303,12 +324,11 @@ Error, UINTPTR_MAX is undefined
303
324
  #define UPB_NODEREF
304
325
  #endif
305
326
 
306
- // Will be defined properly once call sites are updated
307
- #if false && UPB_HAS_C_ATTRIBUTE(nodiscard)
327
+ #if UPB_HAS_C_ATTRIBUTE(nodiscard)
308
328
  #define UPB_NODISCARD [[nodiscard]]
309
- #elif false && UPB_HAS_ATTRIBUTE(warn_unused_result)
329
+ #elif UPB_HAS_ATTRIBUTE(warn_unused_result)
310
330
  #define UPB_NODISCARD __attribute__((warn_unused_result))
311
- #elif false && UPB_HAS_CPP_ATTRIBUTE(nodiscard)
331
+ #elif UPB_HAS_CPP_ATTRIBUTE(nodiscard)
312
332
  #define UPB_NODISCARD [[nodiscard]]
313
333
  #else
314
334
  #define UPB_NODISCARD
@@ -494,7 +514,7 @@ Error, UINTPTR_MAX is undefined
494
514
 
495
515
  /* aarch64 supports big and little endian modes; fasttable performs multibyte
496
516
  * tag loads assumes the tag of a varint is in the low bits. */
497
- #if (defined(__x86_64__) || defined(__AARCH64EL__)) && \
517
+ #if !defined(_WIN32) && (defined(__x86_64__) || defined(__AARCH64EL__)) && \
498
518
  UPB_HAS_ATTRIBUTE(preserve_none) && UPB_HAS_ATTRIBUTE(musttail)
499
519
  #define UPB_FASTTABLE_SUPPORTED 1
500
520
  #else
@@ -513,7 +533,11 @@ Error, UINTPTR_MAX is undefined
513
533
  * This is useful for releasing code that might be used on multiple platforms,
514
534
  * for example the PHP or Ruby C extensions. */
515
535
  #elif defined(UPB_TRY_ENABLE_FASTTABLE)
516
- #define UPB_FASTTABLE UPB_FASTTABLE_SUPPORTED
536
+ #if UPB_FASTTABLE_SUPPORTED
537
+ #define UPB_FASTTABLE 1
538
+ #else
539
+ #define UPB_FASTTABLE 0
540
+ #endif
517
541
  #else
518
542
  #define UPB_FASTTABLE 0
519
543
  #endif
@@ -541,6 +565,25 @@ Error, UINTPTR_MAX is undefined
541
565
  #define UPB_DEPRECATED
542
566
  #endif
543
567
 
568
+ #if defined(__clang__)
569
+ #define UPB_IGNORE_DEPRECATION_START \
570
+ _Pragma("clang diagnostic push") \
571
+ _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
572
+ #define UPB_IGNORE_DEPRECATION_STOP _Pragma("clang diagnostic pop")
573
+ #elif defined(__GNUC__)
574
+ #define UPB_IGNORE_DEPRECATION_START \
575
+ _Pragma("GCC diagnostic push") \
576
+ _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
577
+ #define UPB_IGNORE_DEPRECATION_STOP _Pragma("GCC diagnostic pop")
578
+ #elif defined(_MSC_VER)
579
+ #define UPB_IGNORE_DEPRECATION_START \
580
+ __pragma(warning(push)) __pragma(warning(disable : 4996))
581
+ #define UPB_IGNORE_DEPRECATION_STOP __pragma(warning(pop))
582
+ #else
583
+ #define UPB_IGNORE_DEPRECATION_START
584
+ #define UPB_IGNORE_DEPRECATION_STOP
585
+ #endif
586
+
544
587
  #if defined(UPB_IS_GOOGLE3) && \
545
588
  (!defined(UPB_BOOTSTRAP_STAGE) || UPB_BOOTSTRAP_STAGE != 0)
546
589
  #define UPB_DESC_MINITABLE(sym) &proto2__##sym##_msg_init
@@ -567,6 +610,12 @@ Error, UINTPTR_MAX is undefined
567
610
  #define UPB_RETAIN
568
611
  #endif
569
612
 
613
+ #if defined(__GNUC__) || defined(__clang__)
614
+ #define UPB_HIDDEN __attribute__((visibility("hidden")))
615
+ #else
616
+ #define UPB_HIDDEN
617
+ #endif
618
+
570
619
  // Linker arrays combine elements from multiple translation units into a single
571
620
  // array that can be iterated over at runtime.
572
621
  //
@@ -620,7 +669,7 @@ Error, UINTPTR_MAX is undefined
620
669
 
621
670
  #elif defined(__MACH__)
622
671
 
623
- /* As described in: https://stackoverflow.com/a/22366882 */
672
+ /* As described in: https://stackoverflow.com/a/22366882 */
624
673
  #define UPB_LINKARR_APPEND(name) \
625
674
  __attribute__(( \
626
675
  section("__DATA,__la_" #name))) UPB_LINKARR_ATTR UPB_NO_SANITIZE_ADDRESS
@@ -639,10 +688,10 @@ Error, UINTPTR_MAX is undefined
639
688
 
640
689
  #elif defined(_MSC_VER)
641
690
 
642
- /* See:
643
- * https://devblogs.microsoft.com/oldnewthing/20181107-00/?p=100155
644
- * https://devblogs.microsoft.com/oldnewthing/20181108-00/?p=100165
645
- * https://devblogs.microsoft.com/oldnewthing/20181109-00/?p=100175 */
691
+ /* See:
692
+ * https://devblogs.microsoft.com/oldnewthing/20181107-00/?p=100155
693
+ * https://devblogs.microsoft.com/oldnewthing/20181108-00/?p=100165
694
+ * https://devblogs.microsoft.com/oldnewthing/20181109-00/?p=100175 */
646
695
  #define UPB_STRINGIFY_INTERNAL(x) #x
647
696
  #define UPB_STRINGIFY(x) UPB_STRINGIFY_INTERNAL(x)
648
697
  #define UPB_CONCAT(a, b, c) a##b##c
@@ -651,7 +700,7 @@ Error, UINTPTR_MAX is undefined
651
700
  #define UPB_LINKARR_APPEND(name) \
652
701
  __pragma(section(UPB_LINKARR_NAME(name, $j), read)) \
653
702
  __declspec(allocate(UPB_LINKARR_NAME(name, $j)))
654
- // clang-format off
703
+ // clang-format off
655
704
  #define UPB_LINKARR_DECLARE(name, type) \
656
705
  __pragma(message(UPB_LINKARR_NAME(name, $j))) \
657
706
  __pragma(section(UPB_LINKARR_NAME(name, $a), read)) \
@@ -662,13 +711,13 @@ Error, UINTPTR_MAX is undefined
662
711
  type __stop_linkarr_##name; \
663
712
  UPB_LINKARR_APPEND(name) \
664
713
  __declspec(selectany) type UPB_linkarr_internal_empty_##name[1] = {0}
665
- // clang-format on
714
+ // clang-format on
666
715
  #define UPB_LINKARR_START(name) (&__start_linkarr_##name)
667
716
  #define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name)
668
717
 
669
718
  #else
670
719
 
671
- // Linker arrays are not supported on this platform. Make macros no-ops.
720
+ // Linker arrays are not supported on this platform. Make macros no-ops.
672
721
  #define UPB_LINKARR_APPEND(name)
673
722
  #define UPB_LINKARR_DECLARE(name, type) \
674
723
  UPB_STATIC_ASSERT(sizeof("__la_" #name) <= 17, \
@@ -954,6 +1003,58 @@ UPB_INLINE int upb_StringView_Compare(upb_StringView a, upb_StringView b) {
954
1003
 
955
1004
  #include <stddef.h>
956
1005
 
1006
+ #ifndef GOOGLE_UPB_UPB_MEM_INTERNAL_ALLOC_H__
1007
+ #define GOOGLE_UPB_UPB_MEM_INTERNAL_ALLOC_H__
1008
+
1009
+ #include <stddef.h>
1010
+
1011
+ // Must be last.
1012
+
1013
+ #ifdef __cplusplus
1014
+ extern "C" {
1015
+ #endif
1016
+
1017
+ #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \
1018
+ !defined(__STDC_NO_THREADS__)) || \
1019
+ UPB_HAS_EXTENSION(c_thread_local)
1020
+ #define UPB_THREAD_LOCAL _Thread_local
1021
+ #elif defined(_MSC_VER)
1022
+ #define UPB_THREAD_LOCAL __declspec(thread)
1023
+ #elif defined(__GNUC__) || defined(__clang__)
1024
+ #define UPB_THREAD_LOCAL __thread
1025
+ #else
1026
+ #define UPB_THREAD_LOCAL
1027
+ #endif
1028
+
1029
+ #if !defined(NDEBUG)
1030
+ #define UPB_ALLOCATION_COUNT
1031
+ #endif
1032
+
1033
+ #ifdef UPB_ALLOCATION_COUNT
1034
+ extern UPB_THREAD_LOCAL size_t upb_arena_alloc_count;
1035
+ extern UPB_THREAD_LOCAL size_t upb_arena_alloc_fail_on;
1036
+
1037
+ #undef UPB_THREAD_LOCAL
1038
+
1039
+ #endif
1040
+
1041
+ UPB_NODISCARD UPB_FORCEINLINE bool upb_AllocationCount_IncrementAndCheck(void) {
1042
+ #ifdef UPB_ALLOCATION_COUNT
1043
+ bool ok = (upb_arena_alloc_count != upb_arena_alloc_fail_on);
1044
+ upb_arena_alloc_count++;
1045
+ return ok;
1046
+ #else
1047
+ return true;
1048
+ #endif
1049
+ }
1050
+
1051
+ #ifdef __cplusplus
1052
+ } /* extern "C" */
1053
+ #endif
1054
+
1055
+
1056
+ #endif // GOOGLE_UPB_UPB_MEM_INTERNAL_ALLOC_H__
1057
+
957
1058
  // Must be last.
958
1059
 
959
1060
  #ifdef __cplusplus
@@ -972,6 +1073,8 @@ typedef struct upb_alloc upb_alloc;
972
1073
  typedef void* upb_alloc_func(upb_alloc* alloc, void* ptr, size_t oldsize,
973
1074
  size_t size, size_t* actual_size);
974
1075
 
1076
+ typedef void upb_AllocCleanupFunc(upb_alloc* alloc);
1077
+
975
1078
  /* A upb_alloc is a possibly-stateful allocator object.
976
1079
  *
977
1080
  * It could either be an arena allocator (which doesn't require individual
@@ -980,10 +1083,15 @@ typedef void* upb_alloc_func(upb_alloc* alloc, void* ptr, size_t oldsize,
980
1083
  * allocator. */
981
1084
  struct upb_alloc {
982
1085
  upb_alloc_func* func;
1086
+ // If provided, called when an arena that used this upb_alloc has been freed.
1087
+ upb_AllocCleanupFunc* cleanup;
983
1088
  };
984
1089
 
985
1090
  UPB_NODISCARD UPB_INLINE void* upb_malloc(upb_alloc* alloc, size_t size) {
986
1091
  UPB_ASSERT(alloc);
1092
+ if (!upb_AllocationCount_IncrementAndCheck()) {
1093
+ return NULL;
1094
+ }
987
1095
  return alloc->func(alloc, NULL, 0, size, NULL);
988
1096
  }
989
1097
 
@@ -995,6 +1103,11 @@ typedef struct {
995
1103
  UPB_INLINE upb_SizedPtr upb_SizeReturningMalloc(upb_alloc* alloc, size_t size) {
996
1104
  UPB_ASSERT(alloc);
997
1105
  upb_SizedPtr result;
1106
+ if (!upb_AllocationCount_IncrementAndCheck()) {
1107
+ result.p = NULL;
1108
+ result.n = 0;
1109
+ return result;
1110
+ }
998
1111
  result.n = 0;
999
1112
  result.p = alloc->func(alloc, NULL, 0, size, &result.n);
1000
1113
  result.n = result.p != NULL ? UPB_MAX(result.n, size) : 0;
@@ -1004,6 +1117,11 @@ UPB_INLINE upb_SizedPtr upb_SizeReturningMalloc(upb_alloc* alloc, size_t size) {
1004
1117
  UPB_NODISCARD UPB_INLINE void* upb_realloc(upb_alloc* alloc, void* ptr,
1005
1118
  size_t oldsize, size_t size) {
1006
1119
  UPB_ASSERT(alloc);
1120
+ if (size != 0) {
1121
+ if (!upb_AllocationCount_IncrementAndCheck()) {
1122
+ return NULL;
1123
+ }
1124
+ }
1007
1125
  return alloc->func(alloc, ptr, oldsize, size, NULL);
1008
1126
  }
1009
1127
 
@@ -1037,6 +1155,17 @@ UPB_NODISCARD UPB_INLINE void* upb_grealloc(void* ptr, size_t oldsize,
1037
1155
 
1038
1156
  UPB_INLINE void upb_gfree(void* ptr) { upb_free(&upb_alloc_global, ptr); }
1039
1157
 
1158
+ // Returns whether thread-local allocation count/ OOM-simulation features
1159
+ // are supported.
1160
+ UPB_NODISCARD UPB_API bool upb_AllocationCount_IsAvailable(void);
1161
+ // Returns the thread-local allocation count since the last reset.
1162
+ UPB_NODISCARD UPB_API size_t upb_AllocationCount_Get(void);
1163
+ // Resets the thread-local allocation count and failure threshold.
1164
+ UPB_API void upb_AllocationCount_Reset(void);
1165
+ // Artificially triggers memory allocation failure in the thread on the n-th
1166
+ // allocation.
1167
+ UPB_API void upb_AllocationCount_FailOn(size_t n);
1168
+
1040
1169
  #ifdef __cplusplus
1041
1170
  } /* extern "C" */
1042
1171
  #endif
@@ -1052,6 +1181,75 @@ UPB_INLINE void upb_gfree(void* ptr) { upb_free(&upb_alloc_global, ptr); }
1052
1181
  #include <string.h>
1053
1182
 
1054
1183
 
1184
+ #ifndef UPB_BASE_INTERNAL_LOG2_H_
1185
+ #define UPB_BASE_INTERNAL_LOG2_H_
1186
+
1187
+ #include <limits.h>
1188
+ #include <stddef.h>
1189
+ #include <stdint.h>
1190
+
1191
+ // Must be last.
1192
+
1193
+ #ifdef __cplusplus
1194
+ extern "C" {
1195
+ #endif
1196
+
1197
+ // Returns the number of leading 0-bits in x.
1198
+ // x must be non-zero.
1199
+ UPB_INLINE int UPB_PRIVATE(_upb_ClzSize)(size_t x) {
1200
+ UPB_ASSERT(x > 0);
1201
+ #if SIZE_MAX == ULLONG_MAX && UPB_HAS_BUILTIN(__builtin_clzll)
1202
+ return __builtin_clzll(x);
1203
+ #elif SIZE_MAX == ULONG_MAX && UPB_HAS_BUILTIN(__builtin_clzl)
1204
+ return __builtin_clzl(x);
1205
+ #elif SIZE_MAX == UINT_MAX && UPB_HAS_BUILTIN(__builtin_clz)
1206
+ return __builtin_clz(x);
1207
+ #else
1208
+ int count = 0;
1209
+ for (int i = (int)(sizeof(size_t) * CHAR_BIT) - 1; i >= 0; --i) {
1210
+ if ((x >> i) & 1) break;
1211
+ count++;
1212
+ }
1213
+ return count;
1214
+ #endif
1215
+ }
1216
+
1217
+ UPB_INLINE int upb_Log2Ceiling(size_t x) {
1218
+ if (x <= 1) return 0;
1219
+ return (sizeof(size_t) * CHAR_BIT) - UPB_PRIVATE(_upb_ClzSize)(x - 1);
1220
+ }
1221
+
1222
+ UPB_INLINE int upb_Log2Floor(size_t x) {
1223
+ if (x <= 1) return 0;
1224
+ return (sizeof(size_t) * CHAR_BIT) - 1 - UPB_PRIVATE(_upb_ClzSize)(x);
1225
+ }
1226
+
1227
+ // Returns the smallest power of two that is greater than or equal to x. Returns
1228
+ // SIZE_MAX if the computation would overflow.
1229
+ UPB_INLINE size_t upb_RoundUpToPowerOfTwo(size_t x) {
1230
+ int lg2 = upb_Log2Ceiling(x);
1231
+ UPB_ASSERT(lg2 >= 0 && lg2 <= (int)sizeof(size_t) * CHAR_BIT);
1232
+ if (lg2 == sizeof(size_t) * CHAR_BIT) {
1233
+ return SIZE_MAX;
1234
+ }
1235
+ return ((size_t)1) << lg2;
1236
+ }
1237
+
1238
+ UPB_INLINE bool upb_ShlOverflow(size_t* a, unsigned int b) {
1239
+ if (*a > (SIZE_MAX >> b)) {
1240
+ return true;
1241
+ }
1242
+ *a <<= b;
1243
+ return false;
1244
+ }
1245
+
1246
+ #ifdef __cplusplus
1247
+ } /* extern "C" */
1248
+ #endif
1249
+
1250
+
1251
+ #endif /* UPB_BASE_INTERNAL_LOG2_H_ */
1252
+
1055
1253
  #ifndef UPB_PORT_SANITIZERS_H_
1056
1254
  #define UPB_PORT_SANITIZERS_H_
1057
1255
 
@@ -1246,20 +1444,32 @@ UPB_INLINE void UPB_PRIVATE(upb_Xsan_AccessReadWrite)(upb_Xsan *xsan) {
1246
1444
  // We need this because the decoder inlines a upb_Arena for performance but
1247
1445
  // the full struct is not visible outside of arena.c. Yes, I know, it's awful.
1248
1446
  #ifndef NDEBUG
1249
- #define UPB_ARENA_BASE_SIZE_HACK 10
1447
+ #define UPB_ARENA_BASE_SIZE_HACK 11
1250
1448
  #else
1251
- #define UPB_ARENA_BASE_SIZE_HACK 9
1449
+ #define UPB_ARENA_BASE_SIZE_HACK 10
1252
1450
  #endif
1253
1451
 
1254
1452
  #define UPB_ARENA_SIZE_HACK \
1255
1453
  (sizeof(void*) * (UPB_ARENA_BASE_SIZE_HACK + (UPB_XSAN_STRUCT_SIZE * 2))) + \
1256
1454
  (sizeof(uint32_t) * 2)
1257
1455
 
1456
+ typedef struct UPB_PRIVATE(_upb_ArenaFreeBlock) {
1457
+ struct UPB_PRIVATE(_upb_ArenaFreeBlock) * UPB_PRIVATE(next);
1458
+ } UPB_PRIVATE(_upb_ArenaFreeBlock);
1459
+
1460
+ typedef struct UPB_PRIVATE(_upb_ArenaPool) {
1461
+ size_t UPB_PRIVATE(num_bins);
1462
+ UPB_PRIVATE(_upb_ArenaFreeBlock) * UPB_PRIVATE(bins)[];
1463
+ } UPB_PRIVATE(_upb_ArenaPool);
1464
+
1465
+ extern const UPB_PRIVATE(_upb_ArenaPool) UPB_PRIVATE(_upb_Arena_EmptyPool);
1466
+
1258
1467
  // LINT.IfChange(upb_Arena)
1259
1468
 
1260
1469
  struct upb_Arena {
1261
1470
  char* UPB_ONLYBITS(ptr);
1262
1471
  const UPB_NODEREF char* UPB_ONLYBITS(end);
1472
+ UPB_PRIVATE(_upb_ArenaPool) * UPB_ONLYBITS(pool);
1263
1473
  UPB_XSAN_MEMBER
1264
1474
  };
1265
1475
 
@@ -1274,32 +1484,36 @@ void UPB_PRIVATE(_upb_Arena_SwapIn)(struct upb_Arena* des,
1274
1484
  void UPB_PRIVATE(_upb_Arena_SwapOut)(struct upb_Arena* des,
1275
1485
  const struct upb_Arena* src);
1276
1486
 
1277
- UPB_INLINE size_t UPB_PRIVATE(_upb_ArenaHas)(const struct upb_Arena* a) {
1487
+ UPB_NODISCARD UPB_INLINE size_t
1488
+ UPB_PRIVATE(_upb_ArenaHas)(const struct upb_Arena* a) {
1278
1489
  return (size_t)(a->UPB_ONLYBITS(end) - a->UPB_ONLYBITS(ptr));
1279
1490
  }
1280
1491
 
1281
- UPB_INLINE size_t UPB_PRIVATE(_upb_Arena_AllocSpan)(size_t size) {
1492
+ UPB_NODISCARD UPB_INLINE size_t UPB_PRIVATE(_upb_Arena_AllocSpan)(size_t size) {
1282
1493
  return UPB_ALIGN_MALLOC(size) + UPB_PRIVATE(kUpb_Asan_GuardSize);
1283
1494
  }
1284
1495
 
1285
- UPB_INLINE bool UPB_PRIVATE(_upb_Arena_WasLastAllocFromCurrentBlock)(
1286
- const struct upb_Arena* a, void* ptr, size_t size) {
1496
+ UPB_NODISCARD UPB_INLINE bool UPB_PRIVATE(
1497
+ _upb_Arena_WasLastAllocFromCurrentBlock)(const struct upb_Arena* a,
1498
+ void* ptr, size_t size) {
1287
1499
  return UPB_PRIVATE(upb_Xsan_PtrEq)(
1288
1500
  (char*)ptr + UPB_PRIVATE(_upb_Arena_AllocSpan)(size),
1289
1501
  a->UPB_ONLYBITS(ptr));
1290
1502
  }
1291
1503
 
1292
- UPB_INLINE bool UPB_PRIVATE(_upb_Arena_IsAligned)(const void* ptr) {
1504
+ UPB_NODISCARD UPB_INLINE bool UPB_PRIVATE(_upb_Arena_IsAligned)(
1505
+ const void* ptr) {
1293
1506
  return (uintptr_t)ptr % UPB_MALLOC_ALIGN == 0;
1294
1507
  }
1295
1508
 
1296
- UPB_API_INLINE void* upb_Arena_Malloc(struct upb_Arena* a, size_t size) {
1509
+ UPB_NODISCARD UPB_API_INLINE void* _upb_Arena_Malloc_Unchecked(
1510
+ struct upb_Arena* a, size_t size) {
1297
1511
  UPB_PRIVATE(upb_Xsan_AccessReadWrite)(UPB_XSAN(a));
1298
1512
 
1299
1513
  size_t span = UPB_PRIVATE(_upb_Arena_AllocSpan)(size);
1300
1514
 
1301
1515
  if (UPB_UNLIKELY(UPB_PRIVATE(_upb_ArenaHas)(a) < span)) {
1302
- void* UPB_PRIVATE(_upb_Arena_SlowMalloc)(struct upb_Arena * a, size_t size);
1516
+ void* UPB_PRIVATE(_upb_Arena_SlowMalloc)(struct upb_Arena * a, size_t span);
1303
1517
  return UPB_PRIVATE(_upb_Arena_SlowMalloc)(a, span);
1304
1518
  }
1305
1519
 
@@ -1312,6 +1526,14 @@ UPB_API_INLINE void* upb_Arena_Malloc(struct upb_Arena* a, size_t size) {
1312
1526
  return UPB_PRIVATE(upb_Xsan_NewUnpoisonedRegion)(UPB_XSAN(a), ret, size);
1313
1527
  }
1314
1528
 
1529
+ UPB_NODISCARD UPB_API_INLINE void* upb_Arena_Malloc(struct upb_Arena* a,
1530
+ size_t size) {
1531
+ if (!upb_AllocationCount_IncrementAndCheck()) {
1532
+ return NULL;
1533
+ }
1534
+ return _upb_Arena_Malloc_Unchecked(a, size);
1535
+ }
1536
+
1315
1537
  UPB_API_INLINE void upb_Arena_ShrinkLast(struct upb_Arena* a, void* ptr,
1316
1538
  size_t oldsize, size_t size) {
1317
1539
  UPB_ASSERT(ptr);
@@ -1334,8 +1556,9 @@ UPB_API_INLINE void upb_Arena_ShrinkLast(struct upb_Arena* a, void* ptr,
1334
1556
  }
1335
1557
  }
1336
1558
 
1337
- UPB_API_INLINE bool upb_Arena_TryExtend(struct upb_Arena* a, void* ptr,
1338
- size_t oldsize, size_t size) {
1559
+ UPB_NODISCARD UPB_API_INLINE bool upb_Arena_TryExtend(struct upb_Arena* a,
1560
+ void* ptr, size_t oldsize,
1561
+ size_t size) {
1339
1562
  UPB_ASSERT(ptr);
1340
1563
  UPB_ASSERT(size > oldsize);
1341
1564
 
@@ -1352,8 +1575,9 @@ UPB_API_INLINE bool upb_Arena_TryExtend(struct upb_Arena* a, void* ptr,
1352
1575
  return false;
1353
1576
  }
1354
1577
 
1355
- UPB_API_INLINE void* upb_Arena_Realloc(struct upb_Arena* a, void* ptr,
1356
- size_t oldsize, size_t size) {
1578
+ UPB_NODISCARD UPB_API_INLINE void* upb_Arena_Realloc(struct upb_Arena* a,
1579
+ void* ptr, size_t oldsize,
1580
+ size_t size) {
1357
1581
  UPB_PRIVATE(upb_Xsan_AccessReadWrite)(UPB_XSAN(a));
1358
1582
 
1359
1583
  void* ret;
@@ -1383,6 +1607,124 @@ UPB_API_INLINE void* upb_Arena_Realloc(struct upb_Arena* a, void* ptr,
1383
1607
  return ret;
1384
1608
  }
1385
1609
 
1610
+ // The minimum power-of-2 size class managed by the arena free pool.
1611
+ // A pooled block must be at least large enough to store an initial
1612
+ // _upb_ArenaPool struct with at least 1 bin (sizeof(size_t) + sizeof(void*)).
1613
+ #define _UPB_ARENA_MIN_POOL_BLOCK_SIZE (sizeof(void*) * 2)
1614
+
1615
+ #define _UPB_ARENA_MIN_POOL_BIN_LG2 \
1616
+ (_UPB_ARENA_MIN_POOL_BLOCK_SIZE == 16 ? (size_t)4 : (size_t)3)
1617
+
1618
+ #if defined(_MSC_VER) && !defined(_CRT_USE_BUILTIN_OFFSETOF)
1619
+ // Note: We cannot use UPB_SIZEOF_FLEX here because on MSVC, offsetof() is not
1620
+ // considered an Integer Constant Expression by default.
1621
+ UPB_STATIC_ASSERT(_UPB_ARENA_MIN_POOL_BLOCK_SIZE >=
1622
+ sizeof(UPB_PRIVATE(_upb_ArenaPool)) +
1623
+ sizeof(UPB_PRIVATE(_upb_ArenaFreeBlock) *),
1624
+ "Minimum pool block size must be large enough to host an "
1625
+ "initial pool struct");
1626
+ #else
1627
+ UPB_STATIC_ASSERT(_UPB_ARENA_MIN_POOL_BLOCK_SIZE >=
1628
+ UPB_SIZEOF_FLEX(UPB_PRIVATE(_upb_ArenaPool),
1629
+ UPB_PRIVATE(bins), 1),
1630
+ "Minimum pool block size must be large enough to host an "
1631
+ "initial pool struct");
1632
+ #endif
1633
+
1634
+ UPB_STATIC_ASSERT(_UPB_ARENA_MIN_POOL_BLOCK_SIZE >=
1635
+ sizeof(UPB_PRIVATE(_upb_ArenaFreeBlock)),
1636
+ "Minimum pool block size must be large enough to host a "
1637
+ "free block struct");
1638
+
1639
+ UPB_STATIC_ASSERT(((size_t)1 << _UPB_ARENA_MIN_POOL_BIN_LG2) ==
1640
+ _UPB_ARENA_MIN_POOL_BLOCK_SIZE,
1641
+ "_UPB_ARENA_MIN_POOL_BIN_LG2 must match "
1642
+ "_UPB_ARENA_MIN_POOL_BLOCK_SIZE");
1643
+
1644
+ UPB_INLINE bool UPB_PRIVATE(_upb_Arena_IsValidPoolSize)(size_t size) {
1645
+ return size != 0 &&
1646
+ (size & ((size - 1) | (_UPB_ARENA_MIN_POOL_BLOCK_SIZE - 1))) == 0;
1647
+ }
1648
+
1649
+ // Note that values below the minimum poolable block size will underflow, so
1650
+ // only a single branch comparing to the current bin count is necessary to check
1651
+ // bounds.
1652
+ UPB_INLINE size_t UPB_PRIVATE(_upb_Arena_PoolBinIndex)(size_t pool_size) {
1653
+ return (size_t)upb_Log2Ceiling(pool_size) - _UPB_ARENA_MIN_POOL_BIN_LG2;
1654
+ }
1655
+
1656
+ void UPB_PRIVATE(_upb_Arena_GrowPool)(struct upb_Arena* a, void* ptr,
1657
+ size_t size);
1658
+
1659
+ UPB_NODISCARD UPB_API_INLINE void* upb_Arena_TryAllocPool(struct upb_Arena* a,
1660
+ size_t pool_size) {
1661
+ UPB_ASSERT(a);
1662
+ UPB_ASSERT(UPB_PRIVATE(_upb_Arena_IsValidPoolSize)(pool_size));
1663
+
1664
+ size_t bin = UPB_PRIVATE(_upb_Arena_PoolBinIndex)(pool_size);
1665
+ UPB_PRIVATE(_upb_ArenaPool)* pool = a->UPB_ONLYBITS(pool);
1666
+ if (UPB_LIKELY(bin < pool->UPB_PRIVATE(num_bins))) {
1667
+ UPB_PRIVATE(_upb_ArenaFreeBlock)* block = pool->UPB_PRIVATE(bins)[bin];
1668
+ if (UPB_LIKELY(block != NULL)) {
1669
+ pool->UPB_PRIVATE(bins)[bin] = block->UPB_PRIVATE(next);
1670
+ return UPB_PRIVATE(upb_Xsan_NewUnpoisonedRegion)(UPB_XSAN(a), block,
1671
+ pool_size);
1672
+ }
1673
+ }
1674
+
1675
+ return NULL;
1676
+ }
1677
+
1678
+ UPB_NODISCARD UPB_API_INLINE void* upb_Arena_AllocPool(struct upb_Arena* a,
1679
+ size_t pool_size) {
1680
+ if (!upb_AllocationCount_IncrementAndCheck()) {
1681
+ return NULL;
1682
+ }
1683
+ void* ptr = upb_Arena_TryAllocPool(a, pool_size);
1684
+ if (ptr) return ptr;
1685
+ return _upb_Arena_Malloc_Unchecked(a, pool_size);
1686
+ }
1687
+
1688
+ UPB_API_INLINE void upb_Arena_FreePool(struct upb_Arena* a, void* ptr,
1689
+ size_t pool_size) {
1690
+ UPB_ASSERT(a);
1691
+ UPB_ASSERT(ptr);
1692
+ UPB_ASSERT(UPB_PRIVATE(_upb_Arena_IsValidPoolSize)(pool_size));
1693
+
1694
+ size_t bin = UPB_PRIVATE(_upb_Arena_PoolBinIndex)(pool_size);
1695
+ UPB_PRIVATE(_upb_ArenaPool)* pool = a->UPB_ONLYBITS(pool);
1696
+
1697
+ if (UPB_UNLIKELY(bin >= pool->UPB_PRIVATE(num_bins))) {
1698
+ UPB_PRIVATE(_upb_Arena_GrowPool)(a, ptr, pool_size);
1699
+ return;
1700
+ }
1701
+
1702
+ UPB_ASSERT(UPB_PRIVATE(_upb_Arena_IsAligned)(ptr));
1703
+ UPB_PRIVATE(upb_Xsan_PoisonRegion)(ptr, pool_size);
1704
+ UPB_PRIVATE(_upb_ArenaFreeBlock)* block =
1705
+ (UPB_PRIVATE(_upb_ArenaFreeBlock)*)UPB_PRIVATE(
1706
+ upb_Xsan_NewUnpoisonedRegion)(
1707
+ UPB_XSAN(a), ptr, sizeof(UPB_PRIVATE(_upb_ArenaFreeBlock)));
1708
+ block->UPB_PRIVATE(next) = pool->UPB_PRIVATE(bins)[bin];
1709
+ pool->UPB_PRIVATE(bins)[bin] = block;
1710
+ }
1711
+
1712
+ // Harvests power-of-2 sized blocks from the given memory region into the arena
1713
+ // pool. The region must be aligned to UPB_MALLOC_ALIGN. To limit fragmentation,
1714
+ // we harvest the largest blocks first. This uses a single CLZ instruction per
1715
+ // block, making it nearly as fast as checking contiguous bounds when
1716
+ // harvesting a single perfectly sized power-of-2 block.
1717
+ UPB_INLINE void UPB_PRIVATE(_upb_Arena_Harvest)(struct upb_Arena* a, void* ptr,
1718
+ size_t size) {
1719
+ size_t remaining = size & ~((size_t)_UPB_ARENA_MIN_POOL_BLOCK_SIZE - 1);
1720
+ while (remaining != 0) {
1721
+ size_t harvest_size = (size_t)1 << upb_Log2Floor(remaining);
1722
+ upb_Arena_FreePool(a, ptr, harvest_size);
1723
+ ptr = (char*)ptr + harvest_size;
1724
+ remaining ^= harvest_size;
1725
+ }
1726
+ }
1727
+
1386
1728
  // Returns the next block size to allocate for the arena based on exponential
1387
1729
  // growth and size hint.
1388
1730
  size_t UPB_PRIVATE(_upb_Arena_NextBlockSize)(struct upb_Arena* a, size_t span,
@@ -1446,8 +1788,6 @@ void* UPB_PRIVATE(_upb_Arena_Steal)(struct upb_Arena* a, size_t* size);
1446
1788
 
1447
1789
  typedef struct upb_Arena upb_Arena;
1448
1790
 
1449
- typedef void upb_AllocCleanupFunc(upb_alloc* alloc);
1450
-
1451
1791
  #ifdef __cplusplus
1452
1792
  extern "C" {
1453
1793
  #endif
@@ -1464,18 +1804,12 @@ UPB_NODISCARD UPB_API upb_Arena* upb_Arena_Init(void* mem, size_t n,
1464
1804
  upb_alloc* alloc);
1465
1805
 
1466
1806
  UPB_API void upb_Arena_Free(upb_Arena* a);
1467
- // Sets the cleanup function for the upb_alloc used by the arena. Only one
1468
- // cleanup function can be set, which will be called after all blocks are
1469
- // freed.
1470
- UPB_API void upb_Arena_SetAllocCleanup(upb_Arena* a,
1471
- upb_AllocCleanupFunc* func);
1472
1807
 
1473
1808
  // Fuses the lifetime of two arenas, such that no arenas that have been
1474
1809
  // transitively fused together will be freed until all of them have reached a
1475
1810
  // zero refcount. This operation is safe to use concurrently from multiple
1476
1811
  // threads.
1477
- UPB_NODISCARD UPB_API bool upb_Arena_Fuse(const upb_Arena* a,
1478
- const upb_Arena* b);
1812
+ UPB_API bool upb_Arena_Fuse(const upb_Arena* a, const upb_Arena* b);
1479
1813
 
1480
1814
  // This operation is safe to use concurrently from multiple threads.
1481
1815
  UPB_API bool upb_Arena_IsFused(const upb_Arena* a, const upb_Arena* b);
@@ -1564,6 +1898,26 @@ UPB_NODISCARD UPB_API_INLINE void* upb_Arena_Realloc(upb_Arena* a, void* ptr,
1564
1898
  size_t oldsize,
1565
1899
  size_t size);
1566
1900
 
1901
+ // Attempts to allocate memory from the arena's power-of-2 free pool.
1902
+ // Returns a recycled block of `size` bytes if available in the pool,
1903
+ // or NULL if the pool has no available block of that size.
1904
+ // `size` must be a power of 2 and >= UPB_PRIVATE(kUpb_Arena_MinPoolBlockSize).
1905
+ UPB_NODISCARD UPB_API_INLINE void* upb_Arena_TryAllocPool(upb_Arena* a,
1906
+ size_t size);
1907
+
1908
+ // Allocates memory of `size` bytes, attempting to reuse a recycled block from
1909
+ // the arena's power-of-2 free pool first, and falling back to arena allocation
1910
+ // if no pooled block is available.
1911
+ // `size` must be a power of 2 and >= UPB_PRIVATE(kUpb_Arena_MinPoolBlockSize).
1912
+ UPB_NODISCARD UPB_API_INLINE void* upb_Arena_AllocPool(upb_Arena* a,
1913
+ size_t size);
1914
+
1915
+ // Returns a block of memory to the arena's free pool.
1916
+ UPB_API_INLINE void upb_Arena_FreePool(upb_Arena* a, void* ptr, size_t size);
1917
+
1918
+ static const size_t UPB_PRIVATE(kUpb_Arena_MinPoolBlockSize) =
1919
+ _UPB_ARENA_MIN_POOL_BLOCK_SIZE;
1920
+
1567
1921
  static const size_t UPB_PRIVATE(kUpbDefaultMaxBlockSize) =
1568
1922
  UPB_DEFAULT_MAX_BLOCK_SIZE;
1569
1923
 
@@ -1765,6 +2119,7 @@ UPB_API_INLINE bool upb_Array_IsFrozen(const struct upb_Array* arr) {
1765
2119
 
1766
2120
  UPB_INLINE void UPB_PRIVATE(_upb_Array_SetTaggedPtr)(struct upb_Array* array,
1767
2121
  void* data, size_t lg2) {
2122
+ UPB_ASSERT(data);
1768
2123
  UPB_ASSERT(lg2 != 1);
1769
2124
  UPB_ASSERT(lg2 <= 4);
1770
2125
  const size_t bits = lg2 - (lg2 != 0);
@@ -1778,6 +2133,27 @@ UPB_PRIVATE(_upb_Array_ElemSizeLg2)(const struct upb_Array* array) {
1778
2133
  return lg2;
1779
2134
  }
1780
2135
 
2136
+ // Copies an element of 1 << lg2 bytes (see _upb_Array_ElemSizeLg2()), without
2137
+ // calling the library memcpy() for its variable size.
2138
+ UPB_INLINE void UPB_PRIVATE(_upb_Array_CopyElem)(void* to, const void* from,
2139
+ size_t lg2) {
2140
+ switch (lg2) {
2141
+ case 0:
2142
+ UPB_MEMCPY_INLINE(to, from, 1);
2143
+ return;
2144
+ case 2:
2145
+ UPB_MEMCPY_INLINE(to, from, 4);
2146
+ return;
2147
+ case 3:
2148
+ UPB_MEMCPY_INLINE(to, from, 8);
2149
+ return;
2150
+ case 4:
2151
+ UPB_MEMCPY_INLINE(to, from, 16);
2152
+ return;
2153
+ }
2154
+ UPB_UNREACHABLE();
2155
+ }
2156
+
1781
2157
  UPB_API_INLINE const void* upb_Array_DataPtr(const struct upb_Array* array) {
1782
2158
  UPB_PRIVATE(_upb_Array_ElemSizeLg2)(array); // Check assertions.
1783
2159
  return (void*)(array->UPB_ONLYBITS(data) & ~(uintptr_t)_UPB_ARRAY_MASK_ALL);
@@ -1787,14 +2163,42 @@ UPB_API_INLINE void* upb_Array_MutableDataPtr(struct upb_Array* array) {
1787
2163
  return (void*)upb_Array_DataPtr(array);
1788
2164
  }
1789
2165
 
2166
+ // LINT.ThenChange(GoogleInternalName0)
2167
+
1790
2168
  UPB_NODISCARD UPB_INLINE struct upb_Array* UPB_PRIVATE(
1791
2169
  _upb_Array_NewMaybeAllowSlow)(upb_Arena* arena, size_t init_capacity,
1792
2170
  int elem_size_lg2, bool allow_slow) {
1793
2171
  UPB_ASSERT(elem_size_lg2 != 1);
1794
2172
  UPB_ASSERT(elem_size_lg2 <= 4);
2173
+
2174
+ const size_t elem_bytes = init_capacity << elem_size_lg2;
2175
+
2176
+ // Try to obtain a recycled backing buffer from the arena pool first.
2177
+ if (init_capacity > 0) {
2178
+ const size_t pool_bytes = upb_RoundUpToPowerOfTwo(
2179
+ UPB_MAX(elem_bytes, UPB_PRIVATE(kUpb_Arena_MinPoolBlockSize)));
2180
+ if (pool_bytes != SIZE_MAX) {
2181
+ void* data = upb_Arena_TryAllocPool(arena, pool_bytes);
2182
+ if (data) {
2183
+ struct upb_Array* array = (struct upb_Array*)upb_Arena_Malloc(
2184
+ arena, sizeof(struct upb_Array));
2185
+ if (!array) {
2186
+ upb_Arena_FreePool(arena, data, pool_bytes);
2187
+ return NULL;
2188
+ }
2189
+ UPB_PRIVATE(_upb_Array_SetTaggedPtr)(array, data,
2190
+ (size_t)elem_size_lg2);
2191
+ array->UPB_ONLYBITS(size) = 0;
2192
+ array->UPB_PRIVATE(capacity) = pool_bytes >> elem_size_lg2;
2193
+ return array;
2194
+ }
2195
+ }
2196
+ }
2197
+
2198
+ // Pool miss: perform contiguous single allocation for header + initial data.
1795
2199
  const size_t array_size =
1796
2200
  UPB_ALIGN_UP(sizeof(struct upb_Array), UPB_MALLOC_ALIGN);
1797
- const size_t bytes = array_size + (init_capacity << elem_size_lg2);
2201
+ const size_t bytes = array_size + elem_bytes;
1798
2202
  size_t span = UPB_PRIVATE(_upb_Arena_AllocSpan)(bytes);
1799
2203
  if (!allow_slow && UPB_PRIVATE(_upb_ArenaHas)(arena) < span) return NULL;
1800
2204
  struct upb_Array* array = (struct upb_Array*)upb_Arena_Malloc(arena, bytes);
@@ -1875,8 +2279,6 @@ UPB_API_INLINE size_t upb_Array_Capacity(const struct upb_Array* arr) {
1875
2279
  return arr->UPB_PRIVATE(capacity);
1876
2280
  }
1877
2281
 
1878
- // LINT.ThenChange(GoogleInternalName0)
1879
-
1880
2282
  #ifdef __cplusplus
1881
2283
  } /* extern "C" */
1882
2284
  #endif
@@ -1897,6 +2299,13 @@ UPB_API_INLINE size_t upb_Array_Capacity(const struct upb_Array* arr) {
1897
2299
 
1898
2300
  #define UPB_OPAQUE(x) x##_opaque
1899
2301
 
2302
+ typedef enum {
2303
+ kUpb_TaggedAuxType_Unknown = 0, // tag 000
2304
+ kUpb_TaggedAuxType_NonCanonicalExtension = 1, // tag 001
2305
+ kUpb_TaggedAuxType_CanonicalExtension = 3, // tag 011
2306
+ kUpb_TaggedAuxType_AliasedUnknown = 4 // tag 100
2307
+ } upb_TaggedAuxType;
2308
+
1900
2309
  struct upb_Message {
1901
2310
  union {
1902
2311
  uintptr_t UPB_OPAQUE(internal); // tagged pointer, low bit == frozen
@@ -3150,58 +3559,151 @@ UPB_INLINE uint64_t upb_BigEndian64(uint64_t val) {
3150
3559
  #ifndef UPB_MESSAGE_INTERNAL_EXTENSION_H_
3151
3560
  #define UPB_MESSAGE_INTERNAL_EXTENSION_H_
3152
3561
 
3153
- #include <stddef.h>
3154
-
3155
-
3156
- #ifndef UPB_MESSAGE_INTERNAL_MAP_H_
3157
- #define UPB_MESSAGE_INTERNAL_MAP_H_
3158
-
3562
+ #include <stdbool.h>
3159
3563
  #include <stddef.h>
3160
3564
  #include <stdint.h>
3161
3565
  #include <string.h>
3162
3566
 
3163
3567
 
3164
- /*
3165
- * upb_table
3166
- *
3167
- * This header is INTERNAL-ONLY! Its interfaces are not public or stable!
3168
- * This file defines very fast int->upb_value (inttable) and string->upb_value
3169
- * (strtable) hash tables.
3170
- *
3171
- * The table uses chained scatter with Brent's variation (inspired by the Lua
3172
- * implementation of hash tables). The hash function for strings is Austin
3173
- * Appleby's "MurmurHash."
3174
- *
3175
- * The inttable uses uintptr_t as its key, which guarantees it can be used to
3176
- * store pointers or integers of at least 32 bits (upb isn't really useful on
3177
- * systems where sizeof(void*) < 4).
3178
- *
3179
- * The table must be homogeneous (all values of the same type). In debug
3180
- * mode, we check this on insert and lookup.
3181
- */
3182
-
3183
- #ifndef UPB_HASH_COMMON_H_
3184
- #define UPB_HASH_COMMON_H_
3568
+ #ifndef UPB_MESSAGE_INTERNAL_FIELD_DATA_H_
3569
+ #define UPB_MESSAGE_INTERNAL_FIELD_DATA_H_
3185
3570
 
3186
- #include <stdint.h>
3571
+ #include <stdbool.h>
3187
3572
  #include <string.h>
3188
3573
 
3189
3574
 
3190
3575
  // Must be last.
3191
3576
 
3577
+ // Helpers that copy, compare, and clear the value of a field given only its
3578
+ // representation. They are shared by message fields (accessors.h) and
3579
+ // extensions (extension.h), which differ only in where the value is stored.
3580
+
3581
+ #if defined(__GNUC__) && !defined(__clang__)
3582
+ // See the comment in accessors.h: GCC raises incorrect buffer overrun warnings
3583
+ // for these functions because it fails to constant-propagate the field rep.
3584
+ #pragma GCC diagnostic push
3585
+ #pragma GCC diagnostic ignored "-Warray-bounds"
3586
+ #pragma GCC diagnostic ignored "-Wstringop-overflow"
3587
+ #if __GNUC__ >= 11
3588
+ #pragma GCC diagnostic ignored "-Wstringop-overread"
3589
+ #endif
3590
+ #endif
3591
+
3192
3592
  #ifdef __cplusplus
3193
3593
  extern "C" {
3194
3594
  #endif
3195
3595
 
3196
- /* upb_value ******************************************************************/
3197
-
3198
- typedef struct {
3199
- uint64_t val;
3200
- } upb_value;
3596
+ UPB_INLINE_IF_NOT_GCC void UPB_PRIVATE(_upb_MiniTableField_DataCopy)(
3597
+ const upb_MiniTableField* f, void* to, const void* from) {
3598
+ switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) {
3599
+ case kUpb_FieldRep_1Byte:
3600
+ UPB_MEMCPY_INLINE(to, from, 1);
3601
+ return;
3602
+ case kUpb_FieldRep_4Byte:
3603
+ UPB_MEMCPY_INLINE(to, from, 4);
3604
+ return;
3605
+ case kUpb_FieldRep_8Byte:
3606
+ UPB_MEMCPY_INLINE(to, from, 8);
3607
+ return;
3608
+ case kUpb_FieldRep_StringView: {
3609
+ UPB_MEMCPY_INLINE(to, from, sizeof(upb_StringView));
3610
+ return;
3611
+ }
3612
+ }
3613
+ UPB_UNREACHABLE();
3614
+ }
3201
3615
 
3202
- /* For each value ctype, define the following set of functions:
3203
- *
3204
- * // Get/set an int32 from a upb_value.
3616
+ UPB_INLINE_IF_NOT_GCC bool UPB_PRIVATE(_upb_MiniTableField_DataEquals)(
3617
+ const upb_MiniTableField* f, const void* a, const void* b) {
3618
+ switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) {
3619
+ case kUpb_FieldRep_1Byte:
3620
+ return memcmp(a, b, 1) == 0;
3621
+ case kUpb_FieldRep_4Byte:
3622
+ return memcmp(a, b, 4) == 0;
3623
+ case kUpb_FieldRep_8Byte:
3624
+ return memcmp(a, b, 8) == 0;
3625
+ case kUpb_FieldRep_StringView: {
3626
+ const upb_StringView sa = *(const upb_StringView*)a;
3627
+ const upb_StringView sb = *(const upb_StringView*)b;
3628
+ return upb_StringView_IsEqual(sa, sb);
3629
+ }
3630
+ }
3631
+ UPB_UNREACHABLE();
3632
+ }
3633
+
3634
+ UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_DataClear)(
3635
+ const upb_MiniTableField* f, void* val) {
3636
+ UPB_ALIGN_AS(8) const char zero[16] = {0};
3637
+ UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, val, zero);
3638
+ }
3639
+
3640
+ UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_DataIsZero)(
3641
+ const upb_MiniTableField* f, const void* val) {
3642
+ UPB_ALIGN_AS(8) const char zero[16] = {0};
3643
+ return UPB_PRIVATE(_upb_MiniTableField_DataEquals)(f, val, zero);
3644
+ }
3645
+
3646
+ #ifdef __cplusplus
3647
+ } /* extern "C" */
3648
+ #endif
3649
+
3650
+ #if defined(__GNUC__) && !defined(__clang__)
3651
+ #pragma GCC diagnostic pop
3652
+ #endif
3653
+
3654
+
3655
+ #endif // UPB_MESSAGE_INTERNAL_FIELD_DATA_H_
3656
+
3657
+ #ifndef UPB_MESSAGE_INTERNAL_MAP_H_
3658
+ #define UPB_MESSAGE_INTERNAL_MAP_H_
3659
+
3660
+ #include <stddef.h>
3661
+ #include <stdint.h>
3662
+ #include <string.h>
3663
+
3664
+
3665
+ /*
3666
+ * upb_table
3667
+ *
3668
+ * This header is INTERNAL-ONLY! Its interfaces are not public or stable!
3669
+ * This file defines very fast int->upb_value (inttable) and string->upb_value
3670
+ * (strtable) hash tables.
3671
+ *
3672
+ * The table uses chained scatter with Brent's variation (inspired by the Lua
3673
+ * implementation of hash tables). The hash function for strings is Austin
3674
+ * Appleby's "MurmurHash."
3675
+ *
3676
+ * The inttable uses uintptr_t as its key, which guarantees it can be used to
3677
+ * store pointers or integers of at least 32 bits (upb isn't really useful on
3678
+ * systems where sizeof(void*) < 4).
3679
+ *
3680
+ * The table must be homogeneous (all values of the same type). In debug
3681
+ * mode, we check this on insert and lookup.
3682
+ */
3683
+
3684
+ #ifndef UPB_HASH_COMMON_H_
3685
+ #define UPB_HASH_COMMON_H_
3686
+
3687
+ #include <stddef.h>
3688
+ #include <stdint.h>
3689
+ #include <string.h>
3690
+
3691
+
3692
+ // Must be last.
3693
+
3694
+ #ifdef __cplusplus
3695
+ extern "C" {
3696
+ #endif
3697
+
3698
+ /* upb_value ******************************************************************/
3699
+
3700
+ typedef struct {
3701
+ uint64_t val;
3702
+ } upb_value;
3703
+
3704
+ /* For each value ctype, define the following set of functions:
3705
+ *
3706
+ * // Get/set an int32 from a upb_value.
3205
3707
  * int32_t upb_value_getint32(upb_value val);
3206
3708
  * void upb_value_setint32(upb_value *val, int32_t cval);
3207
3709
  *
@@ -3300,6 +3802,16 @@ typedef struct {
3300
3802
 
3301
3803
  UPB_INLINE size_t upb_table_size(const upb_table* t) { return t->mask + 1; }
3302
3804
 
3805
+ UPB_INLINE upb_tabent* upb_getentry(const upb_table* t, uint32_t hash) {
3806
+ return t->entries + (hash & t->mask);
3807
+ }
3808
+
3809
+ UPB_INLINE bool upb_table_isfull(const upb_table* t) {
3810
+ uint32_t size = upb_table_size(t);
3811
+ // 0.875 load factor
3812
+ return t->count == (size - (size >> 3));
3813
+ }
3814
+
3303
3815
  // Internal-only functions, in .h file only out of necessity.
3304
3816
 
3305
3817
  UPB_INLINE bool upb_tabent_isempty(const upb_tabent* e) { return e->next == 0; }
@@ -3338,6 +3850,14 @@ UPB_INLINE void upb_tabent_setnext(upb_tabent* e, upb_tabent* next) {
3338
3850
 
3339
3851
  uint32_t _upb_Hash(const void* p, size_t n, uint64_t seed);
3340
3852
 
3853
+ /** Calculates the number of entries required to hold an expected number of
3854
+ * values, within the table's load factor (0.875). */
3855
+ UPB_INLINE size_t _upb_entries_needed_for(size_t expected_size) {
3856
+ size_t need_entries = expected_size + 1 + expected_size / 7;
3857
+ UPB_ASSERT(need_entries - (need_entries >> 3) >= expected_size);
3858
+ return need_entries;
3859
+ }
3860
+
3341
3861
  #ifdef __cplusplus
3342
3862
  } /* extern "C" */
3343
3863
  #endif
@@ -3367,7 +3887,9 @@ extern "C" {
3367
3887
  UPB_NODISCARD bool upb_inttable_init(upb_inttable* table, upb_Arena* a);
3368
3888
 
3369
3889
  // Returns the number of values in the table.
3370
- size_t upb_inttable_count(const upb_inttable* t);
3890
+ UPB_INLINE size_t upb_inttable_count(const upb_inttable* t) {
3891
+ return t->t.count;
3892
+ }
3371
3893
 
3372
3894
  // Inserts the given key into the hashtable with the given value.
3373
3895
  // The key must not already exist in the hash table.
@@ -3377,6 +3899,57 @@ size_t upb_inttable_count(const upb_inttable* t);
3377
3899
  UPB_NODISCARD bool upb_inttable_insert(upb_inttable* t, uintptr_t key,
3378
3900
  upb_value val, upb_Arena* a);
3379
3901
 
3902
+ UPB_NODISCARD bool upb_inttable_insert_or_replace_slow(upb_inttable* t,
3903
+ uintptr_t key,
3904
+ upb_value val,
3905
+ bool* replaced,
3906
+ upb_Arena* a);
3907
+
3908
+ UPB_INLINE uint32_t upb_inthash(uintptr_t key) {
3909
+ UPB_STATIC_ASSERT(sizeof(uintptr_t) == 4 || sizeof(uintptr_t) == 8,
3910
+ "Pointers don't fit");
3911
+ if (sizeof(uintptr_t) == 8) {
3912
+ return (uint32_t)key ^ (uint32_t)((uint64_t)key >> 32);
3913
+ } else {
3914
+ return (uint32_t)key;
3915
+ }
3916
+ }
3917
+
3918
+ // Inserts or updates the given key with the given value. Sets *replaced to
3919
+ // true if an existing entry was updated, or false if a new entry was inserted.
3920
+ // Returns false if a table resize was required and memory allocation failed.
3921
+ UPB_NODISCARD UPB_FORCEINLINE bool upb_inttable_insert_or_replace(
3922
+ upb_inttable* t, uintptr_t key, upb_value val, bool* replaced,
3923
+ upb_Arena* a) {
3924
+ if (UPB_LIKELY(t->t.entries != NULL)) {
3925
+ upb_tabent* e = upb_getentry(&t->t, upb_inthash(key));
3926
+ if (UPB_LIKELY(upb_tabent_isempty(e) && !upb_table_isfull(&t->t))) {
3927
+ t->t.count++;
3928
+ e->key.num = key;
3929
+ e->val = val;
3930
+ upb_tabent_clearnext(e);
3931
+ *replaced = false;
3932
+ return true;
3933
+ }
3934
+ }
3935
+ // Use a separate temporary so that `replaced` does not escape on the fast
3936
+ // path above. It must be initialized because the slow path leaves it unset
3937
+ // on allocation failure.
3938
+ bool slow_replaced = false;
3939
+ bool ok = upb_inttable_insert_or_replace_slow(t, key, val, &slow_replaced, a);
3940
+ *replaced = slow_replaced;
3941
+ return ok;
3942
+ }
3943
+
3944
+ // Resizes the table to 1 << size_lg2.
3945
+ UPB_NODISCARD bool upb_inttable_resize(upb_inttable* t, size_t size_lg2,
3946
+ upb_Arena* a);
3947
+
3948
+ // Copies the table without rehashing. Performing a shallow copy of entries;
3949
+ // the caller is responsible for cloning non-primitive values.
3950
+ bool upb_inttable_copy(upb_inttable* dest, const upb_inttable* src,
3951
+ upb_Arena* a);
3952
+
3380
3953
  // Looks up key in this table, returning "true" if the key was found.
3381
3954
  // If v is non-NULL, copies the value for this key into *v.
3382
3955
  bool upb_inttable_lookup(const upb_inttable* t, uintptr_t key, upb_value* v);
@@ -3458,6 +4031,19 @@ void upb_strtable_clear(upb_strtable* t);
3458
4031
  UPB_NODISCARD bool upb_strtable_insert(upb_strtable* t, const char* key,
3459
4032
  size_t len, upb_value val, upb_Arena* a);
3460
4033
 
4034
+ // Inserts or updates the given key with the given value. Sets *replaced to
4035
+ // true if an existing entry was updated, or false if a new entry was inserted.
4036
+ // Returns false if memory allocation failed.
4037
+ UPB_NODISCARD bool upb_strtable_insert_or_replace(upb_strtable* t,
4038
+ const char* key, size_t len,
4039
+ upb_value val, bool* replaced,
4040
+ upb_Arena* a);
4041
+
4042
+ // Copies the table and its keys without rehashing. Performing a shallow copy of
4043
+ // entries; the caller is responsible for cloning non-primitive values.
4044
+ bool upb_strtable_copy(upb_strtable* dest, const upb_strtable* src,
4045
+ upb_Arena* a);
4046
+
3461
4047
  // Looks up key in this table, returning "true" if the key was found.
3462
4048
  // If v is non-NULL, copies the value for this key into *v.
3463
4049
  bool upb_strtable_lookup2(const upb_strtable* t, const char* key, size_t len,
@@ -3596,6 +4182,29 @@ UPB_API_INLINE bool upb_Map_IsFrozen(const struct upb_Map* map) {
3596
4182
  return map->UPB_PRIVATE(is_frozen);
3597
4183
  }
3598
4184
 
4185
+ bool _upb_Map_Reserve(struct upb_Map* map, size_t size, upb_Arena* a);
4186
+
4187
+ UPB_INLINE size_t _upb_Map_Size(const struct upb_Map* map) {
4188
+ if (map->UPB_PRIVATE(is_strtable)) {
4189
+ return map->t.strtable.t.count;
4190
+ } else {
4191
+ return upb_inttable_count(&map->t.inttable);
4192
+ }
4193
+ }
4194
+
4195
+ UPB_INLINE bool _upb_Map_IsInitialized(const struct upb_Map* map) {
4196
+ const upb_table* t =
4197
+ map->UPB_PRIVATE(is_strtable) ? &map->t.strtable.t : &map->t.inttable.t;
4198
+ return t->entries != NULL;
4199
+ }
4200
+
4201
+ UPB_INLINE size_t _upb_Map_Capacity(const struct upb_Map* map) {
4202
+ if (!_upb_Map_IsInitialized(map)) return 0;
4203
+ const upb_table* t =
4204
+ map->UPB_PRIVATE(is_strtable) ? &map->t.strtable.t : &map->t.inttable.t;
4205
+ return upb_table_size(t);
4206
+ }
4207
+
3599
4208
  // Converting between internal table representation and user values.
3600
4209
  //
3601
4210
  // _upb_map_tokey() and _upb_map_fromkey() are inverses.
@@ -3608,13 +4217,33 @@ UPB_INLINE upb_StringView _upb_map_tokey(const void* key, size_t size) {
3608
4217
  if (size == UPB_MAPTYPE_STRING) {
3609
4218
  return *(upb_StringView*)key;
3610
4219
  } else {
4220
+ UPB_ASSUME(size > sizeof(uintptr_t));
3611
4221
  return upb_StringView_FromDataAndSize((const char*)key, size);
3612
4222
  }
3613
4223
  }
3614
4224
 
4225
+ // Avoid emitting an out-of-line memcpy call when the size is not a compile-time
4226
+ // constant (UPB_MEMCPY_INLINE() keeps the compiler from merging the cases back
4227
+ // into one memcpy() of a variable size).
4228
+ UPB_FORCEINLINE void _upb_map_memcpy(void* dst, const void* src, size_t size) {
4229
+ switch (size) {
4230
+ case 1:
4231
+ UPB_MEMCPY_INLINE(dst, src, 1);
4232
+ return;
4233
+ case 4:
4234
+ UPB_MEMCPY_INLINE(dst, src, 4);
4235
+ return;
4236
+ case 8:
4237
+ UPB_MEMCPY_INLINE(dst, src, 8);
4238
+ return;
4239
+ default:
4240
+ UPB_UNREACHABLE();
4241
+ }
4242
+ }
4243
+
3615
4244
  UPB_INLINE uintptr_t _upb_map_tointkey(const void* key, size_t key_size) {
3616
4245
  uintptr_t intkey = 0;
3617
- memcpy(&intkey, key, key_size);
4246
+ _upb_map_memcpy(&intkey, key, key_size);
3618
4247
  return intkey;
3619
4248
  }
3620
4249
 
@@ -3622,7 +4251,7 @@ UPB_INLINE void _upb_map_fromkey(upb_StringView key, void* out, size_t size) {
3622
4251
  if (size == UPB_MAPTYPE_STRING) {
3623
4252
  memcpy(out, &key, sizeof(key));
3624
4253
  } else {
3625
- memcpy(out, key.data, size);
4254
+ _upb_map_memcpy(out, key.data, size);
3626
4255
  }
3627
4256
  }
3628
4257
 
@@ -3634,7 +4263,7 @@ UPB_INLINE bool _upb_map_tovalue(const void* val, size_t size,
3634
4263
  *strp = *(upb_StringView*)val;
3635
4264
  *msgval = upb_value_ptr(strp);
3636
4265
  } else {
3637
- memcpy(msgval, val, size);
4266
+ _upb_map_memcpy(msgval, val, size);
3638
4267
  }
3639
4268
  return true;
3640
4269
  }
@@ -3644,11 +4273,13 @@ UPB_INLINE void _upb_map_fromvalue(upb_value val, void* out, size_t size) {
3644
4273
  const upb_StringView* strp = (const upb_StringView*)upb_value_getptr(val);
3645
4274
  memcpy(out, strp, sizeof(upb_StringView));
3646
4275
  } else {
3647
- memcpy(out, &val, size);
4276
+ _upb_map_memcpy(out, &val, size);
3648
4277
  }
3649
4278
  }
3650
4279
 
3651
- UPB_INLINE bool _upb_map_next(const struct upb_Map* map, size_t* iter) {
4280
+ UPB_NODISCARD UPB_INLINE bool _upb_map_next(const struct upb_Map* map,
4281
+ size_t* iter) {
4282
+ if (_upb_Map_Size(map) == 0) return false;
3652
4283
  if (map->UPB_PRIVATE(is_strtable)) {
3653
4284
  upb_strtable_iter it;
3654
4285
  it.t = &map->t.strtable;
@@ -3670,6 +4301,10 @@ UPB_INLINE bool _upb_map_next(const struct upb_Map* map, size_t* iter) {
3670
4301
  UPB_INLINE void _upb_Map_Clear(struct upb_Map* map) {
3671
4302
  UPB_ASSERT(!upb_Map_IsFrozen(map));
3672
4303
 
4304
+ const upb_table* t =
4305
+ map->UPB_PRIVATE(is_strtable) ? &map->t.strtable.t : &map->t.inttable.t;
4306
+ if (!t->entries || t->count == 0) return;
4307
+
3673
4308
  if (map->UPB_PRIVATE(is_strtable)) {
3674
4309
  upb_strtable_clear(&map->t.strtable);
3675
4310
  } else {
@@ -3680,6 +4315,7 @@ UPB_INLINE void _upb_Map_Clear(struct upb_Map* map) {
3680
4315
  UPB_INLINE bool _upb_Map_Delete(struct upb_Map* map, const void* key,
3681
4316
  size_t key_size, upb_value* val) {
3682
4317
  UPB_ASSERT(!upb_Map_IsFrozen(map));
4318
+ if (_upb_Map_Size(map) == 0) return false;
3683
4319
 
3684
4320
  if (map->UPB_PRIVATE(is_strtable)) {
3685
4321
  upb_StringView k = _upb_map_tokey(key, key_size);
@@ -3707,10 +4343,11 @@ UPB_INLINE bool _upb_Map_Get(const struct upb_Map* map, const void* key,
3707
4343
  return ret;
3708
4344
  }
3709
4345
 
3710
- UPB_INLINE upb_MapInsertStatus _upb_Map_Insert(struct upb_Map* map,
3711
- const void* key, size_t key_size,
3712
- void* val, size_t val_size,
3713
- upb_Arena* a) {
4346
+ UPB_FORCEINLINE upb_MapInsertStatus _upb_Map_Insert(struct upb_Map* map,
4347
+ const void* key,
4348
+ size_t key_size, void* val,
4349
+ size_t val_size,
4350
+ upb_Arena* a) {
3714
4351
  UPB_ASSERT(!upb_Map_IsFrozen(map));
3715
4352
 
3716
4353
  // Prep the value.
@@ -3719,33 +4356,22 @@ UPB_INLINE upb_MapInsertStatus _upb_Map_Insert(struct upb_Map* map,
3719
4356
  return kUpb_MapInsertStatus_OutOfMemory;
3720
4357
  }
3721
4358
 
3722
- bool removed;
4359
+ bool replaced;
3723
4360
  if (map->UPB_PRIVATE(is_strtable)) {
3724
4361
  upb_StringView strkey = _upb_map_tokey(key, key_size);
3725
- // TODO: add overwrite operation to minimize number of lookups.
3726
- removed =
3727
- upb_strtable_remove2(&map->t.strtable, strkey.data, strkey.size, NULL);
3728
- if (!upb_strtable_insert(&map->t.strtable, strkey.data, strkey.size, tabval,
3729
- a)) {
4362
+ if (!upb_strtable_insert_or_replace(&map->t.strtable, strkey.data,
4363
+ strkey.size, tabval, &replaced, a)) {
3730
4364
  return kUpb_MapInsertStatus_OutOfMemory;
3731
4365
  }
3732
4366
  } else {
3733
4367
  uintptr_t intkey = _upb_map_tointkey(key, key_size);
3734
- removed = upb_inttable_remove(&map->t.inttable, intkey, NULL);
3735
- if (!upb_inttable_insert(&map->t.inttable, intkey, tabval, a)) {
4368
+ if (!upb_inttable_insert_or_replace(&map->t.inttable, intkey, tabval,
4369
+ &replaced, a)) {
3736
4370
  return kUpb_MapInsertStatus_OutOfMemory;
3737
4371
  }
3738
4372
  }
3739
- return removed ? kUpb_MapInsertStatus_Replaced
3740
- : kUpb_MapInsertStatus_Inserted;
3741
- }
3742
-
3743
- UPB_INLINE size_t _upb_Map_Size(const struct upb_Map* map) {
3744
- if (map->UPB_PRIVATE(is_strtable)) {
3745
- return map->t.strtable.t.count;
3746
- } else {
3747
- return upb_inttable_count(&map->t.inttable);
3748
- }
4373
+ return replaced ? kUpb_MapInsertStatus_Replaced
4374
+ : kUpb_MapInsertStatus_Inserted;
3749
4375
  }
3750
4376
 
3751
4377
  // Strings/bytes are special-cased in maps.
@@ -3837,6 +4463,9 @@ UPB_API_INLINE bool upb_MiniTableExtension_SetSubMessage(
3837
4463
  kUpb_FieldType_Group) {
3838
4464
  return false;
3839
4465
  }
4466
+ if (m->UPB_PRIVATE(ext) & kUpb_ExtMode_IsMapEntry) {
4467
+ return false;
4468
+ }
3840
4469
  e->UPB_PRIVATE(sub).UPB_PRIVATE(submsg) = m;
3841
4470
  return true;
3842
4471
  }
@@ -3907,24 +4536,307 @@ UPB_API_INLINE const upb_MiniTableField* upb_MiniTableExtension_ToField(
3907
4536
 
3908
4537
  #endif /* UPB_MINI_TABLE_EXTENSION_H_ */
3909
4538
 
3910
- // Must be last.
4539
+ // Must be last.
4540
+
4541
+ // The internal representation of an extension is self-describing: it contains
4542
+ // enough information that we can serialize it to binary format without needing
4543
+ // to look it up in a upb_ExtensionRegistry.
4544
+ //
4545
+ // The value is stored in the same allocation, at offset
4546
+ // kUpb_Extension_DataOffset from the start of the struct, and is sized
4547
+ // according to the extension's field representation (see _upb_Extension_Size).
4548
+ // It is only accessed through offset calculations, like the fields of a
4549
+ // upb_Message.
4550
+ typedef struct upb_Extension {
4551
+ const upb_MiniTableExtension* UPB_ONLYBITS(ext);
4552
+ } upb_Extension;
4553
+
4554
+ // The offset of the value from the start of the upb_Extension. Rounded up to 8
4555
+ // so that 8-byte values are naturally aligned on 32-bit platforms, where
4556
+ // sizeof(upb_Extension) is 4 (the allocation itself is always 8-byte aligned).
4557
+ enum {
4558
+ UPB_ONLYBITS(kUpb_Extension_DataOffset) =
4559
+ UPB_ALIGN_UP(sizeof(upb_Extension), 8),
4560
+ };
4561
+
4562
+ #ifdef __cplusplus
4563
+ extern "C" {
4564
+ #endif
4565
+
4566
+ // Returns the MiniTableExtension that describes this extension. Never NULL.
4567
+ UPB_API_INLINE const upb_MiniTableExtension* upb_Extension_MiniTableExtension(
4568
+ const upb_Extension* ext) {
4569
+ UPB_ASSERT(ext->UPB_ONLYBITS(ext) != NULL);
4570
+ return ext->UPB_ONLYBITS(ext);
4571
+ }
4572
+
4573
+ // Returns the MiniTableField that describes this extension. Prefer this over
4574
+ // upb_Extension_MiniTableExtension() when only field properties (number, type,
4575
+ // sub-message, etc.) are needed. Never NULL.
4576
+ UPB_API_INLINE const upb_MiniTableField* upb_Extension_MiniTableField(
4577
+ const upb_Extension* ext) {
4578
+ return upb_MiniTableExtension_ToField(upb_Extension_MiniTableExtension(ext));
4579
+ }
4580
+
4581
+ // Copies the value of `ext` into `val`, which must point to storage of the
4582
+ // appropriate type for the extension's field representation.
4583
+ UPB_API_INLINE void upb_Extension_GetField(const upb_Extension* ext,
4584
+ void* val) {
4585
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4586
+ UPB_ASSUME(upb_MiniTableField_IsExtension(f));
4587
+ const void* data =
4588
+ UPB_PTR_AT(ext, UPB_ONLYBITS(kUpb_Extension_DataOffset), const void);
4589
+ UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, val, data);
4590
+ }
4591
+
4592
+ // Sets the value of `ext` from `val`, which must point to a value of the
4593
+ // extension's C type (e.g. `const bool*` for a bool extension, `struct
4594
+ // upb_Array**` for a repeated extension).
4595
+ UPB_API_INLINE void upb_Extension_SetField(upb_Extension* ext,
4596
+ const void* val) {
4597
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4598
+ UPB_ASSUME(upb_MiniTableField_IsExtension(f));
4599
+ void* data = UPB_PTR_AT(ext, UPB_ONLYBITS(kUpb_Extension_DataOffset), void);
4600
+ UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, data, val);
4601
+ }
4602
+
4603
+ // Returns the value of this extension.
4604
+ //
4605
+ // For repeated/map extensions, the resulting struct upb_Array*/upb_Map* can be
4606
+ // NULL if an struct upb_Array/upb_Map has not been allocated yet.
4607
+ UPB_API_INLINE upb_MessageValue
4608
+ upb_Extension_GetValue(const upb_Extension* ext) {
4609
+ upb_MessageValue val;
4610
+ memset(&val, 0, sizeof(val));
4611
+ upb_Extension_GetField(ext, &val);
4612
+ return val;
4613
+ }
4614
+
4615
+ // Sets the value of this extension.
4616
+ UPB_API_INLINE void upb_Extension_SetValue(upb_Extension* ext,
4617
+ upb_MessageValue val) {
4618
+ upb_Extension_SetField(ext, &val);
4619
+ }
4620
+
4621
+ UPB_API_INLINE void upb_Extension_SetBool(upb_Extension* ext, bool value) {
4622
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4623
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Bool);
4624
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4625
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_1Byte);
4626
+ upb_Extension_SetField(ext, &value);
4627
+ }
4628
+
4629
+ UPB_API_INLINE void upb_Extension_SetDouble(upb_Extension* ext, double value) {
4630
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4631
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Double);
4632
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4633
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
4634
+ upb_Extension_SetField(ext, &value);
4635
+ }
4636
+
4637
+ UPB_API_INLINE void upb_Extension_SetFloat(upb_Extension* ext, float value) {
4638
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4639
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Float);
4640
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4641
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
4642
+ upb_Extension_SetField(ext, &value);
4643
+ }
4644
+
4645
+ UPB_API_INLINE void upb_Extension_SetInt32(upb_Extension* ext, int32_t value) {
4646
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4647
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int32 ||
4648
+ upb_MiniTableField_CType(f) == kUpb_CType_Enum);
4649
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4650
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
4651
+ upb_Extension_SetField(ext, &value);
4652
+ }
4653
+
4654
+ UPB_API_INLINE void upb_Extension_SetInt64(upb_Extension* ext, int64_t value) {
4655
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4656
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int64);
4657
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4658
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
4659
+ upb_Extension_SetField(ext, &value);
4660
+ }
4661
+
4662
+ UPB_API_INLINE void upb_Extension_SetUInt32(upb_Extension* ext,
4663
+ uint32_t value) {
4664
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4665
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt32);
4666
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4667
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
4668
+ upb_Extension_SetField(ext, &value);
4669
+ }
4670
+
4671
+ UPB_API_INLINE void upb_Extension_SetUInt64(upb_Extension* ext,
4672
+ uint64_t value) {
4673
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4674
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt64);
4675
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4676
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
4677
+ upb_Extension_SetField(ext, &value);
4678
+ }
4679
+
4680
+ // Sets the value of a `string` or `bytes` extension. The bytes of the value
4681
+ // are not copied, so it is the caller's responsibility to ensure that they
4682
+ // remain valid for the lifetime of the extension.
4683
+ UPB_API_INLINE void upb_Extension_SetString(upb_Extension* ext,
4684
+ upb_StringView value) {
4685
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4686
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_String ||
4687
+ upb_MiniTableField_CType(f) == kUpb_CType_Bytes);
4688
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4689
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
4690
+ kUpb_FieldRep_StringView);
4691
+ upb_Extension_SetField(ext, &value);
4692
+ }
4693
+
4694
+ UPB_API_INLINE void upb_Extension_SetMessage(upb_Extension* ext,
4695
+ struct upb_Message* value) {
4696
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4697
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
4698
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4699
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
4700
+ UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte));
4701
+ upb_Extension_SetField(ext, &value);
4702
+ }
4703
+
4704
+ UPB_API_INLINE void upb_Extension_SetArray(upb_Extension* ext,
4705
+ struct upb_Array* value) {
4706
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4707
+ UPB_ASSUME(upb_MiniTableField_IsArray(f));
4708
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
4709
+ kUpb_FieldRep_NativePointer);
4710
+ upb_Extension_SetField(ext, &value);
4711
+ }
4712
+
4713
+ UPB_API_INLINE void upb_Extension_SetMap(upb_Extension* ext,
4714
+ struct upb_Map* value) {
4715
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4716
+ UPB_ASSUME(upb_MiniTableField_IsMap(f));
4717
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
4718
+ kUpb_FieldRep_NativePointer);
4719
+ upb_Extension_SetField(ext, &value);
4720
+ }
4721
+
4722
+ UPB_API_INLINE bool upb_Extension_GetBool(const upb_Extension* ext) {
4723
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4724
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Bool);
4725
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4726
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_1Byte);
4727
+ return upb_Extension_GetValue(ext).bool_val;
4728
+ }
4729
+
4730
+ UPB_API_INLINE double upb_Extension_GetDouble(const upb_Extension* ext) {
4731
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4732
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Double);
4733
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4734
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
4735
+ return upb_Extension_GetValue(ext).double_val;
4736
+ }
4737
+
4738
+ UPB_API_INLINE float upb_Extension_GetFloat(const upb_Extension* ext) {
4739
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4740
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Float);
4741
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4742
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
4743
+ return upb_Extension_GetValue(ext).float_val;
4744
+ }
4745
+
4746
+ UPB_API_INLINE int32_t upb_Extension_GetInt32(const upb_Extension* ext) {
4747
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4748
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int32 ||
4749
+ upb_MiniTableField_CType(f) == kUpb_CType_Enum);
4750
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4751
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
4752
+ return upb_Extension_GetValue(ext).int32_val;
4753
+ }
4754
+
4755
+ UPB_API_INLINE int64_t upb_Extension_GetInt64(const upb_Extension* ext) {
4756
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4757
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int64);
4758
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4759
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
4760
+ return upb_Extension_GetValue(ext).int64_val;
4761
+ }
4762
+
4763
+ UPB_API_INLINE uint32_t upb_Extension_GetUInt32(const upb_Extension* ext) {
4764
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4765
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt32);
4766
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4767
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
4768
+ return upb_Extension_GetValue(ext).uint32_val;
4769
+ }
4770
+
4771
+ UPB_API_INLINE uint64_t upb_Extension_GetUInt64(const upb_Extension* ext) {
4772
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4773
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt64);
4774
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4775
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
4776
+ return upb_Extension_GetValue(ext).uint64_val;
4777
+ }
4778
+
4779
+ // Sets the value of a `string` or `bytes` extension. The bytes of the value
4780
+ // are not copied, so it is the caller's responsibility to ensure that they
4781
+ // remain valid for the lifetime of the extension.
4782
+ UPB_API_INLINE upb_StringView
4783
+ upb_Extension_GetString(const upb_Extension* ext) {
4784
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4785
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_String ||
4786
+ upb_MiniTableField_CType(f) == kUpb_CType_Bytes);
4787
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4788
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
4789
+ kUpb_FieldRep_StringView);
4790
+ return upb_Extension_GetValue(ext).str_val;
4791
+ }
4792
+
4793
+ UPB_API_INLINE const struct upb_Message* upb_Extension_GetMessage(
4794
+ const upb_Extension* ext) {
4795
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4796
+ UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
4797
+ UPB_ASSUME(upb_MiniTableField_IsScalar(f));
4798
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
4799
+ UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte));
4800
+ return upb_Extension_GetValue(ext).msg_val;
4801
+ }
4802
+
4803
+ UPB_API_INLINE struct upb_Message* upb_Extension_GetMutableMessage(
4804
+ upb_Extension* ext) {
4805
+ return (struct upb_Message*)upb_Extension_GetMessage(ext);
4806
+ }
4807
+
4808
+ UPB_API_INLINE const struct upb_Array* upb_Extension_GetArray(
4809
+ const upb_Extension* ext) {
4810
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4811
+ UPB_ASSUME(upb_MiniTableField_IsArray(f));
4812
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
4813
+ kUpb_FieldRep_NativePointer);
4814
+ return upb_Extension_GetValue(ext).array_val;
4815
+ }
4816
+
4817
+ UPB_API_INLINE struct upb_Array* upb_Extension_GetMutableArray(
4818
+ upb_Extension* ext) {
4819
+ return (struct upb_Array*)upb_Extension_GetArray(ext);
4820
+ }
4821
+
4822
+ UPB_API_INLINE const struct upb_Map* upb_Extension_GetMap(
4823
+ const upb_Extension* ext) {
4824
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4825
+ UPB_ASSUME(upb_MiniTableField_IsMap(f));
4826
+ UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
4827
+ kUpb_FieldRep_NativePointer);
4828
+ return upb_Extension_GetValue(ext).map_val;
4829
+ }
3911
4830
 
3912
- // The internal representation of an extension is self-describing: it contains
3913
- // enough information that we can serialize it to binary format without needing
3914
- // to look it up in a upb_ExtensionRegistry.
3915
- //
3916
- // This representation allocates 16 bytes to data on 64-bit platforms.
3917
- // This is rather wasteful for scalars (in the extreme case of bool,
3918
- // it wastes 15 bytes). We accept this because we expect messages to be
3919
- // the most common extension type.
3920
- typedef struct {
3921
- const upb_MiniTableExtension* ext;
3922
- upb_MessageValue data;
3923
- } upb_Extension;
4831
+ UPB_API_INLINE struct upb_Map* upb_Extension_GetMutableMap(upb_Extension* ext) {
4832
+ return (struct upb_Map*)upb_Extension_GetMap(ext);
4833
+ }
3924
4834
 
3925
- #ifdef __cplusplus
3926
- extern "C" {
3927
- #endif
4835
+ UPB_NODISCARD upb_Extension* UPB_PRIVATE(
4836
+ _upb_Message_GetOrCreateExtensionWithTag)(struct upb_Message* msg,
4837
+ const upb_MiniTableExtension* ext,
4838
+ upb_Arena* arena,
4839
+ upb_TaggedAuxType tag);
3928
4840
 
3929
4841
  // Adds the given extension data to the given message.
3930
4842
  // |ext| is copied into the message instance.
@@ -3947,18 +4859,61 @@ const upb_Extension* UPB_PRIVATE(_upb_Message_Getext)(
3947
4859
  const struct upb_Message* msg, const upb_MiniTableExtension* ext);
3948
4860
 
3949
4861
  UPB_INLINE bool UPB_PRIVATE(_upb_Extension_IsEmpty)(const upb_Extension* ext) {
3950
- switch (
3951
- UPB_PRIVATE(_upb_MiniTableField_Mode)(&ext->ext->UPB_PRIVATE(field))) {
4862
+ const upb_MiniTableField* f = upb_Extension_MiniTableField(ext);
4863
+ switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(f)) {
3952
4864
  case kUpb_FieldMode_Scalar:
3953
4865
  return false;
3954
4866
  case kUpb_FieldMode_Array:
3955
- return upb_Array_Size(ext->data.array_val) == 0;
4867
+ return upb_Array_Size(upb_Extension_GetValue(ext).array_val) == 0;
3956
4868
  case kUpb_FieldMode_Map:
3957
- return _upb_Map_Size(ext->data.map_val) == 0;
4869
+ return _upb_Map_Size(upb_Extension_GetValue(ext).map_val) == 0;
3958
4870
  }
3959
4871
  UPB_UNREACHABLE();
3960
4872
  }
3961
4873
 
4874
+ // Returns the allocation size of an upb_Extension described by `f`, including
4875
+ // the value. Rounded up to kUpb_Message_Align (which the arena would do anyway)
4876
+ // so that the extension can be zeroed with _upb_Message_AlignedMemsetZero().
4877
+ UPB_NODISCARD UPB_INLINE size_t
4878
+ UPB_PRIVATE(_upb_Extension_Size)(const upb_MiniTableField* f) {
4879
+ UPB_ASSERT(upb_MiniTableField_IsExtension(f));
4880
+ static const uint8_t rep_sizes[] = {
4881
+ 1, // kUpb_FieldRep_1Byte
4882
+ 4, // kUpb_FieldRep_4Byte
4883
+ sizeof(upb_StringView), // kUpb_FieldRep_StringView
4884
+ 8, // kUpb_FieldRep_8Byte
4885
+ };
4886
+ return UPB_ALIGN_UP(UPB_ONLYBITS(kUpb_Extension_DataOffset) +
4887
+ rep_sizes[UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)],
4888
+ kUpb_Message_Align);
4889
+ }
4890
+
4891
+ // Allocates a shallow copy of `src` on `a`, or returns NULL on allocation
4892
+ // failure.
4893
+ UPB_NODISCARD UPB_INLINE upb_Extension* UPB_PRIVATE(_upb_Extension_Clone)(
4894
+ const upb_Extension* src, upb_Arena* a) {
4895
+ const size_t ext_size =
4896
+ UPB_PRIVATE(_upb_Extension_Size)(upb_Extension_MiniTableField(src));
4897
+ enum {
4898
+ kSmallExtSize = UPB_ALIGN_UP(UPB_ONLYBITS(kUpb_Extension_DataOffset) + 1,
4899
+ kUpb_Message_Align),
4900
+ kBigExtSize = UPB_ALIGN_UP(
4901
+ UPB_ONLYBITS(kUpb_Extension_DataOffset) + sizeof(upb_StringView),
4902
+ kUpb_Message_Align),
4903
+ };
4904
+ UPB_ASSUME(ext_size == kSmallExtSize || ext_size == kBigExtSize);
4905
+ char* dst = (char*)upb_Arena_Malloc(a, ext_size);
4906
+ if (UPB_UNLIKELY(!dst)) return NULL;
4907
+ // Avoid out-of-line variable length memcpy for short length
4908
+ memcpy(dst, src, kSmallExtSize);
4909
+ if (ext_size != kSmallExtSize) {
4910
+ UPB_ASSERT(ext_size == kBigExtSize);
4911
+ memcpy(dst + kSmallExtSize, UPB_PTR_AT(src, kSmallExtSize, const char),
4912
+ kBigExtSize - kSmallExtSize);
4913
+ }
4914
+ return (upb_Extension*)dst;
4915
+ }
4916
+
3962
4917
  #ifdef __cplusplus
3963
4918
  } /* extern "C" */
3964
4919
  #endif
@@ -4035,27 +4990,40 @@ typedef struct upb_TaggedAuxPtr {
4035
4990
  uintptr_t ptr;
4036
4991
  } upb_TaggedAuxPtr;
4037
4992
 
4993
+ UPB_INLINE bool upb_TaggedAuxPtr_IsNull(upb_TaggedAuxPtr ptr) {
4994
+ return ptr.ptr == 0;
4995
+ }
4996
+
4997
+ UPB_INLINE upb_TaggedAuxType upb_TaggedAuxPtr_Type(upb_TaggedAuxPtr ptr) {
4998
+ return (upb_TaggedAuxType)(ptr.ptr & 7);
4999
+ }
5000
+
4038
5001
  // If this returns true, then the entry is semantically known (but may be in
4039
5002
  // either parsed or unparsed form).
4040
5003
  UPB_INLINE bool upb_TaggedAuxPtr_IsSemanticallyKnown(upb_TaggedAuxPtr ptr) {
4041
- return (ptr.ptr & 0x2) != 0;
5004
+ return !upb_TaggedAuxPtr_IsNull(ptr) && ((ptr.ptr & 0x2) != 0);
4042
5005
  }
4043
5006
 
4044
5007
  UPB_INLINE bool upb_TaggedAuxPtr_IsCanonicalExtension(upb_TaggedAuxPtr ptr) {
4045
- return (ptr.ptr & 3) == 3;
5008
+ return !upb_TaggedAuxPtr_IsNull(ptr) && ((ptr.ptr & 3) == 3);
4046
5009
  }
4047
5010
 
4048
5011
  UPB_INLINE bool upb_TaggedAuxPtr_IsNonCanonicalExtension(upb_TaggedAuxPtr ptr) {
4049
- return (ptr.ptr & 3) == 1;
5012
+ return !upb_TaggedAuxPtr_IsNull(ptr) && ((ptr.ptr & 3) == 1);
5013
+ }
5014
+
5015
+ // Returns true if the entry is a canonical or non-canonical extension.
5016
+ UPB_INLINE bool upb_TaggedAuxPtr_IsExtension(upb_TaggedAuxPtr ptr) {
5017
+ return !upb_TaggedAuxPtr_IsNull(ptr) && ((ptr.ptr & 1) != 0);
4050
5018
  }
4051
5019
 
4052
5020
  // Returns true if the entry is aliased/non-aliased unknown data.
4053
5021
  UPB_INLINE bool upb_TaggedAuxPtr_IsUnknownStringView(upb_TaggedAuxPtr ptr) {
4054
- return (ptr.ptr != 0) && ((ptr.ptr & 3) == 0);
5022
+ return !upb_TaggedAuxPtr_IsNull(ptr) && ((ptr.ptr & 3) == 0);
4055
5023
  }
4056
5024
 
4057
5025
  UPB_INLINE bool upb_TaggedAuxPtr_IsUnknownAliased(upb_TaggedAuxPtr ptr) {
4058
- return (ptr.ptr != 0) && ((ptr.ptr & 5) == 4);
5026
+ return !upb_TaggedAuxPtr_IsNull(ptr) && ((ptr.ptr & 5) == 4);
4059
5027
  }
4060
5028
 
4061
5029
  UPB_INLINE upb_Extension* upb_TaggedAuxPtr_CanonicalExtension(
@@ -4070,6 +5038,22 @@ UPB_INLINE upb_Extension* upb_TaggedAuxPtr_NonCanonicalExtension(
4070
5038
  return (upb_Extension*)(ptr.ptr & ~7ULL);
4071
5039
  }
4072
5040
 
5041
+ UPB_INLINE upb_Extension* upb_TaggedAuxPtr_Extension(upb_TaggedAuxPtr ptr) {
5042
+ UPB_ASSERT(upb_TaggedAuxPtr_IsExtension(ptr));
5043
+ return (upb_Extension*)(ptr.ptr & ~7ULL);
5044
+ }
5045
+
5046
+ // Returns the extension pointer if the tagged pointer is a canonical or
5047
+ // non-canonical extension, otherwise returns NULL.
5048
+ UPB_INLINE upb_Extension* upb_TaggedAuxPtr_TryGetExtension(
5049
+ upb_TaggedAuxPtr ptr) {
5050
+ if (upb_TaggedAuxPtr_IsCanonicalExtension(ptr) ||
5051
+ upb_TaggedAuxPtr_IsNonCanonicalExtension(ptr)) {
5052
+ return (upb_Extension*)(ptr.ptr & ~7ULL);
5053
+ }
5054
+ return NULL;
5055
+ }
5056
+
4073
5057
  // Returns a pointer to the aliased or unaliased unknown upb_StringView* data.
4074
5058
  UPB_INLINE upb_StringView* upb_TaggedPtrAux_StringViewRepr(
4075
5059
  upb_TaggedAuxPtr ptr) {
@@ -4079,13 +5063,6 @@ UPB_INLINE upb_StringView* upb_TaggedPtrAux_StringViewRepr(
4079
5063
 
4080
5064
  // LINT.ThenChange(//depot/google3/third_party/upb/bits/golang/message.go:tagged_aux_type)
4081
5065
 
4082
- typedef enum {
4083
- kUpb_TaggedAuxType_Unknown = 0, // tag 000
4084
- kUpb_TaggedAuxType_NonCanonicalExtension = 1, // tag 001
4085
- kUpb_TaggedAuxType_CanonicalExtension = 3, // tag 011
4086
- kUpb_TaggedAuxType_AliasedUnknown = 4 // tag 100
4087
- } upb_TaggedAuxType;
4088
-
4089
5066
  typedef union {
4090
5067
  upb_Extension* extension;
4091
5068
  const upb_StringView* unknown_data;
@@ -4093,10 +5070,11 @@ typedef union {
4093
5070
 
4094
5071
  UPB_INLINE upb_TaggedAuxType upb_TaggedAux_Get(upb_TaggedAuxPtr ptr,
4095
5072
  upb_TaggedAux* data) {
5073
+ UPB_ASSERT(!upb_TaggedAuxPtr_IsNull(ptr));
4096
5074
  uintptr_t untagged = ptr.ptr & ~7ULL;
4097
5075
  UPB_ASSERT((untagged & 7) == 0);
4098
5076
  memcpy(data, &untagged, sizeof(*data));
4099
- return (upb_TaggedAuxType)(ptr.ptr & 7);
5077
+ return upb_TaggedAuxPtr_Type(ptr);
4100
5078
  }
4101
5079
 
4102
5080
  UPB_INLINE upb_TaggedAuxPtr upb_TaggedAuxPtr_Null(void) {
@@ -4105,6 +5083,16 @@ UPB_INLINE upb_TaggedAuxPtr upb_TaggedAuxPtr_Null(void) {
4105
5083
  return ptr;
4106
5084
  }
4107
5085
 
5086
+ UPB_INLINE upb_TaggedAuxPtr
5087
+ upb_TaggedAuxPtr_MakeExtension(const upb_Extension* e, upb_TaggedAuxType type) {
5088
+ UPB_ASSERT(((uintptr_t)e & 7) == 0);
5089
+ UPB_ASSERT(type == kUpb_TaggedAuxType_CanonicalExtension ||
5090
+ type == kUpb_TaggedAuxType_NonCanonicalExtension);
5091
+ upb_TaggedAuxPtr ptr;
5092
+ ptr.ptr = (uintptr_t)e | type;
5093
+ return ptr;
5094
+ }
5095
+
4108
5096
  UPB_INLINE upb_TaggedAuxPtr
4109
5097
  upb_TaggedAuxPtr_MakeCanonicalExtension(const upb_Extension* e) {
4110
5098
  UPB_ASSERT(((uintptr_t)e & 7) == 0);
@@ -4150,6 +5138,10 @@ typedef struct upb_Message_Internal {
4150
5138
  upb_TaggedAuxPtr aux_data[];
4151
5139
  } upb_Message_Internal;
4152
5140
 
5141
+ bool UPB_PRIVATE(_upb_Message_CopyInternal)(struct upb_Message* dst,
5142
+ const struct upb_Message* src,
5143
+ upb_Arena* arena);
5144
+
4153
5145
  #ifdef UPB_TRACING_ENABLED
4154
5146
  UPB_API void upb_Message_LogNewMessage(const upb_MiniTable* m,
4155
5147
  const upb_Arena* arena);
@@ -4229,7 +5221,21 @@ UPB_NODISCARD UPB_INLINE struct upb_Message* _upb_Message_New(
4229
5221
  return msg;
4230
5222
  }
4231
5223
 
4232
- // Discards the unknown fields for this message only.
5224
+ // Allocates a new zero-initialized extension described by `e`, for internal
5225
+ // use.
5226
+ UPB_NODISCARD UPB_INLINE upb_Extension* UPB_PRIVATE(_upb_Extension_New)(
5227
+ const upb_MiniTableExtension* e, upb_Arena* a) {
5228
+ const size_t size =
5229
+ UPB_PRIVATE(_upb_Extension_Size)(upb_MiniTableExtension_ToField(e));
5230
+ upb_Extension* ext = (upb_Extension*)upb_Arena_Malloc(a, size);
5231
+ if (UPB_UNLIKELY(!ext)) return NULL;
5232
+ _upb_Message_AlignedMemsetZero(ext, size);
5233
+ ext->UPB_ONLYBITS(ext) = e;
5234
+ return ext;
5235
+ }
5236
+
5237
+ // Discards the unknown fields (including non-canonical extensions) for this
5238
+ // message only.
4233
5239
  void _upb_Message_DiscardUnknown_shallow(struct upb_Message* msg);
4234
5240
 
4235
5241
  UPB_NODISCARD UPB_NOINLINE bool UPB_PRIVATE(_upb_Message_AddUnknownSlowPath)(
@@ -4314,27 +5320,6 @@ UPB_NODISCARD bool UPB_PRIVATE(_upb_Message_AddUnknownV)(
4314
5320
  UPB_NODISCARD bool UPB_PRIVATE(_upb_Message_ReserveSlot)(
4315
5321
  struct upb_Message* msg, upb_Arena* arena);
4316
5322
 
4317
- typedef enum {
4318
- kUpb_MessageUnknownType_StringView,
4319
- kUpb_MessageUnknownType_NonCanonicalExtension,
4320
- } upb_MessageUnknownType;
4321
-
4322
- // Represents an unknown field in a message, whether it's in a serialized
4323
- // (upb_StringView) or parsed non-canonical extension (upb_Extension*) format.
4324
- typedef struct upb_MessageUnknown {
4325
- uint8_t type;
4326
- union {
4327
- upb_StringView bytes;
4328
- const upb_Extension* extension;
4329
- } value;
4330
- } upb_MessageUnknown;
4331
-
4332
- typedef enum upb_Message_DeleteUnknownStatus {
4333
- kUpb_DeleteUnknown_DeletedLast,
4334
- kUpb_DeleteUnknown_IterUpdated,
4335
- kUpb_DeleteUnknown_AllocFail,
4336
- } upb_Message_DeleteUnknownStatus;
4337
-
4338
5323
  #define kUpb_Message_UnknownBegin 0
4339
5324
  #define kUpb_Message_ExtensionBegin 0
4340
5325
 
@@ -4374,8 +5359,8 @@ UPB_INLINE bool upb_Message_NextExtension(const struct upb_Message* msg,
4374
5359
  // Empty repeated fields or maps semantically don't exist.
4375
5360
  if (UPB_PRIVATE(_upb_Extension_IsEmpty)(ext)) continue;
4376
5361
 
4377
- *out_e = ext->ext;
4378
- *out_v = ext->data;
5362
+ *out_e = upb_Extension_MiniTableExtension(ext);
5363
+ *out_v = upb_Extension_GetValue(ext);
4379
5364
  *iter = i;
4380
5365
  return true;
4381
5366
  }
@@ -4404,8 +5389,8 @@ UPB_INLINE bool UPB_PRIVATE(_upb_Message_NextExtensionReverse)(
4404
5389
  // Empty repeated fields or maps semantically don't exist.
4405
5390
  if (UPB_PRIVATE(_upb_Extension_IsEmpty)(ext)) continue;
4406
5391
 
4407
- *out_e = ext->ext;
4408
- *out_v = ext->data;
5392
+ *out_e = upb_Extension_MiniTableExtension(ext);
5393
+ *out_v = upb_Extension_GetValue(ext);
4409
5394
  *iter = i;
4410
5395
  return true;
4411
5396
  }
@@ -4448,12 +5433,22 @@ extern "C" {
4448
5433
 
4449
5434
  // Hasbit access ///////////////////////////////////////////////////////////////
4450
5435
 
5436
+ UPB_INLINE bool UPB_PRIVATE(_upb_Message_GetHasbitFromOffset)(
5437
+ const struct upb_Message* msg, int byte_offset, int bit_mask) {
5438
+ return (*UPB_PTR_AT(msg, byte_offset, const char) & bit_mask) != 0;
5439
+ }
5440
+
4451
5441
  UPB_INLINE bool UPB_PRIVATE(_upb_Message_GetHasbit)(
4452
5442
  const struct upb_Message* msg, const upb_MiniTableField* f) {
4453
5443
  const uint16_t offset = UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(f);
4454
5444
  const char mask = UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(f);
4455
5445
 
4456
- return (*UPB_PTR_AT(msg, offset, const char) & mask) != 0;
5446
+ return UPB_PRIVATE(_upb_Message_GetHasbitFromOffset)(msg, offset, mask);
5447
+ }
5448
+
5449
+ UPB_INLINE void UPB_PRIVATE(_upb_Message_SetHasbitFromOffset)(
5450
+ const struct upb_Message* msg, int byte_offset, int bit_mask) {
5451
+ (*UPB_PTR_AT(msg, byte_offset, char)) |= bit_mask;
4457
5452
  }
4458
5453
 
4459
5454
  UPB_INLINE void UPB_PRIVATE(_upb_Message_SetHasbit)(
@@ -4461,7 +5456,7 @@ UPB_INLINE void UPB_PRIVATE(_upb_Message_SetHasbit)(
4461
5456
  const uint16_t offset = UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(f);
4462
5457
  const char mask = UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(f);
4463
5458
 
4464
- (*UPB_PTR_AT(msg, offset, char)) |= mask;
5459
+ UPB_PRIVATE(_upb_Message_SetHasbitFromOffset)(msg, offset, mask);
4465
5460
  }
4466
5461
 
4467
5462
  UPB_INLINE void UPB_PRIVATE(_upb_Message_ClearHasbit)(
@@ -4554,57 +5549,8 @@ UPB_INLINE void UPB_PRIVATE(_upb_Message_SetPresence)(
4554
5549
  }
4555
5550
  }
4556
5551
 
4557
- UPB_INLINE_IF_NOT_GCC void UPB_PRIVATE(_upb_MiniTableField_DataCopy)(
4558
- const upb_MiniTableField* f, void* to, const void* from) {
4559
- switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) {
4560
- case kUpb_FieldRep_1Byte:
4561
- memcpy(to, from, 1);
4562
- return;
4563
- case kUpb_FieldRep_4Byte:
4564
- memcpy(to, from, 4);
4565
- return;
4566
- case kUpb_FieldRep_8Byte:
4567
- memcpy(to, from, 8);
4568
- return;
4569
- case kUpb_FieldRep_StringView: {
4570
- memcpy(to, from, sizeof(upb_StringView));
4571
- return;
4572
- }
4573
- }
4574
- UPB_UNREACHABLE();
4575
- }
4576
5552
  // LINT.ThenChange(//depot/google3/third_party/upb/bits/golang/message.go:message_raw_fields)
4577
5553
 
4578
- UPB_INLINE_IF_NOT_GCC bool UPB_PRIVATE(_upb_MiniTableField_DataEquals)(
4579
- const upb_MiniTableField* f, const void* a, const void* b) {
4580
- switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) {
4581
- case kUpb_FieldRep_1Byte:
4582
- return memcmp(a, b, 1) == 0;
4583
- case kUpb_FieldRep_4Byte:
4584
- return memcmp(a, b, 4) == 0;
4585
- case kUpb_FieldRep_8Byte:
4586
- return memcmp(a, b, 8) == 0;
4587
- case kUpb_FieldRep_StringView: {
4588
- const upb_StringView sa = *(const upb_StringView*)a;
4589
- const upb_StringView sb = *(const upb_StringView*)b;
4590
- return upb_StringView_IsEqual(sa, sb);
4591
- }
4592
- }
4593
- UPB_UNREACHABLE();
4594
- }
4595
-
4596
- UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_DataClear)(
4597
- const upb_MiniTableField* f, void* val) {
4598
- const char zero[16] = {0};
4599
- UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, val, zero);
4600
- }
4601
-
4602
- UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_DataIsZero)(
4603
- const upb_MiniTableField* f, const void* val) {
4604
- const char zero[16] = {0};
4605
- return UPB_PRIVATE(_upb_MiniTableField_DataEquals)(f, val, zero);
4606
- }
4607
-
4608
5554
  // Here we define universal getter/setter functions for message fields.
4609
5555
  // These look very branchy and inefficient, but as long as the MiniTableField
4610
5556
  // values are known at compile time, all the branches are optimized away and
@@ -4677,7 +5623,7 @@ UPB_INLINE void _upb_Message_GetExtensionField(
4677
5623
  UPB_ASSUME(upb_MiniTableField_IsExtension(f));
4678
5624
 
4679
5625
  if (ext) {
4680
- UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, val, &ext->data);
5626
+ upb_Extension_GetField(ext, val);
4681
5627
  } else {
4682
5628
  UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, val, default_val);
4683
5629
  }
@@ -4706,34 +5652,45 @@ UPB_API_INLINE void upb_Message_SetBaseField(struct upb_Message* msg,
4706
5652
  const void* val) {
4707
5653
  UPB_ASSERT(!upb_Message_IsFrozen(msg));
4708
5654
  UPB_ASSUME(!upb_MiniTableField_IsExtension(f));
4709
- UPB_PRIVATE(_upb_Message_SetPresence)(msg, f);
5655
+ // Copy the value before setting the presence: the compiler can't tell that
5656
+ // the store to the presence bits of `msg` doesn't modify `f`, so it would
5657
+ // read `f` again after it, without what it knows (e.g. from UPB_ASSUME()s)
5658
+ // about its representation.
4710
5659
  UPB_PRIVATE(_upb_MiniTableField_DataCopy)
4711
5660
  (f, UPB_PRIVATE(_upb_Message_MutableDataPtr)(msg, f), val);
5661
+ UPB_PRIVATE(_upb_Message_SetPresence)(msg, f);
4712
5662
  }
4713
5663
 
4714
- UPB_API_INLINE bool upb_Message_SetExtension(struct upb_Message* msg,
4715
- const upb_MiniTableExtension* e,
4716
- const void* val, upb_Arena* a) {
5664
+ // Returns the extension `e` of `msg` so that its value can be set, creating it
5665
+ // if it is not present. Returns NULL if memory allocation fails.
5666
+ UPB_NODISCARD UPB_INLINE upb_Extension* UPB_PRIVATE(
5667
+ _upb_Message_MutableExtension)(struct upb_Message* msg,
5668
+ const upb_MiniTableExtension* e,
5669
+ upb_Arena* a) {
4717
5670
  UPB_ASSERT(!upb_Message_IsFrozen(msg));
4718
5671
  UPB_ASSERT(a);
4719
- upb_Extension* ext =
4720
- UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(msg, e, a);
4721
- if (!ext) return false;
4722
- UPB_PRIVATE(_upb_MiniTableField_DataCopy)
4723
- (&e->UPB_PRIVATE(field), &ext->data, val);
4724
- return true;
5672
+ return UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(msg, e, a);
4725
5673
  }
4726
5674
 
4727
- UPB_API_INLINE bool UPB_PRIVATE(_upb_Message_SetNonCanonicalExtension)(
5675
+ UPB_NODISCARD UPB_API_INLINE bool upb_Message_SetExtension(
4728
5676
  struct upb_Message* msg, const upb_MiniTableExtension* e, const void* val,
4729
5677
  upb_Arena* a) {
5678
+ upb_Extension* ext = UPB_PRIVATE(_upb_Message_MutableExtension)(msg, e, a);
5679
+ if (!ext) return false;
5680
+ upb_Extension_SetField(ext, val);
5681
+ return true;
5682
+ }
5683
+
5684
+ UPB_NODISCARD UPB_API_INLINE bool UPB_PRIVATE(
5685
+ _upb_Message_SetNonCanonicalExtension)(struct upb_Message* msg,
5686
+ const upb_MiniTableExtension* e,
5687
+ const void* val, upb_Arena* a) {
4730
5688
  UPB_ASSERT(!upb_Message_IsFrozen(msg));
4731
5689
  UPB_ASSERT(a);
4732
5690
  upb_Extension* ext =
4733
5691
  UPB_PRIVATE(_upb_Message_CreateNonCanonicalExtension)(msg, e, a);
4734
5692
  if (!ext) return false;
4735
- UPB_PRIVATE(_upb_MiniTableField_DataCopy)
4736
- (&e->UPB_PRIVATE(field), &ext->data, val);
5693
+ upb_Extension_SetField(ext, val);
4737
5694
  return true;
4738
5695
  }
4739
5696
 
@@ -4753,7 +5710,7 @@ UPB_INLINE bool UPB_PRIVATE(_upb_Message_SetField)(struct upb_Message* msg,
4753
5710
  }
4754
5711
  }
4755
5712
 
4756
- UPB_API_INLINE const upb_Array* upb_Message_GetArray(
5713
+ UPB_NODISCARD UPB_API_INLINE const upb_Array* upb_Message_GetArray(
4757
5714
  const struct upb_Message* msg, const upb_MiniTableField* f) {
4758
5715
  UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
4759
5716
  upb_Array* ret;
@@ -4913,12 +5870,14 @@ upb_Message_GetOrCreateMutableMessage(struct upb_Message* msg,
4913
5870
  UPB_ASSERT(arena);
4914
5871
  UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
4915
5872
  UPB_ASSUME(!upb_MiniTableField_IsExtension(f));
4916
- struct upb_Message* sub_message =
4917
- *UPB_PTR_AT(msg, f->UPB_ONLYBITS(offset), struct upb_Message*);
5873
+ struct upb_Message* sub_message = upb_Message_GetMutableMessage(msg, f);
4918
5874
  if (!sub_message) {
4919
5875
  const upb_MiniTable* sub_mini_table = upb_MiniTable_SubMessage(f);
4920
5876
  UPB_ASSERT(sub_mini_table);
4921
5877
  sub_message = _upb_Message_New(sub_mini_table, arena);
5878
+ if (!sub_message) {
5879
+ return NULL;
5880
+ }
4922
5881
  *UPB_PTR_AT(msg, f->UPB_ONLYBITS(offset), struct upb_Message*) =
4923
5882
  sub_message;
4924
5883
  UPB_PRIVATE(_upb_Message_SetPresence)(msg, f);
@@ -4926,6 +5885,44 @@ upb_Message_GetOrCreateMutableMessage(struct upb_Message* msg,
4926
5885
  return sub_message;
4927
5886
  }
4928
5887
 
5888
+ UPB_INLINE bool UPB_PRIVATE(_upb_Message_FieldIsSet)(
5889
+ const struct upb_Message* msg, const upb_MiniTableField* f) {
5890
+ if (f->presence == 0) {
5891
+ // Proto3 presence or map/array.
5892
+ const void* mem = UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), void);
5893
+ switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) {
5894
+ case kUpb_FieldRep_1Byte: {
5895
+ char ch;
5896
+ memcpy(&ch, mem, 1);
5897
+ return ch != 0;
5898
+ }
5899
+ case kUpb_FieldRep_4Byte: {
5900
+ uint32_t u32;
5901
+ memcpy(&u32, mem, 4);
5902
+ return u32 != 0;
5903
+ }
5904
+ case kUpb_FieldRep_8Byte: {
5905
+ uint64_t u64;
5906
+ memcpy(&u64, mem, 8);
5907
+ return u64 != 0;
5908
+ }
5909
+ case kUpb_FieldRep_StringView: {
5910
+ const upb_StringView* str = (const upb_StringView*)mem;
5911
+ return str->size != 0;
5912
+ }
5913
+ default:
5914
+ UPB_UNREACHABLE();
5915
+ }
5916
+ } else if (UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f)) {
5917
+ // Proto2 presence: hasbit.
5918
+ return UPB_PRIVATE(_upb_Message_GetHasbit)(msg, f);
5919
+ } else {
5920
+ // Field is in a oneof.
5921
+ return UPB_PRIVATE(_upb_Message_GetOneofCase)(msg, f) ==
5922
+ upb_MiniTableField_Number(f);
5923
+ }
5924
+ }
5925
+
4929
5926
  UPB_API_INLINE upb_StringView
4930
5927
  upb_Message_GetString(const struct upb_Message* msg,
4931
5928
  const upb_MiniTableField* f, upb_StringView default_val) {
@@ -5088,84 +6085,82 @@ UPB_NODISCARD UPB_API_INLINE bool upb_Message_SetExtensionMessage(
5088
6085
  struct upb_Message* msg, const upb_MiniTableExtension* e,
5089
6086
  struct upb_Message* value, upb_Arena* a) {
5090
6087
  UPB_ASSERT(value);
5091
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Message);
5092
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5093
- UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte));
5094
- return upb_Message_SetExtension(msg, e, &value, a);
6088
+ upb_Extension* ext = UPB_PRIVATE(_upb_Message_MutableExtension)(msg, e, a);
6089
+ if (!ext) return false;
6090
+ upb_Extension_SetMessage(ext, value);
6091
+ return true;
5095
6092
  }
5096
6093
 
5097
6094
  UPB_NODISCARD UPB_API_INLINE bool upb_Message_SetExtensionBool(
5098
6095
  struct upb_Message* msg, const upb_MiniTableExtension* e, bool value,
5099
6096
  upb_Arena* a) {
5100
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Bool);
5101
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5102
- kUpb_FieldRep_1Byte);
5103
- return upb_Message_SetExtension(msg, e, &value, a);
6097
+ upb_Extension* ext = UPB_PRIVATE(_upb_Message_MutableExtension)(msg, e, a);
6098
+ if (!ext) return false;
6099
+ upb_Extension_SetBool(ext, value);
6100
+ return true;
5104
6101
  }
5105
6102
 
5106
6103
  UPB_NODISCARD UPB_API_INLINE bool upb_Message_SetExtensionDouble(
5107
6104
  struct upb_Message* msg, const upb_MiniTableExtension* e, double value,
5108
6105
  upb_Arena* a) {
5109
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Double);
5110
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5111
- kUpb_FieldRep_8Byte);
5112
- return upb_Message_SetExtension(msg, e, &value, a);
6106
+ upb_Extension* ext = UPB_PRIVATE(_upb_Message_MutableExtension)(msg, e, a);
6107
+ if (!ext) return false;
6108
+ upb_Extension_SetDouble(ext, value);
6109
+ return true;
5113
6110
  }
5114
6111
 
5115
6112
  UPB_NODISCARD UPB_API_INLINE bool upb_Message_SetExtensionFloat(
5116
6113
  struct upb_Message* msg, const upb_MiniTableExtension* e, float value,
5117
6114
  upb_Arena* a) {
5118
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Float);
5119
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5120
- kUpb_FieldRep_4Byte);
5121
- return upb_Message_SetExtension(msg, e, &value, a);
6115
+ upb_Extension* ext = UPB_PRIVATE(_upb_Message_MutableExtension)(msg, e, a);
6116
+ if (!ext) return false;
6117
+ upb_Extension_SetFloat(ext, value);
6118
+ return true;
5122
6119
  }
5123
6120
 
5124
6121
  UPB_NODISCARD UPB_API_INLINE bool upb_Message_SetExtensionInt32(
5125
6122
  struct upb_Message* msg, const upb_MiniTableExtension* e, int32_t value,
5126
6123
  upb_Arena* a) {
5127
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Int32 ||
5128
- upb_MiniTableExtension_CType(e) == kUpb_CType_Enum);
5129
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5130
- kUpb_FieldRep_4Byte);
5131
- return upb_Message_SetExtension(msg, e, &value, a);
6124
+ upb_Extension* ext = UPB_PRIVATE(_upb_Message_MutableExtension)(msg, e, a);
6125
+ if (!ext) return false;
6126
+ upb_Extension_SetInt32(ext, value);
6127
+ return true;
5132
6128
  }
5133
6129
 
5134
6130
  UPB_NODISCARD UPB_API_INLINE bool upb_Message_SetExtensionInt64(
5135
6131
  struct upb_Message* msg, const upb_MiniTableExtension* e, int64_t value,
5136
6132
  upb_Arena* a) {
5137
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Int64);
5138
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5139
- kUpb_FieldRep_8Byte);
5140
- return upb_Message_SetExtension(msg, e, &value, a);
6133
+ upb_Extension* ext = UPB_PRIVATE(_upb_Message_MutableExtension)(msg, e, a);
6134
+ if (!ext) return false;
6135
+ upb_Extension_SetInt64(ext, value);
6136
+ return true;
5141
6137
  }
5142
6138
 
5143
6139
  UPB_NODISCARD UPB_API_INLINE bool upb_Message_SetExtensionString(
5144
6140
  struct upb_Message* msg, const upb_MiniTableExtension* e,
5145
6141
  upb_StringView value, upb_Arena* a) {
5146
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_String ||
5147
- upb_MiniTableExtension_CType(e) == kUpb_CType_Bytes);
5148
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5149
- kUpb_FieldRep_StringView);
5150
- return upb_Message_SetExtension(msg, e, &value, a);
6142
+ upb_Extension* ext = UPB_PRIVATE(_upb_Message_MutableExtension)(msg, e, a);
6143
+ if (!ext) return false;
6144
+ upb_Extension_SetString(ext, value);
6145
+ return true;
5151
6146
  }
5152
6147
 
5153
6148
  UPB_NODISCARD UPB_API_INLINE bool upb_Message_SetExtensionUInt32(
5154
6149
  struct upb_Message* msg, const upb_MiniTableExtension* e, uint32_t value,
5155
6150
  upb_Arena* a) {
5156
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_UInt32);
5157
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5158
- kUpb_FieldRep_4Byte);
5159
- return upb_Message_SetExtension(msg, e, &value, a);
6151
+ upb_Extension* ext = UPB_PRIVATE(_upb_Message_MutableExtension)(msg, e, a);
6152
+ if (!ext) return false;
6153
+ upb_Extension_SetUInt32(ext, value);
6154
+ return true;
5160
6155
  }
5161
6156
 
5162
6157
  UPB_NODISCARD UPB_API_INLINE bool upb_Message_SetExtensionUInt64(
5163
6158
  struct upb_Message* msg, const upb_MiniTableExtension* e, uint64_t value,
5164
6159
  upb_Arena* a) {
5165
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_UInt64);
5166
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5167
- kUpb_FieldRep_8Byte);
5168
- return upb_Message_SetExtension(msg, e, &value, a);
6160
+ upb_Extension* ext = UPB_PRIVATE(_upb_Message_MutableExtension)(msg, e, a);
6161
+ if (!ext) return false;
6162
+ upb_Extension_SetUInt64(ext, value);
6163
+ return true;
5169
6164
  }
5170
6165
 
5171
6166
  // Universal Setters ///////////////////////////////////////////////////////////
@@ -5258,12 +6253,9 @@ UPB_NODISCARD UPB_API_INLINE bool upb_Message_SetUInt64(
5258
6253
  UPB_API_INLINE void upb_Message_Clear(struct upb_Message* msg,
5259
6254
  const upb_MiniTable* m) {
5260
6255
  UPB_ASSERT(!upb_Message_IsFrozen(msg));
5261
- upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
5262
- memset(msg, 0, m->UPB_PRIVATE(size));
5263
- if (in) {
5264
- // Reset the internal buffer to empty.
5265
- in->size = 0;
5266
- }
6256
+ // This also clears the pointer to the internal data (unknown fields and
6257
+ // extensions), which stays in the arena unreferenced.
6258
+ _upb_Message_AlignedMemsetZero(msg, m->UPB_PRIVATE(size));
5267
6259
  }
5268
6260
 
5269
6261
  UPB_API_INLINE void upb_Message_ClearBaseField(struct upb_Message* msg,
@@ -5276,7 +6268,7 @@ UPB_API_INLINE void upb_Message_ClearBaseField(struct upb_Message* msg,
5276
6268
  if (*ptr != upb_MiniTableField_Number(f)) return;
5277
6269
  *ptr = 0;
5278
6270
  }
5279
- const char zeros[16] = {0};
6271
+ UPB_ALIGN_AS(8) const char zeros[16] = {0};
5280
6272
  UPB_PRIVATE(_upb_MiniTableField_DataCopy)
5281
6273
  (f, UPB_PRIVATE(_upb_Message_MutableDataPtr)(msg, f), zeros);
5282
6274
  }
@@ -5291,7 +6283,7 @@ UPB_API_INLINE void upb_Message_ClearExtension(
5291
6283
  if (upb_TaggedAuxPtr_IsCanonicalExtension(tagged_ptr)) {
5292
6284
  const upb_Extension* ext =
5293
6285
  upb_TaggedAuxPtr_CanonicalExtension(tagged_ptr);
5294
- if (ext->ext == e) {
6286
+ if (upb_Extension_MiniTableExtension(ext) == e) {
5295
6287
  in->aux_data[i] = upb_TaggedAuxPtr_Null();
5296
6288
  return;
5297
6289
  }
@@ -5328,128 +6320,78 @@ UPB_NODISCARD UPB_API_INLINE void* upb_Message_ResizeArrayUninitialized(
5328
6320
  UPB_API_INLINE bool upb_Message_GetExtensionBool(
5329
6321
  const struct upb_Message* msg, const upb_MiniTableExtension* e,
5330
6322
  bool default_val) {
5331
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Bool);
5332
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5333
- kUpb_FieldRep_1Byte);
5334
- bool ret;
5335
- _upb_Message_GetExtensionField(msg, e, &default_val, &ret);
5336
- return ret;
6323
+ const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getext)(msg, e);
6324
+ return ext ? upb_Extension_GetBool(ext) : default_val;
5337
6325
  }
5338
6326
 
5339
6327
  UPB_API_INLINE double upb_Message_GetExtensionDouble(
5340
6328
  const struct upb_Message* msg, const upb_MiniTableExtension* e,
5341
6329
  double default_val) {
5342
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Double);
5343
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5344
- kUpb_FieldRep_8Byte);
5345
- double ret;
5346
- _upb_Message_GetExtensionField(msg, e, &default_val, &ret);
5347
- return ret;
6330
+ const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getext)(msg, e);
6331
+ return ext ? upb_Extension_GetDouble(ext) : default_val;
5348
6332
  }
5349
6333
 
5350
6334
  UPB_API_INLINE float upb_Message_GetExtensionFloat(
5351
6335
  const struct upb_Message* msg, const upb_MiniTableExtension* e,
5352
6336
  float default_val) {
5353
- float ret;
5354
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Float);
5355
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5356
- kUpb_FieldRep_4Byte);
5357
- _upb_Message_GetExtensionField(msg, e, &default_val, &ret);
5358
- return ret;
6337
+ const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getext)(msg, e);
6338
+ return ext ? upb_Extension_GetFloat(ext) : default_val;
5359
6339
  }
5360
6340
 
5361
6341
  UPB_API_INLINE int32_t upb_Message_GetExtensionInt32(
5362
6342
  const struct upb_Message* msg, const upb_MiniTableExtension* e,
5363
6343
  int32_t default_val) {
5364
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Int32 ||
5365
- upb_MiniTableExtension_CType(e) == kUpb_CType_Enum);
5366
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5367
- kUpb_FieldRep_4Byte);
5368
- int32_t ret;
5369
- _upb_Message_GetExtensionField(msg, e, &default_val, &ret);
5370
- return ret;
6344
+ const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getext)(msg, e);
6345
+ return ext ? upb_Extension_GetInt32(ext) : default_val;
5371
6346
  }
5372
6347
 
5373
6348
  UPB_API_INLINE int64_t upb_Message_GetExtensionInt64(
5374
6349
  const struct upb_Message* msg, const upb_MiniTableExtension* e,
5375
6350
  int64_t default_val) {
5376
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Int64);
5377
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5378
- kUpb_FieldRep_8Byte);
5379
- int64_t ret;
5380
- _upb_Message_GetExtensionField(msg, e, &default_val, &ret);
5381
- return ret;
6351
+ const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getext)(msg, e);
6352
+ return ext ? upb_Extension_GetInt64(ext) : default_val;
5382
6353
  }
5383
6354
 
5384
6355
  UPB_API_INLINE uint32_t upb_Message_GetExtensionUInt32(
5385
6356
  const struct upb_Message* msg, const upb_MiniTableExtension* e,
5386
6357
  uint32_t default_val) {
5387
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_UInt32);
5388
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5389
- kUpb_FieldRep_4Byte);
5390
- uint32_t ret;
5391
- _upb_Message_GetExtensionField(msg, e, &default_val, &ret);
5392
- return ret;
6358
+ const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getext)(msg, e);
6359
+ return ext ? upb_Extension_GetUInt32(ext) : default_val;
5393
6360
  }
5394
6361
 
5395
6362
  UPB_API_INLINE uint64_t upb_Message_GetExtensionUInt64(
5396
6363
  const struct upb_Message* msg, const upb_MiniTableExtension* e,
5397
6364
  uint64_t default_val) {
5398
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_UInt64);
5399
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5400
- kUpb_FieldRep_8Byte);
5401
- uint64_t ret;
5402
- _upb_Message_GetExtensionField(msg, e, &default_val, &ret);
5403
- return ret;
6365
+ const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getext)(msg, e);
6366
+ return ext ? upb_Extension_GetUInt64(ext) : default_val;
5404
6367
  }
5405
6368
 
5406
6369
  UPB_API_INLINE upb_StringView upb_Message_GetExtensionString(
5407
6370
  const struct upb_Message* msg, const upb_MiniTableExtension* e,
5408
6371
  upb_StringView default_val) {
5409
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_String ||
5410
- upb_MiniTableExtension_CType(e) == kUpb_CType_Bytes);
5411
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5412
- kUpb_FieldRep_StringView);
5413
- upb_StringView ret;
5414
- _upb_Message_GetExtensionField(msg, e, &default_val, &ret);
5415
- return ret;
6372
+ const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getext)(msg, e);
6373
+ return ext ? upb_Extension_GetString(ext) : default_val;
5416
6374
  }
5417
6375
 
5418
6376
  UPB_API_INLINE struct upb_Message* upb_Message_GetExtensionMessage(
5419
6377
  const struct upb_Message* msg, const upb_MiniTableExtension* e,
5420
6378
  struct upb_Message* default_val) {
5421
- UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Message);
5422
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
5423
- UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte));
5424
- struct upb_Message* ret;
5425
- _upb_Message_GetExtensionField(msg, e, &default_val, &ret);
5426
- return ret;
6379
+ const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getext)(msg, e);
6380
+ return ext ? (struct upb_Message*)upb_Extension_GetMessage(ext) : default_val;
5427
6381
  }
5428
6382
 
5429
6383
  // Repeated
5430
6384
  UPB_API_INLINE const upb_Array* upb_Message_GetExtensionArray(
5431
6385
  const struct upb_Message* msg, const upb_MiniTableExtension* e) {
5432
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(&e->UPB_PRIVATE(field)) ==
5433
- kUpb_FieldRep_NativePointer);
5434
- UPB_ASSUME(upb_MiniTableField_IsArray(&e->UPB_PRIVATE(field)));
5435
- UPB_ASSUME(e->UPB_PRIVATE(field).presence == 0);
5436
- upb_Array* ret;
5437
- const upb_Array* default_val = NULL;
5438
- _upb_Message_GetExtensionField(msg, e, &default_val, &ret);
5439
- return ret;
6386
+ const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getext)(msg, e);
6387
+ return ext ? upb_Extension_GetArray(ext) : NULL;
5440
6388
  }
5441
6389
 
5442
6390
  UPB_API_INLINE upb_Array* upb_Message_GetExtensionMutableArray(
5443
6391
  struct upb_Message* msg, const upb_MiniTableExtension* e) {
5444
6392
  UPB_ASSERT(!upb_Message_IsFrozen(msg));
5445
- UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(&e->UPB_PRIVATE(field)) ==
5446
- kUpb_FieldRep_NativePointer);
5447
- UPB_ASSUME(upb_MiniTableField_IsArray(&e->UPB_PRIVATE(field)));
5448
- UPB_ASSUME(e->UPB_PRIVATE(field).presence == 0);
5449
- upb_Array* ret;
5450
- upb_Array* default_val = NULL;
5451
- _upb_Message_GetExtensionField(msg, e, &default_val, &ret);
5452
- return ret;
6393
+ upb_Extension* ext = (upb_Extension*)UPB_PRIVATE(_upb_Message_Getext)(msg, e);
6394
+ return ext ? upb_Extension_GetMutableArray(ext) : NULL;
5453
6395
  }
5454
6396
 
5455
6397
  #ifdef __cplusplus
@@ -5523,6 +6465,13 @@ UPB_NODISCARD UPB_API_INLINE bool upb_Map_Set(upb_Map* map,
5523
6465
  UPB_API bool upb_Map_Delete(upb_Map* map, upb_MessageValue key,
5524
6466
  upb_MessageValue* val);
5525
6467
 
6468
+ // Pre-allocates memory for at least `size` elements in the map.
6469
+ // The `size` parameter accounts for the total capacity (existing elements plus
6470
+ // additional capacity). If the map already has sufficient capacity, this is a
6471
+ // no-op.
6472
+ UPB_NODISCARD UPB_API bool upb_Map_Reserve(upb_Map* map, size_t size,
6473
+ upb_Arena* arena);
6474
+
5526
6475
  // Map iteration:
5527
6476
  //
5528
6477
  // size_t iter = kUpb_Map_Begin;
@@ -5535,8 +6484,9 @@ UPB_API bool upb_Map_Delete(upb_Map* map, upb_MessageValue key,
5535
6484
 
5536
6485
  // Advances to the next entry. Returns false if no more entries are present.
5537
6486
  // Otherwise returns true and populates both *key and *value.
5538
- UPB_API bool upb_Map_Next(const upb_Map* map, upb_MessageValue* key,
5539
- upb_MessageValue* val, size_t* iter);
6487
+ UPB_NODISCARD UPB_API bool upb_Map_Next(const upb_Map* map,
6488
+ upb_MessageValue* key,
6489
+ upb_MessageValue* val, size_t* iter);
5540
6490
 
5541
6491
  // Sets the value for the entry pointed to by iter.
5542
6492
  // WARNING: this does not currently work for string values!
@@ -5555,12 +6505,10 @@ UPB_API void upb_Map_SetEntryValue(upb_Map* map, size_t iter,
5555
6505
  */
5556
6506
 
5557
6507
  // Advances to the next entry. Returns false if no more entries are present.
5558
- UPB_API bool upb_MapIterator_Next(const upb_Map* map, size_t* iter);
5559
-
5560
- // Returns true if the iterator still points to a valid entry, or false if the
5561
- // iterator is past the last element. It is an error to call this function with
5562
- // kUpb_Map_Begin (you must call next() at least once first).
5563
- UPB_API bool upb_MapIterator_Done(const upb_Map* map, size_t iter);
6508
+ // Once this returns false, the value of *iter is unspecified and it must not be
6509
+ // passed to any other iterator function.
6510
+ UPB_NODISCARD UPB_API bool upb_MapIterator_Next(const upb_Map* map,
6511
+ size_t* iter);
5564
6512
 
5565
6513
  // Returns the key and value for this entry of the map.
5566
6514
  UPB_API upb_MessageValue upb_MapIterator_Key(const upb_Map* map, size_t iter);
@@ -5629,7 +6577,7 @@ UPB_INLINE bool upb_Message_HasUnknown(const upb_Message* msg) {
5629
6577
  if (!in) return false;
5630
6578
  for (size_t i = 0; i < in->size; i++) {
5631
6579
  upb_TaggedAuxPtr tagged_ptr = in->aux_data[i];
5632
- if (tagged_ptr.ptr != 0 &&
6580
+ if (!upb_TaggedAuxPtr_IsNull(tagged_ptr) &&
5633
6581
  !upb_TaggedAuxPtr_IsSemanticallyKnown(tagged_ptr)) {
5634
6582
  return true;
5635
6583
  }
@@ -5637,39 +6585,6 @@ UPB_INLINE bool upb_Message_HasUnknown(const upb_Message* msg) {
5637
6585
  return false;
5638
6586
  }
5639
6587
 
5640
- // Removes a segment of unknown data from the message, advancing to the next
5641
- // segment. Returns false if the removed segment was at the end of the last
5642
- // chunk.
5643
- //
5644
- // This must be done while iterating:
5645
- //
5646
- // uintptr_t iter = kUpb_Message_UnknownBegin;
5647
- // upb_StringView data;
5648
- // // Iterate chunks
5649
- // while (upb_Message_NextUnknown(msg, &data, &iter)) {
5650
- // // Iterate within a chunk, deleting ranges
5651
- // while (ShouldDeleteSubSegment(&data)) {
5652
- // // Data now points to the region to be deleted
5653
- // switch (upb_Message_DeleteUnknown(msg, &data, &iter)) {
5654
- // case kUpb_Message_DeleteUnknown_DeletedLast: return ok;
5655
- // case kUpb_Message_DeleteUnknown_IterUpdated: break;
5656
- // // If DeleteUnknown returned kUpb_Message_DeleteUnknown_IterUpdated,
5657
- // // then data now points to the remaining unknown fields after the
5658
- // // region that was just deleted.
5659
- // case kUpb_Message_DeleteUnknown_AllocFail: return err;
5660
- // }
5661
- // }
5662
- // }
5663
- //
5664
- // The range given in `data` must be contained inside the most recently
5665
- // returned region.
5666
- //
5667
- // TODO: b/510055656 - Legacy API that works with messages that only have
5668
- // unknown data in upb_StringView format. Use `upb_Message_DeleteUnknown2` for
5669
- // messages that may have non-canonical extensions.
5670
- UPB_NODISCARD upb_Message_DeleteUnknownStatus upb_Message_DeleteUnknown(
5671
- upb_Message* msg, upb_StringView* data, uintptr_t* iter, upb_Arena* arena);
5672
-
5673
6588
  // Returns the number of extensions present in this message.
5674
6589
  size_t upb_Message_ExtensionCount(const upb_Message* msg);
5675
6590
 
@@ -5684,6 +6599,29 @@ UPB_INLINE bool UPB_PRIVATE(_upb_Message_NextExtensionReverse)(
5684
6599
  const struct upb_Message* msg, const upb_MiniTableExtension** out_e,
5685
6600
  upb_MessageValue* out_v, uintptr_t* iter);
5686
6601
 
6602
+ #define kUpb_Message_SerializableFieldBegin 0
6603
+
6604
+ // Iterates over all fields in the message that are set/present.
6605
+ //
6606
+ // NOTE: Unset/NULL repeated fields and maps are not considered set and will be
6607
+ // ignored. However, allocated repeated fields and maps (non-NULL pointer) with
6608
+ // zero elements are considered set/present and will be returned by this
6609
+ // iterator. Callers that need to skip empty collections should check their
6610
+ // sizes explicitly.
6611
+ //
6612
+ // To start iterating, set `iter = kUpb_Message_SerializableFieldBegin`.
6613
+ // Returns true if a serializable field was found, sets `*f` to that field,
6614
+ // and updates `*iter`. Returns false when iteration is complete.
6615
+ //
6616
+ // const upb_MiniTableField* f;
6617
+ // uintptr_t iter = kUpb_Message_SerializableFieldBegin;
6618
+ // while (upb_Message_NextSerializableField(msg, mt, &f, &iter)) {
6619
+ // // ...
6620
+ // }
6621
+ UPB_NODISCARD UPB_API bool upb_Message_NextSerializableField(
6622
+ const upb_Message* msg, const upb_MiniTable* mt,
6623
+ const upb_MiniTableField** f, uintptr_t* iter);
6624
+
5687
6625
  // Mark a message and all of its descendents as frozen/immutable.
5688
6626
  UPB_API void upb_Message_Freeze(upb_Message* msg, const upb_MiniTable* m);
5689
6627
 
@@ -6768,21 +7706,23 @@ typedef enum {
6768
7706
  } upb_EncodeStatus;
6769
7707
  // LINT.ThenChange(//depot/google3/third_party/upb/rust/sys/wire/wire.rs:encode_status)
6770
7708
 
6771
- UPB_INLINE uint32_t upb_EncodeOptions_MaxDepth(uint16_t depth) {
7709
+ UPB_NODISCARD UPB_INLINE uint32_t upb_EncodeOptions_MaxDepth(uint16_t depth) {
6772
7710
  return (uint32_t)depth << 16;
6773
7711
  }
6774
-
6775
- UPB_INLINE uint16_t upb_EncodeOptions_GetMaxDepth(uint32_t options) {
7712
+ UPB_NODISCARD UPB_INLINE uint16_t
7713
+ upb_EncodeOptions_GetMaxDepth(uint32_t options) {
6776
7714
  return options >> 16;
6777
7715
  }
6778
7716
 
6779
- UPB_INLINE uint16_t upb_EncodeOptions_GetEffectiveMaxDepth(uint32_t options) {
7717
+ UPB_NODISCARD UPB_INLINE uint16_t
7718
+ upb_EncodeOptions_GetEffectiveMaxDepth(uint32_t options) {
6780
7719
  uint16_t max_depth = upb_EncodeOptions_GetMaxDepth(options);
6781
7720
  return max_depth ? max_depth : kUpb_WireFormat_DefaultDepthLimit;
6782
7721
  }
6783
7722
 
6784
7723
  // Enforce an upper bound on recursion depth.
6785
- UPB_INLINE int upb_Encode_LimitDepth(uint32_t encode_options, uint32_t limit) {
7724
+ UPB_NODISCARD UPB_INLINE int upb_Encode_LimitDepth(uint32_t encode_options,
7725
+ uint32_t limit) {
6786
7726
  uint32_t max_depth = upb_EncodeOptions_GetEffectiveMaxDepth(encode_options);
6787
7727
  if (max_depth > limit) max_depth = limit;
6788
7728
  return (int)(upb_EncodeOptions_MaxDepth(max_depth) |
@@ -6800,7 +7740,8 @@ UPB_NODISCARD UPB_API upb_EncodeStatus upb_EncodeLengthPrefixed(
6800
7740
  upb_Arena* arena, char** buf, size_t* size);
6801
7741
  // Utility function for wrapper languages to get an error string from a
6802
7742
  // upb_EncodeStatus.
6803
- UPB_API const char* upb_EncodeStatus_String(upb_EncodeStatus status);
7743
+ UPB_NODISCARD UPB_API const char* upb_EncodeStatus_String(
7744
+ upb_EncodeStatus status);
6804
7745
 
6805
7746
  #ifdef __cplusplus
6806
7747
  } /* extern "C" */
@@ -6916,72 +7857,17 @@ extern const upb_MiniTableFile google_protobuf_descriptor_proto_upb_file_layout;
6916
7857
  extern "C" {
6917
7858
  #endif
6918
7859
 
6919
- extern const upb_MiniTable pb__enumvalue__JsonEnumValueOptions_msg_init;
6920
- extern const upb_MiniTableExtension* pb_enumvalue_json_ext;
6921
-
6922
- extern const upb_MiniTableFile google_protobuf_json_enumvalue_options_proto_upb_file_layout;
6923
-
6924
- #ifdef __cplusplus
6925
- } /* extern "C" */
6926
- #endif
6927
-
6928
-
6929
- #endif /* GOOGLE_PROTOBUF_JSON_ENUMVALUE_OPTIONS_PROTO_UPB_H__UPB_MINITABLE_H_ */
6930
-
6931
- #ifndef UPB_BASE_INTERNAL_LOG2_H_
6932
- #define UPB_BASE_INTERNAL_LOG2_H_
6933
-
6934
- #include <limits.h>
6935
- #include <stddef.h>
6936
- #include <stdint.h>
6937
-
6938
- // Must be last.
6939
-
6940
- #ifdef __cplusplus
6941
- extern "C" {
6942
- #endif
6943
-
6944
- UPB_INLINE int upb_Log2Ceiling(size_t x) {
6945
- if (x <= 1) return 0;
6946
- #if SIZE_MAX == ULLONG_MAX && UPB_HAS_BUILTIN(__builtin_clzll)
6947
- return (sizeof(size_t) * CHAR_BIT) - __builtin_clzll(x - 1);
6948
- #elif SIZE_MAX == ULONG_MAX && UPB_HAS_BUILTIN(__builtin_clzl)
6949
- return (sizeof(size_t) * CHAR_BIT) - __builtin_clzl(x - 1);
6950
- #elif SIZE_MAX == UINT_MAX && UPB_HAS_BUILTIN(__builtin_clz)
6951
- return (sizeof(size_t) * CHAR_BIT) - __builtin_clz(x - 1);
6952
- #else
6953
- if (x > SIZE_MAX / 2) return sizeof(size_t) * CHAR_BIT;
6954
- int lg2 = 0;
6955
- while (((size_t)1 << lg2) < x) lg2++;
6956
- return lg2;
6957
- #endif
6958
- }
6959
-
6960
- // Returns the smallest power of two that is greater than or equal to x. Returns
6961
- // SIZE_MAX if the computation would overflow.
6962
- UPB_INLINE size_t upb_RoundUpToPowerOfTwo(size_t x) {
6963
- int lg2 = upb_Log2Ceiling(x);
6964
- UPB_ASSERT(lg2 >= 0 && lg2 <= (int)sizeof(size_t) * CHAR_BIT);
6965
- if (lg2 == sizeof(size_t) * CHAR_BIT) {
6966
- return SIZE_MAX;
6967
- }
6968
- return ((size_t)1) << lg2;
6969
- }
7860
+ extern const upb_MiniTable pb__enumvalue__JsonEnumValueOptions_msg_init;
7861
+ extern const upb_MiniTableExtension* pb_enumvalue_json_ext;
6970
7862
 
6971
- UPB_INLINE bool upb_ShlOverflow(size_t* a, unsigned int b) {
6972
- if (*a > (SIZE_MAX >> b)) {
6973
- return true;
6974
- }
6975
- *a <<= b;
6976
- return false;
6977
- }
7863
+ extern const upb_MiniTableFile google_protobuf_json_enumvalue_options_proto_upb_file_layout;
6978
7864
 
6979
7865
  #ifdef __cplusplus
6980
- } /* extern "C" */
7866
+ } /* extern "C" */
6981
7867
  #endif
6982
7868
 
6983
7869
 
6984
- #endif /* UPB_BASE_INTERNAL_LOG2_H_ */
7870
+ #endif /* GOOGLE_PROTOBUF_JSON_ENUMVALUE_OPTIONS_PROTO_UPB_H__UPB_MINITABLE_H_ */
6985
7871
 
6986
7872
  #ifndef UPB_HASH_EXT_TABLE_H_
6987
7873
  #define UPB_HASH_EXT_TABLE_H_
@@ -7039,6 +7925,117 @@ size_t upb_exttable_size(const upb_exttable* t);
7039
7925
 
7040
7926
  #endif /* UPB_HASH_EXT_TABLE_H_ */
7041
7927
 
7928
+ #ifndef UPB_PORT_OVERFLOW_H_
7929
+ #define UPB_PORT_OVERFLOW_H_
7930
+
7931
+ #include <limits.h>
7932
+ #include <stddef.h>
7933
+ #include <stdint.h>
7934
+
7935
+ // Must be last
7936
+
7937
+ #if ((UPB_HAS_BUILTIN(__builtin_add_overflow) && \
7938
+ UPB_HAS_BUILTIN(__builtin_mul_overflow)) || \
7939
+ (defined(__GNUC__) && __GNUC__ >= 5)) && \
7940
+ !defined(UPB_DISABLE_BUILTIN_OVERFLOW)
7941
+ #define UPB_USE_BUILTIN_OVERFLOW
7942
+ #endif
7943
+
7944
+ UPB_NODISCARD UPB_INLINE bool upb_AddOverflow_size_t_size_t(size_t a, size_t b,
7945
+ size_t* out) {
7946
+ #ifdef UPB_USE_BUILTIN_OVERFLOW
7947
+ return __builtin_add_overflow(a, b, out);
7948
+ #else
7949
+ if (b > SIZE_MAX - a) return true;
7950
+ *out = a + b;
7951
+ return false;
7952
+ #endif
7953
+ }
7954
+
7955
+ UPB_NODISCARD UPB_INLINE bool upb_AddOverflow_size_t_uint32_t(size_t a,
7956
+ uint32_t b,
7957
+ size_t* out) {
7958
+ #ifdef UPB_USE_BUILTIN_OVERFLOW
7959
+ return __builtin_add_overflow(a, b, out);
7960
+ #else
7961
+ return upb_AddOverflow_size_t_size_t(a, (size_t)b, out);
7962
+ #endif
7963
+ }
7964
+
7965
+ UPB_NODISCARD UPB_INLINE bool upb_AddOverflow_uint32_t_size_t(uint32_t a,
7966
+ size_t b,
7967
+ size_t* out) {
7968
+ #ifdef UPB_USE_BUILTIN_OVERFLOW
7969
+ return __builtin_add_overflow(a, b, out);
7970
+ #else
7971
+ return upb_AddOverflow_size_t_size_t((size_t)a, b, out);
7972
+ #endif
7973
+ }
7974
+
7975
+ UPB_NODISCARD UPB_INLINE bool upb_MulOverflow_size_t_size_t(size_t a, size_t b,
7976
+ size_t* out) {
7977
+ #ifdef UPB_USE_BUILTIN_OVERFLOW
7978
+ return __builtin_mul_overflow(a, b, out);
7979
+ #else
7980
+ if (b != 0 && a > SIZE_MAX / b) return true;
7981
+ *out = a * b;
7982
+ return false;
7983
+ #endif
7984
+ }
7985
+
7986
+ UPB_NODISCARD UPB_INLINE bool upb_MulOverflow_size_t_uint32_t(size_t a,
7987
+ uint32_t b,
7988
+ size_t* out) {
7989
+ #ifdef UPB_USE_BUILTIN_OVERFLOW
7990
+ return __builtin_mul_overflow(a, b, out);
7991
+ #else
7992
+ return upb_MulOverflow_size_t_size_t(a, (size_t)b, out);
7993
+ #endif
7994
+ }
7995
+
7996
+ UPB_NODISCARD UPB_INLINE bool upb_MulOverflow_uint32_t_size_t(uint32_t a,
7997
+ size_t b,
7998
+ size_t* out) {
7999
+ #ifdef UPB_USE_BUILTIN_OVERFLOW
8000
+ return __builtin_mul_overflow(a, b, out);
8001
+ #else
8002
+ return upb_MulOverflow_size_t_size_t((size_t)a, b, out);
8003
+ #endif
8004
+ }
8005
+
8006
+ #if __STDC_VERSION__ >= 201112L && !defined(__cplusplus)
8007
+ #if SIZE_MAX > 0xffffffffULL
8008
+ #define upb_AddOverflow(a, b, out) \
8009
+ _Generic((a), \
8010
+ size_t: _Generic((b), \
8011
+ size_t: upb_AddOverflow_size_t_size_t(a, b, out), \
8012
+ uint32_t: upb_AddOverflow_size_t_uint32_t(a, b, out)), \
8013
+ uint32_t: _Generic((b), \
8014
+ size_t: upb_AddOverflow_uint32_t_size_t(a, b, out)))
8015
+
8016
+ #define upb_MulOverflow(a, b, out) \
8017
+ _Generic((a), \
8018
+ size_t: _Generic((b), \
8019
+ size_t: upb_MulOverflow_size_t_size_t(a, b, out), \
8020
+ uint32_t: upb_MulOverflow_size_t_uint32_t(a, b, out)), \
8021
+ uint32_t: _Generic((b), \
8022
+ size_t: upb_MulOverflow_uint32_t_size_t(a, b, out)))
8023
+ #else
8024
+ // On 32-bit platforms, size_t and uint32_t might be the same type.
8025
+ // We fallback to direct calls without _Generic to avoid duplicate association
8026
+ // errors.
8027
+ #define upb_AddOverflow(a, b, out) upb_AddOverflow_size_t_size_t(a, b, out)
8028
+ #define upb_MulOverflow(a, b, out) upb_MulOverflow_size_t_size_t(a, b, out)
8029
+ #endif
8030
+ #else
8031
+ // Fallback for C++ or older C.
8032
+ #define upb_AddOverflow(a, b, out) upb_AddOverflow_size_t_size_t(a, b, out)
8033
+ #define upb_MulOverflow(a, b, out) upb_MulOverflow_size_t_size_t(a, b, out)
8034
+ #endif
8035
+
8036
+
8037
+ #endif // UPB_PORT_OVERFLOW_H_
8038
+
7042
8039
  #ifndef UPB_JSON_DECODE_H_
7043
8040
  #define UPB_JSON_DECODE_H_
7044
8041
 
@@ -7427,14 +8424,22 @@ UPB_INLINE google_protobuf_FileDescriptorSet* google_protobuf_FileDescriptorSet_
7427
8424
  UPB_INLINE char* google_protobuf_FileDescriptorSet_serialize(const google_protobuf_FileDescriptorSet* msg,
7428
8425
  upb_Arena* arena, size_t* len) {
7429
8426
  char* ptr;
7430
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileDescriptorSet_msg_init, 0, arena, &ptr, len);
8427
+ upb_EncodeStatus status =
8428
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileDescriptorSet_msg_init, 0, arena, &ptr, len);
8429
+ if (status != kUpb_EncodeStatus_Ok) {
8430
+ return NULL;
8431
+ }
7431
8432
  return ptr;
7432
8433
  }
7433
8434
  UPB_INLINE char* google_protobuf_FileDescriptorSet_serialize_ex(const google_protobuf_FileDescriptorSet* msg,
7434
8435
  int options, upb_Arena* arena,
7435
8436
  size_t* len) {
7436
8437
  char* ptr;
7437
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileDescriptorSet_msg_init, options, arena, &ptr, len);
8438
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileDescriptorSet_msg_init,
8439
+ options, arena, &ptr, len);
8440
+ if (status != kUpb_EncodeStatus_Ok) {
8441
+ return NULL;
8442
+ }
7438
8443
  return ptr;
7439
8444
  }
7440
8445
  UPB_INLINE void google_protobuf_FileDescriptorSet_clear_file(google_protobuf_FileDescriptorSet* msg) {
@@ -7547,14 +8552,22 @@ UPB_INLINE google_protobuf_FileDescriptorProto* google_protobuf_FileDescriptorPr
7547
8552
  UPB_INLINE char* google_protobuf_FileDescriptorProto_serialize(const google_protobuf_FileDescriptorProto* msg,
7548
8553
  upb_Arena* arena, size_t* len) {
7549
8554
  char* ptr;
7550
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileDescriptorProto_msg_init, 0, arena, &ptr, len);
8555
+ upb_EncodeStatus status =
8556
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileDescriptorProto_msg_init, 0, arena, &ptr, len);
8557
+ if (status != kUpb_EncodeStatus_Ok) {
8558
+ return NULL;
8559
+ }
7551
8560
  return ptr;
7552
8561
  }
7553
8562
  UPB_INLINE char* google_protobuf_FileDescriptorProto_serialize_ex(const google_protobuf_FileDescriptorProto* msg,
7554
8563
  int options, upb_Arena* arena,
7555
8564
  size_t* len) {
7556
8565
  char* ptr;
7557
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileDescriptorProto_msg_init, options, arena, &ptr, len);
8566
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileDescriptorProto_msg_init,
8567
+ options, arena, &ptr, len);
8568
+ if (status != kUpb_EncodeStatus_Ok) {
8569
+ return NULL;
8570
+ }
7558
8571
  return ptr;
7559
8572
  }
7560
8573
  UPB_INLINE void google_protobuf_FileDescriptorProto_clear_name(google_protobuf_FileDescriptorProto* msg) {
@@ -8333,14 +9346,22 @@ UPB_INLINE google_protobuf_DescriptorProto* google_protobuf_DescriptorProto_pars
8333
9346
  UPB_INLINE char* google_protobuf_DescriptorProto_serialize(const google_protobuf_DescriptorProto* msg,
8334
9347
  upb_Arena* arena, size_t* len) {
8335
9348
  char* ptr;
8336
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto_msg_init, 0, arena, &ptr, len);
9349
+ upb_EncodeStatus status =
9350
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto_msg_init, 0, arena, &ptr, len);
9351
+ if (status != kUpb_EncodeStatus_Ok) {
9352
+ return NULL;
9353
+ }
8337
9354
  return ptr;
8338
9355
  }
8339
9356
  UPB_INLINE char* google_protobuf_DescriptorProto_serialize_ex(const google_protobuf_DescriptorProto* msg,
8340
9357
  int options, upb_Arena* arena,
8341
9358
  size_t* len) {
8342
9359
  char* ptr;
8343
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto_msg_init, options, arena, &ptr, len);
9360
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto_msg_init,
9361
+ options, arena, &ptr, len);
9362
+ if (status != kUpb_EncodeStatus_Ok) {
9363
+ return NULL;
9364
+ }
8344
9365
  return ptr;
8345
9366
  }
8346
9367
  UPB_INLINE void google_protobuf_DescriptorProto_clear_name(google_protobuf_DescriptorProto* msg) {
@@ -9075,14 +10096,22 @@ UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange* google_protobuf_Descr
9075
10096
  UPB_INLINE char* google_protobuf_DescriptorProto_ExtensionRange_serialize(const google_protobuf_DescriptorProto_ExtensionRange* msg,
9076
10097
  upb_Arena* arena, size_t* len) {
9077
10098
  char* ptr;
9078
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto__ExtensionRange_msg_init, 0, arena, &ptr, len);
10099
+ upb_EncodeStatus status =
10100
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto__ExtensionRange_msg_init, 0, arena, &ptr, len);
10101
+ if (status != kUpb_EncodeStatus_Ok) {
10102
+ return NULL;
10103
+ }
9079
10104
  return ptr;
9080
10105
  }
9081
10106
  UPB_INLINE char* google_protobuf_DescriptorProto_ExtensionRange_serialize_ex(const google_protobuf_DescriptorProto_ExtensionRange* msg,
9082
10107
  int options, upb_Arena* arena,
9083
10108
  size_t* len) {
9084
10109
  char* ptr;
9085
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto__ExtensionRange_msg_init, options, arena, &ptr, len);
10110
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto__ExtensionRange_msg_init,
10111
+ options, arena, &ptr, len);
10112
+ if (status != kUpb_EncodeStatus_Ok) {
10113
+ return NULL;
10114
+ }
9086
10115
  return ptr;
9087
10116
  }
9088
10117
  UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_clear_start(google_protobuf_DescriptorProto_ExtensionRange* msg) {
@@ -9186,14 +10215,22 @@ UPB_INLINE google_protobuf_DescriptorProto_ReservedRange* google_protobuf_Descri
9186
10215
  UPB_INLINE char* google_protobuf_DescriptorProto_ReservedRange_serialize(const google_protobuf_DescriptorProto_ReservedRange* msg,
9187
10216
  upb_Arena* arena, size_t* len) {
9188
10217
  char* ptr;
9189
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto__ReservedRange_msg_init, 0, arena, &ptr, len);
10218
+ upb_EncodeStatus status =
10219
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto__ReservedRange_msg_init, 0, arena, &ptr, len);
10220
+ if (status != kUpb_EncodeStatus_Ok) {
10221
+ return NULL;
10222
+ }
9190
10223
  return ptr;
9191
10224
  }
9192
10225
  UPB_INLINE char* google_protobuf_DescriptorProto_ReservedRange_serialize_ex(const google_protobuf_DescriptorProto_ReservedRange* msg,
9193
10226
  int options, upb_Arena* arena,
9194
10227
  size_t* len) {
9195
10228
  char* ptr;
9196
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto__ReservedRange_msg_init, options, arena, &ptr, len);
10229
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto__ReservedRange_msg_init,
10230
+ options, arena, &ptr, len);
10231
+ if (status != kUpb_EncodeStatus_Ok) {
10232
+ return NULL;
10233
+ }
9197
10234
  return ptr;
9198
10235
  }
9199
10236
  UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_clear_start(google_protobuf_DescriptorProto_ReservedRange* msg) {
@@ -9266,14 +10303,22 @@ UPB_INLINE google_protobuf_ExtensionRangeOptions* google_protobuf_ExtensionRange
9266
10303
  UPB_INLINE char* google_protobuf_ExtensionRangeOptions_serialize(const google_protobuf_ExtensionRangeOptions* msg,
9267
10304
  upb_Arena* arena, size_t* len) {
9268
10305
  char* ptr;
9269
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__ExtensionRangeOptions_msg_init, 0, arena, &ptr, len);
10306
+ upb_EncodeStatus status =
10307
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__ExtensionRangeOptions_msg_init, 0, arena, &ptr, len);
10308
+ if (status != kUpb_EncodeStatus_Ok) {
10309
+ return NULL;
10310
+ }
9270
10311
  return ptr;
9271
10312
  }
9272
10313
  UPB_INLINE char* google_protobuf_ExtensionRangeOptions_serialize_ex(const google_protobuf_ExtensionRangeOptions* msg,
9273
10314
  int options, upb_Arena* arena,
9274
10315
  size_t* len) {
9275
10316
  char* ptr;
9276
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__ExtensionRangeOptions_msg_init, options, arena, &ptr, len);
10317
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__ExtensionRangeOptions_msg_init,
10318
+ options, arena, &ptr, len);
10319
+ if (status != kUpb_EncodeStatus_Ok) {
10320
+ return NULL;
10321
+ }
9277
10322
  return ptr;
9278
10323
  }
9279
10324
  UPB_INLINE void google_protobuf_ExtensionRangeOptions_clear_declaration(google_protobuf_ExtensionRangeOptions* msg) {
@@ -9517,14 +10562,22 @@ UPB_INLINE google_protobuf_ExtensionRangeOptions_Declaration* google_protobuf_Ex
9517
10562
  UPB_INLINE char* google_protobuf_ExtensionRangeOptions_Declaration_serialize(const google_protobuf_ExtensionRangeOptions_Declaration* msg,
9518
10563
  upb_Arena* arena, size_t* len) {
9519
10564
  char* ptr;
9520
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__ExtensionRangeOptions__Declaration_msg_init, 0, arena, &ptr, len);
10565
+ upb_EncodeStatus status =
10566
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__ExtensionRangeOptions__Declaration_msg_init, 0, arena, &ptr, len);
10567
+ if (status != kUpb_EncodeStatus_Ok) {
10568
+ return NULL;
10569
+ }
9521
10570
  return ptr;
9522
10571
  }
9523
10572
  UPB_INLINE char* google_protobuf_ExtensionRangeOptions_Declaration_serialize_ex(const google_protobuf_ExtensionRangeOptions_Declaration* msg,
9524
10573
  int options, upb_Arena* arena,
9525
10574
  size_t* len) {
9526
10575
  char* ptr;
9527
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__ExtensionRangeOptions__Declaration_msg_init, options, arena, &ptr, len);
10576
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__ExtensionRangeOptions__Declaration_msg_init,
10577
+ options, arena, &ptr, len);
10578
+ if (status != kUpb_EncodeStatus_Ok) {
10579
+ return NULL;
10580
+ }
9528
10581
  return ptr;
9529
10582
  }
9530
10583
  UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_clear_number(google_protobuf_ExtensionRangeOptions_Declaration* msg) {
@@ -9657,14 +10710,22 @@ UPB_INLINE google_protobuf_FieldDescriptorProto* google_protobuf_FieldDescriptor
9657
10710
  UPB_INLINE char* google_protobuf_FieldDescriptorProto_serialize(const google_protobuf_FieldDescriptorProto* msg,
9658
10711
  upb_Arena* arena, size_t* len) {
9659
10712
  char* ptr;
9660
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldDescriptorProto_msg_init, 0, arena, &ptr, len);
10713
+ upb_EncodeStatus status =
10714
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldDescriptorProto_msg_init, 0, arena, &ptr, len);
10715
+ if (status != kUpb_EncodeStatus_Ok) {
10716
+ return NULL;
10717
+ }
9661
10718
  return ptr;
9662
10719
  }
9663
10720
  UPB_INLINE char* google_protobuf_FieldDescriptorProto_serialize_ex(const google_protobuf_FieldDescriptorProto* msg,
9664
10721
  int options, upb_Arena* arena,
9665
10722
  size_t* len) {
9666
10723
  char* ptr;
9667
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldDescriptorProto_msg_init, options, arena, &ptr, len);
10724
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldDescriptorProto_msg_init,
10725
+ options, arena, &ptr, len);
10726
+ if (status != kUpb_EncodeStatus_Ok) {
10727
+ return NULL;
10728
+ }
9668
10729
  return ptr;
9669
10730
  }
9670
10731
  UPB_INLINE void google_protobuf_FieldDescriptorProto_clear_name(google_protobuf_FieldDescriptorProto* msg) {
@@ -9928,14 +10989,22 @@ UPB_INLINE google_protobuf_OneofDescriptorProto* google_protobuf_OneofDescriptor
9928
10989
  UPB_INLINE char* google_protobuf_OneofDescriptorProto_serialize(const google_protobuf_OneofDescriptorProto* msg,
9929
10990
  upb_Arena* arena, size_t* len) {
9930
10991
  char* ptr;
9931
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__OneofDescriptorProto_msg_init, 0, arena, &ptr, len);
10992
+ upb_EncodeStatus status =
10993
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__OneofDescriptorProto_msg_init, 0, arena, &ptr, len);
10994
+ if (status != kUpb_EncodeStatus_Ok) {
10995
+ return NULL;
10996
+ }
9932
10997
  return ptr;
9933
10998
  }
9934
10999
  UPB_INLINE char* google_protobuf_OneofDescriptorProto_serialize_ex(const google_protobuf_OneofDescriptorProto* msg,
9935
11000
  int options, upb_Arena* arena,
9936
11001
  size_t* len) {
9937
11002
  char* ptr;
9938
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__OneofDescriptorProto_msg_init, options, arena, &ptr, len);
11003
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__OneofDescriptorProto_msg_init,
11004
+ options, arena, &ptr, len);
11005
+ if (status != kUpb_EncodeStatus_Ok) {
11006
+ return NULL;
11007
+ }
9939
11008
  return ptr;
9940
11009
  }
9941
11010
  UPB_INLINE void google_protobuf_OneofDescriptorProto_clear_name(google_protobuf_OneofDescriptorProto* msg) {
@@ -10019,14 +11088,22 @@ UPB_INLINE google_protobuf_EnumDescriptorProto* google_protobuf_EnumDescriptorPr
10019
11088
  UPB_INLINE char* google_protobuf_EnumDescriptorProto_serialize(const google_protobuf_EnumDescriptorProto* msg,
10020
11089
  upb_Arena* arena, size_t* len) {
10021
11090
  char* ptr;
10022
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumDescriptorProto_msg_init, 0, arena, &ptr, len);
11091
+ upb_EncodeStatus status =
11092
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumDescriptorProto_msg_init, 0, arena, &ptr, len);
11093
+ if (status != kUpb_EncodeStatus_Ok) {
11094
+ return NULL;
11095
+ }
10023
11096
  return ptr;
10024
11097
  }
10025
11098
  UPB_INLINE char* google_protobuf_EnumDescriptorProto_serialize_ex(const google_protobuf_EnumDescriptorProto* msg,
10026
11099
  int options, upb_Arena* arena,
10027
11100
  size_t* len) {
10028
11101
  char* ptr;
10029
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumDescriptorProto_msg_init, options, arena, &ptr, len);
11102
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumDescriptorProto_msg_init,
11103
+ options, arena, &ptr, len);
11104
+ if (status != kUpb_EncodeStatus_Ok) {
11105
+ return NULL;
11106
+ }
10030
11107
  return ptr;
10031
11108
  }
10032
11109
  UPB_INLINE void google_protobuf_EnumDescriptorProto_clear_name(google_protobuf_EnumDescriptorProto* msg) {
@@ -10361,14 +11438,22 @@ UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange* google_protobu
10361
11438
  UPB_INLINE char* google_protobuf_EnumDescriptorProto_EnumReservedRange_serialize(const google_protobuf_EnumDescriptorProto_EnumReservedRange* msg,
10362
11439
  upb_Arena* arena, size_t* len) {
10363
11440
  char* ptr;
10364
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init, 0, arena, &ptr, len);
11441
+ upb_EncodeStatus status =
11442
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init, 0, arena, &ptr, len);
11443
+ if (status != kUpb_EncodeStatus_Ok) {
11444
+ return NULL;
11445
+ }
10365
11446
  return ptr;
10366
11447
  }
10367
11448
  UPB_INLINE char* google_protobuf_EnumDescriptorProto_EnumReservedRange_serialize_ex(const google_protobuf_EnumDescriptorProto_EnumReservedRange* msg,
10368
11449
  int options, upb_Arena* arena,
10369
11450
  size_t* len) {
10370
11451
  char* ptr;
10371
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init, options, arena, &ptr, len);
11452
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init,
11453
+ options, arena, &ptr, len);
11454
+ if (status != kUpb_EncodeStatus_Ok) {
11455
+ return NULL;
11456
+ }
10372
11457
  return ptr;
10373
11458
  }
10374
11459
  UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_clear_start(google_protobuf_EnumDescriptorProto_EnumReservedRange* msg) {
@@ -10441,14 +11526,22 @@ UPB_INLINE google_protobuf_EnumValueDescriptorProto* google_protobuf_EnumValueDe
10441
11526
  UPB_INLINE char* google_protobuf_EnumValueDescriptorProto_serialize(const google_protobuf_EnumValueDescriptorProto* msg,
10442
11527
  upb_Arena* arena, size_t* len) {
10443
11528
  char* ptr;
10444
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumValueDescriptorProto_msg_init, 0, arena, &ptr, len);
11529
+ upb_EncodeStatus status =
11530
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumValueDescriptorProto_msg_init, 0, arena, &ptr, len);
11531
+ if (status != kUpb_EncodeStatus_Ok) {
11532
+ return NULL;
11533
+ }
10445
11534
  return ptr;
10446
11535
  }
10447
11536
  UPB_INLINE char* google_protobuf_EnumValueDescriptorProto_serialize_ex(const google_protobuf_EnumValueDescriptorProto* msg,
10448
11537
  int options, upb_Arena* arena,
10449
11538
  size_t* len) {
10450
11539
  char* ptr;
10451
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumValueDescriptorProto_msg_init, options, arena, &ptr, len);
11540
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumValueDescriptorProto_msg_init,
11541
+ options, arena, &ptr, len);
11542
+ if (status != kUpb_EncodeStatus_Ok) {
11543
+ return NULL;
11544
+ }
10452
11545
  return ptr;
10453
11546
  }
10454
11547
  UPB_INLINE void google_protobuf_EnumValueDescriptorProto_clear_name(google_protobuf_EnumValueDescriptorProto* msg) {
@@ -10552,14 +11645,22 @@ UPB_INLINE google_protobuf_ServiceDescriptorProto* google_protobuf_ServiceDescri
10552
11645
  UPB_INLINE char* google_protobuf_ServiceDescriptorProto_serialize(const google_protobuf_ServiceDescriptorProto* msg,
10553
11646
  upb_Arena* arena, size_t* len) {
10554
11647
  char* ptr;
10555
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__ServiceDescriptorProto_msg_init, 0, arena, &ptr, len);
11648
+ upb_EncodeStatus status =
11649
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__ServiceDescriptorProto_msg_init, 0, arena, &ptr, len);
11650
+ if (status != kUpb_EncodeStatus_Ok) {
11651
+ return NULL;
11652
+ }
10556
11653
  return ptr;
10557
11654
  }
10558
11655
  UPB_INLINE char* google_protobuf_ServiceDescriptorProto_serialize_ex(const google_protobuf_ServiceDescriptorProto* msg,
10559
11656
  int options, upb_Arena* arena,
10560
11657
  size_t* len) {
10561
11658
  char* ptr;
10562
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__ServiceDescriptorProto_msg_init, options, arena, &ptr, len);
11659
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__ServiceDescriptorProto_msg_init,
11660
+ options, arena, &ptr, len);
11661
+ if (status != kUpb_EncodeStatus_Ok) {
11662
+ return NULL;
11663
+ }
10563
11664
  return ptr;
10564
11665
  }
10565
11666
  UPB_INLINE void google_protobuf_ServiceDescriptorProto_clear_name(google_protobuf_ServiceDescriptorProto* msg) {
@@ -10723,14 +11824,22 @@ UPB_INLINE google_protobuf_MethodDescriptorProto* google_protobuf_MethodDescript
10723
11824
  UPB_INLINE char* google_protobuf_MethodDescriptorProto_serialize(const google_protobuf_MethodDescriptorProto* msg,
10724
11825
  upb_Arena* arena, size_t* len) {
10725
11826
  char* ptr;
10726
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__MethodDescriptorProto_msg_init, 0, arena, &ptr, len);
11827
+ upb_EncodeStatus status =
11828
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__MethodDescriptorProto_msg_init, 0, arena, &ptr, len);
11829
+ if (status != kUpb_EncodeStatus_Ok) {
11830
+ return NULL;
11831
+ }
10727
11832
  return ptr;
10728
11833
  }
10729
11834
  UPB_INLINE char* google_protobuf_MethodDescriptorProto_serialize_ex(const google_protobuf_MethodDescriptorProto* msg,
10730
11835
  int options, upb_Arena* arena,
10731
11836
  size_t* len) {
10732
11837
  char* ptr;
10733
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__MethodDescriptorProto_msg_init, options, arena, &ptr, len);
11838
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__MethodDescriptorProto_msg_init,
11839
+ options, arena, &ptr, len);
11840
+ if (status != kUpb_EncodeStatus_Ok) {
11841
+ return NULL;
11842
+ }
10734
11843
  return ptr;
10735
11844
  }
10736
11845
  UPB_INLINE void google_protobuf_MethodDescriptorProto_clear_name(google_protobuf_MethodDescriptorProto* msg) {
@@ -10894,14 +12003,22 @@ UPB_INLINE google_protobuf_FileOptions* google_protobuf_FileOptions_parse_ex(
10894
12003
  UPB_INLINE char* google_protobuf_FileOptions_serialize(const google_protobuf_FileOptions* msg,
10895
12004
  upb_Arena* arena, size_t* len) {
10896
12005
  char* ptr;
10897
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileOptions_msg_init, 0, arena, &ptr, len);
12006
+ upb_EncodeStatus status =
12007
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileOptions_msg_init, 0, arena, &ptr, len);
12008
+ if (status != kUpb_EncodeStatus_Ok) {
12009
+ return NULL;
12010
+ }
10898
12011
  return ptr;
10899
12012
  }
10900
12013
  UPB_INLINE char* google_protobuf_FileOptions_serialize_ex(const google_protobuf_FileOptions* msg,
10901
12014
  int options, upb_Arena* arena,
10902
12015
  size_t* len) {
10903
12016
  char* ptr;
10904
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileOptions_msg_init, options, arena, &ptr, len);
12017
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileOptions_msg_init,
12018
+ options, arena, &ptr, len);
12019
+ if (status != kUpb_EncodeStatus_Ok) {
12020
+ return NULL;
12021
+ }
10905
12022
  return ptr;
10906
12023
  }
10907
12024
  UPB_INLINE void google_protobuf_FileOptions_clear_java_package(google_protobuf_FileOptions* msg) {
@@ -11425,14 +12542,22 @@ UPB_INLINE google_protobuf_MessageOptions* google_protobuf_MessageOptions_parse_
11425
12542
  UPB_INLINE char* google_protobuf_MessageOptions_serialize(const google_protobuf_MessageOptions* msg,
11426
12543
  upb_Arena* arena, size_t* len) {
11427
12544
  char* ptr;
11428
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__MessageOptions_msg_init, 0, arena, &ptr, len);
12545
+ upb_EncodeStatus status =
12546
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__MessageOptions_msg_init, 0, arena, &ptr, len);
12547
+ if (status != kUpb_EncodeStatus_Ok) {
12548
+ return NULL;
12549
+ }
11429
12550
  return ptr;
11430
12551
  }
11431
12552
  UPB_INLINE char* google_protobuf_MessageOptions_serialize_ex(const google_protobuf_MessageOptions* msg,
11432
12553
  int options, upb_Arena* arena,
11433
12554
  size_t* len) {
11434
12555
  char* ptr;
11435
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__MessageOptions_msg_init, options, arena, &ptr, len);
12556
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__MessageOptions_msg_init,
12557
+ options, arena, &ptr, len);
12558
+ if (status != kUpb_EncodeStatus_Ok) {
12559
+ return NULL;
12560
+ }
11436
12561
  return ptr;
11437
12562
  }
11438
12563
  UPB_INLINE void google_protobuf_MessageOptions_clear_message_set_wire_format(google_protobuf_MessageOptions* msg) {
@@ -11676,14 +12801,22 @@ UPB_INLINE google_protobuf_FieldOptions* google_protobuf_FieldOptions_parse_ex(
11676
12801
  UPB_INLINE char* google_protobuf_FieldOptions_serialize(const google_protobuf_FieldOptions* msg,
11677
12802
  upb_Arena* arena, size_t* len) {
11678
12803
  char* ptr;
11679
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions_msg_init, 0, arena, &ptr, len);
12804
+ upb_EncodeStatus status =
12805
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions_msg_init, 0, arena, &ptr, len);
12806
+ if (status != kUpb_EncodeStatus_Ok) {
12807
+ return NULL;
12808
+ }
11680
12809
  return ptr;
11681
12810
  }
11682
12811
  UPB_INLINE char* google_protobuf_FieldOptions_serialize_ex(const google_protobuf_FieldOptions* msg,
11683
12812
  int options, upb_Arena* arena,
11684
12813
  size_t* len) {
11685
12814
  char* ptr;
11686
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions_msg_init, options, arena, &ptr, len);
12815
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions_msg_init,
12816
+ options, arena, &ptr, len);
12817
+ if (status != kUpb_EncodeStatus_Ok) {
12818
+ return NULL;
12819
+ }
11687
12820
  return ptr;
11688
12821
  }
11689
12822
  UPB_INLINE void google_protobuf_FieldOptions_clear_ctype(google_protobuf_FieldOptions* msg) {
@@ -12189,14 +13322,22 @@ UPB_INLINE google_protobuf_FieldOptions_EditionDefault* google_protobuf_FieldOpt
12189
13322
  UPB_INLINE char* google_protobuf_FieldOptions_EditionDefault_serialize(const google_protobuf_FieldOptions_EditionDefault* msg,
12190
13323
  upb_Arena* arena, size_t* len) {
12191
13324
  char* ptr;
12192
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions__EditionDefault_msg_init, 0, arena, &ptr, len);
13325
+ upb_EncodeStatus status =
13326
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions__EditionDefault_msg_init, 0, arena, &ptr, len);
13327
+ if (status != kUpb_EncodeStatus_Ok) {
13328
+ return NULL;
13329
+ }
12193
13330
  return ptr;
12194
13331
  }
12195
13332
  UPB_INLINE char* google_protobuf_FieldOptions_EditionDefault_serialize_ex(const google_protobuf_FieldOptions_EditionDefault* msg,
12196
13333
  int options, upb_Arena* arena,
12197
13334
  size_t* len) {
12198
13335
  char* ptr;
12199
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions__EditionDefault_msg_init, options, arena, &ptr, len);
13336
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions__EditionDefault_msg_init,
13337
+ options, arena, &ptr, len);
13338
+ if (status != kUpb_EncodeStatus_Ok) {
13339
+ return NULL;
13340
+ }
12200
13341
  return ptr;
12201
13342
  }
12202
13343
  UPB_INLINE void google_protobuf_FieldOptions_EditionDefault_clear_value(google_protobuf_FieldOptions_EditionDefault* msg) {
@@ -12269,14 +13410,22 @@ UPB_INLINE google_protobuf_FieldOptions_FeatureSupport* google_protobuf_FieldOpt
12269
13410
  UPB_INLINE char* google_protobuf_FieldOptions_FeatureSupport_serialize(const google_protobuf_FieldOptions_FeatureSupport* msg,
12270
13411
  upb_Arena* arena, size_t* len) {
12271
13412
  char* ptr;
12272
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions__FeatureSupport_msg_init, 0, arena, &ptr, len);
13413
+ upb_EncodeStatus status =
13414
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions__FeatureSupport_msg_init, 0, arena, &ptr, len);
13415
+ if (status != kUpb_EncodeStatus_Ok) {
13416
+ return NULL;
13417
+ }
12273
13418
  return ptr;
12274
13419
  }
12275
13420
  UPB_INLINE char* google_protobuf_FieldOptions_FeatureSupport_serialize_ex(const google_protobuf_FieldOptions_FeatureSupport* msg,
12276
13421
  int options, upb_Arena* arena,
12277
13422
  size_t* len) {
12278
13423
  char* ptr;
12279
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions__FeatureSupport_msg_init, options, arena, &ptr, len);
13424
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions__FeatureSupport_msg_init,
13425
+ options, arena, &ptr, len);
13426
+ if (status != kUpb_EncodeStatus_Ok) {
13427
+ return NULL;
13428
+ }
12280
13429
  return ptr;
12281
13430
  }
12282
13431
  UPB_INLINE void google_protobuf_FieldOptions_FeatureSupport_clear_edition_introduced(google_protobuf_FieldOptions_FeatureSupport* msg) {
@@ -12409,14 +13558,22 @@ UPB_INLINE google_protobuf_OneofOptions* google_protobuf_OneofOptions_parse_ex(
12409
13558
  UPB_INLINE char* google_protobuf_OneofOptions_serialize(const google_protobuf_OneofOptions* msg,
12410
13559
  upb_Arena* arena, size_t* len) {
12411
13560
  char* ptr;
12412
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__OneofOptions_msg_init, 0, arena, &ptr, len);
13561
+ upb_EncodeStatus status =
13562
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__OneofOptions_msg_init, 0, arena, &ptr, len);
13563
+ if (status != kUpb_EncodeStatus_Ok) {
13564
+ return NULL;
13565
+ }
12413
13566
  return ptr;
12414
13567
  }
12415
13568
  UPB_INLINE char* google_protobuf_OneofOptions_serialize_ex(const google_protobuf_OneofOptions* msg,
12416
13569
  int options, upb_Arena* arena,
12417
13570
  size_t* len) {
12418
13571
  char* ptr;
12419
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__OneofOptions_msg_init, options, arena, &ptr, len);
13572
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__OneofOptions_msg_init,
13573
+ options, arena, &ptr, len);
13574
+ if (status != kUpb_EncodeStatus_Ok) {
13575
+ return NULL;
13576
+ }
12420
13577
  return ptr;
12421
13578
  }
12422
13579
  UPB_INLINE void google_protobuf_OneofOptions_clear_features(google_protobuf_OneofOptions* msg) {
@@ -12560,14 +13717,22 @@ UPB_INLINE google_protobuf_EnumOptions* google_protobuf_EnumOptions_parse_ex(
12560
13717
  UPB_INLINE char* google_protobuf_EnumOptions_serialize(const google_protobuf_EnumOptions* msg,
12561
13718
  upb_Arena* arena, size_t* len) {
12562
13719
  char* ptr;
12563
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumOptions_msg_init, 0, arena, &ptr, len);
13720
+ upb_EncodeStatus status =
13721
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumOptions_msg_init, 0, arena, &ptr, len);
13722
+ if (status != kUpb_EncodeStatus_Ok) {
13723
+ return NULL;
13724
+ }
12564
13725
  return ptr;
12565
13726
  }
12566
13727
  UPB_INLINE char* google_protobuf_EnumOptions_serialize_ex(const google_protobuf_EnumOptions* msg,
12567
13728
  int options, upb_Arena* arena,
12568
13729
  size_t* len) {
12569
13730
  char* ptr;
12570
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumOptions_msg_init, options, arena, &ptr, len);
13731
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumOptions_msg_init,
13732
+ options, arena, &ptr, len);
13733
+ if (status != kUpb_EncodeStatus_Ok) {
13734
+ return NULL;
13735
+ }
12571
13736
  return ptr;
12572
13737
  }
12573
13738
  UPB_INLINE void google_protobuf_EnumOptions_clear_allow_alias(google_protobuf_EnumOptions* msg) {
@@ -12771,14 +13936,22 @@ UPB_INLINE google_protobuf_EnumValueOptions* google_protobuf_EnumValueOptions_pa
12771
13936
  UPB_INLINE char* google_protobuf_EnumValueOptions_serialize(const google_protobuf_EnumValueOptions* msg,
12772
13937
  upb_Arena* arena, size_t* len) {
12773
13938
  char* ptr;
12774
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumValueOptions_msg_init, 0, arena, &ptr, len);
13939
+ upb_EncodeStatus status =
13940
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumValueOptions_msg_init, 0, arena, &ptr, len);
13941
+ if (status != kUpb_EncodeStatus_Ok) {
13942
+ return NULL;
13943
+ }
12775
13944
  return ptr;
12776
13945
  }
12777
13946
  UPB_INLINE char* google_protobuf_EnumValueOptions_serialize_ex(const google_protobuf_EnumValueOptions* msg,
12778
13947
  int options, upb_Arena* arena,
12779
13948
  size_t* len) {
12780
13949
  char* ptr;
12781
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumValueOptions_msg_init, options, arena, &ptr, len);
13950
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumValueOptions_msg_init,
13951
+ options, arena, &ptr, len);
13952
+ if (status != kUpb_EncodeStatus_Ok) {
13953
+ return NULL;
13954
+ }
12782
13955
  return ptr;
12783
13956
  }
12784
13957
  UPB_INLINE void google_protobuf_EnumValueOptions_clear_deprecated(google_protobuf_EnumValueOptions* msg) {
@@ -12993,14 +14166,22 @@ UPB_INLINE google_protobuf_ServiceOptions* google_protobuf_ServiceOptions_parse_
12993
14166
  UPB_INLINE char* google_protobuf_ServiceOptions_serialize(const google_protobuf_ServiceOptions* msg,
12994
14167
  upb_Arena* arena, size_t* len) {
12995
14168
  char* ptr;
12996
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__ServiceOptions_msg_init, 0, arena, &ptr, len);
14169
+ upb_EncodeStatus status =
14170
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__ServiceOptions_msg_init, 0, arena, &ptr, len);
14171
+ if (status != kUpb_EncodeStatus_Ok) {
14172
+ return NULL;
14173
+ }
12997
14174
  return ptr;
12998
14175
  }
12999
14176
  UPB_INLINE char* google_protobuf_ServiceOptions_serialize_ex(const google_protobuf_ServiceOptions* msg,
13000
14177
  int options, upb_Arena* arena,
13001
14178
  size_t* len) {
13002
14179
  char* ptr;
13003
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__ServiceOptions_msg_init, options, arena, &ptr, len);
14180
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__ServiceOptions_msg_init,
14181
+ options, arena, &ptr, len);
14182
+ if (status != kUpb_EncodeStatus_Ok) {
14183
+ return NULL;
14184
+ }
13004
14185
  return ptr;
13005
14186
  }
13006
14187
  UPB_INLINE void google_protobuf_ServiceOptions_clear_deprecated(google_protobuf_ServiceOptions* msg) {
@@ -13164,14 +14345,22 @@ UPB_INLINE google_protobuf_MethodOptions* google_protobuf_MethodOptions_parse_ex
13164
14345
  UPB_INLINE char* google_protobuf_MethodOptions_serialize(const google_protobuf_MethodOptions* msg,
13165
14346
  upb_Arena* arena, size_t* len) {
13166
14347
  char* ptr;
13167
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__MethodOptions_msg_init, 0, arena, &ptr, len);
14348
+ upb_EncodeStatus status =
14349
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__MethodOptions_msg_init, 0, arena, &ptr, len);
14350
+ if (status != kUpb_EncodeStatus_Ok) {
14351
+ return NULL;
14352
+ }
13168
14353
  return ptr;
13169
14354
  }
13170
14355
  UPB_INLINE char* google_protobuf_MethodOptions_serialize_ex(const google_protobuf_MethodOptions* msg,
13171
14356
  int options, upb_Arena* arena,
13172
14357
  size_t* len) {
13173
14358
  char* ptr;
13174
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__MethodOptions_msg_init, options, arena, &ptr, len);
14359
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__MethodOptions_msg_init,
14360
+ options, arena, &ptr, len);
14361
+ if (status != kUpb_EncodeStatus_Ok) {
14362
+ return NULL;
14363
+ }
13175
14364
  return ptr;
13176
14365
  }
13177
14366
  UPB_INLINE void google_protobuf_MethodOptions_clear_deprecated(google_protobuf_MethodOptions* msg) {
@@ -13355,14 +14544,22 @@ UPB_INLINE google_protobuf_UninterpretedOption* google_protobuf_UninterpretedOpt
13355
14544
  UPB_INLINE char* google_protobuf_UninterpretedOption_serialize(const google_protobuf_UninterpretedOption* msg,
13356
14545
  upb_Arena* arena, size_t* len) {
13357
14546
  char* ptr;
13358
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__UninterpretedOption_msg_init, 0, arena, &ptr, len);
14547
+ upb_EncodeStatus status =
14548
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__UninterpretedOption_msg_init, 0, arena, &ptr, len);
14549
+ if (status != kUpb_EncodeStatus_Ok) {
14550
+ return NULL;
14551
+ }
13359
14552
  return ptr;
13360
14553
  }
13361
14554
  UPB_INLINE char* google_protobuf_UninterpretedOption_serialize_ex(const google_protobuf_UninterpretedOption* msg,
13362
14555
  int options, upb_Arena* arena,
13363
14556
  size_t* len) {
13364
14557
  char* ptr;
13365
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__UninterpretedOption_msg_init, options, arena, &ptr, len);
14558
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__UninterpretedOption_msg_init,
14559
+ options, arena, &ptr, len);
14560
+ if (status != kUpb_EncodeStatus_Ok) {
14561
+ return NULL;
14562
+ }
13366
14563
  return ptr;
13367
14564
  }
13368
14565
  UPB_INLINE void google_protobuf_UninterpretedOption_clear_name(google_protobuf_UninterpretedOption* msg) {
@@ -13595,14 +14792,22 @@ UPB_INLINE google_protobuf_UninterpretedOption_NamePart* google_protobuf_Uninter
13595
14792
  UPB_INLINE char* google_protobuf_UninterpretedOption_NamePart_serialize(const google_protobuf_UninterpretedOption_NamePart* msg,
13596
14793
  upb_Arena* arena, size_t* len) {
13597
14794
  char* ptr;
13598
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__UninterpretedOption__NamePart_msg_init, 0, arena, &ptr, len);
14795
+ upb_EncodeStatus status =
14796
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__UninterpretedOption__NamePart_msg_init, 0, arena, &ptr, len);
14797
+ if (status != kUpb_EncodeStatus_Ok) {
14798
+ return NULL;
14799
+ }
13599
14800
  return ptr;
13600
14801
  }
13601
14802
  UPB_INLINE char* google_protobuf_UninterpretedOption_NamePart_serialize_ex(const google_protobuf_UninterpretedOption_NamePart* msg,
13602
14803
  int options, upb_Arena* arena,
13603
14804
  size_t* len) {
13604
14805
  char* ptr;
13605
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__UninterpretedOption__NamePart_msg_init, options, arena, &ptr, len);
14806
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__UninterpretedOption__NamePart_msg_init,
14807
+ options, arena, &ptr, len);
14808
+ if (status != kUpb_EncodeStatus_Ok) {
14809
+ return NULL;
14810
+ }
13606
14811
  return ptr;
13607
14812
  }
13608
14813
  UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_clear_name_part(google_protobuf_UninterpretedOption_NamePart* msg) {
@@ -13675,14 +14880,22 @@ UPB_INLINE google_protobuf_FeatureSet* google_protobuf_FeatureSet_parse_ex(
13675
14880
  UPB_INLINE char* google_protobuf_FeatureSet_serialize(const google_protobuf_FeatureSet* msg,
13676
14881
  upb_Arena* arena, size_t* len) {
13677
14882
  char* ptr;
13678
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSet_msg_init, 0, arena, &ptr, len);
14883
+ upb_EncodeStatus status =
14884
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSet_msg_init, 0, arena, &ptr, len);
14885
+ if (status != kUpb_EncodeStatus_Ok) {
14886
+ return NULL;
14887
+ }
13679
14888
  return ptr;
13680
14889
  }
13681
14890
  UPB_INLINE char* google_protobuf_FeatureSet_serialize_ex(const google_protobuf_FeatureSet* msg,
13682
14891
  int options, upb_Arena* arena,
13683
14892
  size_t* len) {
13684
14893
  char* ptr;
13685
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSet_msg_init, options, arena, &ptr, len);
14894
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSet_msg_init,
14895
+ options, arena, &ptr, len);
14896
+ if (status != kUpb_EncodeStatus_Ok) {
14897
+ return NULL;
14898
+ }
13686
14899
  return ptr;
13687
14900
  }
13688
14901
  UPB_INLINE void google_protobuf_FeatureSet_clear_field_presence(google_protobuf_FeatureSet* msg) {
@@ -13895,14 +15108,22 @@ UPB_INLINE google_protobuf_FeatureSet_VisibilityFeature* google_protobuf_Feature
13895
15108
  UPB_INLINE char* google_protobuf_FeatureSet_VisibilityFeature_serialize(const google_protobuf_FeatureSet_VisibilityFeature* msg,
13896
15109
  upb_Arena* arena, size_t* len) {
13897
15110
  char* ptr;
13898
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSet__VisibilityFeature_msg_init, 0, arena, &ptr, len);
15111
+ upb_EncodeStatus status =
15112
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSet__VisibilityFeature_msg_init, 0, arena, &ptr, len);
15113
+ if (status != kUpb_EncodeStatus_Ok) {
15114
+ return NULL;
15115
+ }
13899
15116
  return ptr;
13900
15117
  }
13901
15118
  UPB_INLINE char* google_protobuf_FeatureSet_VisibilityFeature_serialize_ex(const google_protobuf_FeatureSet_VisibilityFeature* msg,
13902
15119
  int options, upb_Arena* arena,
13903
15120
  size_t* len) {
13904
15121
  char* ptr;
13905
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSet__VisibilityFeature_msg_init, options, arena, &ptr, len);
15122
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSet__VisibilityFeature_msg_init,
15123
+ options, arena, &ptr, len);
15124
+ if (status != kUpb_EncodeStatus_Ok) {
15125
+ return NULL;
15126
+ }
13906
15127
  return ptr;
13907
15128
  }
13908
15129
 
@@ -13935,14 +15156,22 @@ UPB_INLINE google_protobuf_FeatureSet_ProtoLimitsFeature* google_protobuf_Featur
13935
15156
  UPB_INLINE char* google_protobuf_FeatureSet_ProtoLimitsFeature_serialize(const google_protobuf_FeatureSet_ProtoLimitsFeature* msg,
13936
15157
  upb_Arena* arena, size_t* len) {
13937
15158
  char* ptr;
13938
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSet__ProtoLimitsFeature_msg_init, 0, arena, &ptr, len);
15159
+ upb_EncodeStatus status =
15160
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSet__ProtoLimitsFeature_msg_init, 0, arena, &ptr, len);
15161
+ if (status != kUpb_EncodeStatus_Ok) {
15162
+ return NULL;
15163
+ }
13939
15164
  return ptr;
13940
15165
  }
13941
15166
  UPB_INLINE char* google_protobuf_FeatureSet_ProtoLimitsFeature_serialize_ex(const google_protobuf_FeatureSet_ProtoLimitsFeature* msg,
13942
15167
  int options, upb_Arena* arena,
13943
15168
  size_t* len) {
13944
15169
  char* ptr;
13945
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSet__ProtoLimitsFeature_msg_init, options, arena, &ptr, len);
15170
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSet__ProtoLimitsFeature_msg_init,
15171
+ options, arena, &ptr, len);
15172
+ if (status != kUpb_EncodeStatus_Ok) {
15173
+ return NULL;
15174
+ }
13946
15175
  return ptr;
13947
15176
  }
13948
15177
 
@@ -13975,14 +15204,22 @@ UPB_INLINE google_protobuf_FeatureSetDefaults* google_protobuf_FeatureSetDefault
13975
15204
  UPB_INLINE char* google_protobuf_FeatureSetDefaults_serialize(const google_protobuf_FeatureSetDefaults* msg,
13976
15205
  upb_Arena* arena, size_t* len) {
13977
15206
  char* ptr;
13978
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSetDefaults_msg_init, 0, arena, &ptr, len);
15207
+ upb_EncodeStatus status =
15208
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSetDefaults_msg_init, 0, arena, &ptr, len);
15209
+ if (status != kUpb_EncodeStatus_Ok) {
15210
+ return NULL;
15211
+ }
13979
15212
  return ptr;
13980
15213
  }
13981
15214
  UPB_INLINE char* google_protobuf_FeatureSetDefaults_serialize_ex(const google_protobuf_FeatureSetDefaults* msg,
13982
15215
  int options, upb_Arena* arena,
13983
15216
  size_t* len) {
13984
15217
  char* ptr;
13985
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSetDefaults_msg_init, options, arena, &ptr, len);
15218
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSetDefaults_msg_init,
15219
+ options, arena, &ptr, len);
15220
+ if (status != kUpb_EncodeStatus_Ok) {
15221
+ return NULL;
15222
+ }
13986
15223
  return ptr;
13987
15224
  }
13988
15225
  UPB_INLINE void google_protobuf_FeatureSetDefaults_clear_defaults(google_protobuf_FeatureSetDefaults* msg) {
@@ -14135,14 +15372,22 @@ UPB_INLINE google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* google_p
14135
15372
  UPB_INLINE char* google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_serialize(const google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg,
14136
15373
  upb_Arena* arena, size_t* len) {
14137
15374
  char* ptr;
14138
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init, 0, arena, &ptr, len);
15375
+ upb_EncodeStatus status =
15376
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init, 0, arena, &ptr, len);
15377
+ if (status != kUpb_EncodeStatus_Ok) {
15378
+ return NULL;
15379
+ }
14139
15380
  return ptr;
14140
15381
  }
14141
15382
  UPB_INLINE char* google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_serialize_ex(const google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg,
14142
15383
  int options, upb_Arena* arena,
14143
15384
  size_t* len) {
14144
15385
  char* ptr;
14145
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init, options, arena, &ptr, len);
15386
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init,
15387
+ options, arena, &ptr, len);
15388
+ if (status != kUpb_EncodeStatus_Ok) {
15389
+ return NULL;
15390
+ }
14146
15391
  return ptr;
14147
15392
  }
14148
15393
  UPB_INLINE void google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_clear_edition(google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg) {
@@ -14257,14 +15502,22 @@ UPB_INLINE google_protobuf_SourceCodeInfo* google_protobuf_SourceCodeInfo_parse_
14257
15502
  UPB_INLINE char* google_protobuf_SourceCodeInfo_serialize(const google_protobuf_SourceCodeInfo* msg,
14258
15503
  upb_Arena* arena, size_t* len) {
14259
15504
  char* ptr;
14260
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__SourceCodeInfo_msg_init, 0, arena, &ptr, len);
15505
+ upb_EncodeStatus status =
15506
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__SourceCodeInfo_msg_init, 0, arena, &ptr, len);
15507
+ if (status != kUpb_EncodeStatus_Ok) {
15508
+ return NULL;
15509
+ }
14261
15510
  return ptr;
14262
15511
  }
14263
15512
  UPB_INLINE char* google_protobuf_SourceCodeInfo_serialize_ex(const google_protobuf_SourceCodeInfo* msg,
14264
15513
  int options, upb_Arena* arena,
14265
15514
  size_t* len) {
14266
15515
  char* ptr;
14267
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__SourceCodeInfo_msg_init, options, arena, &ptr, len);
15516
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__SourceCodeInfo_msg_init,
15517
+ options, arena, &ptr, len);
15518
+ if (status != kUpb_EncodeStatus_Ok) {
15519
+ return NULL;
15520
+ }
14268
15521
  return ptr;
14269
15522
  }
14270
15523
  UPB_INLINE void google_protobuf_SourceCodeInfo_clear_location(google_protobuf_SourceCodeInfo* msg) {
@@ -14377,14 +15630,22 @@ UPB_INLINE google_protobuf_SourceCodeInfo_Location* google_protobuf_SourceCodeIn
14377
15630
  UPB_INLINE char* google_protobuf_SourceCodeInfo_Location_serialize(const google_protobuf_SourceCodeInfo_Location* msg,
14378
15631
  upb_Arena* arena, size_t* len) {
14379
15632
  char* ptr;
14380
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__SourceCodeInfo__Location_msg_init, 0, arena, &ptr, len);
15633
+ upb_EncodeStatus status =
15634
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__SourceCodeInfo__Location_msg_init, 0, arena, &ptr, len);
15635
+ if (status != kUpb_EncodeStatus_Ok) {
15636
+ return NULL;
15637
+ }
14381
15638
  return ptr;
14382
15639
  }
14383
15640
  UPB_INLINE char* google_protobuf_SourceCodeInfo_Location_serialize_ex(const google_protobuf_SourceCodeInfo_Location* msg,
14384
15641
  int options, upb_Arena* arena,
14385
15642
  size_t* len) {
14386
15643
  char* ptr;
14387
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__SourceCodeInfo__Location_msg_init, options, arena, &ptr, len);
15644
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__SourceCodeInfo__Location_msg_init,
15645
+ options, arena, &ptr, len);
15646
+ if (status != kUpb_EncodeStatus_Ok) {
15647
+ return NULL;
15648
+ }
14388
15649
  return ptr;
14389
15650
  }
14390
15651
  UPB_INLINE void google_protobuf_SourceCodeInfo_Location_clear_path(google_protobuf_SourceCodeInfo_Location* msg) {
@@ -14670,14 +15931,22 @@ UPB_INLINE google_protobuf_GeneratedCodeInfo* google_protobuf_GeneratedCodeInfo_
14670
15931
  UPB_INLINE char* google_protobuf_GeneratedCodeInfo_serialize(const google_protobuf_GeneratedCodeInfo* msg,
14671
15932
  upb_Arena* arena, size_t* len) {
14672
15933
  char* ptr;
14673
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__GeneratedCodeInfo_msg_init, 0, arena, &ptr, len);
15934
+ upb_EncodeStatus status =
15935
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__GeneratedCodeInfo_msg_init, 0, arena, &ptr, len);
15936
+ if (status != kUpb_EncodeStatus_Ok) {
15937
+ return NULL;
15938
+ }
14674
15939
  return ptr;
14675
15940
  }
14676
15941
  UPB_INLINE char* google_protobuf_GeneratedCodeInfo_serialize_ex(const google_protobuf_GeneratedCodeInfo* msg,
14677
15942
  int options, upb_Arena* arena,
14678
15943
  size_t* len) {
14679
15944
  char* ptr;
14680
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__GeneratedCodeInfo_msg_init, options, arena, &ptr, len);
15945
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__GeneratedCodeInfo_msg_init,
15946
+ options, arena, &ptr, len);
15947
+ if (status != kUpb_EncodeStatus_Ok) {
15948
+ return NULL;
15949
+ }
14681
15950
  return ptr;
14682
15951
  }
14683
15952
  UPB_INLINE void google_protobuf_GeneratedCodeInfo_clear_annotation(google_protobuf_GeneratedCodeInfo* msg) {
@@ -14790,14 +16059,22 @@ UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation* google_protobuf_Generat
14790
16059
  UPB_INLINE char* google_protobuf_GeneratedCodeInfo_Annotation_serialize(const google_protobuf_GeneratedCodeInfo_Annotation* msg,
14791
16060
  upb_Arena* arena, size_t* len) {
14792
16061
  char* ptr;
14793
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__GeneratedCodeInfo__Annotation_msg_init, 0, arena, &ptr, len);
16062
+ upb_EncodeStatus status =
16063
+ upb_Encode(UPB_UPCAST(msg), &google__protobuf__GeneratedCodeInfo__Annotation_msg_init, 0, arena, &ptr, len);
16064
+ if (status != kUpb_EncodeStatus_Ok) {
16065
+ return NULL;
16066
+ }
14794
16067
  return ptr;
14795
16068
  }
14796
16069
  UPB_INLINE char* google_protobuf_GeneratedCodeInfo_Annotation_serialize_ex(const google_protobuf_GeneratedCodeInfo_Annotation* msg,
14797
16070
  int options, upb_Arena* arena,
14798
16071
  size_t* len) {
14799
16072
  char* ptr;
14800
- (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__GeneratedCodeInfo__Annotation_msg_init, options, arena, &ptr, len);
16073
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &google__protobuf__GeneratedCodeInfo__Annotation_msg_init,
16074
+ options, arena, &ptr, len);
16075
+ if (status != kUpb_EncodeStatus_Ok) {
16076
+ return NULL;
16077
+ }
14801
16078
  return ptr;
14802
16079
  }
14803
16080
  UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_clear_path(google_protobuf_GeneratedCodeInfo_Annotation* msg) {
@@ -15312,6 +16589,8 @@ const upb_FileDef* upb_FileDef_Dependency(const upb_FileDef* f, int i);
15312
16589
  int upb_FileDef_DependencyCount(const upb_FileDef* f);
15313
16590
  bool upb_FileDef_HasOptions(const upb_FileDef* f);
15314
16591
  UPB_API const char* upb_FileDef_Name(const upb_FileDef* f);
16592
+ const char* upb_FileDef_OptionDependency(const upb_FileDef* f, int i);
16593
+ int upb_FileDef_OptionDependencyCount(const upb_FileDef* f);
15315
16594
  const google_protobuf_FileOptions* upb_FileDef_Options(const upb_FileDef* f);
15316
16595
  const google_protobuf_FeatureSet* upb_FileDef_ResolvedFeatures(const upb_FileDef* f);
15317
16596
  const char* upb_FileDef_Package(const upb_FileDef* f);
@@ -15614,7 +16893,19 @@ const google_protobuf_FeatureSet* upb_ServiceDef_ResolvedFeatures(
15614
16893
  extern "C" {
15615
16894
  #endif
15616
16895
 
15617
- enum { upb_JsonDecode_IgnoreUnknown = 1 };
16896
+ enum {
16897
+ upb_JsonDecode_IgnoreUnknown = 1 << 0,
16898
+
16899
+ // Controls whether the decoder performs UTF-8 validation on the JSON input.
16900
+ // Under Enforce, it will fail with a decode error if the provided input is
16901
+ // not well-formed UTF-8.
16902
+ // Callers must not set both upb_JsonDecode_ValidateUtf8_Disable and
16903
+ // upb_JsonDecode_ValidateUtf8_Enforce.
16904
+ // The default behavior (when neither is set) is to warn on invalid UTF-8,
16905
+ // but this will become Enforce in an upcoming breaking change.
16906
+ upb_JsonDecode_ValidateUtf8_Disable = 1 << 1,
16907
+ upb_JsonDecode_ValidateUtf8_Enforce = 2 << 1,
16908
+ };
15618
16909
 
15619
16910
  enum {
15620
16911
  kUpb_JsonDecodeResult_Ok = 0,
@@ -15821,7 +17112,12 @@ enum {
15821
17112
  upb_JsonEncode_UseProtoNames = 1 << 1,
15822
17113
 
15823
17114
  /* When set, emits enums as their integer values instead of as their names. */
15824
- upb_JsonEncode_FormatEnumsAsIntegers = 1 << 2
17115
+ upb_JsonEncode_FormatEnumsAsIntegers = 1 << 2,
17116
+
17117
+ /* When set, encodes `bytes` fields with the URL- and filename-safe base64
17118
+ alphabet from RFC 4648 section 5 ('-' and '_' instead of '+' and '/').
17119
+ Padding is unchanged. The decoder accepts both alphabets regardless. */
17120
+ upb_JsonEncode_WebSafeBase64 = 1 << 3
15825
17121
  };
15826
17122
 
15827
17123
  /* Encodes the given |msg| to JSON format. The message's reflection is given in
@@ -16339,6 +17635,7 @@ UPB_INLINE void _upb_mapsorter_popmap(_upb_mapsorter* s,
16339
17635
  bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type,
16340
17636
  const struct upb_Map* map, _upb_sortedmap* sorted);
16341
17637
 
17638
+ // Pushes canonical extensions from the given message onto the sorter.
16342
17639
  bool _upb_mapsorter_pushexts(_upb_mapsorter* s, const upb_Message_Internal* in,
16343
17640
  _upb_sortedmap* sorted);
16344
17641
 
@@ -16362,6 +17659,21 @@ bool _upb_mapsorter_pushexts(_upb_mapsorter* s, const upb_Message_Internal* in,
16362
17659
  extern "C" {
16363
17660
  #endif
16364
17661
 
17662
+ typedef enum {
17663
+ kUpb_MessageUnknownType_StringView,
17664
+ kUpb_MessageUnknownType_NonCanonicalExtension,
17665
+ } upb_MessageUnknownType;
17666
+
17667
+ // Represents an unknown field in a message, whether it's in a serialized
17668
+ // (upb_StringView) or parsed non-canonical extension (upb_Extension*) format.
17669
+ typedef struct upb_MessageUnknown {
17670
+ uint8_t type;
17671
+ union {
17672
+ upb_StringView bytes;
17673
+ const upb_Extension* extension;
17674
+ } value;
17675
+ } upb_MessageUnknown;
17676
+
16365
17677
  // Support iteration over unknown (upb_MessageUnknown*), including unknown
16366
17678
  // upb_StringView and non-canonical extensions (upb_Extension*).
16367
17679
  UPB_INLINE bool upb_Message_NextUnknown2(const struct upb_Message* msg,
@@ -16393,6 +17705,24 @@ UPB_INLINE bool upb_Message_NextUnknown2(const struct upb_Message* msg,
16393
17705
  return false;
16394
17706
  }
16395
17707
 
17708
+ // Iterates over unknown fields in wire format (upb_StringView).
17709
+ // If an unknown field is a non-canonical extension, it is automatically
17710
+ // encoded into wire format into `*arena` using default encode options (0).
17711
+ //
17712
+ // `arena` is a pointer to `upb_Arena*`. If `*arena` is NULL when a
17713
+ // non-canonical extension is encountered, an arena will be lazily created via
17714
+ // `upb_Arena_New()`. The caller is responsible for freeing `*arena` (if
17715
+ // non-NULL) using `upb_Arena_Free(*arena)` after iteration completes.
17716
+ //
17717
+ // NOTE: Automatically encoding non-canonical extensions into wire format may
17718
+ // incur a performance penalty if non-canonical extensions are present, as
17719
+ // encoding requires allocating temporary buffers in `*arena`. Use
17720
+ // `upb_Message_NextUnknown2` if you want to inspect non-canonical extensions
17721
+ // directly without encoding them.
17722
+ UPB_NODISCARD bool upb_Message_NextWireFormatUnknown(
17723
+ const struct upb_Message* msg, struct upb_Arena** arena,
17724
+ upb_StringView* data, uintptr_t* iter);
17725
+
16396
17726
  typedef enum {
16397
17727
  kUpb_FindUnknown_Ok,
16398
17728
  kUpb_FindUnknown_NotPresent,
@@ -16414,10 +17744,49 @@ typedef struct {
16414
17744
  // returns the one encountered first in internal iteration order (which follows
16415
17745
  // the order they were added or parsed).
16416
17746
  //
16417
- // A depth_limit of zero means to just use the upb default depth limit.
16418
- upb_FindUnknownRet2 upb_Message_FindUnknown2(const struct upb_Message* msg,
16419
- uint32_t field_number,
16420
- int depth_limit);
17747
+ // A depth_limit of zero means to just use the upb default depth limit.
17748
+ upb_FindUnknownRet2 upb_Message_FindUnknown2(const struct upb_Message* msg,
17749
+ uint32_t field_number,
17750
+ int depth_limit);
17751
+
17752
+ typedef enum {
17753
+ kUpb_DeleteUnknown_DeletedLast,
17754
+ kUpb_DeleteUnknown_IterUpdated,
17755
+ kUpb_DeleteUnknown_AllocFail,
17756
+ } upb_Message_DeleteUnknownStatus;
17757
+
17758
+ // Removes a segment of unknown data from the message, advancing to the next
17759
+ // segment. Returns false if the removed segment was at the end of the last
17760
+ // chunk.
17761
+ //
17762
+ // This must be done while iterating:
17763
+ //
17764
+ // uintptr_t iter = kUpb_Message_UnknownBegin;
17765
+ // upb_StringView data;
17766
+ // // Iterate chunks
17767
+ // while (upb_Message_NextUnknown(msg, &data, &iter)) {
17768
+ // // Iterate within a chunk, deleting ranges
17769
+ // while (ShouldDeleteSubSegment(&data)) {
17770
+ // // Data now points to the region to be deleted
17771
+ // switch (upb_Message_DeleteUnknown(msg, &data, &iter)) {
17772
+ // case kUpb_DeleteUnknown_DeletedLast: return ok;
17773
+ // case kUpb_DeleteUnknown_IterUpdated: break;
17774
+ // // If DeleteUnknown returned kUpb_DeleteUnknown_IterUpdated,
17775
+ // // then data now points to the remaining unknown fields after the
17776
+ // // region that was just deleted.
17777
+ // case kUpb_DeleteUnknown_AllocFail: return err;
17778
+ // }
17779
+ // }
17780
+ // }
17781
+ //
17782
+ // The range given in `data` must be contained inside the most recently
17783
+ // returned region.
17784
+ // TODO: b/510055656 - Legacy API that works with messages that only have
17785
+ // unknown data in upb_StringView format. Use `upb_Message_DeleteUnknown2` for
17786
+ // messages that may have non-canonical extensions.
17787
+ UPB_NODISCARD upb_Message_DeleteUnknownStatus
17788
+ upb_Message_DeleteUnknown(struct upb_Message* msg, upb_StringView* data,
17789
+ uintptr_t* iter, struct upb_Arena* arena);
16421
17790
 
16422
17791
  // Removes a segment of unknown data from the message, advancing to the next
16423
17792
  // segment. Returns false if the removed segment was at the end of the last
@@ -16433,12 +17802,12 @@ upb_FindUnknownRet2 upb_Message_FindUnknown2(const struct upb_Message* msg,
16433
17802
  // while (ShouldDeleteSubSegment(&data)) {
16434
17803
  // // Data now points to the region to be deleted
16435
17804
  // switch (upb_Message_DeleteUnknown2(msg, &data, &iter)) {
16436
- // case kUpb_Message_DeleteUnknown_DeletedLast: return ok;
16437
- // case kUpb_Message_DeleteUnknown_IterUpdated: break;
16438
- // // If DeleteUnknown returned kUpb_Message_DeleteUnknown_IterUpdated,
17805
+ // case kUpb_DeleteUnknown_DeletedLast: return ok;
17806
+ // case kUpb_DeleteUnknown_IterUpdated: break;
17807
+ // // If DeleteUnknown returned kUpb_DeleteUnknown_IterUpdated,
16439
17808
  // // then data now points to the remaining unknown fields after the
16440
17809
  // // region that was just deleted.
16441
- // case kUpb_Message_DeleteUnknown_AllocFail: return err;
17810
+ // case kUpb_DeleteUnknown_AllocFail: return err;
16442
17811
  // }
16443
17812
  // }
16444
17813
  // }
@@ -16459,6 +17828,33 @@ UPB_NODISCARD upb_Message_DeleteUnknownStatus upb_Message_DeleteUnknown2(
16459
17828
 
16460
17829
  #endif /* UPB_MESSAGE_UNKNOWN_FIELDS_H_ */
16461
17830
 
17831
+ #ifndef UPB_WIRE_ENCODE_EXTENSION_H_
17832
+ #define UPB_WIRE_ENCODE_EXTENSION_H_
17833
+
17834
+
17835
+ // Must be last.
17836
+
17837
+ #ifdef __cplusplus
17838
+ extern "C" {
17839
+ #endif
17840
+
17841
+ struct upb_Extension;
17842
+
17843
+ // Encodes an extension (`upb_Extension*`) to bytes.
17844
+ //
17845
+ // This can be used to encode an extension into the provided arena.
17846
+ // Returns `kUpb_EncodeStatus_Ok` on success.
17847
+ UPB_NODISCARD upb_EncodeStatus
17848
+ upb_EncodeExtension(const struct upb_Extension* ext, struct upb_Arena* arena,
17849
+ upb_StringView* view, int encode_options);
17850
+
17851
+ #ifdef __cplusplus
17852
+ } /* extern "C" */
17853
+ #endif
17854
+
17855
+
17856
+ #endif /* UPB_WIRE_ENCODE_EXTENSION_H_ */
17857
+
16462
17858
  #ifndef UPB_WIRE_EPS_COPY_INPUT_STREAM_H_
16463
17859
  #define UPB_WIRE_EPS_COPY_INPUT_STREAM_H_
16464
17860
 
@@ -16512,7 +17908,9 @@ struct upb_EpsCopyInputStream {
16512
17908
  };
16513
17909
 
16514
17910
  UPB_INLINE void UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)(
16515
- struct upb_EpsCopyInputStream* e);
17911
+ struct upb_EpsCopyInputStream* e, int n);
17912
+ UPB_INLINE int UPB_PRIVATE(upb_EpsCopyInputStream_GetBoundsCheckedBytes)(
17913
+ const struct upb_EpsCopyInputStream* e);
16516
17914
 
16517
17915
  UPB_INLINE bool upb_EpsCopyInputStream_IsError(
16518
17916
  struct upb_EpsCopyInputStream* e) {
@@ -16538,7 +17936,8 @@ UPB_INLINE void upb_EpsCopyInputStream_InitWithErrorHandler(
16538
17936
  }
16539
17937
  e->limit_ptr = e->end;
16540
17938
  e->error = false;
16541
- UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)(e);
17939
+ UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)(
17940
+ e, kUpb_EpsCopyInputStream_SlopBytes);
16542
17941
  }
16543
17942
 
16544
17943
  UPB_INLINE void upb_EpsCopyInputStream_Init(struct upb_EpsCopyInputStream* e,
@@ -16584,9 +17983,18 @@ UPB_INLINE const char* UPB_PRIVATE(upb_EpsCopyInputStream_AssumeResult)(
16584
17983
  // bytes, even if each varint is its maximum possible length.
16585
17984
 
16586
17985
  UPB_INLINE void UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)(
16587
- struct upb_EpsCopyInputStream* e) {
17986
+ struct upb_EpsCopyInputStream* e, int n) {
17987
+ #ifndef NDEBUG
17988
+ e->guaranteed_bytes = n;
17989
+ #endif
17990
+ }
17991
+
17992
+ UPB_INLINE int UPB_PRIVATE(upb_EpsCopyInputStream_GetBoundsCheckedBytes)(
17993
+ const struct upb_EpsCopyInputStream* e) {
16588
17994
  #ifndef NDEBUG
16589
- e->guaranteed_bytes = kUpb_EpsCopyInputStream_SlopBytes;
17995
+ return e ? e->guaranteed_bytes : 0;
17996
+ #else
17997
+ return 0;
16590
17998
  #endif
16591
17999
  }
16592
18000
 
@@ -16629,7 +18037,8 @@ UPB_INLINE upb_IsDoneStatus UPB_PRIVATE(upb_EpsCopyInputStream_IsDoneStatus)(
16629
18037
  struct upb_EpsCopyInputStream* e, const char* ptr, int* overrun) {
16630
18038
  *overrun = ptr - e->end;
16631
18039
  if (UPB_LIKELY(ptr < e->limit_ptr)) {
16632
- UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)(e);
18040
+ UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)(
18041
+ e, kUpb_EpsCopyInputStream_SlopBytes);
16633
18042
  return kUpb_IsDoneStatus_NotDone;
16634
18043
  } else if (UPB_LIKELY(*overrun == e->limit)) {
16635
18044
  UPB_PRIVATE(upb_EpsCopyInputStream_BoundsHit)(e);
@@ -16651,13 +18060,15 @@ UPB_INLINE bool upb_EpsCopyInputStream_IsDone(struct upb_EpsCopyInputStream* e,
16651
18060
  UPB_PRIVATE(upb_EpsCopyInputStream_BoundsHit)(e);
16652
18061
  return true;
16653
18062
  case kUpb_IsDoneStatus_NotDone:
16654
- UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)(e);
18063
+ UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)(
18064
+ e, kUpb_EpsCopyInputStream_SlopBytes);
16655
18065
  return false;
16656
18066
  case kUpb_IsDoneStatus_NeedFallback:
16657
18067
  *ptr =
16658
18068
  UPB_PRIVATE(upb_EpsCopyInputStream_IsDoneFallback)(e, *ptr, overrun);
16659
18069
  if (*ptr) {
16660
- UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)(e);
18070
+ UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)(
18071
+ e, kUpb_EpsCopyInputStream_SlopBytes);
16661
18072
  } else {
16662
18073
  UPB_PRIVATE(upb_EpsCopyInputStream_BoundsHit)(e);
16663
18074
  }
@@ -16672,6 +18083,47 @@ UPB_INLINE bool upb_EpsCopyInputStream_CheckSize(
16672
18083
  return size <= e->limit - (ptr - e->end);
16673
18084
  }
16674
18085
 
18086
+ // Returns the bounds-check budget for a position from which `size` bytes of
18087
+ // payload are known to be present in the buffer: the payload itself plus the
18088
+ // slop bytes that always follow it.
18089
+ UPB_INLINE int UPB_PRIVATE(upb_EpsCopyInputStream_GuaranteedBytes)(
18090
+ ptrdiff_t size) {
18091
+ return size > INT32_MAX - kUpb_EpsCopyInputStream_SlopBytes
18092
+ ? INT32_MAX
18093
+ : (int)size + kUpb_EpsCopyInputStream_SlopBytes;
18094
+ }
18095
+
18096
+ UPB_FORCEINLINE bool upb_EpsCopyInputStream_SizeFitsWithoutSwap(
18097
+ struct upb_EpsCopyInputStream* e, const char* ptr, int size) {
18098
+ if (UPB_UNLIKELY(size < 0 || (ptrdiff_t)size > e->limit_ptr - ptr)) {
18099
+ return false;
18100
+ }
18101
+ UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)(
18102
+ e, UPB_PRIVATE(upb_EpsCopyInputStream_GuaranteedBytes)(size));
18103
+ return true;
18104
+ }
18105
+
18106
+ // Re-establishes the bounds-check budget at `ptr` for a region ending at `end`
18107
+ // that was already validated by upb_EpsCopyInputStream_SizeFitsWithoutSwap().
18108
+ //
18109
+ // upb_EpsCopyInputStream_ConsumeBytes() charges every read its worst-case
18110
+ // length (eg. 10 bytes for a varint, even though most varints are one byte),
18111
+ // and those charges accumulate without regard for how far `ptr` actually
18112
+ // advanced. Code that performs several reads inside a single validated region
18113
+ // therefore has to restore the budget as it goes, or it will exhaust an
18114
+ // allowance that the reads never really spent.
18115
+ //
18116
+ // This grants reads up to `end + kUpb_EpsCopyInputStream_SlopBytes`, which is
18117
+ // the exact same final address that SizeFitsWithoutSwap() already established
18118
+ // for the region, so it relaxes nothing: it only re-expresses the remaining
18119
+ // guarantee relative to the new `ptr`.
18120
+ UPB_INLINE void UPB_PRIVATE(upb_EpsCopyInputStream_BoundsCheckedToEnd)(
18121
+ struct upb_EpsCopyInputStream* e, const char* ptr, const char* end) {
18122
+ UPB_ASSERT(ptr <= end);
18123
+ UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)(
18124
+ e, UPB_PRIVATE(upb_EpsCopyInputStream_GuaranteedBytes)(end - ptr));
18125
+ }
18126
+
16675
18127
  // Returns a pointer into an input buffer that corresponds to the parsing
16676
18128
  // pointer `ptr`. The returned pointer may be the same as `ptr`, but also may
16677
18129
  // be different if we are currently parsing out of the patch buffer.
@@ -16866,6 +18318,13 @@ UPB_INLINE bool upb_EpsCopyInputStream_IsDone(upb_EpsCopyInputStream* e,
16866
18318
  UPB_INLINE bool upb_EpsCopyInputStream_CheckSize(
16867
18319
  const upb_EpsCopyInputStream* e, const char* ptr, int size);
16868
18320
 
18321
+ // Returns true if the given delimited field size fits entirely within the
18322
+ // current buffer before limit_ptr (i.e. without needing to swap into the slop
18323
+ // patch buffer). If so, updates debug bounds tracking with the guaranteed
18324
+ // readable bytes (size + slop).
18325
+ UPB_FORCEINLINE bool upb_EpsCopyInputStream_SizeFitsWithoutSwap(
18326
+ upb_EpsCopyInputStream* e, const char* ptr, int size);
18327
+
16869
18328
  // Marks the start of a capture operation. The capture operation will be
16870
18329
  // finalized by a call to upb_EpsCopyCapture_End(). The captured string will
16871
18330
  // be returned in sv, and will point to the original input buffer if possible.
@@ -17185,6 +18644,9 @@ UPB_INLINE const char* upb_WireReader_SkipGroup(
17185
18644
  UPB_FORCEINLINE const char* _upb_WireReader_SkipValueForceInline(
17186
18645
  const char* ptr, uint32_t tag, int depth_limit,
17187
18646
  upb_EpsCopyInputStream* stream) {
18647
+ if (UPB_UNLIKELY((tag >> 3) == 0)) {
18648
+ return UPB_PRIVATE(upb_EpsCopyInputStream_ReturnError)(stream);
18649
+ }
17188
18650
  switch (upb_WireReader_GetWireType(tag)) {
17189
18651
  case kUpb_WireType_Varint:
17190
18652
  return upb_WireReader_SkipVarint(ptr, stream);
@@ -17255,7 +18717,10 @@ UPB_INLINE const char* upb_WireReader_SkipValue(
17255
18717
  enum {
17256
18718
  // If set, upb_Message_IsEqual() will attempt to compare unknown fields.
17257
18719
  // By its very nature this comparison is inexact.
17258
- kUpb_CompareOption_IncludeUnknownFields = (1 << 0)
18720
+ kUpb_CompareOption_IncludeUnknownFields = (1 << 0),
18721
+
18722
+ // If set, only fields present in msg2 (the expected message) are compared.
18723
+ kUpb_CompareOption_Partial = (1 << 1)
17259
18724
  };
17260
18725
 
17261
18726
  #ifdef __cplusplus
@@ -17334,40 +18799,246 @@ extern "C" {
17334
18799
  // 2. We don't know about oneof/non-repeated fields, which should semantically
17335
18800
  // discard every value except the last.
17336
18801
 
17337
- typedef enum {
17338
- kUpb_UnknownCompareResult_Equal = 0,
17339
- kUpb_UnknownCompareResult_NotEqual = 1,
17340
- kUpb_UnknownCompareResult_OutOfMemory = 2,
17341
- kUpb_UnknownCompareResult_MaxDepthExceeded = 3,
17342
- } upb_UnknownCompareResult;
18802
+ typedef enum {
18803
+ kUpb_UnknownCompareResult_Equal = 0,
18804
+ kUpb_UnknownCompareResult_NotEqual = 1,
18805
+ kUpb_UnknownCompareResult_OutOfMemory = 2,
18806
+ kUpb_UnknownCompareResult_MaxDepthExceeded = 3,
18807
+ } upb_UnknownCompareResult;
18808
+
18809
+ upb_UnknownCompareResult _upb_Message_UnknownFieldsAreEqual(
18810
+ const upb_Message* msg1, const upb_Message* msg2, int max_depth);
18811
+
18812
+ #ifdef __cplusplus
18813
+ } /* extern "C" */
18814
+ #endif
18815
+
18816
+
18817
+ #endif /* UPB_MESSAGE_INTERNAL_COMPARE_UNKNOWN_H_ */
18818
+
18819
+ #ifndef GOOGLE_UPB_UPB_MESSAGE_INTERNAL_ITERATOR_H__
18820
+ #define GOOGLE_UPB_UPB_MESSAGE_INTERNAL_ITERATOR_H__
18821
+
18822
+ #include <stddef.h>
18823
+ #include <stdint.h>
18824
+
18825
+
18826
+ // Must be last.
18827
+
18828
+ #define kUpb_BaseField_Begin ((size_t)-1)
18829
+ bool UPB_PRIVATE(_upb_Message_NextBaseField)(const upb_Message* msg,
18830
+ const upb_MiniTable* m,
18831
+ const upb_MiniTableField** out_f,
18832
+ upb_MessageValue* out_v,
18833
+ size_t* iter);
18834
+
18835
+ #endif // GOOGLE_UPB_UPB_MESSAGE_INTERNAL_ITERATOR_H__
18836
+
18837
+ #ifndef GOOGLE_UPB_UPB_WIRE_INTERNAL_BACK_ALLOC_H__
18838
+ #define GOOGLE_UPB_UPB_WIRE_INTERNAL_BACK_ALLOC_H__
18839
+
18840
+ #include <stddef.h>
18841
+
18842
+
18843
+ // Must be last.
18844
+
18845
+ #ifdef __cplusplus
18846
+ extern "C" {
18847
+ #endif
18848
+
18849
+ // Allocates memory from the back of the arena.
18850
+ typedef struct {
18851
+ upb_Arena* arena;
18852
+ char *buf, *limit;
18853
+ bool standalone;
18854
+ } upb_BackAlloc;
18855
+
18856
+ // Needed because C doesn't allow NULL - NULL.
18857
+ extern char upb_BackAlloc_sentinel;
18858
+
18859
+ char* upb_BackAlloc_Grow(upb_BackAlloc* a, char* ptr, size_t need);
18860
+
18861
+ UPB_INLINE void _upb_BackAlloc_Reset(upb_BackAlloc* a) {
18862
+ a->buf = &upb_BackAlloc_sentinel;
18863
+ a->limit = &upb_BackAlloc_sentinel;
18864
+ a->standalone = false;
18865
+ }
18866
+
18867
+ UPB_INLINE char* upb_BackAlloc_Init(upb_BackAlloc* a, upb_Arena* arena) {
18868
+ a->arena = arena;
18869
+ // This could eagerly steal whatever's in the arena, since stealing with a
18870
+ // minimum of 0 can't fail.
18871
+ _upb_BackAlloc_Reset(a);
18872
+ return a->limit;
18873
+ }
18874
+
18875
+ UPB_INLINE void upb_BackAlloc_Abort(upb_BackAlloc* a) {
18876
+ if (a->standalone) {
18877
+ UPB_PRIVATE(_upb_Arena_FreeBlock)(a->arena, a->buf);
18878
+ } else if (a->limit != a->buf) {
18879
+ UPB_PRIVATE(_upb_Arena_UseBlock)(a->arena, a->buf, a->limit - a->buf);
18880
+ }
18881
+ _upb_BackAlloc_Reset(a);
18882
+ }
18883
+
18884
+ UPB_INLINE size_t upb_BackAlloc_Finish(upb_BackAlloc* a, const char* ptr) {
18885
+ if (a->standalone) {
18886
+ UPB_PRIVATE(_upb_Arena_AddBlock)(a->arena, a->buf);
18887
+ }
18888
+ if (ptr != a->buf) {
18889
+ UPB_PRIVATE(_upb_Arena_UseBlock)(a->arena, a->buf, ptr - a->buf);
18890
+ }
18891
+ size_t size = a->limit - ptr;
18892
+ _upb_BackAlloc_Reset(a);
18893
+ return size;
18894
+ }
18895
+
18896
+ UPB_FORCEINLINE bool upb_BackAlloc_HasBytes(const upb_BackAlloc* a,
18897
+ const char* ptr, size_t need) {
18898
+ size_t have = ptr - a->buf;
18899
+ return have >= need;
18900
+ }
18901
+
18902
+ UPB_FORCEINLINE char* upb_BackAlloc_Reserve(upb_BackAlloc* a, char* ptr,
18903
+ size_t need) {
18904
+ return upb_BackAlloc_HasBytes(a, ptr, need)
18905
+ ? ptr - need
18906
+ : upb_BackAlloc_Grow(a, ptr, need);
18907
+ }
18908
+
18909
+ UPB_INLINE size_t upb_BackAlloc_Size(const upb_BackAlloc* a, const char* ptr) {
18910
+ return a->limit - ptr;
18911
+ }
18912
+
18913
+ #ifdef __cplusplus
18914
+ } /* extern "C" */
18915
+ #endif
18916
+
18917
+
18918
+ #endif // GOOGLE_UPB_UPB_WIRE_INTERNAL_BACK_ALLOC_H__
18919
+
18920
+ #ifndef UPB_WIRE_INTERNAL_ENCODE_H_
18921
+ #define UPB_WIRE_INTERNAL_ENCODE_H_
18922
+
18923
+ #include <setjmp.h>
18924
+ #include <stddef.h>
18925
+ #include <stdint.h>
18926
+
18927
+
18928
+ // Must be last.
18929
+
18930
+ #ifdef __cplusplus
18931
+ extern "C" {
18932
+ #endif
18933
+
18934
+ typedef struct {
18935
+ upb_BackAlloc alloc;
18936
+ int options;
18937
+ int depth;
18938
+ upb_EncodeStatus status;
18939
+ _upb_mapsorter sorter;
18940
+ jmp_buf* err;
18941
+ } upb_encstate;
18942
+
18943
+ UPB_INLINE char* UPB_PRIVATE(_upb_encstate_init)(upb_encstate* e, jmp_buf* err,
18944
+ upb_Arena* arena) {
18945
+ e->status = kUpb_EncodeStatus_Ok;
18946
+ char* ptr = upb_BackAlloc_Init(&e->alloc, arena);
18947
+ e->options = 0;
18948
+ e->depth = 0;
18949
+ e->err = err;
18950
+ _upb_mapsorter_init(&e->sorter);
18951
+ return ptr;
18952
+ }
18953
+
18954
+ UPB_INLINE void UPB_PRIVATE(_upb_encstate_destroy)(upb_encstate* e) {
18955
+ _upb_mapsorter_destroy(&e->sorter);
18956
+ }
18957
+
18958
+ // Encodes a single field into the back_alloc buffer without finishing it.
18959
+ // Returns the updated buffer pointer (pointing to the start of encoded data).
18960
+ char* UPB_PRIVATE(_upb_Encode_FieldToBuffer)(char* ptr, upb_encstate* e,
18961
+ const upb_Message* msg,
18962
+ const upb_MiniTableField* field);
18963
+
18964
+ // Internal version of upb_Encode that encodes a single field.
18965
+ //
18966
+ // The caller must clean up the `upb_encstate` by calling
18967
+ // `_upb_encstate_destroy(e)` when done.
18968
+ upb_EncodeStatus UPB_PRIVATE(_upb_Encode_Field)(upb_encstate* e,
18969
+ const upb_Message* msg,
18970
+ const upb_MiniTableField* field,
18971
+ char** buf, size_t* size,
18972
+ int options);
18973
+
18974
+ // Internal version of upb_Encode that encodes a single extension.
18975
+ //
18976
+ // The caller must clean up the `upb_encstate` by calling
18977
+ // `_upb_encstate_destroy(e)` when done.
18978
+ upb_EncodeStatus UPB_PRIVATE(_upb_Encode_Extension)(
18979
+ upb_encstate* e, const upb_MiniTableExtension* ext,
18980
+ upb_MessageValue ext_val, bool is_message_set, char** buf, size_t* size,
18981
+ int options);
18982
+
18983
+ char* encode_message(char* ptr, upb_encstate* e, const upb_Message* msg,
18984
+ const upb_MiniTable* m, size_t* size);
18985
+
18986
+ #define kUpb_Encoder_EncodeVarint32MaxSize 5
18987
+ #define kUpb_Encoder_EncodeVarint64MaxSize 10
18988
+
18989
+ static char* upb_Encoder_EncodeVarint32(uint32_t val, char* ptr) {
18990
+ do {
18991
+ uint8_t byte = val & 0x7fU;
18992
+ val >>= 7;
18993
+ if (val) byte |= 0x80U;
18994
+ *(ptr++) = byte;
18995
+ } while (val);
18996
+ return ptr;
18997
+ }
18998
+
18999
+ static char* upb_Encoder_EncodeVarint64(uint64_t val, char* ptr) {
19000
+ do {
19001
+ uint8_t byte = val & 0x7fU;
19002
+ val >>= 7;
19003
+ if (val) byte |= 0x80U;
19004
+ *(ptr++) = byte;
19005
+ } while (val);
19006
+ return ptr;
19007
+ }
19008
+
19009
+ UPB_INLINE
19010
+ bool _upb_Encoder_AddEnumValueToUnknown(upb_Message* msg, uint32_t field_num,
19011
+ uint64_t val, upb_Arena* arena) {
19012
+ // Unrecognized enum goes into unknown fields.
19013
+ // For packed fields the tag could be arbitrarily far in the past,
19014
+ // so we just re-encode the tag and value here.
19015
+ const uint32_t tag = (field_num << 3) | kUpb_WireType_Varint;
19016
+ char buf[kUpb_Encoder_EncodeVarint32MaxSize +
19017
+ kUpb_Encoder_EncodeVarint64MaxSize];
19018
+ char* end = buf;
19019
+ end = upb_Encoder_EncodeVarint32(tag, end);
19020
+ end = upb_Encoder_EncodeVarint64(val, end);
19021
+
19022
+ return UPB_PRIVATE(_upb_Message_AddUnknown)(msg, buf, end - buf, arena,
19023
+ kUpb_AddUnknown_Copy);
19024
+ }
17343
19025
 
17344
- upb_UnknownCompareResult UPB_PRIVATE(_upb_Message_UnknownFieldsAreEqual)(
17345
- const upb_Message* msg1, const upb_Message* msg2, int max_depth);
19026
+ bool _upb_Encoder_AddMapEntryUnknown(upb_Message* msg,
19027
+ const upb_MiniTableField* field,
19028
+ upb_Message* ent_msg,
19029
+ const upb_MiniTable* entry,
19030
+ upb_Arena* arena);
19031
+
19032
+ upb_EncodeStatus _upb_Encode(const upb_Message* msg, const upb_MiniTable* l,
19033
+ int options, upb_Arena* arena, char** buf,
19034
+ size_t* size, bool prepend_len);
17346
19035
 
17347
19036
  #ifdef __cplusplus
17348
19037
  } /* extern "C" */
17349
19038
  #endif
17350
19039
 
17351
19040
 
17352
- #endif /* UPB_MESSAGE_INTERNAL_COMPARE_UNKNOWN_H_ */
17353
-
17354
- #ifndef GOOGLE_UPB_UPB_MESSAGE_INTERNAL_ITERATOR_H__
17355
- #define GOOGLE_UPB_UPB_MESSAGE_INTERNAL_ITERATOR_H__
17356
-
17357
- #include <stddef.h>
17358
- #include <stdint.h>
17359
-
17360
-
17361
- // Must be last.
17362
-
17363
- #define kUpb_BaseField_Begin ((size_t)-1)
17364
- bool UPB_PRIVATE(_upb_Message_NextBaseField)(const upb_Message* msg,
17365
- const upb_MiniTable* m,
17366
- const upb_MiniTableField** out_f,
17367
- upb_MessageValue* out_v,
17368
- uintptr_t* iter);
17369
-
17370
- #endif // GOOGLE_UPB_UPB_MESSAGE_INTERNAL_ITERATOR_H__
19041
+ #endif /* UPB_WIRE_INTERNAL_ENCODE_H_ */
17371
19042
 
17372
19043
  #ifndef UPB_MESSAGE_COPY_H_
17373
19044
  #define UPB_MESSAGE_COPY_H_
@@ -17963,10 +19634,11 @@ UPB_INLINE void* _upb_DefBuilder_Alloc(upb_DefBuilder* ctx, size_t bytes) {
17963
19634
  UPB_INLINE void* _upb_DefBuilder_AllocCounted(upb_DefBuilder* ctx, size_t size,
17964
19635
  size_t count) {
17965
19636
  if (count == 0) return NULL;
17966
- if (SIZE_MAX / size < count) {
19637
+ size_t total_bytes;
19638
+ if (upb_MulOverflow(size, count, &total_bytes)) {
17967
19639
  _upb_DefBuilder_OomErr(ctx);
17968
19640
  }
17969
- return _upb_DefBuilder_Alloc(ctx, size * count);
19641
+ return _upb_DefBuilder_Alloc(ctx, total_bytes);
17970
19642
  }
17971
19643
 
17972
19644
  #define UPB_DEFBUILDER_ALLOCARRAY(ctx, type, count) \
@@ -18006,14 +19678,16 @@ UPB_INLINE void _upb_DefBuilder_CheckIdentFull(upb_DefBuilder* ctx,
18006
19678
  const char c = name.data[i];
18007
19679
  const char d = c | 0x20; // force lowercase
18008
19680
  const bool is_alpha = (('a' <= d) & (d <= 'z')) | (c == '_');
18009
- const bool is_numer = ('0' <= c) & (c <= '9') & !start;
19681
+ // Accept a leading digit even though protoc would reject it because the
19682
+ // C++ DescriptorPool accepts it, and upb should be as relaxed as C++.
19683
+ const bool is_number = ('0' <= c) & (c <= '9');
18010
19684
  const bool is_dot = (c == '.') & !start;
18011
19685
 
18012
- good &= is_alpha | is_numer | is_dot;
18013
- start = is_dot;
19686
+ good &= is_alpha | is_number | is_dot;
19687
+ start = (c == '.');
18014
19688
  }
18015
19689
 
18016
- if (!good) _upb_DefBuilder_CheckIdentSlow(ctx, name, true);
19690
+ if (!good || start) _upb_DefBuilder_CheckIdentSlow(ctx, name, true);
18017
19691
  }
18018
19692
 
18019
19693
  UPB_INLINE bool _upb_DefBuilder_IsLegacyEdition(google_protobuf_Edition edition) {
@@ -18333,14 +20007,22 @@ UPB_INLINE pb_enumvalue_JsonEnumValueOptions* pb_enumvalue_JsonEnumValueOptions_
18333
20007
  UPB_INLINE char* pb_enumvalue_JsonEnumValueOptions_serialize(const pb_enumvalue_JsonEnumValueOptions* msg,
18334
20008
  upb_Arena* arena, size_t* len) {
18335
20009
  char* ptr;
18336
- (void)upb_Encode(UPB_UPCAST(msg), &pb__enumvalue__JsonEnumValueOptions_msg_init, 0, arena, &ptr, len);
20010
+ upb_EncodeStatus status =
20011
+ upb_Encode(UPB_UPCAST(msg), &pb__enumvalue__JsonEnumValueOptions_msg_init, 0, arena, &ptr, len);
20012
+ if (status != kUpb_EncodeStatus_Ok) {
20013
+ return NULL;
20014
+ }
18337
20015
  return ptr;
18338
20016
  }
18339
20017
  UPB_INLINE char* pb_enumvalue_JsonEnumValueOptions_serialize_ex(const pb_enumvalue_JsonEnumValueOptions* msg,
18340
20018
  int options, upb_Arena* arena,
18341
20019
  size_t* len) {
18342
20020
  char* ptr;
18343
- (void)upb_Encode(UPB_UPCAST(msg), &pb__enumvalue__JsonEnumValueOptions_msg_init, options, arena, &ptr, len);
20021
+ upb_EncodeStatus status = upb_Encode(UPB_UPCAST(msg), &pb__enumvalue__JsonEnumValueOptions_msg_init,
20022
+ options, arena, &ptr, len);
20023
+ if (status != kUpb_EncodeStatus_Ok) {
20024
+ return NULL;
20025
+ }
18344
20026
  return ptr;
18345
20027
  }
18346
20028
  UPB_INLINE void pb_enumvalue_JsonEnumValueOptions_clear_string(pb_enumvalue_JsonEnumValueOptions* msg) {
@@ -18696,7 +20378,6 @@ int32_t upb_EnumReservedRange_End(const upb_EnumReservedRange* r);
18696
20378
  // Must be last.
18697
20379
 
18698
20380
  #define DECODE_NOGROUP (uint32_t)-1
18699
- #define kUpb_Decoder_EncodeVarint32MaxSize 5
18700
20381
 
18701
20382
  typedef union {
18702
20383
  bool bool_val;
@@ -18742,6 +20423,9 @@ UPB_INLINE const char* upb_Decoder_Init(upb_Decoder* d, const char* buf,
18742
20423
  d->err = err;
18743
20424
  upb_EpsCopyInputStream_InitWithErrorHandler(&d->input, &buf, size, d->err);
18744
20425
 
20426
+ UPB_STATIC_ASSERT(
20427
+ offsetof(upb_Decoder, input) == 0,
20428
+ "EpsCopyInputStream must be pointer-interconvertible with upb_Decoder");
18745
20429
  UPB_STATIC_ASSERT((int)kUpb_DecodeStatus_Ok == (int)kUpb_ErrorCode_Ok,
18746
20430
  "mismatched error codes");
18747
20431
  UPB_STATIC_ASSERT(
@@ -18879,12 +20563,15 @@ const char* _upb_Decoder_CheckRequired(upb_Decoder* d, const char* ptr,
18879
20563
  const upb_Message* msg,
18880
20564
  const upb_MiniTable* m);
18881
20565
 
20566
+ struct upb_Map* _upb_Decoder_CreateMap(upb_Decoder* d,
20567
+ const upb_MiniTable* entry);
20568
+
18882
20569
  #if UPB_FASTTABLE
18883
20570
  UPB_PRESERVE_NONE
18884
20571
  #endif
18885
20572
  const char* _upb_Decoder_DecodeMessage(upb_Decoder* d, const char* ptr,
18886
20573
  upb_Message* msg,
18887
- const upb_MiniTable* layout);
20574
+ const upb_MiniTable* mt);
18888
20575
 
18889
20576
  UPB_INLINE bool _upb_Decoder_FieldRequiresUtf8Validation(
18890
20577
  const upb_Decoder* d, const upb_MiniTableField* field) {
@@ -18920,236 +20607,20 @@ UPB_INLINE bool _upb_Decoder_ReadString(upb_Decoder* d, const char** ptr,
18920
20607
  return true;
18921
20608
  }
18922
20609
 
18923
- UPB_INLINE char* upb_Decoder_EncodeVarint32(uint32_t val, char* ptr) {
18924
- do {
18925
- uint8_t byte = val & 0x7fU;
18926
- val >>= 7;
18927
- if (val) byte |= 0x80U;
18928
- *(ptr++) = byte;
18929
- } while (val);
18930
- return ptr;
20610
+ // Zig-zag decoding for sint32/sint64. The 32-bit variant must truncate the
20611
+ // varint to 32 bits *before* decoding (proto semantics for overlong sint32
20612
+ // varints, e.g. 2^32 decodes to 0).
20613
+ UPB_INLINE uint32_t _upb_Decoder_ZigZagDecode32(uint64_t val) {
20614
+ uint32_t n = (uint32_t)val;
20615
+ return (n >> 1) ^ -(int32_t)(n & 1);
18931
20616
  }
18932
20617
 
18933
- UPB_FORCEINLINE
18934
- void _upb_Decoder_AddEnumValueToUnknown(upb_Decoder* d, upb_Message* msg,
18935
- const upb_MiniTableField* field,
18936
- uint64_t val) {
18937
- // Unrecognized enum goes into unknown fields.
18938
- // For packed fields the tag could be arbitrarily far in the past,
18939
- // so we just re-encode the tag and value here.
18940
- const uint32_t tag =
18941
- ((uint32_t)field->UPB_PRIVATE(number) << 3) | kUpb_WireType_Varint;
18942
- upb_Message* unknown_msg =
18943
- field->UPB_PRIVATE(mode) & kUpb_LabelFlags_IsExtension ? d->original_msg
18944
- : msg;
18945
- char buf[2 * kUpb_Decoder_EncodeVarint32MaxSize];
18946
- char* end = buf;
18947
- end = upb_Decoder_EncodeVarint32(tag, end);
18948
- end = upb_Decoder_EncodeVarint32(val, end);
18949
-
18950
- if (!UPB_PRIVATE(_upb_Message_AddUnknown)(unknown_msg, buf, end - buf,
18951
- &d->arena, kUpb_AddUnknown_Copy)) {
18952
- upb_ErrorHandler_ThrowError(d->err, kUpb_DecodeStatus_OutOfMemory);
18953
- }
20618
+ UPB_INLINE uint64_t _upb_Decoder_ZigZagDecode64(uint64_t n) {
20619
+ return (n >> 1) ^ -(int64_t)(n & 1);
18954
20620
  }
18955
20621
 
18956
20622
 
18957
20623
  #endif /* UPB_WIRE_INTERNAL_DECODER_H_ */
18958
-
18959
- #ifndef UPB_WIRE_INTERNAL_ENCODE_H_
18960
- #define UPB_WIRE_INTERNAL_ENCODE_H_
18961
-
18962
- #include <setjmp.h>
18963
- #include <stddef.h>
18964
- #include <stdint.h>
18965
-
18966
-
18967
- #ifndef GOOGLE_UPB_UPB_WIRE_INTERNAL_BACK_ALLOC_H__
18968
- #define GOOGLE_UPB_UPB_WIRE_INTERNAL_BACK_ALLOC_H__
18969
-
18970
- #include <stddef.h>
18971
-
18972
-
18973
- // Must be last.
18974
-
18975
- #ifdef __cplusplus
18976
- extern "C" {
18977
- #endif
18978
-
18979
- // Allocates memory from the back of the arena.
18980
- typedef struct {
18981
- upb_Arena* arena;
18982
- char *buf, *limit;
18983
- bool standalone;
18984
- } upb_BackAlloc;
18985
-
18986
- // Needed because C doesn't allow NULL - NULL.
18987
- extern char upb_BackAlloc_sentinel;
18988
-
18989
- char* upb_BackAlloc_Grow(upb_BackAlloc* a, char* ptr, size_t need);
18990
-
18991
- UPB_INLINE char* upb_BackAlloc_Init(upb_BackAlloc* a, upb_Arena* arena) {
18992
- a->arena = arena;
18993
- // This could eagerly steal whatever's in the arena, since stealing with a
18994
- // minimum of 0 can't fail.
18995
- a->buf = &upb_BackAlloc_sentinel;
18996
- a->limit = &upb_BackAlloc_sentinel;
18997
- a->standalone = false;
18998
- return a->limit;
18999
- }
19000
-
19001
- UPB_INLINE void upb_BackAlloc_Abort(upb_BackAlloc* a) {
19002
- if (a->standalone) {
19003
- UPB_PRIVATE(_upb_Arena_FreeBlock)(a->arena, a->buf);
19004
- } else if (a->limit != a->buf) {
19005
- UPB_PRIVATE(_upb_Arena_UseBlock)(a->arena, a->buf, a->limit - a->buf);
19006
- }
19007
- }
19008
-
19009
- UPB_INLINE size_t upb_BackAlloc_Finish(upb_BackAlloc* a, const char* ptr) {
19010
- if (a->standalone) {
19011
- UPB_PRIVATE(_upb_Arena_AddBlock)(a->arena, a->buf);
19012
- }
19013
- if (ptr != a->buf) {
19014
- UPB_PRIVATE(_upb_Arena_UseBlock)(a->arena, a->buf, ptr - a->buf);
19015
- }
19016
- return a->limit - ptr;
19017
- }
19018
-
19019
- UPB_FORCEINLINE bool upb_BackAlloc_HasBytes(const upb_BackAlloc* a,
19020
- const char* ptr, size_t need) {
19021
- size_t have = ptr - a->buf;
19022
- return have >= need;
19023
- }
19024
-
19025
- UPB_FORCEINLINE char* upb_BackAlloc_Reserve(upb_BackAlloc* a, char* ptr,
19026
- size_t need) {
19027
- return upb_BackAlloc_HasBytes(a, ptr, need)
19028
- ? ptr - need
19029
- : upb_BackAlloc_Grow(a, ptr, need);
19030
- }
19031
-
19032
- UPB_INLINE size_t upb_BackAlloc_Size(const upb_BackAlloc* a, const char* ptr) {
19033
- return a->limit - ptr;
19034
- }
19035
-
19036
- #ifdef __cplusplus
19037
- } /* extern "C" */
19038
- #endif
19039
-
19040
-
19041
- #endif // GOOGLE_UPB_UPB_WIRE_INTERNAL_BACK_ALLOC_H__
19042
-
19043
- // Must be last.
19044
-
19045
- #ifdef __cplusplus
19046
- extern "C" {
19047
- #endif
19048
-
19049
- typedef struct {
19050
- upb_BackAlloc alloc;
19051
- int options;
19052
- int depth;
19053
- upb_EncodeStatus status;
19054
- _upb_mapsorter sorter;
19055
- jmp_buf* err;
19056
- } upb_encstate;
19057
-
19058
- UPB_INLINE char* UPB_PRIVATE(_upb_encstate_init)(upb_encstate* e, jmp_buf* err,
19059
- upb_Arena* arena) {
19060
- e->status = kUpb_EncodeStatus_Ok;
19061
- char* ptr = upb_BackAlloc_Init(&e->alloc, arena);
19062
- e->options = 0;
19063
- e->depth = 0;
19064
- e->err = err;
19065
- _upb_mapsorter_init(&e->sorter);
19066
- return ptr;
19067
- }
19068
-
19069
- UPB_INLINE void UPB_PRIVATE(_upb_encstate_destroy)(upb_encstate* e) {
19070
- _upb_mapsorter_destroy(&e->sorter);
19071
- }
19072
-
19073
- // Internal version of upb_Encode that encodes a single field.
19074
- //
19075
- // The caller must clean up the `upb_encstate` by calling
19076
- // `_upb_encstate_destroy(e)` when done.
19077
- upb_EncodeStatus UPB_PRIVATE(_upb_Encode_Field)(upb_encstate* e,
19078
- const upb_Message* msg,
19079
- const upb_MiniTableField* field,
19080
- char** buf, size_t* size,
19081
- int options);
19082
-
19083
- // Internal version of upb_Encode that encodes a single extension.
19084
- //
19085
- // The caller must clean up the `upb_encstate` by calling
19086
- // `_upb_encstate_destroy(e)` when done.
19087
- upb_EncodeStatus UPB_PRIVATE(_upb_Encode_Extension)(
19088
- upb_encstate* e, const upb_MiniTableExtension* ext,
19089
- upb_MessageValue ext_val, bool is_message_set, char** buf, size_t* size,
19090
- int options);
19091
-
19092
- char* encode_message(char* ptr, upb_encstate* e, const upb_Message* msg,
19093
- const upb_MiniTable* m, size_t* size);
19094
-
19095
- #define kUpb_Encoder_EncodeVarint32MaxSize 5
19096
- #define kUpb_Encoder_EncodeVarint64MaxSize 10
19097
-
19098
- static char* upb_Encoder_EncodeVarint32(uint32_t val, char* ptr) {
19099
- do {
19100
- uint8_t byte = val & 0x7fU;
19101
- val >>= 7;
19102
- if (val) byte |= 0x80U;
19103
- *(ptr++) = byte;
19104
- } while (val);
19105
- return ptr;
19106
- }
19107
-
19108
- static char* upb_Encoder_EncodeVarint64(uint64_t val, char* ptr) {
19109
- do {
19110
- uint8_t byte = val & 0x7fU;
19111
- val >>= 7;
19112
- if (val) byte |= 0x80U;
19113
- *(ptr++) = byte;
19114
- } while (val);
19115
- return ptr;
19116
- }
19117
-
19118
- UPB_INLINE
19119
- bool _upb_Encoder_AddEnumValueToUnknown(upb_Message* msg,
19120
- const upb_MiniTableField* field,
19121
- uint64_t val, upb_Arena* arena) {
19122
- // Unrecognized enum goes into unknown fields.
19123
- // For packed fields the tag could be arbitrarily far in the past,
19124
- // so we just re-encode the tag and value here.
19125
- const uint32_t tag =
19126
- ((uint32_t)field->UPB_PRIVATE(number) << 3) | kUpb_WireType_Varint;
19127
- char buf[kUpb_Encoder_EncodeVarint32MaxSize +
19128
- kUpb_Encoder_EncodeVarint64MaxSize];
19129
- char* end = buf;
19130
- end = upb_Encoder_EncodeVarint32(tag, end);
19131
- end = upb_Encoder_EncodeVarint64(val, end);
19132
-
19133
- return UPB_PRIVATE(_upb_Message_AddUnknown)(msg, buf, end - buf, arena,
19134
- kUpb_AddUnknown_Copy);
19135
- }
19136
-
19137
- bool _upb_Encoder_AddMapEntryUnknown(upb_Message* msg,
19138
- const upb_MiniTableField* field,
19139
- upb_Message* ent_msg,
19140
- const upb_MiniTable* entry,
19141
- upb_Arena* arena);
19142
-
19143
- upb_EncodeStatus _upb_Encode(const upb_Message* msg, const upb_MiniTable* l,
19144
- int options, upb_Arena* arena, char** buf,
19145
- size_t* size, bool prepend_len);
19146
-
19147
- #ifdef __cplusplus
19148
- } /* extern "C" */
19149
- #endif
19150
-
19151
-
19152
- #endif /* UPB_WIRE_INTERNAL_ENCODE_H_ */
19153
20624
  #ifndef GOOGLE_UPB_UPB_WIRE_WRITER_H__
19154
20625
  #define GOOGLE_UPB_UPB_WIRE_WRITER_H__
19155
20626
 
@@ -19174,6 +20645,7 @@ UPB_PRIVATE(upb_WireWriter_VarintUnusedSizeFromLeadingZeros64)(uint64_t clz) {
19174
20645
  #undef UPB_SIZE
19175
20646
  #undef UPB_PTR_AT
19176
20647
  #undef UPB_SIZEOF_FLEX
20648
+ #undef UPB_FLEX_CAPACITY
19177
20649
  #undef UPB_SIZEOF_FLEX_WOULD_OVERFLOW
19178
20650
  #undef UPB_MAPTYPE_STRING
19179
20651
  #undef UPB_EXPORT
@@ -19227,6 +20699,8 @@ UPB_PRIVATE(upb_WireWriter_VarintUnusedSizeFromLeadingZeros64)(uint64_t clz) {
19227
20699
  #undef UPB_MALLOC_ALIGN
19228
20700
  #undef UPB_TSAN
19229
20701
  #undef UPB_DEPRECATED
20702
+ #undef UPB_IGNORE_DEPRECATION_START
20703
+ #undef UPB_IGNORE_DEPRECATION_STOP
19230
20704
  #undef UPB_GNUC_MIN
19231
20705
  #undef UPB_CLANG_MIN
19232
20706
  #undef UPB_DESCRIPTOR_UPB_H_FILENAME
@@ -19244,6 +20718,7 @@ UPB_PRIVATE(upb_WireWriter_VarintUnusedSizeFromLeadingZeros64)(uint64_t clz) {
19244
20718
  #undef UPB_HAS_ATTRIBUTE
19245
20719
  #undef UPB_HAS_CPP_ATTRIBUTE
19246
20720
  #undef UPB_HAS_BUILTIN
20721
+ #undef UPB_MEMCPY_INLINE
19247
20722
  #undef UPB_HAS_EXTENSION
19248
20723
  #undef UPB_HAS_FEATURE
19249
20724
  #undef UPB_XSAN_MEMBER
@@ -19258,3 +20733,5 @@ UPB_PRIVATE(upb_WireWriter_VarintUnusedSizeFromLeadingZeros64)(uint64_t clz) {
19258
20733
  #undef _UPB_STRINGIFY
19259
20734
  #undef _UPB_STRINGIFY2
19260
20735
  #undef UPB_CONSTRUCTOR
20736
+ #undef UPB_RETAIN
20737
+ #undef UPB_HIDDEN