ruby_memprofiler_pprof 0.0.1

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.
Files changed (200) hide show
  1. checksums.yaml +7 -0
  2. data/ext/ruby_memprofiler_pprof/backtrace.c +429 -0
  3. data/ext/ruby_memprofiler_pprof/collector.c +1055 -0
  4. data/ext/ruby_memprofiler_pprof/compat.c +182 -0
  5. data/ext/ruby_memprofiler_pprof/extconf.rb +72 -0
  6. data/ext/ruby_memprofiler_pprof/pprof.upb.c +170 -0
  7. data/ext/ruby_memprofiler_pprof/pprof.upb.h +848 -0
  8. data/ext/ruby_memprofiler_pprof/pprof_out.c +285 -0
  9. data/ext/ruby_memprofiler_pprof/ruby_memprofiler_pprof.c +11 -0
  10. data/ext/ruby_memprofiler_pprof/ruby_memprofiler_pprof.h +301 -0
  11. data/ext/ruby_memprofiler_pprof/strtab.c +391 -0
  12. data/ext/ruby_memprofiler_pprof/vendor/upb/BUILD +719 -0
  13. data/ext/ruby_memprofiler_pprof/vendor/upb/CONTRIBUTING.md +37 -0
  14. data/ext/ruby_memprofiler_pprof/vendor/upb/DESIGN.md +201 -0
  15. data/ext/ruby_memprofiler_pprof/vendor/upb/LICENSE +26 -0
  16. data/ext/ruby_memprofiler_pprof/vendor/upb/README.md +78 -0
  17. data/ext/ruby_memprofiler_pprof/vendor/upb/WORKSPACE +58 -0
  18. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/BUILD +53 -0
  19. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/amalgamate.py +129 -0
  20. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/build_defs.bzl +160 -0
  21. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/lua.BUILD +127 -0
  22. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/protobuf.patch +54 -0
  23. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/py_proto_library.bzl +137 -0
  24. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/python_downloads.bzl +84 -0
  25. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/system_python.bzl +101 -0
  26. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/upb_proto_library.bzl +388 -0
  27. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/workspace_deps.bzl +89 -0
  28. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/BUILD +252 -0
  29. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/BUILD.googleapis +54 -0
  30. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/benchmark.cc +333 -0
  31. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/build_defs.bzl +88 -0
  32. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/compare.py +118 -0
  33. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/descriptor.proto +888 -0
  34. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/descriptor_sv.proto +890 -0
  35. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/empty.proto +6 -0
  36. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_protobuf_binary_cc.py +64 -0
  37. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_synthetic_protos.py +118 -0
  38. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_upb_binary_c.py +65 -0
  39. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/BUILD.bazel +102 -0
  40. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/README.md +23 -0
  41. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/build_defs.bzl +73 -0
  42. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/make_cmakelists.py +340 -0
  43. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/staleness_test.py +57 -0
  44. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/staleness_test_lib.py +186 -0
  45. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/render.py +43 -0
  46. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/style-guide.md +65 -0
  47. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/vs-cpp-protos.md +255 -0
  48. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/wrapping-upb.md +444 -0
  49. data/ext/ruby_memprofiler_pprof/vendor/upb/python/BUILD +216 -0
  50. data/ext/ruby_memprofiler_pprof/vendor/upb/python/convert.c +394 -0
  51. data/ext/ruby_memprofiler_pprof/vendor/upb/python/convert.h +63 -0
  52. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor.c +1694 -0
  53. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor.h +80 -0
  54. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_containers.c +704 -0
  55. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_containers.h +114 -0
  56. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_pool.c +650 -0
  57. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_pool.h +48 -0
  58. data/ext/ruby_memprofiler_pprof/vendor/upb/python/dist/BUILD.bazel +193 -0
  59. data/ext/ruby_memprofiler_pprof/vendor/upb/python/dist/dist.bzl +190 -0
  60. data/ext/ruby_memprofiler_pprof/vendor/upb/python/extension_dict.c +247 -0
  61. data/ext/ruby_memprofiler_pprof/vendor/upb/python/extension_dict.h +39 -0
  62. data/ext/ruby_memprofiler_pprof/vendor/upb/python/map.c +522 -0
  63. data/ext/ruby_memprofiler_pprof/vendor/upb/python/map.h +66 -0
  64. data/ext/ruby_memprofiler_pprof/vendor/upb/python/message.c +1909 -0
  65. data/ext/ruby_memprofiler_pprof/vendor/upb/python/message.h +101 -0
  66. data/ext/ruby_memprofiler_pprof/vendor/upb/python/minimal_test.py +183 -0
  67. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/BUILD +70 -0
  68. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/README.md +11 -0
  69. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_database_test_wrapper.py +30 -0
  70. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_pool_test_wrapper.py +45 -0
  71. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_test_wrapper.py +46 -0
  72. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/generator_test_wrapper.py +30 -0
  73. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/json_format_test_wrapper.py +30 -0
  74. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/keywords_test_wrapper.py +30 -0
  75. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/message_factory_test_wrapper.py +37 -0
  76. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/message_test_wrapper.py +52 -0
  77. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/proto_builder_test_wrapper.py +32 -0
  78. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/pyproto_test_wrapper.bzl +36 -0
  79. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/reflection_test_wrapper.py +45 -0
  80. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/service_reflection_test_wrapper.py +30 -0
  81. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/symbol_database_test_wrapper.py +30 -0
  82. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/text_encoding_test_wrapper.py +30 -0
  83. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/text_format_test_wrapper.py +30 -0
  84. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/unknown_fields_test_wrapper.py +30 -0
  85. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/well_known_types_test_wrapper.py +36 -0
  86. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/wire_format_test_wrapper.py +30 -0
  87. data/ext/ruby_memprofiler_pprof/vendor/upb/python/protobuf.c +350 -0
  88. data/ext/ruby_memprofiler_pprof/vendor/upb/python/protobuf.h +230 -0
  89. data/ext/ruby_memprofiler_pprof/vendor/upb/python/py_extension.bzl +55 -0
  90. data/ext/ruby_memprofiler_pprof/vendor/upb/python/python_api.h +61 -0
  91. data/ext/ruby_memprofiler_pprof/vendor/upb/python/repeated.c +828 -0
  92. data/ext/ruby_memprofiler_pprof/vendor/upb/python/repeated.h +69 -0
  93. data/ext/ruby_memprofiler_pprof/vendor/upb/python/unknown_fields.c +404 -0
  94. data/ext/ruby_memprofiler_pprof/vendor/upb/python/unknown_fields.h +39 -0
  95. data/ext/ruby_memprofiler_pprof/vendor/upb/python/version_script.lds +6 -0
  96. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/LICENSE +32 -0
  97. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/README.google +9 -0
  98. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/console.lua +156 -0
  99. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/lunit.lua +725 -0
  100. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/BUILD +19 -0
  101. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/LICENSE +21 -0
  102. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/naive.c +92 -0
  103. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/range2-neon.c +157 -0
  104. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/range2-sse.c +170 -0
  105. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/utf8_range.h +9 -0
  106. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/BUILD.bazel +129 -0
  107. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/README.md +8 -0
  108. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/def.c +939 -0
  109. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/lua_proto_library.bzl +138 -0
  110. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/main.c +83 -0
  111. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/msg.c +1118 -0
  112. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/test.proto +69 -0
  113. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/test_upb.lua +846 -0
  114. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.c +258 -0
  115. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.h +132 -0
  116. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.lua +58 -0
  117. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upbc.cc +134 -0
  118. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/collections.c +192 -0
  119. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/collections.h +174 -0
  120. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/conformance_upb.c +346 -0
  121. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/conformance_upb_failures.txt +1 -0
  122. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode.c +1221 -0
  123. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode.h +94 -0
  124. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_fast.c +1055 -0
  125. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_fast.h +153 -0
  126. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_internal.h +211 -0
  127. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.c +3262 -0
  128. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.h +414 -0
  129. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.hpp +438 -0
  130. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/empty.proto +1 -0
  131. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/encode.c +604 -0
  132. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/encode.h +71 -0
  133. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/fuzz/BUILD +13 -0
  134. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/fuzz/file_descriptor_parsenew_fuzzer.cc +43 -0
  135. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_decode.c +1509 -0
  136. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_decode.h +47 -0
  137. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_encode.c +776 -0
  138. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_encode.h +62 -0
  139. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.c +1147 -0
  140. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.h +189 -0
  141. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.hpp +112 -0
  142. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors.c +363 -0
  143. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors.h +263 -0
  144. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors_internal.h +59 -0
  145. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors_test.cc +425 -0
  146. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_test.cc +230 -0
  147. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg.c +428 -0
  148. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg.h +114 -0
  149. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_internal.h +836 -0
  150. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_test.cc +491 -0
  151. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_test.proto +195 -0
  152. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/port_def.inc +261 -0
  153. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/port_undef.inc +62 -0
  154. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.c +323 -0
  155. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.h +109 -0
  156. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.hpp +37 -0
  157. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/table.c +926 -0
  158. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/table_internal.h +385 -0
  159. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test.proto +74 -0
  160. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_cpp.cc +186 -0
  161. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_cpp.proto +12 -0
  162. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_generated_code.cc +977 -0
  163. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_table.cc +580 -0
  164. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/text_encode.c +472 -0
  165. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/text_encode.h +64 -0
  166. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.c +362 -0
  167. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.h +378 -0
  168. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.hpp +115 -0
  169. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb_internal.h +68 -0
  170. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/BUILD +121 -0
  171. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/README.md +7 -0
  172. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare.c +300 -0
  173. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare.h +66 -0
  174. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare_test.cc +236 -0
  175. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto.c +572 -0
  176. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto.h +62 -0
  177. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_public_import_test.proto +32 -0
  178. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_regular_import_test.proto +36 -0
  179. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_test.cc +143 -0
  180. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_test.proto +119 -0
  181. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_weak_import_test.proto +28 -0
  182. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_wweak_import_test.proto +28 -0
  183. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields.c +311 -0
  184. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields.h +94 -0
  185. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields_test.cc +202 -0
  186. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields_test.proto +48 -0
  187. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/BUILD +78 -0
  188. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/common.cc +77 -0
  189. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/common.h +112 -0
  190. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/protoc-gen-upb.cc +1997 -0
  191. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/protoc-gen-upbdefs.cc +193 -0
  192. data/lib/ruby_memprofiler_pprof/atfork.rb +77 -0
  193. data/lib/ruby_memprofiler_pprof/block_flusher.rb +61 -0
  194. data/lib/ruby_memprofiler_pprof/file_flusher.rb +45 -0
  195. data/lib/ruby_memprofiler_pprof/profile_app.rb +30 -0
  196. data/lib/ruby_memprofiler_pprof/profile_data.rb +18 -0
  197. data/lib/ruby_memprofiler_pprof/version.rb +5 -0
  198. data/lib/ruby_memprofiler_pprof.rb +8 -0
  199. data/libexec/ruby_memprofiler_pprof_profile +16 -0
  200. metadata +257 -0
@@ -0,0 +1,261 @@
1
+ /*
2
+ * Copyright (c) 2009-2021, Google LLC
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ * * Redistributions of source code must retain the above copyright
8
+ * notice, this list of conditions and the following disclaimer.
9
+ * * Redistributions in binary form must reproduce the above copyright
10
+ * notice, this list of conditions and the following disclaimer in the
11
+ * documentation and/or other materials provided with the distribution.
12
+ * * Neither the name of Google LLC nor the
13
+ * names of its contributors may be used to endorse or promote products
14
+ * derived from this software without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ * DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
20
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ /*
29
+ * This is where we define macros used across upb.
30
+ *
31
+ * All of these macros are undef'd in port_undef.inc to avoid leaking them to
32
+ * users.
33
+ *
34
+ * The correct usage is:
35
+ *
36
+ * #include "upb/foobar.h"
37
+ * #include "upb/baz.h"
38
+ *
39
+ * // MUST be last included header.
40
+ * #include "upb/port_def.inc"
41
+ *
42
+ * // Code for this file.
43
+ * // <...>
44
+ *
45
+ * // Can be omitted for .c files, required for .h.
46
+ * #include "upb/port_undef.inc"
47
+ *
48
+ * This file is private and must not be included by users!
49
+ */
50
+
51
+ #if !((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
52
+ (defined(__cplusplus) && __cplusplus >= 201103L) || \
53
+ (defined(_MSC_VER) && _MSC_VER >= 1900))
54
+ #error upb requires C99 or C++11 or MSVC >= 2015.
55
+ #endif
56
+
57
+ #include <stdint.h>
58
+ #include <stddef.h>
59
+
60
+ #if UINTPTR_MAX == 0xffffffff
61
+ #define UPB_SIZE(size32, size64) size32
62
+ #else
63
+ #define UPB_SIZE(size32, size64) size64
64
+ #endif
65
+
66
+ /* If we always read/write as a consistent type to each address, this shouldn't
67
+ * violate aliasing.
68
+ */
69
+ #define UPB_PTR_AT(msg, ofs, type) ((type*)((char*)(msg) + (ofs)))
70
+
71
+ #define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \
72
+ *UPB_PTR_AT(msg, case_offset, int) == case_val \
73
+ ? *UPB_PTR_AT(msg, offset, fieldtype) \
74
+ : default
75
+
76
+ #define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \
77
+ *UPB_PTR_AT(msg, case_offset, int) = case_val; \
78
+ *UPB_PTR_AT(msg, offset, fieldtype) = value;
79
+
80
+ #define UPB_MAPTYPE_STRING 0
81
+
82
+ /* UPB_INLINE: inline if possible, emit standalone code if required. */
83
+ #ifdef __cplusplus
84
+ #define UPB_INLINE inline
85
+ #elif defined (__GNUC__) || defined(__clang__)
86
+ #define UPB_INLINE static __inline__
87
+ #else
88
+ #define UPB_INLINE static
89
+ #endif
90
+
91
+ #define UPB_ALIGN_UP(size, align) (((size) + (align) - 1) / (align) * (align))
92
+ #define UPB_ALIGN_DOWN(size, align) ((size) / (align) * (align))
93
+ #define UPB_ALIGN_MALLOC(size) UPB_ALIGN_UP(size, 16)
94
+ #define UPB_ALIGN_OF(type) offsetof (struct { char c; type member; }, member)
95
+
96
+ /* Hints to the compiler about likely/unlikely branches. */
97
+ #if defined (__GNUC__) || defined(__clang__)
98
+ #define UPB_LIKELY(x) __builtin_expect((x),1)
99
+ #define UPB_UNLIKELY(x) __builtin_expect((x),0)
100
+ #else
101
+ #define UPB_LIKELY(x) (x)
102
+ #define UPB_UNLIKELY(x) (x)
103
+ #endif
104
+
105
+ /* Macros for function attributes on compilers that support them. */
106
+ #ifdef __GNUC__
107
+ #define UPB_FORCEINLINE __inline__ __attribute__((always_inline))
108
+ #define UPB_NOINLINE __attribute__((noinline))
109
+ #define UPB_NORETURN __attribute__((__noreturn__))
110
+ #define UPB_PRINTF(str, first_vararg) __attribute__((format (printf, str, first_vararg)))
111
+ #elif defined(_MSC_VER)
112
+ #define UPB_NOINLINE
113
+ #define UPB_FORCEINLINE
114
+ #define UPB_NORETURN __declspec(noreturn)
115
+ #define UPB_PRINTF(str, first_vararg)
116
+ #else /* !defined(__GNUC__) */
117
+ #define UPB_FORCEINLINE
118
+ #define UPB_NOINLINE
119
+ #define UPB_NORETURN
120
+ #define UPB_PRINTF(str, first_vararg)
121
+ #endif
122
+
123
+ #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
124
+ #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
125
+
126
+ #define UPB_UNUSED(var) (void)var
127
+
128
+ /* UPB_ASSUME(): in release mode, we tell the compiler to assume this is true.
129
+ */
130
+ #ifdef NDEBUG
131
+ #ifdef __GNUC__
132
+ #define UPB_ASSUME(expr) if (!(expr)) __builtin_unreachable()
133
+ #elif defined _MSC_VER
134
+ #define UPB_ASSUME(expr) if (!(expr)) __assume(0)
135
+ #else
136
+ #define UPB_ASSUME(expr) do {} while (false && (expr))
137
+ #endif
138
+ #else
139
+ #define UPB_ASSUME(expr) assert(expr)
140
+ #endif
141
+
142
+ /* UPB_ASSERT(): in release mode, we use the expression without letting it be
143
+ * evaluated. This prevents "unused variable" warnings. */
144
+ #ifdef NDEBUG
145
+ #define UPB_ASSERT(expr) do {} while (false && (expr))
146
+ #else
147
+ #define UPB_ASSERT(expr) assert(expr)
148
+ #endif
149
+
150
+ #if defined(__GNUC__) || defined(__clang__)
151
+ #define UPB_UNREACHABLE() do { assert(0); __builtin_unreachable(); } while(0)
152
+ #else
153
+ #define UPB_UNREACHABLE() do { assert(0); } while(0)
154
+ #endif
155
+
156
+ /* UPB_SETJMP() / UPB_LONGJMP(): avoid setting/restoring signal mask. */
157
+ #ifdef __APPLE__
158
+ #define UPB_SETJMP(buf) _setjmp(buf)
159
+ #define UPB_LONGJMP(buf, val) _longjmp(buf, val)
160
+ #else
161
+ #define UPB_SETJMP(buf) setjmp(buf)
162
+ #define UPB_LONGJMP(buf, val) longjmp(buf, val)
163
+ #endif
164
+
165
+ /* UPB_PTRADD(ptr, ofs): add pointer while avoiding "NULL + 0" UB */
166
+ #define UPB_PTRADD(ptr, ofs) ((ofs) ? (ptr) + (ofs) : (ptr))
167
+
168
+ /* Configure whether fasttable is switched on or not. *************************/
169
+
170
+ #ifdef __has_attribute
171
+ #define UPB_HAS_ATTRIBUTE(x) __has_attribute(x)
172
+ #else
173
+ #define UPB_HAS_ATTRIBUTE(x) 0
174
+ #endif
175
+
176
+ #if UPB_HAS_ATTRIBUTE(musttail)
177
+ #define UPB_MUSTTAIL __attribute__((musttail))
178
+ #else
179
+ #define UPB_MUSTTAIL
180
+ #endif
181
+
182
+ #undef UPB_HAS_ATTRIBUTE
183
+
184
+ /* This check is not fully robust: it does not require that we have "musttail"
185
+ * support available. We need tail calls to avoid consuming arbitrary amounts
186
+ * of stack space.
187
+ *
188
+ * GCC/Clang can mostly be trusted to generate tail calls as long as
189
+ * optimization is enabled, but, debug builds will not generate tail calls
190
+ * unless "musttail" is available.
191
+ *
192
+ * We should probably either:
193
+ * 1. require that the compiler supports musttail.
194
+ * 2. add some fallback code for when musttail isn't available (ie. return
195
+ * instead of tail calling). This is safe and portable, but this comes at
196
+ * a CPU cost.
197
+ */
198
+ #if (defined(__x86_64__) || defined(__aarch64__)) && defined(__GNUC__)
199
+ #define UPB_FASTTABLE_SUPPORTED 1
200
+ #else
201
+ #define UPB_FASTTABLE_SUPPORTED 0
202
+ #endif
203
+
204
+ /* define UPB_ENABLE_FASTTABLE to force fast table support.
205
+ * This is useful when we want to ensure we are really getting fasttable,
206
+ * for example for testing or benchmarking. */
207
+ #if defined(UPB_ENABLE_FASTTABLE)
208
+ #if !UPB_FASTTABLE_SUPPORTED
209
+ #error fasttable is x86-64/ARM64 only and requires GCC or Clang.
210
+ #endif
211
+ #define UPB_FASTTABLE 1
212
+ /* Define UPB_TRY_ENABLE_FASTTABLE to use fasttable if possible.
213
+ * This is useful for releasing code that might be used on multiple platforms,
214
+ * for example the PHP or Ruby C extensions. */
215
+ #elif defined(UPB_TRY_ENABLE_FASTTABLE)
216
+ #define UPB_FASTTABLE UPB_FASTTABLE_SUPPORTED
217
+ #else
218
+ #define UPB_FASTTABLE 0
219
+ #endif
220
+
221
+ /* UPB_FASTTABLE_INIT() allows protos compiled for fasttable to gracefully
222
+ * degrade to non-fasttable if we are using UPB_TRY_ENABLE_FASTTABLE. */
223
+ #if !UPB_FASTTABLE && defined(UPB_TRY_ENABLE_FASTTABLE)
224
+ #define UPB_FASTTABLE_INIT(...)
225
+ #else
226
+ #define UPB_FASTTABLE_INIT(...) __VA_ARGS__
227
+ #endif
228
+
229
+ #undef UPB_FASTTABLE_SUPPORTED
230
+
231
+ /* ASAN poisoning (for arena) *************************************************/
232
+
233
+ #if defined(__SANITIZE_ADDRESS__)
234
+ #define UPB_ASAN 1
235
+ #ifdef __cplusplus
236
+ extern "C" {
237
+ #endif
238
+ void __asan_poison_memory_region(void const volatile *addr, size_t size);
239
+ void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
240
+ #ifdef __cplusplus
241
+ } /* extern "C" */
242
+ #endif
243
+ #define UPB_POISON_MEMORY_REGION(addr, size) \
244
+ __asan_poison_memory_region((addr), (size))
245
+ #define UPB_UNPOISON_MEMORY_REGION(addr, size) \
246
+ __asan_unpoison_memory_region((addr), (size))
247
+ #else
248
+ #define UPB_ASAN 0
249
+ #define UPB_POISON_MEMORY_REGION(addr, size) \
250
+ ((void)(addr), (void)(size))
251
+ #define UPB_UNPOISON_MEMORY_REGION(addr, size) \
252
+ ((void)(addr), (void)(size))
253
+ #endif
254
+
255
+ /* Disable proto2 arena behavior (TEMPORARY) **********************************/
256
+
257
+ #ifdef UPB_DISABLE_PROTO2_ENUM_CHECKING
258
+ #define UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3 1
259
+ #else
260
+ #define UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3 0
261
+ #endif
@@ -0,0 +1,62 @@
1
+ /*
2
+ * Copyright (c) 2009-2021, Google LLC
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ * * Redistributions of source code must retain the above copyright
8
+ * notice, this list of conditions and the following disclaimer.
9
+ * * Redistributions in binary form must reproduce the above copyright
10
+ * notice, this list of conditions and the following disclaimer in the
11
+ * documentation and/or other materials provided with the distribution.
12
+ * * Neither the name of Google LLC nor the
13
+ * names of its contributors may be used to endorse or promote products
14
+ * derived from this software without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ * DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
20
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ /* See port_def.inc. This should #undef all macros #defined there. */
29
+
30
+ #undef UPB_SIZE
31
+ #undef UPB_PTR_AT
32
+ #undef UPB_READ_ONEOF
33
+ #undef UPB_WRITE_ONEOF
34
+ #undef UPB_MAPTYPE_STRING
35
+ #undef UPB_INLINE
36
+ #undef UPB_ALIGN_UP
37
+ #undef UPB_ALIGN_DOWN
38
+ #undef UPB_ALIGN_MALLOC
39
+ #undef UPB_ALIGN_OF
40
+ #undef UPB_LIKELY
41
+ #undef UPB_UNLIKELY
42
+ #undef UPB_FORCEINLINE
43
+ #undef UPB_NOINLINE
44
+ #undef UPB_NORETURN
45
+ #undef UPB_PRINTF
46
+ #undef UPB_MAX
47
+ #undef UPB_MIN
48
+ #undef UPB_UNUSED
49
+ #undef UPB_ASSUME
50
+ #undef UPB_ASSERT
51
+ #undef UPB_UNREACHABLE
52
+ #undef UPB_SETJMP
53
+ #undef UPB_LONGJMP
54
+ #undef UPB_PTRADD
55
+ #undef UPB_MUSTTAIL
56
+ #undef UPB_FASTTABLE_SUPPORTED
57
+ #undef UPB_FASTTABLE
58
+ #undef UPB_FASTTABLE_INIT
59
+ #undef UPB_POISON_MEMORY_REGION
60
+ #undef UPB_UNPOISON_MEMORY_REGION
61
+ #undef UPB_ASAN
62
+ #undef UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3
@@ -0,0 +1,323 @@
1
+ /*
2
+ * Copyright (c) 2009-2021, Google LLC
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ * * Redistributions of source code must retain the above copyright
8
+ * notice, this list of conditions and the following disclaimer.
9
+ * * Redistributions in binary form must reproduce the above copyright
10
+ * notice, this list of conditions and the following disclaimer in the
11
+ * documentation and/or other materials provided with the distribution.
12
+ * * Neither the name of Google LLC nor the
13
+ * names of its contributors may be used to endorse or promote products
14
+ * derived from this software without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
+ * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ #include "upb/reflection.h"
29
+
30
+ #include <string.h>
31
+
32
+ #include "upb/msg.h"
33
+ #include "upb/port_def.inc"
34
+ #include "upb/table_internal.h"
35
+
36
+ static size_t get_field_size(const upb_MiniTable_Field* f) {
37
+ static unsigned char sizes[] = {
38
+ 0, /* 0 */
39
+ 8, /* kUpb_FieldType_Double */
40
+ 4, /* kUpb_FieldType_Float */
41
+ 8, /* kUpb_FieldType_Int64 */
42
+ 8, /* kUpb_FieldType_UInt64 */
43
+ 4, /* kUpb_FieldType_Int32 */
44
+ 8, /* kUpb_FieldType_Fixed64 */
45
+ 4, /* kUpb_FieldType_Fixed32 */
46
+ 1, /* kUpb_FieldType_Bool */
47
+ sizeof(upb_StringView), /* kUpb_FieldType_String */
48
+ sizeof(void*), /* kUpb_FieldType_Group */
49
+ sizeof(void*), /* kUpb_FieldType_Message */
50
+ sizeof(upb_StringView), /* kUpb_FieldType_Bytes */
51
+ 4, /* kUpb_FieldType_UInt32 */
52
+ 4, /* kUpb_FieldType_Enum */
53
+ 4, /* kUpb_FieldType_SFixed32 */
54
+ 8, /* kUpb_FieldType_SFixed64 */
55
+ 4, /* kUpb_FieldType_SInt32 */
56
+ 8, /* kUpb_FieldType_SInt64 */
57
+ };
58
+ return upb_IsRepeatedOrMap(f) ? sizeof(void*) : sizes[f->descriptortype];
59
+ }
60
+
61
+ /** upb_Message
62
+ * *******************************************************************/
63
+
64
+ upb_Message* upb_Message_New(const upb_MessageDef* m, upb_Arena* a) {
65
+ return _upb_Message_New(upb_MessageDef_MiniTable(m), a);
66
+ }
67
+
68
+ static bool in_oneof(const upb_MiniTable_Field* field) {
69
+ return field->presence < 0;
70
+ }
71
+
72
+ static upb_MessageValue _upb_Message_Getraw(const upb_Message* msg,
73
+ const upb_FieldDef* f) {
74
+ const upb_MiniTable_Field* field = upb_FieldDef_MiniTable(f);
75
+ const char* mem = UPB_PTR_AT(msg, field->offset, char);
76
+ upb_MessageValue val = {0};
77
+ memcpy(&val, mem, get_field_size(field));
78
+ return val;
79
+ }
80
+
81
+ bool upb_Message_Has(const upb_Message* msg, const upb_FieldDef* f) {
82
+ assert(upb_FieldDef_HasPresence(f));
83
+ if (upb_FieldDef_IsExtension(f)) {
84
+ const upb_MiniTable_Extension* ext = _upb_FieldDef_ExtensionMiniTable(f);
85
+ return _upb_Message_Getext(msg, ext) != NULL;
86
+ } else {
87
+ const upb_MiniTable_Field* field = upb_FieldDef_MiniTable(f);
88
+ if (in_oneof(field)) {
89
+ return _upb_getoneofcase_field(msg, field) == field->number;
90
+ } else if (field->presence > 0) {
91
+ return _upb_hasbit_field(msg, field);
92
+ } else {
93
+ UPB_ASSERT(field->descriptortype == kUpb_FieldType_Message ||
94
+ field->descriptortype == kUpb_FieldType_Group);
95
+ return _upb_Message_Getraw(msg, f).msg_val != NULL;
96
+ }
97
+ }
98
+ }
99
+
100
+ const upb_FieldDef* upb_Message_WhichOneof(const upb_Message* msg,
101
+ const upb_OneofDef* o) {
102
+ const upb_FieldDef* f = upb_OneofDef_Field(o, 0);
103
+ if (upb_OneofDef_IsSynthetic(o)) {
104
+ UPB_ASSERT(upb_OneofDef_FieldCount(o) == 1);
105
+ return upb_Message_Has(msg, f) ? f : NULL;
106
+ } else {
107
+ const upb_MiniTable_Field* field = upb_FieldDef_MiniTable(f);
108
+ uint32_t oneof_case = _upb_getoneofcase_field(msg, field);
109
+ f = oneof_case ? upb_OneofDef_LookupNumber(o, oneof_case) : NULL;
110
+ UPB_ASSERT((f != NULL) == (oneof_case != 0));
111
+ return f;
112
+ }
113
+ }
114
+
115
+ upb_MessageValue upb_Message_Get(const upb_Message* msg,
116
+ const upb_FieldDef* f) {
117
+ if (upb_FieldDef_IsExtension(f)) {
118
+ const upb_Message_Extension* ext =
119
+ _upb_Message_Getext(msg, _upb_FieldDef_ExtensionMiniTable(f));
120
+ if (ext) {
121
+ upb_MessageValue val;
122
+ memcpy(&val, &ext->data, sizeof(val));
123
+ return val;
124
+ } else if (upb_FieldDef_IsRepeated(f)) {
125
+ return (upb_MessageValue){.array_val = NULL};
126
+ }
127
+ } else if (!upb_FieldDef_HasPresence(f) || upb_Message_Has(msg, f)) {
128
+ return _upb_Message_Getraw(msg, f);
129
+ }
130
+ return upb_FieldDef_Default(f);
131
+ }
132
+
133
+ upb_MutableMessageValue upb_Message_Mutable(upb_Message* msg,
134
+ const upb_FieldDef* f,
135
+ upb_Arena* a) {
136
+ UPB_ASSERT(upb_FieldDef_IsSubMessage(f) || upb_FieldDef_IsRepeated(f));
137
+ if (upb_FieldDef_HasPresence(f) && !upb_Message_Has(msg, f)) {
138
+ // We need to skip the upb_Message_Get() call in this case.
139
+ goto make;
140
+ }
141
+
142
+ upb_MessageValue val = upb_Message_Get(msg, f);
143
+ if (val.array_val) {
144
+ return (upb_MutableMessageValue){.array = (upb_Array*)val.array_val};
145
+ }
146
+
147
+ upb_MutableMessageValue ret;
148
+ make:
149
+ if (!a) return (upb_MutableMessageValue){.array = NULL};
150
+ if (upb_FieldDef_IsMap(f)) {
151
+ const upb_MessageDef* entry = upb_FieldDef_MessageSubDef(f);
152
+ const upb_FieldDef* key =
153
+ upb_MessageDef_FindFieldByNumber(entry, kUpb_MapEntry_KeyFieldNumber);
154
+ const upb_FieldDef* value =
155
+ upb_MessageDef_FindFieldByNumber(entry, kUpb_MapEntry_ValueFieldNumber);
156
+ ret.map =
157
+ upb_Map_New(a, upb_FieldDef_CType(key), upb_FieldDef_CType(value));
158
+ } else if (upb_FieldDef_IsRepeated(f)) {
159
+ ret.array = upb_Array_New(a, upb_FieldDef_CType(f));
160
+ } else {
161
+ UPB_ASSERT(upb_FieldDef_IsSubMessage(f));
162
+ ret.msg = upb_Message_New(upb_FieldDef_MessageSubDef(f), a);
163
+ }
164
+
165
+ val.array_val = ret.array;
166
+ upb_Message_Set(msg, f, val, a);
167
+
168
+ return ret;
169
+ }
170
+
171
+ bool upb_Message_Set(upb_Message* msg, const upb_FieldDef* f,
172
+ upb_MessageValue val, upb_Arena* a) {
173
+ if (upb_FieldDef_IsExtension(f)) {
174
+ upb_Message_Extension* ext = _upb_Message_GetOrCreateExtension(
175
+ msg, _upb_FieldDef_ExtensionMiniTable(f), a);
176
+ if (!ext) return false;
177
+ memcpy(&ext->data, &val, sizeof(val));
178
+ } else {
179
+ const upb_MiniTable_Field* field = upb_FieldDef_MiniTable(f);
180
+ char* mem = UPB_PTR_AT(msg, field->offset, char);
181
+ memcpy(mem, &val, get_field_size(field));
182
+ if (field->presence > 0) {
183
+ _upb_sethas_field(msg, field);
184
+ } else if (in_oneof(field)) {
185
+ *_upb_oneofcase_field(msg, field) = field->number;
186
+ }
187
+ }
188
+ return true;
189
+ }
190
+
191
+ void upb_Message_ClearField(upb_Message* msg, const upb_FieldDef* f) {
192
+ if (upb_FieldDef_IsExtension(f)) {
193
+ _upb_Message_Clearext(msg, _upb_FieldDef_ExtensionMiniTable(f));
194
+ } else {
195
+ const upb_MiniTable_Field* field = upb_FieldDef_MiniTable(f);
196
+ char* mem = UPB_PTR_AT(msg, field->offset, char);
197
+
198
+ if (field->presence > 0) {
199
+ _upb_clearhas_field(msg, field);
200
+ } else if (in_oneof(field)) {
201
+ uint32_t* oneof_case = _upb_oneofcase_field(msg, field);
202
+ if (*oneof_case != field->number) return;
203
+ *oneof_case = 0;
204
+ }
205
+
206
+ memset(mem, 0, get_field_size(field));
207
+ }
208
+ }
209
+
210
+ void upb_Message_Clear(upb_Message* msg, const upb_MessageDef* m) {
211
+ _upb_Message_Clear(msg, upb_MessageDef_MiniTable(m));
212
+ }
213
+
214
+ bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
215
+ const upb_DefPool* ext_pool, const upb_FieldDef** out_f,
216
+ upb_MessageValue* out_val, size_t* iter) {
217
+ size_t i = *iter;
218
+ size_t n = upb_MessageDef_FieldCount(m);
219
+ const upb_MessageValue zero = {0};
220
+ UPB_UNUSED(ext_pool);
221
+
222
+ /* Iterate over normal fields, returning the first one that is set. */
223
+ while (++i < n) {
224
+ const upb_FieldDef* f = upb_MessageDef_Field(m, i);
225
+ upb_MessageValue val = _upb_Message_Getraw(msg, f);
226
+
227
+ /* Skip field if unset or empty. */
228
+ if (upb_FieldDef_HasPresence(f)) {
229
+ if (!upb_Message_Has(msg, f)) continue;
230
+ } else {
231
+ upb_MessageValue test = val;
232
+ if (upb_FieldDef_IsString(f) && !upb_FieldDef_IsRepeated(f)) {
233
+ /* Clear string pointer, only size matters (ptr could be non-NULL). */
234
+ test.str_val.data = NULL;
235
+ }
236
+ /* Continue if NULL or 0. */
237
+ if (memcmp(&test, &zero, sizeof(test)) == 0) continue;
238
+
239
+ /* Continue on empty array or map. */
240
+ if (upb_FieldDef_IsMap(f)) {
241
+ if (upb_Map_Size(test.map_val) == 0) continue;
242
+ } else if (upb_FieldDef_IsRepeated(f)) {
243
+ if (upb_Array_Size(test.array_val) == 0) continue;
244
+ }
245
+ }
246
+
247
+ *out_val = val;
248
+ *out_f = f;
249
+ *iter = i;
250
+ return true;
251
+ }
252
+
253
+ if (ext_pool) {
254
+ /* Return any extensions that are set. */
255
+ size_t count;
256
+ const upb_Message_Extension* ext = _upb_Message_Getexts(msg, &count);
257
+ if (i - n < count) {
258
+ ext += count - 1 - (i - n);
259
+ memcpy(out_val, &ext->data, sizeof(*out_val));
260
+ *out_f = _upb_DefPool_FindExtensionByMiniTable(ext_pool, ext->ext);
261
+ *iter = i;
262
+ return true;
263
+ }
264
+ }
265
+
266
+ *iter = i;
267
+ return false;
268
+ }
269
+
270
+ bool _upb_Message_DiscardUnknown(upb_Message* msg, const upb_MessageDef* m,
271
+ int depth) {
272
+ size_t iter = kUpb_Message_Begin;
273
+ const upb_FieldDef* f;
274
+ upb_MessageValue val;
275
+ bool ret = true;
276
+
277
+ if (--depth == 0) return false;
278
+
279
+ _upb_Message_DiscardUnknown_shallow(msg);
280
+
281
+ while (upb_Message_Next(msg, m, NULL /*ext_pool*/, &f, &val, &iter)) {
282
+ const upb_MessageDef* subm = upb_FieldDef_MessageSubDef(f);
283
+ if (!subm) continue;
284
+ if (upb_FieldDef_IsMap(f)) {
285
+ const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(subm, 2);
286
+ const upb_MessageDef* val_m = upb_FieldDef_MessageSubDef(val_f);
287
+ upb_Map* map = (upb_Map*)val.map_val;
288
+ size_t iter = kUpb_Map_Begin;
289
+
290
+ if (!val_m) continue;
291
+
292
+ while (upb_MapIterator_Next(map, &iter)) {
293
+ upb_MessageValue map_val = upb_MapIterator_Value(map, iter);
294
+ if (!_upb_Message_DiscardUnknown((upb_Message*)map_val.msg_val, val_m,
295
+ depth)) {
296
+ ret = false;
297
+ }
298
+ }
299
+ } else if (upb_FieldDef_IsRepeated(f)) {
300
+ const upb_Array* arr = val.array_val;
301
+ size_t i, n = upb_Array_Size(arr);
302
+ for (i = 0; i < n; i++) {
303
+ upb_MessageValue elem = upb_Array_Get(arr, i);
304
+ if (!_upb_Message_DiscardUnknown((upb_Message*)elem.msg_val, subm,
305
+ depth)) {
306
+ ret = false;
307
+ }
308
+ }
309
+ } else {
310
+ if (!_upb_Message_DiscardUnknown((upb_Message*)val.msg_val, subm,
311
+ depth)) {
312
+ ret = false;
313
+ }
314
+ }
315
+ }
316
+
317
+ return ret;
318
+ }
319
+
320
+ bool upb_Message_DiscardUnknown(upb_Message* msg, const upb_MessageDef* m,
321
+ int maxdepth) {
322
+ return _upb_Message_DiscardUnknown(msg, m, maxdepth);
323
+ }