ruby_memprofiler_pprof 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/ruby_memprofiler_pprof/backtrace.c +429 -0
- data/ext/ruby_memprofiler_pprof/collector.c +1055 -0
- data/ext/ruby_memprofiler_pprof/compat.c +182 -0
- data/ext/ruby_memprofiler_pprof/extconf.rb +72 -0
- data/ext/ruby_memprofiler_pprof/pprof.upb.c +170 -0
- data/ext/ruby_memprofiler_pprof/pprof.upb.h +848 -0
- data/ext/ruby_memprofiler_pprof/pprof_out.c +285 -0
- data/ext/ruby_memprofiler_pprof/ruby_memprofiler_pprof.c +11 -0
- data/ext/ruby_memprofiler_pprof/ruby_memprofiler_pprof.h +301 -0
- data/ext/ruby_memprofiler_pprof/strtab.c +391 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/BUILD +719 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/CONTRIBUTING.md +37 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/DESIGN.md +201 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/LICENSE +26 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/README.md +78 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/WORKSPACE +58 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/BUILD +53 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/amalgamate.py +129 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/build_defs.bzl +160 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/lua.BUILD +127 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/protobuf.patch +54 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/py_proto_library.bzl +137 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/python_downloads.bzl +84 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/system_python.bzl +101 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/upb_proto_library.bzl +388 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/workspace_deps.bzl +89 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/BUILD +252 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/BUILD.googleapis +54 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/benchmark.cc +333 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/build_defs.bzl +88 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/compare.py +118 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/descriptor.proto +888 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/descriptor_sv.proto +890 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/empty.proto +6 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_protobuf_binary_cc.py +64 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_synthetic_protos.py +118 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_upb_binary_c.py +65 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/BUILD.bazel +102 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/README.md +23 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/build_defs.bzl +73 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/make_cmakelists.py +340 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/staleness_test.py +57 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/staleness_test_lib.py +186 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/docs/render.py +43 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/docs/style-guide.md +65 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/docs/vs-cpp-protos.md +255 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/docs/wrapping-upb.md +444 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/BUILD +216 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/convert.c +394 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/convert.h +63 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor.c +1694 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor.h +80 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_containers.c +704 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_containers.h +114 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_pool.c +650 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_pool.h +48 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/dist/BUILD.bazel +193 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/dist/dist.bzl +190 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/extension_dict.c +247 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/extension_dict.h +39 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/map.c +522 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/map.h +66 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/message.c +1909 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/message.h +101 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/minimal_test.py +183 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/BUILD +70 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/README.md +11 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_database_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_pool_test_wrapper.py +45 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_test_wrapper.py +46 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/generator_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/json_format_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/keywords_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/message_factory_test_wrapper.py +37 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/message_test_wrapper.py +52 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/proto_builder_test_wrapper.py +32 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/pyproto_test_wrapper.bzl +36 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/reflection_test_wrapper.py +45 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/service_reflection_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/symbol_database_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/text_encoding_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/text_format_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/unknown_fields_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/well_known_types_test_wrapper.py +36 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/wire_format_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/protobuf.c +350 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/protobuf.h +230 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/py_extension.bzl +55 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/python_api.h +61 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/repeated.c +828 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/repeated.h +69 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/unknown_fields.c +404 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/unknown_fields.h +39 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/version_script.lds +6 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/LICENSE +32 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/README.google +9 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/console.lua +156 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/lunit.lua +725 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/BUILD +19 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/LICENSE +21 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/naive.c +92 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/range2-neon.c +157 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/range2-sse.c +170 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/utf8_range.h +9 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/BUILD.bazel +129 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/README.md +8 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/def.c +939 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/lua_proto_library.bzl +138 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/main.c +83 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/msg.c +1118 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/test.proto +69 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/test_upb.lua +846 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.c +258 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.h +132 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.lua +58 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upbc.cc +134 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/collections.c +192 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/collections.h +174 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/conformance_upb.c +346 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/conformance_upb_failures.txt +1 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode.c +1221 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode.h +94 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_fast.c +1055 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_fast.h +153 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_internal.h +211 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.c +3262 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.h +414 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.hpp +438 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/empty.proto +1 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/encode.c +604 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/encode.h +71 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/fuzz/BUILD +13 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/fuzz/file_descriptor_parsenew_fuzzer.cc +43 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_decode.c +1509 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_decode.h +47 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_encode.c +776 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_encode.h +62 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.c +1147 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.h +189 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.hpp +112 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors.c +363 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors.h +263 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors_internal.h +59 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors_test.cc +425 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_test.cc +230 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg.c +428 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg.h +114 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_internal.h +836 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_test.cc +491 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_test.proto +195 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/port_def.inc +261 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/port_undef.inc +62 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.c +323 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.h +109 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.hpp +37 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/table.c +926 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/table_internal.h +385 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test.proto +74 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_cpp.cc +186 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_cpp.proto +12 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_generated_code.cc +977 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_table.cc +580 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/text_encode.c +472 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/text_encode.h +64 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.c +362 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.h +378 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.hpp +115 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb_internal.h +68 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/BUILD +121 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/README.md +7 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare.c +300 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare.h +66 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare_test.cc +236 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto.c +572 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto.h +62 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_public_import_test.proto +32 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_regular_import_test.proto +36 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_test.cc +143 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_test.proto +119 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_weak_import_test.proto +28 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_wweak_import_test.proto +28 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields.c +311 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields.h +94 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields_test.cc +202 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields_test.proto +48 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/BUILD +78 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/common.cc +77 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/common.h +112 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/protoc-gen-upb.cc +1997 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/protoc-gen-upbdefs.cc +193 -0
- data/lib/ruby_memprofiler_pprof/atfork.rb +77 -0
- data/lib/ruby_memprofiler_pprof/block_flusher.rb +61 -0
- data/lib/ruby_memprofiler_pprof/file_flusher.rb +45 -0
- data/lib/ruby_memprofiler_pprof/profile_app.rb +30 -0
- data/lib/ruby_memprofiler_pprof/profile_data.rb +18 -0
- data/lib/ruby_memprofiler_pprof/version.rb +5 -0
- data/lib/ruby_memprofiler_pprof.rb +8 -0
- data/libexec/ruby_memprofiler_pprof_profile +16 -0
- metadata +257 -0
@@ -0,0 +1,385 @@
|
|
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
|
+
/*
|
29
|
+
* upb_table
|
30
|
+
*
|
31
|
+
* This header is INTERNAL-ONLY! Its interfaces are not public or stable!
|
32
|
+
* This file defines very fast int->upb_value (inttable) and string->upb_value
|
33
|
+
* (strtable) hash tables.
|
34
|
+
*
|
35
|
+
* The table uses chained scatter with Brent's variation (inspired by the Lua
|
36
|
+
* implementation of hash tables). The hash function for strings is Austin
|
37
|
+
* Appleby's "MurmurHash."
|
38
|
+
*
|
39
|
+
* The inttable uses uintptr_t as its key, which guarantees it can be used to
|
40
|
+
* store pointers or integers of at least 32 bits (upb isn't really useful on
|
41
|
+
* systems where sizeof(void*) < 4).
|
42
|
+
*
|
43
|
+
* The table must be homogeneous (all values of the same type). In debug
|
44
|
+
* mode, we check this on insert and lookup.
|
45
|
+
*/
|
46
|
+
|
47
|
+
#ifndef UPB_TABLE_H_
|
48
|
+
#define UPB_TABLE_H_
|
49
|
+
|
50
|
+
#include <stdint.h>
|
51
|
+
#include <string.h>
|
52
|
+
|
53
|
+
#include "upb/upb.h"
|
54
|
+
|
55
|
+
// Must be last.
|
56
|
+
#include "upb/port_def.inc"
|
57
|
+
|
58
|
+
#ifdef __cplusplus
|
59
|
+
extern "C" {
|
60
|
+
#endif
|
61
|
+
|
62
|
+
/* upb_value ******************************************************************/
|
63
|
+
|
64
|
+
typedef struct {
|
65
|
+
uint64_t val;
|
66
|
+
} upb_value;
|
67
|
+
|
68
|
+
/* Variant that works with a length-delimited rather than NULL-delimited string,
|
69
|
+
* as supported by strtable. */
|
70
|
+
char* upb_strdup2(const char* s, size_t len, upb_Arena* a);
|
71
|
+
|
72
|
+
UPB_INLINE void _upb_value_setval(upb_value* v, uint64_t val) { v->val = val; }
|
73
|
+
|
74
|
+
/* For each value ctype, define the following set of functions:
|
75
|
+
*
|
76
|
+
* // Get/set an int32 from a upb_value.
|
77
|
+
* int32_t upb_value_getint32(upb_value val);
|
78
|
+
* void upb_value_setint32(upb_value *val, int32_t cval);
|
79
|
+
*
|
80
|
+
* // Construct a new upb_value from an int32.
|
81
|
+
* upb_value upb_value_int32(int32_t val); */
|
82
|
+
#define FUNCS(name, membername, type_t, converter, proto_type) \
|
83
|
+
UPB_INLINE void upb_value_set##name(upb_value* val, type_t cval) { \
|
84
|
+
val->val = (converter)cval; \
|
85
|
+
} \
|
86
|
+
UPB_INLINE upb_value upb_value_##name(type_t val) { \
|
87
|
+
upb_value ret; \
|
88
|
+
upb_value_set##name(&ret, val); \
|
89
|
+
return ret; \
|
90
|
+
} \
|
91
|
+
UPB_INLINE type_t upb_value_get##name(upb_value val) { \
|
92
|
+
return (type_t)(converter)val.val; \
|
93
|
+
}
|
94
|
+
|
95
|
+
FUNCS(int32, int32, int32_t, int32_t, UPB_CTYPE_INT32)
|
96
|
+
FUNCS(int64, int64, int64_t, int64_t, UPB_CTYPE_INT64)
|
97
|
+
FUNCS(uint32, uint32, uint32_t, uint32_t, UPB_CTYPE_UINT32)
|
98
|
+
FUNCS(uint64, uint64, uint64_t, uint64_t, UPB_CTYPE_UINT64)
|
99
|
+
FUNCS(bool, _bool, bool, bool, UPB_CTYPE_BOOL)
|
100
|
+
FUNCS(cstr, cstr, char*, uintptr_t, UPB_CTYPE_CSTR)
|
101
|
+
FUNCS(ptr, ptr, void*, uintptr_t, UPB_CTYPE_PTR)
|
102
|
+
FUNCS(constptr, constptr, const void*, uintptr_t, UPB_CTYPE_CONSTPTR)
|
103
|
+
|
104
|
+
#undef FUNCS
|
105
|
+
|
106
|
+
UPB_INLINE void upb_value_setfloat(upb_value* val, float cval) {
|
107
|
+
memcpy(&val->val, &cval, sizeof(cval));
|
108
|
+
}
|
109
|
+
|
110
|
+
UPB_INLINE void upb_value_setdouble(upb_value* val, double cval) {
|
111
|
+
memcpy(&val->val, &cval, sizeof(cval));
|
112
|
+
}
|
113
|
+
|
114
|
+
UPB_INLINE upb_value upb_value_float(float cval) {
|
115
|
+
upb_value ret;
|
116
|
+
upb_value_setfloat(&ret, cval);
|
117
|
+
return ret;
|
118
|
+
}
|
119
|
+
|
120
|
+
UPB_INLINE upb_value upb_value_double(double cval) {
|
121
|
+
upb_value ret;
|
122
|
+
upb_value_setdouble(&ret, cval);
|
123
|
+
return ret;
|
124
|
+
}
|
125
|
+
|
126
|
+
#undef SET_TYPE
|
127
|
+
|
128
|
+
/* upb_tabkey *****************************************************************/
|
129
|
+
|
130
|
+
/* Either:
|
131
|
+
* 1. an actual integer key, or
|
132
|
+
* 2. a pointer to a string prefixed by its uint32_t length, owned by us.
|
133
|
+
*
|
134
|
+
* ...depending on whether this is a string table or an int table. We would
|
135
|
+
* make this a union of those two types, but C89 doesn't support statically
|
136
|
+
* initializing a non-first union member. */
|
137
|
+
typedef uintptr_t upb_tabkey;
|
138
|
+
|
139
|
+
UPB_INLINE char* upb_tabstr(upb_tabkey key, uint32_t* len) {
|
140
|
+
char* mem = (char*)key;
|
141
|
+
if (len) memcpy(len, mem, sizeof(*len));
|
142
|
+
return mem + sizeof(*len);
|
143
|
+
}
|
144
|
+
|
145
|
+
UPB_INLINE upb_StringView upb_tabstrview(upb_tabkey key) {
|
146
|
+
upb_StringView ret;
|
147
|
+
uint32_t len;
|
148
|
+
ret.data = upb_tabstr(key, &len);
|
149
|
+
ret.size = len;
|
150
|
+
return ret;
|
151
|
+
}
|
152
|
+
|
153
|
+
/* upb_tabval *****************************************************************/
|
154
|
+
|
155
|
+
typedef struct upb_tabval {
|
156
|
+
uint64_t val;
|
157
|
+
} upb_tabval;
|
158
|
+
|
159
|
+
#define UPB_TABVALUE_EMPTY_INIT \
|
160
|
+
{ -1 }
|
161
|
+
|
162
|
+
/* upb_table ******************************************************************/
|
163
|
+
|
164
|
+
typedef struct _upb_tabent {
|
165
|
+
upb_tabkey key;
|
166
|
+
upb_tabval val;
|
167
|
+
|
168
|
+
/* Internal chaining. This is const so we can create static initializers for
|
169
|
+
* tables. We cast away const sometimes, but *only* when the containing
|
170
|
+
* upb_table is known to be non-const. This requires a bit of care, but
|
171
|
+
* the subtlety is confined to table.c. */
|
172
|
+
const struct _upb_tabent* next;
|
173
|
+
} upb_tabent;
|
174
|
+
|
175
|
+
typedef struct {
|
176
|
+
size_t count; /* Number of entries in the hash part. */
|
177
|
+
uint32_t mask; /* Mask to turn hash value -> bucket. */
|
178
|
+
uint32_t max_count; /* Max count before we hit our load limit. */
|
179
|
+
uint8_t size_lg2; /* Size of the hashtable part is 2^size_lg2 entries. */
|
180
|
+
upb_tabent* entries;
|
181
|
+
} upb_table;
|
182
|
+
|
183
|
+
typedef struct {
|
184
|
+
upb_table t;
|
185
|
+
} upb_strtable;
|
186
|
+
|
187
|
+
typedef struct {
|
188
|
+
upb_table t; /* For entries that don't fit in the array part. */
|
189
|
+
const upb_tabval* array; /* Array part of the table. See const note above. */
|
190
|
+
size_t array_size; /* Array part size. */
|
191
|
+
size_t array_count; /* Array part number of elements. */
|
192
|
+
} upb_inttable;
|
193
|
+
|
194
|
+
UPB_INLINE size_t upb_table_size(const upb_table* t) {
|
195
|
+
if (t->size_lg2 == 0)
|
196
|
+
return 0;
|
197
|
+
else
|
198
|
+
return 1 << t->size_lg2;
|
199
|
+
}
|
200
|
+
|
201
|
+
/* Internal-only functions, in .h file only out of necessity. */
|
202
|
+
UPB_INLINE bool upb_tabent_isempty(const upb_tabent* e) { return e->key == 0; }
|
203
|
+
|
204
|
+
/* Initialize and uninitialize a table, respectively. If memory allocation
|
205
|
+
* failed, false is returned that the table is uninitialized. */
|
206
|
+
bool upb_inttable_init(upb_inttable* table, upb_Arena* a);
|
207
|
+
bool upb_strtable_init(upb_strtable* table, size_t expected_size, upb_Arena* a);
|
208
|
+
|
209
|
+
/* Returns the number of values in the table. */
|
210
|
+
size_t upb_inttable_count(const upb_inttable* t);
|
211
|
+
UPB_INLINE size_t upb_strtable_count(const upb_strtable* t) {
|
212
|
+
return t->t.count;
|
213
|
+
}
|
214
|
+
|
215
|
+
void upb_strtable_clear(upb_strtable* t);
|
216
|
+
|
217
|
+
/* Inserts the given key into the hashtable with the given value. The key must
|
218
|
+
* not already exist in the hash table. For string tables, the key must be
|
219
|
+
* NULL-terminated, and the table will make an internal copy of the key.
|
220
|
+
* Inttables must not insert a value of UINTPTR_MAX.
|
221
|
+
*
|
222
|
+
* If a table resize was required but memory allocation failed, false is
|
223
|
+
* returned and the table is unchanged. */
|
224
|
+
bool upb_inttable_insert(upb_inttable* t, uintptr_t key, upb_value val,
|
225
|
+
upb_Arena* a);
|
226
|
+
bool upb_strtable_insert(upb_strtable* t, const char* key, size_t len,
|
227
|
+
upb_value val, upb_Arena* a);
|
228
|
+
|
229
|
+
/* Looks up key in this table, returning "true" if the key was found.
|
230
|
+
* If v is non-NULL, copies the value for this key into *v. */
|
231
|
+
bool upb_inttable_lookup(const upb_inttable* t, uintptr_t key, upb_value* v);
|
232
|
+
bool upb_strtable_lookup2(const upb_strtable* t, const char* key, size_t len,
|
233
|
+
upb_value* v);
|
234
|
+
|
235
|
+
/* For NULL-terminated strings. */
|
236
|
+
UPB_INLINE bool upb_strtable_lookup(const upb_strtable* t, const char* key,
|
237
|
+
upb_value* v) {
|
238
|
+
return upb_strtable_lookup2(t, key, strlen(key), v);
|
239
|
+
}
|
240
|
+
|
241
|
+
/* Removes an item from the table. Returns true if the remove was successful,
|
242
|
+
* and stores the removed item in *val if non-NULL. */
|
243
|
+
bool upb_inttable_remove(upb_inttable* t, uintptr_t key, upb_value* val);
|
244
|
+
bool upb_strtable_remove2(upb_strtable* t, const char* key, size_t len,
|
245
|
+
upb_value* val);
|
246
|
+
|
247
|
+
UPB_INLINE bool upb_strtable_remove(upb_strtable* t, const char* key,
|
248
|
+
upb_value* v) {
|
249
|
+
return upb_strtable_remove2(t, key, strlen(key), v);
|
250
|
+
}
|
251
|
+
|
252
|
+
/* Updates an existing entry in an inttable. If the entry does not exist,
|
253
|
+
* returns false and does nothing. Unlike insert/remove, this does not
|
254
|
+
* invalidate iterators. */
|
255
|
+
bool upb_inttable_replace(upb_inttable* t, uintptr_t key, upb_value val);
|
256
|
+
|
257
|
+
/* Optimizes the table for the current set of entries, for both memory use and
|
258
|
+
* lookup time. Client should call this after all entries have been inserted;
|
259
|
+
* inserting more entries is legal, but will likely require a table resize. */
|
260
|
+
void upb_inttable_compact(upb_inttable* t, upb_Arena* a);
|
261
|
+
|
262
|
+
/* Exposed for testing only. */
|
263
|
+
bool upb_strtable_resize(upb_strtable* t, size_t size_lg2, upb_Arena* a);
|
264
|
+
|
265
|
+
/* Iterators ******************************************************************/
|
266
|
+
|
267
|
+
/* Iteration over inttable.
|
268
|
+
*
|
269
|
+
* intptr_t iter = UPB_INTTABLE_BEGIN;
|
270
|
+
* uintptr_t key;
|
271
|
+
* upb_value val;
|
272
|
+
* while (upb_inttable_next2(t, &key, &val, &iter)) {
|
273
|
+
* // ...
|
274
|
+
* }
|
275
|
+
*/
|
276
|
+
|
277
|
+
#define UPB_INTTABLE_BEGIN -1
|
278
|
+
|
279
|
+
bool upb_inttable_next2(const upb_inttable* t, uintptr_t* key, upb_value* val,
|
280
|
+
intptr_t* iter);
|
281
|
+
void upb_inttable_removeiter(upb_inttable* t, intptr_t* iter);
|
282
|
+
|
283
|
+
/* Iteration over strtable.
|
284
|
+
*
|
285
|
+
* intptr_t iter = UPB_INTTABLE_BEGIN;
|
286
|
+
* upb_StringView key;
|
287
|
+
* upb_value val;
|
288
|
+
* while (upb_strtable_next2(t, &key, &val, &iter)) {
|
289
|
+
* // ...
|
290
|
+
* }
|
291
|
+
*/
|
292
|
+
|
293
|
+
#define UPB_STRTABLE_BEGIN -1
|
294
|
+
|
295
|
+
bool upb_strtable_next2(const upb_strtable* t, upb_StringView* key,
|
296
|
+
upb_value* val, intptr_t* iter);
|
297
|
+
void upb_strtable_removeiter(upb_strtable* t, intptr_t* iter);
|
298
|
+
|
299
|
+
/* DEPRECATED iterators, slated for removal.
|
300
|
+
*
|
301
|
+
* Iterators for int and string tables. We are subject to some kind of unusual
|
302
|
+
* design constraints:
|
303
|
+
*
|
304
|
+
* For high-level languages:
|
305
|
+
* - we must be able to guarantee that we don't crash or corrupt memory even if
|
306
|
+
* the program accesses an invalidated iterator.
|
307
|
+
*
|
308
|
+
* For C++11 range-based for:
|
309
|
+
* - iterators must be copyable
|
310
|
+
* - iterators must be comparable
|
311
|
+
* - it must be possible to construct an "end" value.
|
312
|
+
*
|
313
|
+
* Iteration order is undefined.
|
314
|
+
*
|
315
|
+
* Modifying the table invalidates iterators. upb_{str,int}table_done() is
|
316
|
+
* guaranteed to work even on an invalidated iterator, as long as the table it
|
317
|
+
* is iterating over has not been freed. Calling next() or accessing data from
|
318
|
+
* an invalidated iterator yields unspecified elements from the table, but it is
|
319
|
+
* guaranteed not to crash and to return real table elements (except when done()
|
320
|
+
* is true). */
|
321
|
+
|
322
|
+
/* upb_strtable_iter **********************************************************/
|
323
|
+
|
324
|
+
/* upb_strtable_iter i;
|
325
|
+
* upb_strtable_begin(&i, t);
|
326
|
+
* for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
|
327
|
+
* const char *key = upb_strtable_iter_key(&i);
|
328
|
+
* const upb_value val = upb_strtable_iter_value(&i);
|
329
|
+
* // ...
|
330
|
+
* }
|
331
|
+
*/
|
332
|
+
|
333
|
+
typedef struct {
|
334
|
+
const upb_strtable* t;
|
335
|
+
size_t index;
|
336
|
+
} upb_strtable_iter;
|
337
|
+
|
338
|
+
void upb_strtable_begin(upb_strtable_iter* i, const upb_strtable* t);
|
339
|
+
void upb_strtable_next(upb_strtable_iter* i);
|
340
|
+
bool upb_strtable_done(const upb_strtable_iter* i);
|
341
|
+
upb_StringView upb_strtable_iter_key(const upb_strtable_iter* i);
|
342
|
+
upb_value upb_strtable_iter_value(const upb_strtable_iter* i);
|
343
|
+
void upb_strtable_iter_setdone(upb_strtable_iter* i);
|
344
|
+
bool upb_strtable_iter_isequal(const upb_strtable_iter* i1,
|
345
|
+
const upb_strtable_iter* i2);
|
346
|
+
|
347
|
+
/* upb_inttable_iter **********************************************************/
|
348
|
+
|
349
|
+
/* upb_inttable_iter i;
|
350
|
+
* upb_inttable_begin(&i, t);
|
351
|
+
* for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
|
352
|
+
* uintptr_t key = upb_inttable_iter_key(&i);
|
353
|
+
* upb_value val = upb_inttable_iter_value(&i);
|
354
|
+
* // ...
|
355
|
+
* }
|
356
|
+
*/
|
357
|
+
|
358
|
+
typedef struct {
|
359
|
+
const upb_inttable* t;
|
360
|
+
size_t index;
|
361
|
+
bool array_part;
|
362
|
+
} upb_inttable_iter;
|
363
|
+
|
364
|
+
UPB_INLINE const upb_tabent* str_tabent(const upb_strtable_iter* i) {
|
365
|
+
return &i->t->t.entries[i->index];
|
366
|
+
}
|
367
|
+
|
368
|
+
void upb_inttable_begin(upb_inttable_iter* i, const upb_inttable* t);
|
369
|
+
void upb_inttable_next(upb_inttable_iter* i);
|
370
|
+
bool upb_inttable_done(const upb_inttable_iter* i);
|
371
|
+
uintptr_t upb_inttable_iter_key(const upb_inttable_iter* i);
|
372
|
+
upb_value upb_inttable_iter_value(const upb_inttable_iter* i);
|
373
|
+
void upb_inttable_iter_setdone(upb_inttable_iter* i);
|
374
|
+
bool upb_inttable_iter_isequal(const upb_inttable_iter* i1,
|
375
|
+
const upb_inttable_iter* i2);
|
376
|
+
|
377
|
+
uint32_t _upb_Hash(const void* p, size_t n, uint64_t seed);
|
378
|
+
|
379
|
+
#ifdef __cplusplus
|
380
|
+
} /* extern "C" */
|
381
|
+
#endif
|
382
|
+
|
383
|
+
#include "upb/port_undef.inc"
|
384
|
+
|
385
|
+
#endif /* UPB_TABLE_H_ */
|
@@ -0,0 +1,74 @@
|
|
1
|
+
|
2
|
+
syntax = "proto2";
|
3
|
+
|
4
|
+
package upb_test;
|
5
|
+
|
6
|
+
message MapTest {
|
7
|
+
map<string, double> map_string_double = 1;
|
8
|
+
}
|
9
|
+
|
10
|
+
message MessageName {
|
11
|
+
optional int32 field1 = 1;
|
12
|
+
optional int32 field2 = 2;
|
13
|
+
}
|
14
|
+
|
15
|
+
message HelloRequest {
|
16
|
+
optional uint32 id = 1;
|
17
|
+
optional uint32 random_name_a0 = 2;
|
18
|
+
optional uint32 random_name_a1 = 3;
|
19
|
+
optional uint32 random_name_a2 = 4;
|
20
|
+
optional uint32 random_name_a3 = 5;
|
21
|
+
optional uint32 random_name_a4 = 6;
|
22
|
+
optional uint32 random_name_a5 = 7;
|
23
|
+
optional uint32 random_name_a6 = 8;
|
24
|
+
optional uint32 random_name_a7 = 9;
|
25
|
+
optional uint32 random_name_a8 = 10;
|
26
|
+
optional uint32 random_name_a9 = 11;
|
27
|
+
optional uint32 random_name_b0 = 12;
|
28
|
+
optional uint32 random_name_b1 = 13;
|
29
|
+
optional uint32 random_name_b2 = 14;
|
30
|
+
optional uint32 random_name_b3 = 15;
|
31
|
+
optional uint32 random_name_b4 = 16;
|
32
|
+
optional uint32 random_name_b5 = 17;
|
33
|
+
optional uint32 random_name_b6 = 18;
|
34
|
+
optional uint32 random_name_b7 = 19;
|
35
|
+
optional uint32 random_name_b8 = 20;
|
36
|
+
optional uint32 random_name_b9 = 21;
|
37
|
+
optional uint32 random_name_c0 = 22;
|
38
|
+
optional uint32 random_name_c1 = 23;
|
39
|
+
optional uint32 random_name_c2 = 24;
|
40
|
+
optional uint32 random_name_c3 = 25;
|
41
|
+
optional uint32 random_name_c4 = 26;
|
42
|
+
optional uint32 random_name_c5 = 27;
|
43
|
+
optional uint32 random_name_c6 = 28;
|
44
|
+
optional uint32 random_name_c7 = 29;
|
45
|
+
optional uint32 random_name_c8 = 30;
|
46
|
+
optional uint32 random_name_c9 = 31;
|
47
|
+
optional string version = 32;
|
48
|
+
}
|
49
|
+
|
50
|
+
message EmptyMessageWithExtensions {
|
51
|
+
// Reserved for unknown fields/extensions test.
|
52
|
+
reserved 1000 to max;
|
53
|
+
}
|
54
|
+
|
55
|
+
message ModelWithExtensions {
|
56
|
+
optional int32 random_int32 = 3;
|
57
|
+
optional string random_name = 4;
|
58
|
+
// Reserved for unknown fields/extensions test.
|
59
|
+
extensions 1000 to max;
|
60
|
+
}
|
61
|
+
|
62
|
+
message ModelExtension1 {
|
63
|
+
extend ModelWithExtensions {
|
64
|
+
optional ModelExtension1 model_ext = 1547;
|
65
|
+
}
|
66
|
+
optional string str = 25;
|
67
|
+
}
|
68
|
+
|
69
|
+
message ModelExtension2 {
|
70
|
+
extend ModelWithExtensions {
|
71
|
+
optional ModelExtension2 model_ext = 4135;
|
72
|
+
}
|
73
|
+
optional int32 i = 9;
|
74
|
+
}
|
@@ -0,0 +1,186 @@
|
|
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 C++ wrappers.
|
28
|
+
*/
|
29
|
+
|
30
|
+
#include <stdio.h>
|
31
|
+
#include <string.h>
|
32
|
+
|
33
|
+
#include <fstream>
|
34
|
+
#include <iostream>
|
35
|
+
#include <set>
|
36
|
+
#include <sstream>
|
37
|
+
|
38
|
+
#include "google/protobuf/timestamp.upb.h"
|
39
|
+
#include "google/protobuf/timestamp.upbdefs.h"
|
40
|
+
#include "gtest/gtest.h"
|
41
|
+
#include "upb/def.h"
|
42
|
+
#include "upb/def.hpp"
|
43
|
+
#include "upb/json_decode.h"
|
44
|
+
#include "upb/json_encode.h"
|
45
|
+
#include "upb/test_cpp.upb.h"
|
46
|
+
#include "upb/test_cpp.upbdefs.h"
|
47
|
+
#include "upb/upb.h"
|
48
|
+
|
49
|
+
// Must be last.
|
50
|
+
#include "upb/port_def.inc"
|
51
|
+
|
52
|
+
TEST(Cpp, Iteration) {
|
53
|
+
upb::SymbolTable symtab;
|
54
|
+
upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr()));
|
55
|
+
|
56
|
+
// Test range-based for on both fields and oneofs (with the iterator adaptor).
|
57
|
+
int field_count = 0;
|
58
|
+
for (auto field : md.fields()) {
|
59
|
+
UPB_UNUSED(field);
|
60
|
+
field_count++;
|
61
|
+
}
|
62
|
+
EXPECT_EQ(field_count, md.field_count());
|
63
|
+
|
64
|
+
int oneof_count = 0;
|
65
|
+
for (auto oneof : md.oneofs()) {
|
66
|
+
UPB_UNUSED(oneof);
|
67
|
+
oneof_count++;
|
68
|
+
}
|
69
|
+
EXPECT_EQ(oneof_count, md.oneof_count());
|
70
|
+
}
|
71
|
+
|
72
|
+
TEST(Cpp, Arena) {
|
73
|
+
int n = 100000;
|
74
|
+
|
75
|
+
struct Decrementer {
|
76
|
+
Decrementer(int* _p) : p(_p) {}
|
77
|
+
~Decrementer() { (*p)--; }
|
78
|
+
int* p;
|
79
|
+
};
|
80
|
+
|
81
|
+
{
|
82
|
+
upb::Arena arena;
|
83
|
+
for (int i = 0; i < n; i++) {
|
84
|
+
arena.Own(new Decrementer(&n));
|
85
|
+
|
86
|
+
// Intersperse allocation and ensure we can write to it.
|
87
|
+
int* val = static_cast<int*>(upb_Arena_Malloc(arena.ptr(), sizeof(int)));
|
88
|
+
*val = i;
|
89
|
+
}
|
90
|
+
|
91
|
+
// Test a large allocation.
|
92
|
+
upb_Arena_Malloc(arena.ptr(), 1000000);
|
93
|
+
}
|
94
|
+
EXPECT_EQ(0, n);
|
95
|
+
|
96
|
+
{
|
97
|
+
// Test fuse.
|
98
|
+
upb::Arena arena1;
|
99
|
+
upb::Arena arena2;
|
100
|
+
|
101
|
+
arena1.Fuse(arena2);
|
102
|
+
|
103
|
+
upb_Arena_Malloc(arena1.ptr(), 10000);
|
104
|
+
upb_Arena_Malloc(arena2.ptr(), 10000);
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
TEST(Cpp, InlinedArena) {
|
109
|
+
int n = 100000;
|
110
|
+
|
111
|
+
struct Decrementer {
|
112
|
+
Decrementer(int* _p) : p(_p) {}
|
113
|
+
~Decrementer() { (*p)--; }
|
114
|
+
int* p;
|
115
|
+
};
|
116
|
+
|
117
|
+
{
|
118
|
+
upb::InlinedArena<1024> arena;
|
119
|
+
for (int i = 0; i < n; i++) {
|
120
|
+
arena.Own(new Decrementer(&n));
|
121
|
+
|
122
|
+
// Intersperse allocation and ensure we can write to it.
|
123
|
+
int* val = static_cast<int*>(upb_Arena_Malloc(arena.ptr(), sizeof(int)));
|
124
|
+
*val = i;
|
125
|
+
}
|
126
|
+
|
127
|
+
// Test a large allocation.
|
128
|
+
upb_Arena_Malloc(arena.ptr(), 1000000);
|
129
|
+
}
|
130
|
+
EXPECT_EQ(0, n);
|
131
|
+
}
|
132
|
+
|
133
|
+
TEST(Cpp, Default) {
|
134
|
+
upb::SymbolTable symtab;
|
135
|
+
upb::Arena arena;
|
136
|
+
upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr()));
|
137
|
+
upb_test_TestMessage* msg = upb_test_TestMessage_new(arena.ptr());
|
138
|
+
size_t size = upb_JsonEncode(msg, md.ptr(), NULL, 0, NULL, 0, NULL);
|
139
|
+
EXPECT_EQ(2, size); // "{}"
|
140
|
+
}
|
141
|
+
|
142
|
+
TEST(Cpp, JsonNull) {
|
143
|
+
upb::SymbolTable symtab;
|
144
|
+
upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr()));
|
145
|
+
upb::FieldDefPtr i32_f = md.FindFieldByName("i32");
|
146
|
+
upb::FieldDefPtr str_f = md.FindFieldByName("str");
|
147
|
+
ASSERT_TRUE(i32_f);
|
148
|
+
ASSERT_TRUE(str_f);
|
149
|
+
EXPECT_EQ(5, i32_f.default_value().int32_val);
|
150
|
+
EXPECT_EQ(0, strcmp(str_f.default_value().str_val.data, "abc"));
|
151
|
+
EXPECT_EQ(3, str_f.default_value().str_val.size);
|
152
|
+
}
|
153
|
+
|
154
|
+
TEST(Cpp, TimestampEncoder) {
|
155
|
+
upb::SymbolTable symtab;
|
156
|
+
upb::Arena arena;
|
157
|
+
upb::MessageDefPtr md(google_protobuf_Timestamp_getmsgdef(symtab.ptr()));
|
158
|
+
google_protobuf_Timestamp* timestamp_upb =
|
159
|
+
google_protobuf_Timestamp_new(arena.ptr());
|
160
|
+
google_protobuf_Timestamp* timestamp_upb_decoded =
|
161
|
+
google_protobuf_Timestamp_new(arena.ptr());
|
162
|
+
|
163
|
+
long timestamps[] = {
|
164
|
+
253402300799, // 9999-12-31T23:59:59Z
|
165
|
+
1641006000, // 2022-01-01T03:00:00Z
|
166
|
+
0, // 1970-01-01T00:00:00Z
|
167
|
+
-31525200, // 1969-01-01T03:00:00Z
|
168
|
+
-2208988800, // 1900-01-01T00:00:00Z
|
169
|
+
-62135596800, // 0000-01-01T00:00:00Z
|
170
|
+
};
|
171
|
+
|
172
|
+
for (long timestamp : timestamps) {
|
173
|
+
google_protobuf_Timestamp_set_seconds(timestamp_upb, timestamp);
|
174
|
+
|
175
|
+
char json[128];
|
176
|
+
size_t size = upb_JsonEncode(timestamp_upb, md.ptr(), NULL, 0, json,
|
177
|
+
sizeof(json), NULL);
|
178
|
+
bool result = upb_JsonDecode(json, size, timestamp_upb_decoded, md.ptr(),
|
179
|
+
NULL, 0, arena.ptr(), NULL);
|
180
|
+
const long timestamp_decoded =
|
181
|
+
google_protobuf_Timestamp_seconds(timestamp_upb_decoded);
|
182
|
+
|
183
|
+
ASSERT_TRUE(result);
|
184
|
+
EXPECT_EQ(timestamp, timestamp_decoded);
|
185
|
+
}
|
186
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
syntax = "proto2";
|
2
|
+
|
3
|
+
package upb.test;
|
4
|
+
|
5
|
+
message TestMessage {
|
6
|
+
optional int32 i32 = 1 [default = 5];
|
7
|
+
repeated int32 r_i32 = 2;
|
8
|
+
optional string str = 3 [default = "abc"];
|
9
|
+
repeated string r_str = 4;
|
10
|
+
optional TestMessage msg = 5;
|
11
|
+
repeated TestMessage r_msg = 6;
|
12
|
+
}
|