ruby_memprofiler_pprof 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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,580 @@
1
+ // Copyright (c) 2009-2021, Google LLC
2
+ // All rights reserved.
3
+ //
4
+ // Redistribution and use in source and binary forms, with or without
5
+ // modification, are permitted provided that the following conditions are met:
6
+ // * Redistributions of source code must retain the above copyright
7
+ // notice, this list of conditions and the following disclaimer.
8
+ // * Redistributions in binary form must reproduce the above copyright
9
+ // notice, this list of conditions and the following disclaimer in the
10
+ // documentation and/or other materials provided with the distribution.
11
+ // * Neither the name of Google LLC nor the
12
+ // names of its contributors may be used to endorse or promote products
13
+ // derived from this software without specific prior written permission.
14
+ //
15
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
+ // ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
19
+ // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
+ // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
+ // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
+ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+
26
+ /*
27
+ * Tests for upb_table.
28
+ */
29
+
30
+ #include <limits.h>
31
+ #include <string.h>
32
+ #include <sys/resource.h>
33
+
34
+ #include <iostream>
35
+ #include <map>
36
+ #include <set>
37
+ #include <string>
38
+ #include <unordered_map>
39
+ #include <vector>
40
+
41
+ #include "gtest/gtest.h"
42
+ #include "upb/table_internal.h"
43
+ #include "upb/upb.hpp"
44
+
45
+ // Must be last.
46
+ #include "upb/port_def.inc"
47
+
48
+ // Convenience interface for C++. We don't put this in upb itself because
49
+ // the table is not exposed to users.
50
+
51
+ namespace upb {
52
+
53
+ template <class T>
54
+ upb_value MakeUpbValue(T val);
55
+ template <class T>
56
+ T GetUpbValue(upb_value val);
57
+
58
+ #define FUNCS(name, type_t, enumval) \
59
+ template <> \
60
+ upb_value MakeUpbValue<type_t>(type_t val) { \
61
+ return upb_value_##name(val); \
62
+ } \
63
+ template <> \
64
+ type_t GetUpbValue<type_t>(upb_value val) { \
65
+ return upb_value_get##name(val); \
66
+ }
67
+
68
+ FUNCS(int32, int32_t, UPB_CTYPE_INT32)
69
+ FUNCS(int64, int64_t, UPB_CTYPE_INT64)
70
+ FUNCS(uint32, uint32_t, UPB_CTYPE_UINT32)
71
+ FUNCS(uint64, uint64_t, UPB_CTYPE_UINT64)
72
+ FUNCS(bool, bool, UPB_CTYPE_BOOL)
73
+ FUNCS(cstr, char*, UPB_CTYPE_CSTR)
74
+ FUNCS(ptr, void*, UPB_CTYPE_PTR)
75
+ FUNCS(constptr, const void*, UPB_CTYPE_CONSTPTR)
76
+
77
+ #undef FUNCS
78
+
79
+ class IntTable {
80
+ public:
81
+ IntTable() { upb_inttable_init(&table_, arena_.ptr()); }
82
+
83
+ size_t count() { return upb_inttable_count(&table_); }
84
+
85
+ bool Insert(uintptr_t key, upb_value val) {
86
+ return upb_inttable_insert(&table_, key, val, arena_.ptr());
87
+ }
88
+
89
+ bool Replace(uintptr_t key, upb_value val) {
90
+ return upb_inttable_replace(&table_, key, val);
91
+ }
92
+
93
+ std::pair<bool, upb_value> Remove(uintptr_t key) {
94
+ std::pair<bool, upb_value> ret;
95
+ ret.first = upb_inttable_remove(&table_, key, &ret.second);
96
+ return ret;
97
+ }
98
+
99
+ std::pair<bool, upb_value> Lookup(uintptr_t key) const {
100
+ std::pair<bool, upb_value> ret;
101
+ ret.first = upb_inttable_lookup(&table_, key, &ret.second);
102
+ return ret;
103
+ }
104
+
105
+ std::pair<bool, upb_value> Lookup32(uint32_t key) const {
106
+ std::pair<bool, upb_value> ret;
107
+ ret.first = upb_inttable_lookup(&table_, key, &ret.second);
108
+ return ret;
109
+ }
110
+
111
+ void Compact() { upb_inttable_compact(&table_, arena_.ptr()); }
112
+
113
+ class iterator : public std::iterator<std::forward_iterator_tag,
114
+ std::pair<uintptr_t, upb_value> > {
115
+ public:
116
+ explicit iterator(IntTable* table) {
117
+ upb_inttable_begin(&iter_, &table->table_);
118
+ }
119
+
120
+ static iterator end(IntTable* table) {
121
+ iterator iter(table);
122
+ upb_inttable_iter_setdone(&iter.iter_);
123
+ return iter;
124
+ }
125
+
126
+ void operator++() { return upb_inttable_next(&iter_); }
127
+
128
+ std::pair<uintptr_t, upb_value> operator*() const {
129
+ std::pair<uintptr_t, upb_value> ret;
130
+ ret.first = upb_inttable_iter_key(&iter_);
131
+ ret.second = upb_inttable_iter_value(&iter_);
132
+ return ret;
133
+ }
134
+
135
+ bool operator==(const iterator& other) const {
136
+ return upb_inttable_iter_isequal(&iter_, &other.iter_);
137
+ }
138
+
139
+ bool operator!=(const iterator& other) const { return !(*this == other); }
140
+
141
+ private:
142
+ upb_inttable_iter iter_;
143
+ };
144
+
145
+ upb::Arena arena_;
146
+ upb_inttable table_;
147
+ };
148
+
149
+ class StrTable {
150
+ public:
151
+ StrTable() { upb_strtable_init(&table_, 4, arena_.ptr()); }
152
+
153
+ size_t count() { return upb_strtable_count(&table_); }
154
+
155
+ bool Insert(const std::string& key, upb_value val) {
156
+ return upb_strtable_insert(&table_, key.c_str(), key.size(), val,
157
+ arena_.ptr());
158
+ }
159
+
160
+ std::pair<bool, upb_value> Remove(const std::string& key) {
161
+ std::pair<bool, upb_value> ret;
162
+ ret.first =
163
+ upb_strtable_remove2(&table_, key.c_str(), key.size(), &ret.second);
164
+ return ret;
165
+ }
166
+
167
+ std::pair<bool, upb_value> Lookup(const std::string& key) const {
168
+ std::pair<bool, upb_value> ret;
169
+ ret.first =
170
+ upb_strtable_lookup2(&table_, key.c_str(), key.size(), &ret.second);
171
+ return ret;
172
+ }
173
+
174
+ void Resize(size_t size_lg2) {
175
+ upb_strtable_resize(&table_, size_lg2, arena_.ptr());
176
+ }
177
+
178
+ class iterator : public std::iterator<std::forward_iterator_tag,
179
+ std::pair<std::string, upb_value> > {
180
+ public:
181
+ explicit iterator(StrTable* table) {
182
+ upb_strtable_begin(&iter_, &table->table_);
183
+ }
184
+
185
+ static iterator end(StrTable* table) {
186
+ iterator iter(table);
187
+ upb_strtable_iter_setdone(&iter.iter_);
188
+ return iter;
189
+ }
190
+
191
+ void operator++() { return upb_strtable_next(&iter_); }
192
+
193
+ std::pair<std::string, upb_value> operator*() const {
194
+ std::pair<std::string, upb_value> ret;
195
+ upb_StringView view = upb_strtable_iter_key(&iter_);
196
+ ret.first.assign(view.data, view.size);
197
+ ret.second = upb_strtable_iter_value(&iter_);
198
+ return ret;
199
+ }
200
+
201
+ bool operator==(const iterator& other) const {
202
+ return upb_strtable_iter_isequal(&iter_, &other.iter_);
203
+ }
204
+
205
+ bool operator!=(const iterator& other) const { return !(*this == other); }
206
+
207
+ private:
208
+ upb_strtable_iter iter_;
209
+ };
210
+
211
+ upb::Arena arena_;
212
+ upb_strtable table_;
213
+ };
214
+
215
+ template <class T>
216
+ class TypedStrTable {
217
+ public:
218
+ size_t count() { return table_.count(); }
219
+
220
+ bool Insert(const std::string& key, T val) {
221
+ return table_.Insert(key, MakeUpbValue<T>(val));
222
+ }
223
+
224
+ std::pair<bool, T> Remove(const std::string& key) {
225
+ std::pair<bool, upb_value> found = table_.Remove(key);
226
+ std::pair<bool, T> ret;
227
+ ret.first = found.first;
228
+ if (ret.first) {
229
+ ret.second = GetUpbValue<T>(found.second);
230
+ }
231
+ return ret;
232
+ }
233
+
234
+ std::pair<bool, T> Lookup(const std::string& key) const {
235
+ std::pair<bool, upb_value> found = table_.Lookup(key);
236
+ std::pair<bool, T> ret;
237
+ ret.first = found.first;
238
+ if (ret.first) {
239
+ ret.second = GetUpbValue<T>(found.second);
240
+ }
241
+ return ret;
242
+ }
243
+
244
+ void Resize(size_t size_lg2) { table_.Resize(size_lg2); }
245
+
246
+ class iterator : public std::iterator<std::forward_iterator_tag,
247
+ std::pair<std::string, T> > {
248
+ public:
249
+ explicit iterator(TypedStrTable* table) : iter_(&table->table_) {}
250
+ static iterator end(TypedStrTable* table) {
251
+ iterator iter(table);
252
+ iter.iter_ = StrTable::iterator::end(&table->table_);
253
+ return iter;
254
+ }
255
+
256
+ void operator++() { ++iter_; }
257
+
258
+ std::pair<std::string, T> operator*() const {
259
+ std::pair<std::string, upb_value> val = *iter_;
260
+ std::pair<std::string, T> ret;
261
+ ret.first = val.first;
262
+ ret.second = GetUpbValue<T>(val.second);
263
+ return ret;
264
+ }
265
+
266
+ bool operator==(const iterator& other) const {
267
+ return iter_ == other.iter_;
268
+ }
269
+
270
+ bool operator!=(const iterator& other) const {
271
+ return iter_ != other.iter_;
272
+ }
273
+
274
+ private:
275
+ StrTable::iterator iter_;
276
+ };
277
+
278
+ iterator begin() { return iterator(this); }
279
+ iterator end() { return iterator::end(this); }
280
+
281
+ StrTable table_;
282
+ };
283
+
284
+ template <class T>
285
+ class TypedIntTable {
286
+ public:
287
+ size_t count() { return table_.count(); }
288
+
289
+ bool Insert(uintptr_t key, T val) {
290
+ return table_.Insert(key, MakeUpbValue<T>(val));
291
+ }
292
+
293
+ bool Replace(uintptr_t key, T val) {
294
+ return table_.Replace(key, MakeUpbValue<T>(val));
295
+ }
296
+
297
+ std::pair<bool, T> Remove(uintptr_t key) {
298
+ std::pair<bool, upb_value> found = table_.Remove(key);
299
+ std::pair<bool, T> ret;
300
+ ret.first = found.first;
301
+ if (ret.first) {
302
+ ret.second = GetUpbValue<T>(found.second);
303
+ }
304
+ return ret;
305
+ }
306
+
307
+ std::pair<bool, T> Lookup(uintptr_t key) const {
308
+ std::pair<bool, upb_value> found = table_.Lookup(key);
309
+ std::pair<bool, T> ret;
310
+ ret.first = found.first;
311
+ if (ret.first) {
312
+ ret.second = GetUpbValue<T>(found.second);
313
+ }
314
+ return ret;
315
+ }
316
+
317
+ void Compact() { table_.Compact(); }
318
+
319
+ class iterator : public std::iterator<std::forward_iterator_tag,
320
+ std::pair<uintptr_t, T> > {
321
+ public:
322
+ explicit iterator(TypedIntTable* table) : iter_(&table->table_) {}
323
+ static iterator end(TypedIntTable* table) {
324
+ return IntTable::iterator::end(&table->table_);
325
+ }
326
+
327
+ void operator++() { ++iter_; }
328
+
329
+ std::pair<uintptr_t, T> operator*() const {
330
+ std::pair<uintptr_t, upb_value> val = *iter_;
331
+ std::pair<uintptr_t, T> ret;
332
+ ret.first = val.first;
333
+ ret.second = GetUpbValue<T>(val.second);
334
+ return ret;
335
+ }
336
+
337
+ bool operator==(const iterator& other) const {
338
+ return iter_ == other.iter_;
339
+ }
340
+
341
+ bool operator!=(const iterator& other) const {
342
+ return iter_ != other.iter_;
343
+ }
344
+
345
+ private:
346
+ IntTable::iterator iter_;
347
+ };
348
+
349
+ iterator begin() { return iterator(this); }
350
+ iterator end() { return iterator::end(this); }
351
+
352
+ IntTable table_;
353
+ };
354
+
355
+ } // namespace upb
356
+
357
+ #define CPU_TIME_PER_TEST 0.5
358
+
359
+ using std::vector;
360
+
361
+ double get_usertime() {
362
+ struct rusage usage;
363
+ getrusage(RUSAGE_SELF, &usage);
364
+ return usage.ru_utime.tv_sec + (usage.ru_utime.tv_usec / 1000000.0);
365
+ }
366
+
367
+ TEST(Table, StringTable) {
368
+ vector<std::string> keys;
369
+ keys.push_back("google.protobuf.FileDescriptorSet");
370
+ keys.push_back("google.protobuf.FileDescriptorProto");
371
+ keys.push_back("google.protobuf.DescriptorProto");
372
+ keys.push_back("google.protobuf.DescriptorProto.ExtensionRange");
373
+ keys.push_back("google.protobuf.FieldDescriptorProto");
374
+ keys.push_back("google.protobuf.EnumDescriptorProto");
375
+ keys.push_back("google.protobuf.EnumValueDescriptorProto");
376
+ keys.push_back("google.protobuf.ServiceDescriptorProto");
377
+ keys.push_back("google.protobuf.MethodDescriptorProto");
378
+ keys.push_back("google.protobuf.FileOptions");
379
+ keys.push_back("google.protobuf.MessageOptions");
380
+ keys.push_back("google.protobuf.FieldOptions");
381
+ keys.push_back("google.protobuf.EnumOptions");
382
+ keys.push_back("google.protobuf.EnumValueOptions");
383
+ keys.push_back("google.protobuf.ServiceOptions");
384
+ keys.push_back("google.protobuf.MethodOptions");
385
+ keys.push_back("google.protobuf.UninterpretedOption");
386
+ keys.push_back("google.protobuf.UninterpretedOption.NamePart");
387
+
388
+ /* Initialize structures. */
389
+ std::map<std::string, int32_t> m;
390
+ typedef upb::TypedStrTable<int32_t> Table;
391
+ Table table;
392
+ std::set<std::string> all;
393
+ for (const auto& key : keys) {
394
+ all.insert(key);
395
+ table.Insert(key, key[0]);
396
+ m[key] = key[0];
397
+ }
398
+
399
+ /* Test correctness. */
400
+ for (const auto& key : keys) {
401
+ std::pair<bool, int32_t> found = table.Lookup(key);
402
+ if (m.find(key) != m.end()) { /* Assume map implementation is correct. */
403
+ EXPECT_TRUE(found.first);
404
+ EXPECT_EQ(found.second, key[0]);
405
+ EXPECT_EQ(m[key], key[0]);
406
+ } else {
407
+ EXPECT_FALSE(found.first);
408
+ }
409
+ }
410
+
411
+ for (Table::iterator it = table.begin(); it != table.end(); ++it) {
412
+ std::set<std::string>::iterator i = all.find((*it).first);
413
+ EXPECT_NE(i, all.end());
414
+ all.erase(i);
415
+ }
416
+ EXPECT_TRUE(all.empty());
417
+
418
+ // Test iteration with resizes.
419
+
420
+ for (int i = 0; i < 10; i++) {
421
+ for (Table::iterator it = table.begin(); it != table.end(); ++it) {
422
+ // Even if we invalidate the iterator it should only return real elements.
423
+ EXPECT_EQ((*it).second, m[(*it).first]);
424
+
425
+ // Force a resize even though the size isn't changing.
426
+ // Also forces the table size to grow so some new buckets end up empty.
427
+ int new_lg2 = table.table_.table_.t.size_lg2 + 1;
428
+ // Don't use more than 64k tables, to avoid exhausting memory.
429
+ new_lg2 = UPB_MIN(new_lg2, 16);
430
+ table.Resize(new_lg2);
431
+ }
432
+ }
433
+ }
434
+
435
+ class IntTableTest : public testing::TestWithParam<int> {
436
+ void SetUp() override {
437
+ if (GetParam() > 0) {
438
+ for (int i = 0; i < GetParam(); i++) {
439
+ keys_.push_back(i + 1);
440
+ }
441
+ } else {
442
+ for (int32_t i = 0; i < 64; i++) {
443
+ if (i < 32)
444
+ keys_.push_back(i + 1);
445
+ else
446
+ keys_.push_back(10101 + i);
447
+ }
448
+ }
449
+ }
450
+
451
+ protected:
452
+ std::vector<int32_t> keys_;
453
+ };
454
+
455
+ TEST_P(IntTableTest, TestIntTable) {
456
+ /* Initialize structures. */
457
+ typedef upb::TypedIntTable<uint32_t> Table;
458
+ Table table;
459
+ uint32_t largest_key = 0;
460
+ std::map<uint32_t, uint32_t> m;
461
+ std::unordered_map<uint32_t, uint32_t> hm;
462
+ for (const auto& key : keys_) {
463
+ largest_key = UPB_MAX((int32_t)largest_key, key);
464
+ table.Insert(key, key * 2);
465
+ m[key] = key * 2;
466
+ hm[key] = key * 2;
467
+ }
468
+
469
+ /* Test correctness. */
470
+ for (uint32_t i = 0; i <= largest_key; i++) {
471
+ std::pair<bool, uint32_t> found = table.Lookup(i);
472
+ if (m.find(i) != m.end()) { /* Assume map implementation is correct. */
473
+ EXPECT_TRUE(found.first);
474
+ EXPECT_EQ(found.second, i * 2);
475
+ EXPECT_EQ(m[i], i * 2);
476
+ EXPECT_EQ(hm[i], i * 2);
477
+ } else {
478
+ EXPECT_FALSE(found.first);
479
+ }
480
+ }
481
+
482
+ for (size_t i = 0; i < keys_.size(); i += 2) {
483
+ std::pair<bool, uint32_t> found = table.Remove(keys_[i]);
484
+ EXPECT_EQ(found.first, m.erase(keys_[i]) == 1);
485
+ if (found.first) {
486
+ EXPECT_EQ(found.second, (uint32_t)keys_[i] * 2);
487
+ }
488
+ hm.erase(keys_[i]);
489
+ m.erase(keys_[i]);
490
+ }
491
+
492
+ EXPECT_EQ(table.count(), hm.size());
493
+
494
+ /* Test correctness. */
495
+ for (uint32_t i = 0; i <= largest_key; i++) {
496
+ std::pair<bool, uint32_t> found = table.Lookup(i);
497
+ if (m.find(i) != m.end()) { /* Assume map implementation is correct. */
498
+ EXPECT_TRUE(found.first);
499
+ EXPECT_EQ(found.second, i * 2);
500
+ EXPECT_EQ(m[i], i * 2);
501
+ EXPECT_EQ(hm[i], i * 2);
502
+ } else {
503
+ EXPECT_FALSE(found.first);
504
+ }
505
+ }
506
+
507
+ // Test replace.
508
+ for (uint32_t i = 0; i <= largest_key; i++) {
509
+ bool replaced = table.Replace(i, i * 3);
510
+ if (m.find(i) != m.end()) { /* Assume map implementation is correct. */
511
+ EXPECT_TRUE(replaced);
512
+ m[i] = i * 3;
513
+ hm[i] = i * 3;
514
+ } else {
515
+ EXPECT_FALSE(replaced);
516
+ }
517
+ }
518
+
519
+ // Compact and test correctness again.
520
+ table.Compact();
521
+ for (uint32_t i = 0; i <= largest_key; i++) {
522
+ std::pair<bool, uint32_t> found = table.Lookup(i);
523
+ if (m.find(i) != m.end()) { /* Assume map implementation is correct. */
524
+ EXPECT_TRUE(found.first);
525
+ EXPECT_EQ(found.second, i * 3);
526
+ EXPECT_EQ(m[i], i * 3);
527
+ EXPECT_EQ(hm[i], i * 3);
528
+ } else {
529
+ EXPECT_FALSE(found.first);
530
+ }
531
+ }
532
+ }
533
+
534
+ INSTANTIATE_TEST_SUITE_P(IntTableParams, IntTableTest,
535
+ testing::Values(8, 64, 512, -32));
536
+
537
+ /*
538
+ * This test can't pass right now because the table can't store a value of
539
+ * (uint64_t)-1.
540
+ */
541
+ TEST(Table, MaxValue) {
542
+ /*
543
+ typedef upb::TypedIntTable<uint64_t> Table;
544
+ Table table;
545
+ uintptr_t uint64_max = (uint64_t)-1;
546
+ table.Insert(1, uint64_max);
547
+ std::pair<bool, uint64_t> found = table.Lookup(1);
548
+ ASSERT(found.first);
549
+ ASSERT(found.second == uint64_max);
550
+ */
551
+ }
552
+
553
+ TEST(Table, Delete) {
554
+ upb::Arena arena;
555
+ upb_inttable t;
556
+ upb_inttable_init(&t, arena.ptr());
557
+ upb_inttable_insert(&t, 0, upb_value_bool(true), arena.ptr());
558
+ upb_inttable_insert(&t, 2, upb_value_bool(true), arena.ptr());
559
+ upb_inttable_insert(&t, 4, upb_value_bool(true), arena.ptr());
560
+ upb_inttable_compact(&t, arena.ptr());
561
+ upb_inttable_remove(&t, 0, NULL);
562
+ upb_inttable_remove(&t, 2, NULL);
563
+ upb_inttable_remove(&t, 4, NULL);
564
+
565
+ upb_inttable_iter iter;
566
+ for (upb_inttable_begin(&iter, &t); !upb_inttable_done(&iter);
567
+ upb_inttable_next(&iter)) {
568
+ ASSERT_TRUE(false);
569
+ }
570
+ }
571
+
572
+ TEST(Table, Init) {
573
+ for (int i = 0; i < 2048; i++) {
574
+ /* Tests that the size calculations in init() (lg2 size for target load)
575
+ * work for all expected sizes. */
576
+ upb::Arena arena;
577
+ upb_strtable t;
578
+ upb_strtable_init(&t, i, arena.ptr());
579
+ }
580
+ }