From 70db663ae8773dc45464c904603a3e73045b45c5 Mon Sep 17 00:00:00 2001 From: "Mark D. Roth" Date: Mon, 27 Nov 2017 14:53:26 -0800 Subject: Add ReferenceCounted base class. --- package.xml | 3 +++ 1 file changed, 3 insertions(+) (limited to 'package.xml') diff --git a/package.xml b/package.xml index 59d49dd165..c0d9b49828 100644 --- a/package.xml +++ b/package.xml @@ -101,11 +101,13 @@ + + @@ -135,6 +137,7 @@ + -- cgit v1.2.3 From cf9ca843eb3dec132195e8a8b21f33a7525d2197 Mon Sep 17 00:00:00 2001 From: "Mark D. Roth" Date: Tue, 28 Nov 2017 08:24:35 -0800 Subject: Add ReferenceCountedPtr class. --- BUILD | 6 + CMakeLists.txt | 41 +++++++ Makefile | 48 ++++++++ build.yaml | 15 +++ gRPC-Core.podspec | 2 + grpc.gemspec | 1 + package.xml | 1 + src/core/lib/support/reference_counted_ptr.h | 81 +++++++++++++ test/core/support/BUILD | 14 +++ test/core/support/reference_counted_ptr_test.cc | 135 +++++++++++++++++++++ tools/doxygen/Doxyfile.c++.internal | 1 + tools/doxygen/Doxyfile.core.internal | 1 + tools/run_tests/generated/sources_and_headers.json | 21 ++++ tools/run_tests/generated/tests.json | 24 ++++ 14 files changed, 391 insertions(+) create mode 100644 src/core/lib/support/reference_counted_ptr.h create mode 100644 test/core/support/reference_counted_ptr_test.cc (limited to 'package.xml') diff --git a/BUILD b/BUILD index 5289023ef5..9c93a5c099 100644 --- a/BUILD +++ b/BUILD @@ -554,6 +554,12 @@ grpc_cc_library( ], ) +grpc_cc_library( + name = "reference_counted_ptr", + public_hdrs = ["src/core/lib/support/reference_counted_ptr.h"], + language = "c++", +) + grpc_cc_library( name = "grpc_base_c", srcs = [ diff --git a/CMakeLists.txt b/CMakeLists.txt index ca59c84e50..7e18465c92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -739,6 +739,7 @@ endif() add_dependencies(buildtests_cxx qps_worker) add_dependencies(buildtests_cxx reconnect_interop_client) add_dependencies(buildtests_cxx reconnect_interop_server) +add_dependencies(buildtests_cxx reference_counted_ptr_test) add_dependencies(buildtests_cxx reference_counted_test) add_dependencies(buildtests_cxx secure_auth_context_test) if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_POSIX) @@ -12262,6 +12263,46 @@ target_link_libraries(reconnect_interop_server endif (gRPC_BUILD_TESTS) if (gRPC_BUILD_TESTS) +add_executable(reference_counted_ptr_test + test/core/support/reference_counted_ptr_test.cc + third_party/googletest/googletest/src/gtest-all.cc + third_party/googletest/googlemock/src/gmock-all.cc +) + + +target_include_directories(reference_counted_ptr_test + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include + PRIVATE ${BORINGSSL_ROOT_DIR}/include + PRIVATE ${PROTOBUF_ROOT_DIR}/src + PRIVATE ${BENCHMARK_ROOT_DIR}/include + PRIVATE ${ZLIB_ROOT_DIR} + PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/third_party/zlib + PRIVATE ${CARES_INCLUDE_DIR} + PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/third_party/cares/cares + PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/third_party/gflags/include + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/third_party/abseil-cpp + PRIVATE third_party/googletest/googletest/include + PRIVATE third_party/googletest/googletest + PRIVATE third_party/googletest/googlemock/include + PRIVATE third_party/googletest/googlemock + PRIVATE ${_gRPC_PROTO_GENS_DIR} +) + +target_link_libraries(reference_counted_ptr_test + ${_gRPC_PROTOBUF_LIBRARIES} + ${_gRPC_ALLTARGETS_LIBRARIES} + grpc_test_util + grpc++ + grpc + gpr_test_util + gpr + ${_gRPC_GFLAGS_LIBRARIES} +) + +endif (gRPC_BUILD_TESTS) +if (gRPC_BUILD_TESTS) + add_executable(reference_counted_test test/core/support/reference_counted_test.cc third_party/googletest/googletest/src/gtest-all.cc diff --git a/Makefile b/Makefile index 0e8437a05c..848182d1bd 100644 --- a/Makefile +++ b/Makefile @@ -1165,6 +1165,7 @@ qps_openloop_test: $(BINDIR)/$(CONFIG)/qps_openloop_test qps_worker: $(BINDIR)/$(CONFIG)/qps_worker reconnect_interop_client: $(BINDIR)/$(CONFIG)/reconnect_interop_client reconnect_interop_server: $(BINDIR)/$(CONFIG)/reconnect_interop_server +reference_counted_ptr_test: $(BINDIR)/$(CONFIG)/reference_counted_ptr_test reference_counted_test: $(BINDIR)/$(CONFIG)/reference_counted_test secure_auth_context_test: $(BINDIR)/$(CONFIG)/secure_auth_context_test secure_sync_unary_ping_pong_test: $(BINDIR)/$(CONFIG)/secure_sync_unary_ping_pong_test @@ -1602,6 +1603,7 @@ buildtests_cxx: privatelibs_cxx \ $(BINDIR)/$(CONFIG)/qps_worker \ $(BINDIR)/$(CONFIG)/reconnect_interop_client \ $(BINDIR)/$(CONFIG)/reconnect_interop_server \ + $(BINDIR)/$(CONFIG)/reference_counted_ptr_test \ $(BINDIR)/$(CONFIG)/reference_counted_test \ $(BINDIR)/$(CONFIG)/secure_auth_context_test \ $(BINDIR)/$(CONFIG)/secure_sync_unary_ping_pong_test \ @@ -1729,6 +1731,7 @@ buildtests_cxx: privatelibs_cxx \ $(BINDIR)/$(CONFIG)/qps_worker \ $(BINDIR)/$(CONFIG)/reconnect_interop_client \ $(BINDIR)/$(CONFIG)/reconnect_interop_server \ + $(BINDIR)/$(CONFIG)/reference_counted_ptr_test \ $(BINDIR)/$(CONFIG)/reference_counted_test \ $(BINDIR)/$(CONFIG)/secure_auth_context_test \ $(BINDIR)/$(CONFIG)/secure_sync_unary_ping_pong_test \ @@ -2133,6 +2136,8 @@ test_cxx: buildtests_cxx $(Q) $(BINDIR)/$(CONFIG)/proto_utils_test || ( echo test proto_utils_test failed ; exit 1 ) $(E) "[RUN] Testing qps_openloop_test" $(Q) $(BINDIR)/$(CONFIG)/qps_openloop_test || ( echo test qps_openloop_test failed ; exit 1 ) + $(E) "[RUN] Testing reference_counted_ptr_test" + $(Q) $(BINDIR)/$(CONFIG)/reference_counted_ptr_test || ( echo test reference_counted_ptr_test failed ; exit 1 ) $(E) "[RUN] Testing reference_counted_test" $(Q) $(BINDIR)/$(CONFIG)/reference_counted_test || ( echo test reference_counted_test failed ; exit 1 ) $(E) "[RUN] Testing secure_auth_context_test" @@ -16501,6 +16506,49 @@ endif $(OBJDIR)/$(CONFIG)/test/cpp/interop/reconnect_interop_server.o: $(GENDIR)/src/proto/grpc/testing/empty.pb.cc $(GENDIR)/src/proto/grpc/testing/empty.grpc.pb.cc $(GENDIR)/src/proto/grpc/testing/messages.pb.cc $(GENDIR)/src/proto/grpc/testing/messages.grpc.pb.cc $(GENDIR)/src/proto/grpc/testing/test.pb.cc $(GENDIR)/src/proto/grpc/testing/test.grpc.pb.cc +REFERENCE_COUNTED_PTR_TEST_SRC = \ + test/core/support/reference_counted_ptr_test.cc \ + +REFERENCE_COUNTED_PTR_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(REFERENCE_COUNTED_PTR_TEST_SRC)))) +ifeq ($(NO_SECURE),true) + +# You can't build secure targets if you don't have OpenSSL. + +$(BINDIR)/$(CONFIG)/reference_counted_ptr_test: openssl_dep_error + +else + + + + +ifeq ($(NO_PROTOBUF),true) + +# You can't build the protoc plugins or protobuf-enabled targets if you don't have protobuf 3.0.0+. + +$(BINDIR)/$(CONFIG)/reference_counted_ptr_test: protobuf_dep_error + +else + +$(BINDIR)/$(CONFIG)/reference_counted_ptr_test: $(PROTOBUF_DEP) $(REFERENCE_COUNTED_PTR_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a + $(E) "[LD] Linking $@" + $(Q) mkdir -p `dirname $@` + $(Q) $(LDXX) $(LDFLAGS) $(REFERENCE_COUNTED_PTR_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/reference_counted_ptr_test + +endif + +endif + +$(OBJDIR)/$(CONFIG)/test/core/support/reference_counted_ptr_test.o: $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a + +deps_reference_counted_ptr_test: $(REFERENCE_COUNTED_PTR_TEST_OBJS:.o=.dep) + +ifneq ($(NO_SECURE),true) +ifneq ($(NO_DEPS),true) +-include $(REFERENCE_COUNTED_PTR_TEST_OBJS:.o=.dep) +endif +endif + + REFERENCE_COUNTED_TEST_SRC = \ test/core/support/reference_counted_test.cc \ diff --git a/build.yaml b/build.yaml index 2903b866dc..9ac86eb430 100644 --- a/build.yaml +++ b/build.yaml @@ -117,6 +117,7 @@ filegroups: - src/core/lib/support/mpscq.h - src/core/lib/support/murmur_hash.h - src/core/lib/support/reference_counted.h + - src/core/lib/support/reference_counted_ptr.h - src/core/lib/support/spinlock.h - src/core/lib/support/stack_lockfree.h - src/core/lib/support/string.h @@ -4544,6 +4545,20 @@ targets: - gpr_test_util - gpr - grpc++_test_config +- name: reference_counted_ptr_test + gtest: true + build: test + language: c++ + src: + - test/core/support/reference_counted_ptr_test.cc + deps: + - grpc_test_util + - grpc++ + - grpc + - gpr_test_util + - gpr + uses: + - grpc++_test - name: reference_counted_test gtest: true build: test diff --git a/gRPC-Core.podspec b/gRPC-Core.podspec index 71e13633ae..f623f26831 100644 --- a/gRPC-Core.podspec +++ b/gRPC-Core.podspec @@ -199,6 +199,7 @@ Pod::Spec.new do |s| 'src/core/lib/support/mpscq.h', 'src/core/lib/support/murmur_hash.h', 'src/core/lib/support/reference_counted.h', + 'src/core/lib/support/reference_counted_ptr.h', 'src/core/lib/support/spinlock.h', 'src/core/lib/support/stack_lockfree.h', 'src/core/lib/support/string.h', @@ -722,6 +723,7 @@ Pod::Spec.new do |s| 'src/core/lib/support/mpscq.h', 'src/core/lib/support/murmur_hash.h', 'src/core/lib/support/reference_counted.h', + 'src/core/lib/support/reference_counted_ptr.h', 'src/core/lib/support/spinlock.h', 'src/core/lib/support/stack_lockfree.h', 'src/core/lib/support/string.h', diff --git a/grpc.gemspec b/grpc.gemspec index 900f4afe7a..da1b6c0aef 100644 --- a/grpc.gemspec +++ b/grpc.gemspec @@ -96,6 +96,7 @@ Gem::Specification.new do |s| s.files += %w( src/core/lib/support/mpscq.h ) s.files += %w( src/core/lib/support/murmur_hash.h ) s.files += %w( src/core/lib/support/reference_counted.h ) + s.files += %w( src/core/lib/support/reference_counted_ptr.h ) s.files += %w( src/core/lib/support/spinlock.h ) s.files += %w( src/core/lib/support/stack_lockfree.h ) s.files += %w( src/core/lib/support/string.h ) diff --git a/package.xml b/package.xml index c0d9b49828..92eab40b87 100644 --- a/package.xml +++ b/package.xml @@ -108,6 +108,7 @@ + diff --git a/src/core/lib/support/reference_counted_ptr.h b/src/core/lib/support/reference_counted_ptr.h new file mode 100644 index 0000000000..48a763ebe0 --- /dev/null +++ b/src/core/lib/support/reference_counted_ptr.h @@ -0,0 +1,81 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_PTR_H +#define GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_PTR_H + +namespace grpc_core { + +// A smart pointer class for objects that provide Ref() and Unref() methods, +// such as those provided by the ReferenceCounted base class. +template +class ReferenceCountedPtr { + public: + ReferenceCountedPtr() {} + + // If value is non-null, we take ownership of a ref to it. + explicit ReferenceCountedPtr(T* value) { + value_ = value; + } + + // Move support. + ReferenceCountedPtr(ReferenceCountedPtr&& other) { + value_ = other.value_; + other.value_ = nullptr; + } + ReferenceCountedPtr& operator=(ReferenceCountedPtr&& other) { + if (value_ != nullptr) value_->Unref(); + value_ = other.value_; + other.value_ = nullptr; + return *this; + } + + // Copy support. + ReferenceCountedPtr(const ReferenceCountedPtr& other) { + if (other.value_ != nullptr) other.value_->Ref(); + value_ = other.value_; + } + ReferenceCountedPtr& operator=(const ReferenceCountedPtr& other) { + if (value_ != nullptr) value_->Unref(); + if (other.value_ != nullptr) other.value_->Ref(); + value_ = other.value_; + return *this; + } + + ~ReferenceCountedPtr() { + if (value_ != nullptr) value_->Unref(); + } + + // If value is non-null, we take ownership of a ref to it. + void reset(T* value = nullptr) { + if (value_ != nullptr) value_->Unref(); + value_ = value; + } + + T* get() const { return value_; } + + T& operator*() const { return *value_; } + T* operator->() const { return value_; } + + private: + T* value_ = nullptr; +}; + +} // namespace grpc_core + +#endif // GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_PTR_H diff --git a/test/core/support/BUILD b/test/core/support/BUILD index ee20ef8692..1a8d2a1359 100644 --- a/test/core/support/BUILD +++ b/test/core/support/BUILD @@ -246,3 +246,17 @@ grpc_cc_test( "gtest", ], ) + +grpc_cc_test( + name = "reference_counted_ptr_test", + srcs = ["reference_counted_ptr_test.cc"], + language = "C++", + deps = [ + "//:reference_counted", + "//:reference_counted_ptr", + "//test/core/util:gpr_test_util", + ], + external_deps = [ + "gtest", + ], +) diff --git a/test/core/support/reference_counted_ptr_test.cc b/test/core/support/reference_counted_ptr_test.cc new file mode 100644 index 0000000000..7347ef5e4b --- /dev/null +++ b/test/core/support/reference_counted_ptr_test.cc @@ -0,0 +1,135 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "src/core/lib/support/reference_counted_ptr.h" + +#include + +#include + +#include "src/core/lib/support/memory.h" +#include "src/core/lib/support/reference_counted.h" +#include "test/core/util/test_config.h" + +namespace grpc_core { +namespace testing { + +class Foo : public ReferenceCounted { + public: + Foo() : ReferenceCounted(nullptr) {} + + void log() { + gpr_log(GPR_INFO, "==> log()"); + } +}; + +TEST(ReferenceCountedPtr, DefaultConstructor) { + ReferenceCountedPtr foo; +} + +TEST(ReferenceCountedPtr, ExplicitConstructorEmpty) { + ReferenceCountedPtr foo(nullptr); +} + +TEST(ReferenceCountedPtr, ExplicitConstructor) { + ReferenceCountedPtr foo(New()); +} + +TEST(ReferenceCountedPtr, MoveConstructor) { + ReferenceCountedPtr foo(New()); + ReferenceCountedPtr foo2(std::move(foo)); + EXPECT_EQ(nullptr, foo.get()); + EXPECT_NE(nullptr, foo2.get()); +} + +TEST(ReferenceCountedPtr, MoveAssignment) { + ReferenceCountedPtr foo(New()); + ReferenceCountedPtr foo2 = std::move(foo); + EXPECT_EQ(nullptr, foo.get()); + EXPECT_NE(nullptr, foo2.get()); +} + +TEST(ReferenceCountedPtr, CopyConstructor) { + ReferenceCountedPtr foo(New()); + ReferenceCountedPtr foo2(foo); + EXPECT_NE(nullptr, foo.get()); + EXPECT_EQ(foo.get(), foo2.get()); +} + +TEST(ReferenceCountedPtr, CopyAssignment) { + ReferenceCountedPtr foo(New()); + ReferenceCountedPtr foo2 = foo; + EXPECT_NE(nullptr, foo.get()); + EXPECT_EQ(foo.get(), foo2.get()); +} + +TEST(ReferenceCountedPtr, EnclosedScope) { + ReferenceCountedPtr foo(New()); + { + ReferenceCountedPtr foo2(std::move(foo)); + EXPECT_EQ(nullptr, foo.get()); + EXPECT_NE(nullptr, foo2.get()); + } + EXPECT_EQ(nullptr, foo.get()); +} + +TEST(ReferenceCountedPtr, ResetFromNullToNonNull) { + ReferenceCountedPtr foo; + EXPECT_EQ(nullptr, foo.get()); + foo.reset(New()); + EXPECT_NE(nullptr, foo.get()); +} + +TEST(ReferenceCountedPtr, ResetFromNonNullToNonNull) { + ReferenceCountedPtr foo(New()); + EXPECT_NE(nullptr, foo.get()); + Foo* original = foo.get(); + foo.reset(New()); + EXPECT_NE(nullptr, foo.get()); + EXPECT_NE(original, foo.get()); +} + +TEST(ReferenceCountedPtr, ResetFromNonNullToNull) { + ReferenceCountedPtr foo(New()); + EXPECT_NE(nullptr, foo.get()); + foo.reset(); + EXPECT_EQ(nullptr, foo.get()); +} + +TEST(ReferenceCountedPtr, ResetFromNullToNull) { + ReferenceCountedPtr foo; + EXPECT_EQ(nullptr, foo.get()); + foo.reset(nullptr); + EXPECT_EQ(nullptr, foo.get()); +} + +TEST(ReferenceCountedPtr, DerefernceOperators) { + ReferenceCountedPtr foo(New()); + foo->log(); + Foo& foo_ref = *foo; + foo_ref.log(); +} + +} // namespace testing +} // namespace grpc_core + +int main(int argc, char** argv) { + grpc_test_init(argc, argv); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index 1d84dcba33..078dd7bb87 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -1038,6 +1038,7 @@ src/core/lib/support/memory.h \ src/core/lib/support/mpscq.h \ src/core/lib/support/murmur_hash.h \ src/core/lib/support/reference_counted.h \ +src/core/lib/support/reference_counted_ptr.h \ src/core/lib/support/spinlock.h \ src/core/lib/support/stack_lockfree.h \ src/core/lib/support/string.h \ diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal index 5b289e7bc4..37ffff6b79 100644 --- a/tools/doxygen/Doxyfile.core.internal +++ b/tools/doxygen/Doxyfile.core.internal @@ -1301,6 +1301,7 @@ src/core/lib/support/murmur_hash.cc \ src/core/lib/support/murmur_hash.h \ src/core/lib/support/reference_counted.cc \ src/core/lib/support/reference_counted.h \ +src/core/lib/support/reference_counted_ptr.h \ src/core/lib/support/spinlock.h \ src/core/lib/support/stack_lockfree.cc \ src/core/lib/support/stack_lockfree.h \ diff --git a/tools/run_tests/generated/sources_and_headers.json b/tools/run_tests/generated/sources_and_headers.json index 8ec9619874..ed5128d9c1 100644 --- a/tools/run_tests/generated/sources_and_headers.json +++ b/tools/run_tests/generated/sources_and_headers.json @@ -3917,6 +3917,25 @@ "third_party": false, "type": "target" }, + { + "deps": [ + "gpr", + "gpr_test_util", + "grpc", + "grpc++", + "grpc++_test", + "grpc_test_util" + ], + "headers": [], + "is_filegroup": false, + "language": "c++", + "name": "reference_counted_ptr_test", + "src": [ + "test/core/support/reference_counted_ptr_test.cc" + ], + "third_party": false, + "type": "target" + }, { "deps": [ "gpr", @@ -7861,6 +7880,7 @@ "src/core/lib/support/mpscq.h", "src/core/lib/support/murmur_hash.h", "src/core/lib/support/reference_counted.h", + "src/core/lib/support/reference_counted_ptr.h", "src/core/lib/support/spinlock.h", "src/core/lib/support/stack_lockfree.h", "src/core/lib/support/string.h", @@ -7912,6 +7932,7 @@ "src/core/lib/support/mpscq.h", "src/core/lib/support/murmur_hash.h", "src/core/lib/support/reference_counted.h", + "src/core/lib/support/reference_counted_ptr.h", "src/core/lib/support/spinlock.h", "src/core/lib/support/stack_lockfree.h", "src/core/lib/support/string.h", diff --git a/tools/run_tests/generated/tests.json b/tools/run_tests/generated/tests.json index a6b58d8ba6..ba70d56085 100644 --- a/tools/run_tests/generated/tests.json +++ b/tools/run_tests/generated/tests.json @@ -4123,6 +4123,30 @@ ], "uses_polling": true }, + { + "args": [], + "benchmark": false, + "ci_platforms": [ + "linux", + "mac", + "posix", + "windows" + ], + "cpu_cost": 1.0, + "exclude_configs": [], + "exclude_iomgrs": [], + "flaky": false, + "gtest": true, + "language": "c++", + "name": "reference_counted_ptr_test", + "platforms": [ + "linux", + "mac", + "posix", + "windows" + ], + "uses_polling": true + }, { "args": [], "benchmark": false, -- cgit v1.2.3 From b319f491ff63a109917d69fc5020aa5c4eb184a7 Mon Sep 17 00:00:00 2001 From: "Mark D. Roth" Date: Tue, 28 Nov 2017 14:50:07 -0800 Subject: Code review changes. --- BUILD | 1 - CMakeLists.txt | 1 - Makefile | 1 - build.yaml | 1 - config.m4 | 1 - config.w32 | 1 - gRPC-Core.podspec | 1 - grpc.gemspec | 1 - grpc.gyp | 1 - package.xml | 1 - src/core/lib/support/debug_location.h | 3 ++ src/core/lib/support/reference_counted.cc | 58 ---------------------- src/core/lib/support/reference_counted.h | 39 +++++++++++++-- src/core/lib/support/reference_counted_ptr.h | 4 +- src/python/grpcio/grpc_core_dependencies.py | 1 - test/core/support/reference_counted_ptr_test.cc | 13 +++++ test/core/support/reference_counted_test.cc | 2 +- tools/doxygen/Doxyfile.core.internal | 1 - tools/run_tests/generated/sources_and_headers.json | 1 - 19 files changed, 54 insertions(+), 78 deletions(-) delete mode 100644 src/core/lib/support/reference_counted.cc (limited to 'package.xml') diff --git a/BUILD b/BUILD index 9c93a5c099..6ba5e6f655 100644 --- a/BUILD +++ b/BUILD @@ -545,7 +545,6 @@ grpc_cc_library( grpc_cc_library( name = "reference_counted", - srcs = ["src/core/lib/support/reference_counted.cc"], public_hdrs = ["src/core/lib/support/reference_counted.h"], language = "c++", deps = [ diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e18465c92..7b214be435 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -809,7 +809,6 @@ add_library(gpr src/core/lib/support/log_windows.cc src/core/lib/support/mpscq.cc src/core/lib/support/murmur_hash.cc - src/core/lib/support/reference_counted.cc src/core/lib/support/stack_lockfree.cc src/core/lib/support/string.cc src/core/lib/support/string_posix.cc diff --git a/Makefile b/Makefile index 848182d1bd..a81e62b440 100644 --- a/Makefile +++ b/Makefile @@ -2846,7 +2846,6 @@ LIBGPR_SRC = \ src/core/lib/support/log_windows.cc \ src/core/lib/support/mpscq.cc \ src/core/lib/support/murmur_hash.cc \ - src/core/lib/support/reference_counted.cc \ src/core/lib/support/stack_lockfree.cc \ src/core/lib/support/string.cc \ src/core/lib/support/string_posix.cc \ diff --git a/build.yaml b/build.yaml index 9ac86eb430..57a65b3707 100644 --- a/build.yaml +++ b/build.yaml @@ -49,7 +49,6 @@ filegroups: - src/core/lib/support/log_windows.cc - src/core/lib/support/mpscq.cc - src/core/lib/support/murmur_hash.cc - - src/core/lib/support/reference_counted.cc - src/core/lib/support/stack_lockfree.cc - src/core/lib/support/string.cc - src/core/lib/support/string_posix.cc diff --git a/config.m4 b/config.m4 index 9d514c8552..d2f2520fea 100644 --- a/config.m4 +++ b/config.m4 @@ -62,7 +62,6 @@ if test "$PHP_GRPC" != "no"; then src/core/lib/support/log_windows.cc \ src/core/lib/support/mpscq.cc \ src/core/lib/support/murmur_hash.cc \ - src/core/lib/support/reference_counted.cc \ src/core/lib/support/stack_lockfree.cc \ src/core/lib/support/string.cc \ src/core/lib/support/string_posix.cc \ diff --git a/config.w32 b/config.w32 index 2f24fcd8b0..8a713751dc 100644 --- a/config.w32 +++ b/config.w32 @@ -39,7 +39,6 @@ if (PHP_GRPC != "no") { "src\\core\\lib\\support\\log_windows.cc " + "src\\core\\lib\\support\\mpscq.cc " + "src\\core\\lib\\support\\murmur_hash.cc " + - "src\\core\\lib\\support\\reference_counted.cc " + "src\\core\\lib\\support\\stack_lockfree.cc " + "src\\core\\lib\\support\\string.cc " + "src\\core\\lib\\support\\string_posix.cc " + diff --git a/gRPC-Core.podspec b/gRPC-Core.podspec index f623f26831..bc500cc0d7 100644 --- a/gRPC-Core.podspec +++ b/gRPC-Core.podspec @@ -229,7 +229,6 @@ Pod::Spec.new do |s| 'src/core/lib/support/log_windows.cc', 'src/core/lib/support/mpscq.cc', 'src/core/lib/support/murmur_hash.cc', - 'src/core/lib/support/reference_counted.cc', 'src/core/lib/support/stack_lockfree.cc', 'src/core/lib/support/string.cc', 'src/core/lib/support/string_posix.cc', diff --git a/grpc.gemspec b/grpc.gemspec index da1b6c0aef..d1e8b5254d 100644 --- a/grpc.gemspec +++ b/grpc.gemspec @@ -126,7 +126,6 @@ Gem::Specification.new do |s| s.files += %w( src/core/lib/support/log_windows.cc ) s.files += %w( src/core/lib/support/mpscq.cc ) s.files += %w( src/core/lib/support/murmur_hash.cc ) - s.files += %w( src/core/lib/support/reference_counted.cc ) s.files += %w( src/core/lib/support/stack_lockfree.cc ) s.files += %w( src/core/lib/support/string.cc ) s.files += %w( src/core/lib/support/string_posix.cc ) diff --git a/grpc.gyp b/grpc.gyp index 0ee6529013..fb153915ff 100644 --- a/grpc.gyp +++ b/grpc.gyp @@ -184,7 +184,6 @@ 'src/core/lib/support/log_windows.cc', 'src/core/lib/support/mpscq.cc', 'src/core/lib/support/murmur_hash.cc', - 'src/core/lib/support/reference_counted.cc', 'src/core/lib/support/stack_lockfree.cc', 'src/core/lib/support/string.cc', 'src/core/lib/support/string_posix.cc', diff --git a/package.xml b/package.xml index 92eab40b87..6f786a13ad 100644 --- a/package.xml +++ b/package.xml @@ -138,7 +138,6 @@ - diff --git a/src/core/lib/support/debug_location.h b/src/core/lib/support/debug_location.h index 47c5082874..0939da595d 100644 --- a/src/core/lib/support/debug_location.h +++ b/src/core/lib/support/debug_location.h @@ -21,6 +21,9 @@ namespace grpc_core { +// Used for tracking file and line where a call is made for debug builds. +// No-op for non-debug builds. +// Callers can use the DEBUG_LOCATION macro in either case. #ifndef NDEBUG class DebugLocation { public: diff --git a/src/core/lib/support/reference_counted.cc b/src/core/lib/support/reference_counted.cc deleted file mode 100644 index 222628a6fa..0000000000 --- a/src/core/lib/support/reference_counted.cc +++ /dev/null @@ -1,58 +0,0 @@ -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "src/core/lib/support/reference_counted.h" - -#include - -#include "src/core/lib/support/memory.h" - -namespace grpc_core { - -void ReferenceCounted::Ref(const DebugLocation& location, const char* reason) { - if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) { - gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count); - gpr_log(GPR_DEBUG, "%s:%p %s:%d ref %" PRIdPTR " -> %" PRIdPTR " %s", - trace_flag_->name(), this, location.file(), location.line(), - old_refs, old_refs + 1, reason); - } - Ref(); -} - -void ReferenceCounted::Ref() { gpr_ref(&refs_); } - -bool ReferenceCounted::Unref(const DebugLocation& location, - const char* reason) { - if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) { - gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count); - gpr_log(GPR_DEBUG, "%s:%p %s:%d unref %" PRIdPTR " -> %" PRIdPTR " %s", - trace_flag_->name(), this, location.file(), location.line(), - old_refs, old_refs - 1, reason); - } - return Unref(); -} - -bool ReferenceCounted::Unref() { - if (gpr_unref(&refs_)) { - Delete(this); - return true; - } - return false; -} - -} // namespace grpc_core diff --git a/src/core/lib/support/reference_counted.h b/src/core/lib/support/reference_counted.h index 42bedcbd3a..ff74de082c 100644 --- a/src/core/lib/support/reference_counted.h +++ b/src/core/lib/support/reference_counted.h @@ -20,19 +20,46 @@ #define GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H #include +#include #include "src/core/lib/debug/trace.h" #include "src/core/lib/support/debug_location.h" +#include "src/core/lib/support/memory.h" namespace grpc_core { +// A base class for reference-counted objects. +// New objects should be created via New() and start with a refcount of 1. +// When the refcount reaches 0, the object will be deleted via Delete(). class ReferenceCounted { public: - void Ref(); - void Ref(const DebugLocation& location, const char* reason); + void Ref() { gpr_ref(&refs_); } - bool Unref(); - bool Unref(const DebugLocation& location, const char* reason); + void Ref(const DebugLocation& location, const char* reason) { + if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) { + gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count); + gpr_log(GPR_DEBUG, "%s:%p %s:%d ref %" PRIdPTR " -> %" PRIdPTR " %s", + trace_flag_->name(), this, location.file(), location.line(), + old_refs, old_refs + 1, reason); + } + Ref(); + } + + void Unref() { + if (gpr_unref(&refs_)) { + Delete(this); + } + } + + void Unref(const DebugLocation& location, const char* reason) { + if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) { + gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count); + gpr_log(GPR_DEBUG, "%s:%p %s:%d unref %" PRIdPTR " -> %" PRIdPTR " %s", + trace_flag_->name(), this, location.file(), location.line(), + old_refs, old_refs - 1, reason); + } + Unref(); + } // Not copyable nor movable. ReferenceCounted(const ReferenceCounted&) = delete; @@ -43,6 +70,8 @@ class ReferenceCounted { template friend void Delete(T*); + ReferenceCounted() : ReferenceCounted(nullptr) {} + explicit ReferenceCounted(TraceFlag* trace_flag) : trace_flag_(trace_flag) { gpr_ref_init(&refs_, 1); } @@ -50,7 +79,7 @@ class ReferenceCounted { virtual ~ReferenceCounted() {} private: - TraceFlag* trace_flag_; + TraceFlag* trace_flag_ = nullptr; gpr_refcount refs_; }; diff --git a/src/core/lib/support/reference_counted_ptr.h b/src/core/lib/support/reference_counted_ptr.h index 3782380943..1590549bfb 100644 --- a/src/core/lib/support/reference_counted_ptr.h +++ b/src/core/lib/support/reference_counted_ptr.h @@ -53,8 +53,10 @@ class ReferenceCountedPtr { value_ = other.value_; } ReferenceCountedPtr& operator=(const ReferenceCountedPtr& other) { - if (value_ != nullptr) value_->Unref(); + // Note: Order of reffing and unreffing is important here in case value_ + // and other.value_ are the same object. if (other.value_ != nullptr) other.value_->Ref(); + if (value_ != nullptr) value_->Unref(); value_ = other.value_; return *this; } diff --git a/src/python/grpcio/grpc_core_dependencies.py b/src/python/grpcio/grpc_core_dependencies.py index 38f2a40133..330c4185c6 100644 --- a/src/python/grpcio/grpc_core_dependencies.py +++ b/src/python/grpcio/grpc_core_dependencies.py @@ -38,7 +38,6 @@ CORE_SOURCE_FILES = [ 'src/core/lib/support/log_windows.cc', 'src/core/lib/support/mpscq.cc', 'src/core/lib/support/murmur_hash.cc', - 'src/core/lib/support/reference_counted.cc', 'src/core/lib/support/stack_lockfree.cc', 'src/core/lib/support/string.cc', 'src/core/lib/support/string_posix.cc', diff --git a/test/core/support/reference_counted_ptr_test.cc b/test/core/support/reference_counted_ptr_test.cc index 942c724724..14dd620887 100644 --- a/test/core/support/reference_counted_ptr_test.cc +++ b/test/core/support/reference_counted_ptr_test.cc @@ -79,6 +79,19 @@ TEST(ReferenceCountedPtr, CopyAssignment) { EXPECT_EQ(foo.get(), foo2.get()); } +TEST(ReferenceCountedPtr, CopyAssignmentWhenEmpty) { + ReferenceCountedPtr foo; + ReferenceCountedPtr foo2; + foo2 = foo; + EXPECT_EQ(nullptr, foo.get()); + EXPECT_EQ(nullptr, foo2.get()); +} + +TEST(ReferenceCountedPtr, CopyAssignmentToSelf) { + ReferenceCountedPtr foo(New()); + foo = foo; +} + TEST(ReferenceCountedPtr, EnclosedScope) { ReferenceCountedPtr foo(New()); { diff --git a/test/core/support/reference_counted_test.cc b/test/core/support/reference_counted_test.cc index c5795429c4..b1f79e61e6 100644 --- a/test/core/support/reference_counted_test.cc +++ b/test/core/support/reference_counted_test.cc @@ -28,7 +28,7 @@ namespace testing { class Foo : public ReferenceCounted { public: - Foo() : ReferenceCounted(nullptr) {} + Foo() {} }; TEST(ReferenceCounted, StackAllocated) { Foo foo; } diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal index 37ffff6b79..8343f88a37 100644 --- a/tools/doxygen/Doxyfile.core.internal +++ b/tools/doxygen/Doxyfile.core.internal @@ -1299,7 +1299,6 @@ src/core/lib/support/mpscq.cc \ src/core/lib/support/mpscq.h \ src/core/lib/support/murmur_hash.cc \ src/core/lib/support/murmur_hash.h \ -src/core/lib/support/reference_counted.cc \ src/core/lib/support/reference_counted.h \ src/core/lib/support/reference_counted_ptr.h \ src/core/lib/support/spinlock.h \ diff --git a/tools/run_tests/generated/sources_and_headers.json b/tools/run_tests/generated/sources_and_headers.json index ed5128d9c1..284a2098e3 100644 --- a/tools/run_tests/generated/sources_and_headers.json +++ b/tools/run_tests/generated/sources_and_headers.json @@ -7808,7 +7808,6 @@ "src/core/lib/support/log_windows.cc", "src/core/lib/support/mpscq.cc", "src/core/lib/support/murmur_hash.cc", - "src/core/lib/support/reference_counted.cc", "src/core/lib/support/stack_lockfree.cc", "src/core/lib/support/string.cc", "src/core/lib/support/string_posix.cc", -- cgit v1.2.3 From 240bad3d2d15628ccf13dddfc143e226fc4d9874 Mon Sep 17 00:00:00 2001 From: "Mark D. Roth" Date: Wed, 29 Nov 2017 10:50:45 -0800 Subject: Fix check_sources_and_headers. --- build.yaml | 6 +++--- gRPC-Core.podspec | 12 ++++++------ grpc.gemspec | 6 +++--- package.xml | 6 +++--- tools/run_tests/generated/sources_and_headers.json | 12 ++++++------ 5 files changed, 21 insertions(+), 21 deletions(-) (limited to 'package.xml') diff --git a/build.yaml b/build.yaml index 57a65b3707..c41bf193e6 100644 --- a/build.yaml +++ b/build.yaml @@ -109,14 +109,11 @@ filegroups: - src/core/lib/support/atomic.h - src/core/lib/support/atomic_with_atm.h - src/core/lib/support/atomic_with_std.h - - src/core/lib/support/debug_location.h - src/core/lib/support/env.h - src/core/lib/support/manual_constructor.h - src/core/lib/support/memory.h - src/core/lib/support/mpscq.h - src/core/lib/support/murmur_hash.h - - src/core/lib/support/reference_counted.h - - src/core/lib/support/reference_counted_ptr.h - src/core/lib/support/spinlock.h - src/core/lib/support/stack_lockfree.h - src/core/lib/support/string.h @@ -396,6 +393,9 @@ filegroups: - src/core/lib/slice/slice_hash_table.h - src/core/lib/slice/slice_internal.h - src/core/lib/slice/slice_string_helpers.h + - src/core/lib/support/debug_location.h + - src/core/lib/support/reference_counted.h + - src/core/lib/support/reference_counted_ptr.h - src/core/lib/support/vector.h - src/core/lib/surface/alarm_internal.h - src/core/lib/surface/api_trace.h diff --git a/gRPC-Core.podspec b/gRPC-Core.podspec index bc500cc0d7..e90e356aaa 100644 --- a/gRPC-Core.podspec +++ b/gRPC-Core.podspec @@ -192,14 +192,11 @@ Pod::Spec.new do |s| 'src/core/lib/support/atomic.h', 'src/core/lib/support/atomic_with_atm.h', 'src/core/lib/support/atomic_with_std.h', - 'src/core/lib/support/debug_location.h', 'src/core/lib/support/env.h', 'src/core/lib/support/manual_constructor.h', 'src/core/lib/support/memory.h', 'src/core/lib/support/mpscq.h', 'src/core/lib/support/murmur_hash.h', - 'src/core/lib/support/reference_counted.h', - 'src/core/lib/support/reference_counted_ptr.h', 'src/core/lib/support/spinlock.h', 'src/core/lib/support/stack_lockfree.h', 'src/core/lib/support/string.h', @@ -416,6 +413,9 @@ Pod::Spec.new do |s| 'src/core/lib/slice/slice_hash_table.h', 'src/core/lib/slice/slice_internal.h', 'src/core/lib/slice/slice_string_helpers.h', + 'src/core/lib/support/debug_location.h', + 'src/core/lib/support/reference_counted.h', + 'src/core/lib/support/reference_counted_ptr.h', 'src/core/lib/support/vector.h', 'src/core/lib/surface/alarm_internal.h', 'src/core/lib/surface/api_trace.h', @@ -715,14 +715,11 @@ Pod::Spec.new do |s| 'src/core/lib/support/atomic.h', 'src/core/lib/support/atomic_with_atm.h', 'src/core/lib/support/atomic_with_std.h', - 'src/core/lib/support/debug_location.h', 'src/core/lib/support/env.h', 'src/core/lib/support/manual_constructor.h', 'src/core/lib/support/memory.h', 'src/core/lib/support/mpscq.h', 'src/core/lib/support/murmur_hash.h', - 'src/core/lib/support/reference_counted.h', - 'src/core/lib/support/reference_counted_ptr.h', 'src/core/lib/support/spinlock.h', 'src/core/lib/support/stack_lockfree.h', 'src/core/lib/support/string.h', @@ -894,6 +891,9 @@ Pod::Spec.new do |s| 'src/core/lib/slice/slice_hash_table.h', 'src/core/lib/slice/slice_internal.h', 'src/core/lib/slice/slice_string_helpers.h', + 'src/core/lib/support/debug_location.h', + 'src/core/lib/support/reference_counted.h', + 'src/core/lib/support/reference_counted_ptr.h', 'src/core/lib/support/vector.h', 'src/core/lib/surface/alarm_internal.h', 'src/core/lib/surface/api_trace.h', diff --git a/grpc.gemspec b/grpc.gemspec index d1e8b5254d..a58c73a59d 100644 --- a/grpc.gemspec +++ b/grpc.gemspec @@ -89,14 +89,11 @@ Gem::Specification.new do |s| s.files += %w( src/core/lib/support/atomic.h ) s.files += %w( src/core/lib/support/atomic_with_atm.h ) s.files += %w( src/core/lib/support/atomic_with_std.h ) - s.files += %w( src/core/lib/support/debug_location.h ) s.files += %w( src/core/lib/support/env.h ) s.files += %w( src/core/lib/support/manual_constructor.h ) s.files += %w( src/core/lib/support/memory.h ) s.files += %w( src/core/lib/support/mpscq.h ) s.files += %w( src/core/lib/support/murmur_hash.h ) - s.files += %w( src/core/lib/support/reference_counted.h ) - s.files += %w( src/core/lib/support/reference_counted_ptr.h ) s.files += %w( src/core/lib/support/spinlock.h ) s.files += %w( src/core/lib/support/stack_lockfree.h ) s.files += %w( src/core/lib/support/string.h ) @@ -347,6 +344,9 @@ Gem::Specification.new do |s| s.files += %w( src/core/lib/slice/slice_hash_table.h ) s.files += %w( src/core/lib/slice/slice_internal.h ) s.files += %w( src/core/lib/slice/slice_string_helpers.h ) + s.files += %w( src/core/lib/support/debug_location.h ) + s.files += %w( src/core/lib/support/reference_counted.h ) + s.files += %w( src/core/lib/support/reference_counted_ptr.h ) s.files += %w( src/core/lib/support/vector.h ) s.files += %w( src/core/lib/surface/alarm_internal.h ) s.files += %w( src/core/lib/surface/api_trace.h ) diff --git a/package.xml b/package.xml index 6f786a13ad..0036481bd1 100644 --- a/package.xml +++ b/package.xml @@ -101,14 +101,11 @@ - - - @@ -359,6 +356,9 @@ + + + diff --git a/tools/run_tests/generated/sources_and_headers.json b/tools/run_tests/generated/sources_and_headers.json index 284a2098e3..091aa17141 100644 --- a/tools/run_tests/generated/sources_and_headers.json +++ b/tools/run_tests/generated/sources_and_headers.json @@ -7872,14 +7872,11 @@ "src/core/lib/support/atomic.h", "src/core/lib/support/atomic_with_atm.h", "src/core/lib/support/atomic_with_std.h", - "src/core/lib/support/debug_location.h", "src/core/lib/support/env.h", "src/core/lib/support/manual_constructor.h", "src/core/lib/support/memory.h", "src/core/lib/support/mpscq.h", "src/core/lib/support/murmur_hash.h", - "src/core/lib/support/reference_counted.h", - "src/core/lib/support/reference_counted_ptr.h", "src/core/lib/support/spinlock.h", "src/core/lib/support/stack_lockfree.h", "src/core/lib/support/string.h", @@ -7924,14 +7921,11 @@ "src/core/lib/support/atomic.h", "src/core/lib/support/atomic_with_atm.h", "src/core/lib/support/atomic_with_std.h", - "src/core/lib/support/debug_location.h", "src/core/lib/support/env.h", "src/core/lib/support/manual_constructor.h", "src/core/lib/support/memory.h", "src/core/lib/support/mpscq.h", "src/core/lib/support/murmur_hash.h", - "src/core/lib/support/reference_counted.h", - "src/core/lib/support/reference_counted_ptr.h", "src/core/lib/support/spinlock.h", "src/core/lib/support/stack_lockfree.h", "src/core/lib/support/string.h", @@ -8264,6 +8258,9 @@ "src/core/lib/slice/slice_hash_table.h", "src/core/lib/slice/slice_internal.h", "src/core/lib/slice/slice_string_helpers.h", + "src/core/lib/support/debug_location.h", + "src/core/lib/support/reference_counted.h", + "src/core/lib/support/reference_counted_ptr.h", "src/core/lib/support/vector.h", "src/core/lib/surface/alarm_internal.h", "src/core/lib/surface/api_trace.h", @@ -8400,6 +8397,9 @@ "src/core/lib/slice/slice_hash_table.h", "src/core/lib/slice/slice_internal.h", "src/core/lib/slice/slice_string_helpers.h", + "src/core/lib/support/debug_location.h", + "src/core/lib/support/reference_counted.h", + "src/core/lib/support/reference_counted_ptr.h", "src/core/lib/support/vector.h", "src/core/lib/surface/alarm_internal.h", "src/core/lib/surface/api_trace.h", -- cgit v1.2.3 From bf816d325e4281804c3f75e63c2c1f7827176300 Mon Sep 17 00:00:00 2001 From: "Mark D. Roth" Date: Wed, 29 Nov 2017 11:25:34 -0800 Subject: Shorted "reference" to "ref". --- BUILD | 8 +- CMakeLists.txt | 20 +-- Makefile | 60 +++---- build.yaml | 12 +- gRPC-Core.podspec | 8 +- grpc.gemspec | 4 +- package.xml | 4 +- src/core/lib/support/ref_counted.h | 122 +++++++++++++++ src/core/lib/support/ref_counted_ptr.h | 90 +++++++++++ src/core/lib/support/reference_counted.h | 123 --------------- src/core/lib/support/reference_counted_ptr.h | 90 ----------- test/core/support/BUILD | 14 +- test/core/support/ref_counted_ptr_test.cc | 174 +++++++++++++++++++++ test/core/support/ref_counted_test.cc | 72 +++++++++ test/core/support/reference_counted_ptr_test.cc | 174 --------------------- test/core/support/reference_counted_test.cc | 72 --------- tools/doxygen/Doxyfile.c++.internal | 4 +- tools/doxygen/Doxyfile.core.internal | 4 +- tools/run_tests/generated/sources_and_headers.json | 16 +- tools/run_tests/generated/tests.json | 4 +- 20 files changed, 537 insertions(+), 538 deletions(-) create mode 100644 src/core/lib/support/ref_counted.h create mode 100644 src/core/lib/support/ref_counted_ptr.h delete mode 100644 src/core/lib/support/reference_counted.h delete mode 100644 src/core/lib/support/reference_counted_ptr.h create mode 100644 test/core/support/ref_counted_ptr_test.cc create mode 100644 test/core/support/ref_counted_test.cc delete mode 100644 test/core/support/reference_counted_ptr_test.cc delete mode 100644 test/core/support/reference_counted_test.cc (limited to 'package.xml') diff --git a/BUILD b/BUILD index 6ba5e6f655..e88cdfedc5 100644 --- a/BUILD +++ b/BUILD @@ -544,8 +544,8 @@ grpc_cc_library( ) grpc_cc_library( - name = "reference_counted", - public_hdrs = ["src/core/lib/support/reference_counted.h"], + name = "ref_counted", + public_hdrs = ["src/core/lib/support/ref_counted.h"], language = "c++", deps = [ "grpc_trace", @@ -554,8 +554,8 @@ grpc_cc_library( ) grpc_cc_library( - name = "reference_counted_ptr", - public_hdrs = ["src/core/lib/support/reference_counted_ptr.h"], + name = "ref_counted_ptr", + public_hdrs = ["src/core/lib/support/ref_counted_ptr.h"], language = "c++", ) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7b214be435..f77b650a48 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -739,8 +739,8 @@ endif() add_dependencies(buildtests_cxx qps_worker) add_dependencies(buildtests_cxx reconnect_interop_client) add_dependencies(buildtests_cxx reconnect_interop_server) -add_dependencies(buildtests_cxx reference_counted_ptr_test) -add_dependencies(buildtests_cxx reference_counted_test) +add_dependencies(buildtests_cxx ref_counted_ptr_test) +add_dependencies(buildtests_cxx ref_counted_test) add_dependencies(buildtests_cxx secure_auth_context_test) if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_POSIX) add_dependencies(buildtests_cxx secure_sync_unary_ping_pong_test) @@ -12262,14 +12262,14 @@ target_link_libraries(reconnect_interop_server endif (gRPC_BUILD_TESTS) if (gRPC_BUILD_TESTS) -add_executable(reference_counted_ptr_test - test/core/support/reference_counted_ptr_test.cc +add_executable(ref_counted_ptr_test + test/core/support/ref_counted_ptr_test.cc third_party/googletest/googletest/src/gtest-all.cc third_party/googletest/googlemock/src/gmock-all.cc ) -target_include_directories(reference_counted_ptr_test +target_include_directories(ref_counted_ptr_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include PRIVATE ${BORINGSSL_ROOT_DIR}/include @@ -12288,7 +12288,7 @@ target_include_directories(reference_counted_ptr_test PRIVATE ${_gRPC_PROTO_GENS_DIR} ) -target_link_libraries(reference_counted_ptr_test +target_link_libraries(ref_counted_ptr_test ${_gRPC_PROTOBUF_LIBRARIES} ${_gRPC_ALLTARGETS_LIBRARIES} grpc_test_util @@ -12302,14 +12302,14 @@ target_link_libraries(reference_counted_ptr_test endif (gRPC_BUILD_TESTS) if (gRPC_BUILD_TESTS) -add_executable(reference_counted_test - test/core/support/reference_counted_test.cc +add_executable(ref_counted_test + test/core/support/ref_counted_test.cc third_party/googletest/googletest/src/gtest-all.cc third_party/googletest/googlemock/src/gmock-all.cc ) -target_include_directories(reference_counted_test +target_include_directories(ref_counted_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include PRIVATE ${BORINGSSL_ROOT_DIR}/include @@ -12328,7 +12328,7 @@ target_include_directories(reference_counted_test PRIVATE ${_gRPC_PROTO_GENS_DIR} ) -target_link_libraries(reference_counted_test +target_link_libraries(ref_counted_test ${_gRPC_PROTOBUF_LIBRARIES} ${_gRPC_ALLTARGETS_LIBRARIES} grpc_test_util diff --git a/Makefile b/Makefile index a81e62b440..ef6006c624 100644 --- a/Makefile +++ b/Makefile @@ -1165,8 +1165,8 @@ qps_openloop_test: $(BINDIR)/$(CONFIG)/qps_openloop_test qps_worker: $(BINDIR)/$(CONFIG)/qps_worker reconnect_interop_client: $(BINDIR)/$(CONFIG)/reconnect_interop_client reconnect_interop_server: $(BINDIR)/$(CONFIG)/reconnect_interop_server -reference_counted_ptr_test: $(BINDIR)/$(CONFIG)/reference_counted_ptr_test -reference_counted_test: $(BINDIR)/$(CONFIG)/reference_counted_test +ref_counted_ptr_test: $(BINDIR)/$(CONFIG)/ref_counted_ptr_test +ref_counted_test: $(BINDIR)/$(CONFIG)/ref_counted_test secure_auth_context_test: $(BINDIR)/$(CONFIG)/secure_auth_context_test secure_sync_unary_ping_pong_test: $(BINDIR)/$(CONFIG)/secure_sync_unary_ping_pong_test server_builder_plugin_test: $(BINDIR)/$(CONFIG)/server_builder_plugin_test @@ -1603,8 +1603,8 @@ buildtests_cxx: privatelibs_cxx \ $(BINDIR)/$(CONFIG)/qps_worker \ $(BINDIR)/$(CONFIG)/reconnect_interop_client \ $(BINDIR)/$(CONFIG)/reconnect_interop_server \ - $(BINDIR)/$(CONFIG)/reference_counted_ptr_test \ - $(BINDIR)/$(CONFIG)/reference_counted_test \ + $(BINDIR)/$(CONFIG)/ref_counted_ptr_test \ + $(BINDIR)/$(CONFIG)/ref_counted_test \ $(BINDIR)/$(CONFIG)/secure_auth_context_test \ $(BINDIR)/$(CONFIG)/secure_sync_unary_ping_pong_test \ $(BINDIR)/$(CONFIG)/server_builder_plugin_test \ @@ -1731,8 +1731,8 @@ buildtests_cxx: privatelibs_cxx \ $(BINDIR)/$(CONFIG)/qps_worker \ $(BINDIR)/$(CONFIG)/reconnect_interop_client \ $(BINDIR)/$(CONFIG)/reconnect_interop_server \ - $(BINDIR)/$(CONFIG)/reference_counted_ptr_test \ - $(BINDIR)/$(CONFIG)/reference_counted_test \ + $(BINDIR)/$(CONFIG)/ref_counted_ptr_test \ + $(BINDIR)/$(CONFIG)/ref_counted_test \ $(BINDIR)/$(CONFIG)/secure_auth_context_test \ $(BINDIR)/$(CONFIG)/secure_sync_unary_ping_pong_test \ $(BINDIR)/$(CONFIG)/server_builder_plugin_test \ @@ -2136,10 +2136,10 @@ test_cxx: buildtests_cxx $(Q) $(BINDIR)/$(CONFIG)/proto_utils_test || ( echo test proto_utils_test failed ; exit 1 ) $(E) "[RUN] Testing qps_openloop_test" $(Q) $(BINDIR)/$(CONFIG)/qps_openloop_test || ( echo test qps_openloop_test failed ; exit 1 ) - $(E) "[RUN] Testing reference_counted_ptr_test" - $(Q) $(BINDIR)/$(CONFIG)/reference_counted_ptr_test || ( echo test reference_counted_ptr_test failed ; exit 1 ) - $(E) "[RUN] Testing reference_counted_test" - $(Q) $(BINDIR)/$(CONFIG)/reference_counted_test || ( echo test reference_counted_test failed ; exit 1 ) + $(E) "[RUN] Testing ref_counted_ptr_test" + $(Q) $(BINDIR)/$(CONFIG)/ref_counted_ptr_test || ( echo test ref_counted_ptr_test failed ; exit 1 ) + $(E) "[RUN] Testing ref_counted_test" + $(Q) $(BINDIR)/$(CONFIG)/ref_counted_test || ( echo test ref_counted_test failed ; exit 1 ) $(E) "[RUN] Testing secure_auth_context_test" $(Q) $(BINDIR)/$(CONFIG)/secure_auth_context_test || ( echo test secure_auth_context_test failed ; exit 1 ) $(E) "[RUN] Testing secure_sync_unary_ping_pong_test" @@ -16505,15 +16505,15 @@ endif $(OBJDIR)/$(CONFIG)/test/cpp/interop/reconnect_interop_server.o: $(GENDIR)/src/proto/grpc/testing/empty.pb.cc $(GENDIR)/src/proto/grpc/testing/empty.grpc.pb.cc $(GENDIR)/src/proto/grpc/testing/messages.pb.cc $(GENDIR)/src/proto/grpc/testing/messages.grpc.pb.cc $(GENDIR)/src/proto/grpc/testing/test.pb.cc $(GENDIR)/src/proto/grpc/testing/test.grpc.pb.cc -REFERENCE_COUNTED_PTR_TEST_SRC = \ - test/core/support/reference_counted_ptr_test.cc \ +REF_COUNTED_PTR_TEST_SRC = \ + test/core/support/ref_counted_ptr_test.cc \ -REFERENCE_COUNTED_PTR_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(REFERENCE_COUNTED_PTR_TEST_SRC)))) +REF_COUNTED_PTR_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(REF_COUNTED_PTR_TEST_SRC)))) ifeq ($(NO_SECURE),true) # You can't build secure targets if you don't have OpenSSL. -$(BINDIR)/$(CONFIG)/reference_counted_ptr_test: openssl_dep_error +$(BINDIR)/$(CONFIG)/ref_counted_ptr_test: openssl_dep_error else @@ -16524,39 +16524,39 @@ ifeq ($(NO_PROTOBUF),true) # You can't build the protoc plugins or protobuf-enabled targets if you don't have protobuf 3.0.0+. -$(BINDIR)/$(CONFIG)/reference_counted_ptr_test: protobuf_dep_error +$(BINDIR)/$(CONFIG)/ref_counted_ptr_test: protobuf_dep_error else -$(BINDIR)/$(CONFIG)/reference_counted_ptr_test: $(PROTOBUF_DEP) $(REFERENCE_COUNTED_PTR_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a +$(BINDIR)/$(CONFIG)/ref_counted_ptr_test: $(PROTOBUF_DEP) $(REF_COUNTED_PTR_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(E) "[LD] Linking $@" $(Q) mkdir -p `dirname $@` - $(Q) $(LDXX) $(LDFLAGS) $(REFERENCE_COUNTED_PTR_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/reference_counted_ptr_test + $(Q) $(LDXX) $(LDFLAGS) $(REF_COUNTED_PTR_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/ref_counted_ptr_test endif endif -$(OBJDIR)/$(CONFIG)/test/core/support/reference_counted_ptr_test.o: $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a +$(OBJDIR)/$(CONFIG)/test/core/support/ref_counted_ptr_test.o: $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a -deps_reference_counted_ptr_test: $(REFERENCE_COUNTED_PTR_TEST_OBJS:.o=.dep) +deps_ref_counted_ptr_test: $(REF_COUNTED_PTR_TEST_OBJS:.o=.dep) ifneq ($(NO_SECURE),true) ifneq ($(NO_DEPS),true) --include $(REFERENCE_COUNTED_PTR_TEST_OBJS:.o=.dep) +-include $(REF_COUNTED_PTR_TEST_OBJS:.o=.dep) endif endif -REFERENCE_COUNTED_TEST_SRC = \ - test/core/support/reference_counted_test.cc \ +REF_COUNTED_TEST_SRC = \ + test/core/support/ref_counted_test.cc \ -REFERENCE_COUNTED_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(REFERENCE_COUNTED_TEST_SRC)))) +REF_COUNTED_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(REF_COUNTED_TEST_SRC)))) ifeq ($(NO_SECURE),true) # You can't build secure targets if you don't have OpenSSL. -$(BINDIR)/$(CONFIG)/reference_counted_test: openssl_dep_error +$(BINDIR)/$(CONFIG)/ref_counted_test: openssl_dep_error else @@ -16567,26 +16567,26 @@ ifeq ($(NO_PROTOBUF),true) # You can't build the protoc plugins or protobuf-enabled targets if you don't have protobuf 3.0.0+. -$(BINDIR)/$(CONFIG)/reference_counted_test: protobuf_dep_error +$(BINDIR)/$(CONFIG)/ref_counted_test: protobuf_dep_error else -$(BINDIR)/$(CONFIG)/reference_counted_test: $(PROTOBUF_DEP) $(REFERENCE_COUNTED_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a +$(BINDIR)/$(CONFIG)/ref_counted_test: $(PROTOBUF_DEP) $(REF_COUNTED_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(E) "[LD] Linking $@" $(Q) mkdir -p `dirname $@` - $(Q) $(LDXX) $(LDFLAGS) $(REFERENCE_COUNTED_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/reference_counted_test + $(Q) $(LDXX) $(LDFLAGS) $(REF_COUNTED_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/ref_counted_test endif endif -$(OBJDIR)/$(CONFIG)/test/core/support/reference_counted_test.o: $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a +$(OBJDIR)/$(CONFIG)/test/core/support/ref_counted_test.o: $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a -deps_reference_counted_test: $(REFERENCE_COUNTED_TEST_OBJS:.o=.dep) +deps_ref_counted_test: $(REF_COUNTED_TEST_OBJS:.o=.dep) ifneq ($(NO_SECURE),true) ifneq ($(NO_DEPS),true) --include $(REFERENCE_COUNTED_TEST_OBJS:.o=.dep) +-include $(REF_COUNTED_TEST_OBJS:.o=.dep) endif endif diff --git a/build.yaml b/build.yaml index c41bf193e6..bfb847e748 100644 --- a/build.yaml +++ b/build.yaml @@ -394,8 +394,8 @@ filegroups: - src/core/lib/slice/slice_internal.h - src/core/lib/slice/slice_string_helpers.h - src/core/lib/support/debug_location.h - - src/core/lib/support/reference_counted.h - - src/core/lib/support/reference_counted_ptr.h + - src/core/lib/support/ref_counted.h + - src/core/lib/support/ref_counted_ptr.h - src/core/lib/support/vector.h - src/core/lib/surface/alarm_internal.h - src/core/lib/surface/api_trace.h @@ -4544,12 +4544,12 @@ targets: - gpr_test_util - gpr - grpc++_test_config -- name: reference_counted_ptr_test +- name: ref_counted_ptr_test gtest: true build: test language: c++ src: - - test/core/support/reference_counted_ptr_test.cc + - test/core/support/ref_counted_ptr_test.cc deps: - grpc_test_util - grpc++ @@ -4558,12 +4558,12 @@ targets: - gpr uses: - grpc++_test -- name: reference_counted_test +- name: ref_counted_test gtest: true build: test language: c++ src: - - test/core/support/reference_counted_test.cc + - test/core/support/ref_counted_test.cc deps: - grpc_test_util - grpc++ diff --git a/gRPC-Core.podspec b/gRPC-Core.podspec index e90e356aaa..d6aa1fd6c0 100644 --- a/gRPC-Core.podspec +++ b/gRPC-Core.podspec @@ -414,8 +414,8 @@ Pod::Spec.new do |s| 'src/core/lib/slice/slice_internal.h', 'src/core/lib/slice/slice_string_helpers.h', 'src/core/lib/support/debug_location.h', - 'src/core/lib/support/reference_counted.h', - 'src/core/lib/support/reference_counted_ptr.h', + 'src/core/lib/support/ref_counted.h', + 'src/core/lib/support/ref_counted_ptr.h', 'src/core/lib/support/vector.h', 'src/core/lib/surface/alarm_internal.h', 'src/core/lib/surface/api_trace.h', @@ -892,8 +892,8 @@ Pod::Spec.new do |s| 'src/core/lib/slice/slice_internal.h', 'src/core/lib/slice/slice_string_helpers.h', 'src/core/lib/support/debug_location.h', - 'src/core/lib/support/reference_counted.h', - 'src/core/lib/support/reference_counted_ptr.h', + 'src/core/lib/support/ref_counted.h', + 'src/core/lib/support/ref_counted_ptr.h', 'src/core/lib/support/vector.h', 'src/core/lib/surface/alarm_internal.h', 'src/core/lib/surface/api_trace.h', diff --git a/grpc.gemspec b/grpc.gemspec index a58c73a59d..7c04356cc7 100644 --- a/grpc.gemspec +++ b/grpc.gemspec @@ -345,8 +345,8 @@ Gem::Specification.new do |s| s.files += %w( src/core/lib/slice/slice_internal.h ) s.files += %w( src/core/lib/slice/slice_string_helpers.h ) s.files += %w( src/core/lib/support/debug_location.h ) - s.files += %w( src/core/lib/support/reference_counted.h ) - s.files += %w( src/core/lib/support/reference_counted_ptr.h ) + s.files += %w( src/core/lib/support/ref_counted.h ) + s.files += %w( src/core/lib/support/ref_counted_ptr.h ) s.files += %w( src/core/lib/support/vector.h ) s.files += %w( src/core/lib/surface/alarm_internal.h ) s.files += %w( src/core/lib/surface/api_trace.h ) diff --git a/package.xml b/package.xml index 0036481bd1..68e37b6280 100644 --- a/package.xml +++ b/package.xml @@ -357,8 +357,8 @@ - - + + diff --git a/src/core/lib/support/ref_counted.h b/src/core/lib/support/ref_counted.h new file mode 100644 index 0000000000..4c662f9119 --- /dev/null +++ b/src/core/lib/support/ref_counted.h @@ -0,0 +1,122 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef GRPC_CORE_LIB_SUPPORT_REF_COUNTED_H +#define GRPC_CORE_LIB_SUPPORT_REF_COUNTED_H + +#include +#include + +#include "src/core/lib/debug/trace.h" +#include "src/core/lib/support/debug_location.h" +#include "src/core/lib/support/memory.h" + +namespace grpc_core { + +// A base class for reference-counted objects. +// New objects should be created via New() and start with a refcount of 1. +// When the refcount reaches 0, the object will be deleted via Delete(). +class RefCounted { + public: + void Ref() { gpr_ref(&refs_); } + + void Unref() { + if (gpr_unref(&refs_)) { + Delete(this); + } + } + + // Not copyable nor movable. + RefCounted(const RefCounted&) = delete; + RefCounted& operator=(const RefCounted&) = delete; + + protected: + // Allow Delete() to access destructor. + template + friend void Delete(T*); + + RefCounted() { gpr_ref_init(&refs_, 1); } + + virtual ~RefCounted() {} + + private: + gpr_refcount refs_; +}; + +// An alternative version of the RefCounted base class that +// supports tracing. This is intended to be used in cases where the +// object will be handled both by idiomatic C++ code using smart +// pointers and legacy code that is manually calling Ref() and Unref(). +// Once all of our code is converted to idiomatic C++, we may be able to +// eliminate this class. +class RefCountedWithTracing { + public: + void Ref() { gpr_ref(&refs_); } + + void Ref(const DebugLocation& location, const char* reason) { + if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) { + gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count); + gpr_log(GPR_DEBUG, "%s:%p %s:%d ref %" PRIdPTR " -> %" PRIdPTR " %s", + trace_flag_->name(), this, location.file(), location.line(), + old_refs, old_refs + 1, reason); + } + Ref(); + } + + void Unref() { + if (gpr_unref(&refs_)) { + Delete(this); + } + } + + void Unref(const DebugLocation& location, const char* reason) { + if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) { + gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count); + gpr_log(GPR_DEBUG, "%s:%p %s:%d unref %" PRIdPTR " -> %" PRIdPTR " %s", + trace_flag_->name(), this, location.file(), location.line(), + old_refs, old_refs - 1, reason); + } + Unref(); + } + + // Not copyable nor movable. + RefCountedWithTracing(const RefCountedWithTracing&) = delete; + RefCountedWithTracing& operator=(const RefCountedWithTracing&) = delete; + + protected: + // Allow Delete() to access destructor. + template + friend void Delete(T*); + + RefCountedWithTracing() : RefCountedWithTracing(nullptr) {} + + explicit RefCountedWithTracing(TraceFlag* trace_flag) + : trace_flag_(trace_flag) { + gpr_ref_init(&refs_, 1); + } + + virtual ~RefCountedWithTracing() {} + + private: + TraceFlag* trace_flag_ = nullptr; + gpr_refcount refs_; +}; + +} // namespace grpc_core + +#endif /* GRPC_CORE_LIB_SUPPORT_REF_COUNTED_H */ diff --git a/src/core/lib/support/ref_counted_ptr.h b/src/core/lib/support/ref_counted_ptr.h new file mode 100644 index 0000000000..dc2385e369 --- /dev/null +++ b/src/core/lib/support/ref_counted_ptr.h @@ -0,0 +1,90 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef GRPC_CORE_LIB_SUPPORT_REF_COUNTED_PTR_H +#define GRPC_CORE_LIB_SUPPORT_REF_COUNTED_PTR_H + +#include + +#include "src/core/lib/support/memory.h" + +namespace grpc_core { + +// A smart pointer class for objects that provide Ref() and Unref() methods, +// such as those provided by the RefCounted base class. +template +class RefCountedPtr { + public: + RefCountedPtr() {} + + // If value is non-null, we take ownership of a ref to it. + explicit RefCountedPtr(T* value) { value_ = value; } + + // Move support. + RefCountedPtr(RefCountedPtr&& other) { + value_ = other.value_; + other.value_ = nullptr; + } + RefCountedPtr& operator=(RefCountedPtr&& other) { + if (value_ != nullptr) value_->Unref(); + value_ = other.value_; + other.value_ = nullptr; + return *this; + } + + // Copy support. + RefCountedPtr(const RefCountedPtr& other) { + if (other.value_ != nullptr) other.value_->Ref(); + value_ = other.value_; + } + RefCountedPtr& operator=(const RefCountedPtr& other) { + // Note: Order of reffing and unreffing is important here in case value_ + // and other.value_ are the same object. + if (other.value_ != nullptr) other.value_->Ref(); + if (value_ != nullptr) value_->Unref(); + value_ = other.value_; + return *this; + } + + ~RefCountedPtr() { + if (value_ != nullptr) value_->Unref(); + } + + // If value is non-null, we take ownership of a ref to it. + void reset(T* value = nullptr) { + if (value_ != nullptr) value_->Unref(); + value_ = value; + } + + T* get() const { return value_; } + + T& operator*() const { return *value_; } + T* operator->() const { return value_; } + + private: + T* value_ = nullptr; +}; + +template +inline RefCountedPtr MakeRefCounted(Args&&... args) { + return RefCountedPtr(New(std::forward(args)...)); +} + +} // namespace grpc_core + +#endif /* GRPC_CORE_LIB_SUPPORT_REF_COUNTED_PTR_H */ diff --git a/src/core/lib/support/reference_counted.h b/src/core/lib/support/reference_counted.h deleted file mode 100644 index 3858d61b72..0000000000 --- a/src/core/lib/support/reference_counted.h +++ /dev/null @@ -1,123 +0,0 @@ -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H -#define GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H - -#include -#include - -#include "src/core/lib/debug/trace.h" -#include "src/core/lib/support/debug_location.h" -#include "src/core/lib/support/memory.h" - -namespace grpc_core { - -// A base class for reference-counted objects. -// New objects should be created via New() and start with a refcount of 1. -// When the refcount reaches 0, the object will be deleted via Delete(). -class ReferenceCounted { - public: - void Ref() { gpr_ref(&refs_); } - - void Unref() { - if (gpr_unref(&refs_)) { - Delete(this); - } - } - - // Not copyable nor movable. - ReferenceCounted(const ReferenceCounted&) = delete; - ReferenceCounted& operator=(const ReferenceCounted&) = delete; - - protected: - // Allow Delete() to access destructor. - template - friend void Delete(T*); - - ReferenceCounted() { gpr_ref_init(&refs_, 1); } - - virtual ~ReferenceCounted() {} - - private: - gpr_refcount refs_; -}; - -// An alternative version of the ReferenceCounted base class that -// supports tracing. This is intended to be used in cases where the -// object will be handled both by idiomatic C++ code using smart -// pointers and legacy code that is manually calling Ref() and Unref(). -// Once all of our code is converted to idiomatic C++, we may be able to -// eliminate this class. -class ReferenceCountedWithTracing { - public: - void Ref() { gpr_ref(&refs_); } - - void Ref(const DebugLocation& location, const char* reason) { - if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) { - gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count); - gpr_log(GPR_DEBUG, "%s:%p %s:%d ref %" PRIdPTR " -> %" PRIdPTR " %s", - trace_flag_->name(), this, location.file(), location.line(), - old_refs, old_refs + 1, reason); - } - Ref(); - } - - void Unref() { - if (gpr_unref(&refs_)) { - Delete(this); - } - } - - void Unref(const DebugLocation& location, const char* reason) { - if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) { - gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count); - gpr_log(GPR_DEBUG, "%s:%p %s:%d unref %" PRIdPTR " -> %" PRIdPTR " %s", - trace_flag_->name(), this, location.file(), location.line(), - old_refs, old_refs - 1, reason); - } - Unref(); - } - - // Not copyable nor movable. - ReferenceCountedWithTracing(const ReferenceCountedWithTracing&) = delete; - ReferenceCountedWithTracing& operator=(const ReferenceCountedWithTracing&) = - delete; - - protected: - // Allow Delete() to access destructor. - template - friend void Delete(T*); - - ReferenceCountedWithTracing() : ReferenceCountedWithTracing(nullptr) {} - - explicit ReferenceCountedWithTracing(TraceFlag* trace_flag) - : trace_flag_(trace_flag) { - gpr_ref_init(&refs_, 1); - } - - virtual ~ReferenceCountedWithTracing() {} - - private: - TraceFlag* trace_flag_ = nullptr; - gpr_refcount refs_; -}; - -} // namespace grpc_core - -#endif /* GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H */ diff --git a/src/core/lib/support/reference_counted_ptr.h b/src/core/lib/support/reference_counted_ptr.h deleted file mode 100644 index 1590549bfb..0000000000 --- a/src/core/lib/support/reference_counted_ptr.h +++ /dev/null @@ -1,90 +0,0 @@ -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_PTR_H -#define GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_PTR_H - -#include - -#include "src/core/lib/support/memory.h" - -namespace grpc_core { - -// A smart pointer class for objects that provide Ref() and Unref() methods, -// such as those provided by the ReferenceCounted base class. -template -class ReferenceCountedPtr { - public: - ReferenceCountedPtr() {} - - // If value is non-null, we take ownership of a ref to it. - explicit ReferenceCountedPtr(T* value) { value_ = value; } - - // Move support. - ReferenceCountedPtr(ReferenceCountedPtr&& other) { - value_ = other.value_; - other.value_ = nullptr; - } - ReferenceCountedPtr& operator=(ReferenceCountedPtr&& other) { - if (value_ != nullptr) value_->Unref(); - value_ = other.value_; - other.value_ = nullptr; - return *this; - } - - // Copy support. - ReferenceCountedPtr(const ReferenceCountedPtr& other) { - if (other.value_ != nullptr) other.value_->Ref(); - value_ = other.value_; - } - ReferenceCountedPtr& operator=(const ReferenceCountedPtr& other) { - // Note: Order of reffing and unreffing is important here in case value_ - // and other.value_ are the same object. - if (other.value_ != nullptr) other.value_->Ref(); - if (value_ != nullptr) value_->Unref(); - value_ = other.value_; - return *this; - } - - ~ReferenceCountedPtr() { - if (value_ != nullptr) value_->Unref(); - } - - // If value is non-null, we take ownership of a ref to it. - void reset(T* value = nullptr) { - if (value_ != nullptr) value_->Unref(); - value_ = value; - } - - T* get() const { return value_; } - - T& operator*() const { return *value_; } - T* operator->() const { return value_; } - - private: - T* value_ = nullptr; -}; - -template -inline ReferenceCountedPtr MakeReferenceCounted(Args&&... args) { - return ReferenceCountedPtr(New(std::forward(args)...)); -} - -} // namespace grpc_core - -#endif /* GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_PTR_H */ diff --git a/test/core/support/BUILD b/test/core/support/BUILD index 1a8d2a1359..0e5a31c7d2 100644 --- a/test/core/support/BUILD +++ b/test/core/support/BUILD @@ -235,11 +235,11 @@ grpc_cc_test( ) grpc_cc_test( - name = "reference_counted_test", - srcs = ["reference_counted_test.cc"], + name = "ref_counted_test", + srcs = ["ref_counted_test.cc"], language = "C++", deps = [ - "//:reference_counted", + "//:ref_counted", "//test/core/util:gpr_test_util", ], external_deps = [ @@ -248,12 +248,12 @@ grpc_cc_test( ) grpc_cc_test( - name = "reference_counted_ptr_test", - srcs = ["reference_counted_ptr_test.cc"], + name = "ref_counted_ptr_test", + srcs = ["ref_counted_ptr_test.cc"], language = "C++", deps = [ - "//:reference_counted", - "//:reference_counted_ptr", + "//:ref_counted", + "//:ref_counted_ptr", "//test/core/util:gpr_test_util", ], external_deps = [ diff --git a/test/core/support/ref_counted_ptr_test.cc b/test/core/support/ref_counted_ptr_test.cc new file mode 100644 index 0000000000..fdf5e0e8d6 --- /dev/null +++ b/test/core/support/ref_counted_ptr_test.cc @@ -0,0 +1,174 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "src/core/lib/support/ref_counted_ptr.h" + +#include + +#include + +#include "src/core/lib/support/memory.h" +#include "src/core/lib/support/ref_counted.h" +#include "test/core/util/test_config.h" + +namespace grpc_core { +namespace testing { +namespace { + +class Foo : public RefCounted { + public: + Foo() : value_(0) {} + + explicit Foo(int value) : value_(value) {} + + int value() const { return value_; } + + private: + int value_; +}; + +TEST(RefCountedPtr, DefaultConstructor) { RefCountedPtr foo; } + +TEST(RefCountedPtr, ExplicitConstructorEmpty) { + RefCountedPtr foo(nullptr); +} + +TEST(RefCountedPtr, ExplicitConstructor) { + RefCountedPtr foo(New()); +} + +TEST(RefCountedPtr, MoveConstructor) { + RefCountedPtr foo(New()); + RefCountedPtr foo2(std::move(foo)); + EXPECT_EQ(nullptr, foo.get()); + EXPECT_NE(nullptr, foo2.get()); +} + +TEST(RefCountedPtr, MoveAssignment) { + RefCountedPtr foo(New()); + RefCountedPtr foo2 = std::move(foo); + EXPECT_EQ(nullptr, foo.get()); + EXPECT_NE(nullptr, foo2.get()); +} + +TEST(RefCountedPtr, CopyConstructor) { + RefCountedPtr foo(New()); + RefCountedPtr foo2(foo); + EXPECT_NE(nullptr, foo.get()); + EXPECT_EQ(foo.get(), foo2.get()); +} + +TEST(RefCountedPtr, CopyAssignment) { + RefCountedPtr foo(New()); + RefCountedPtr foo2 = foo; + EXPECT_NE(nullptr, foo.get()); + EXPECT_EQ(foo.get(), foo2.get()); +} + +TEST(RefCountedPtr, CopyAssignmentWhenEmpty) { + RefCountedPtr foo; + RefCountedPtr foo2; + foo2 = foo; + EXPECT_EQ(nullptr, foo.get()); + EXPECT_EQ(nullptr, foo2.get()); +} + +TEST(RefCountedPtr, CopyAssignmentToSelf) { + RefCountedPtr foo(New()); + foo = foo; +} + +TEST(RefCountedPtr, EnclosedScope) { + RefCountedPtr foo(New()); + { + RefCountedPtr foo2(std::move(foo)); + EXPECT_EQ(nullptr, foo.get()); + EXPECT_NE(nullptr, foo2.get()); + } + EXPECT_EQ(nullptr, foo.get()); +} + +TEST(RefCountedPtr, ResetFromNullToNonNull) { + RefCountedPtr foo; + EXPECT_EQ(nullptr, foo.get()); + foo.reset(New()); + EXPECT_NE(nullptr, foo.get()); +} + +TEST(RefCountedPtr, ResetFromNonNullToNonNull) { + RefCountedPtr foo(New()); + EXPECT_NE(nullptr, foo.get()); + Foo* original = foo.get(); + foo.reset(New()); + EXPECT_NE(nullptr, foo.get()); + EXPECT_NE(original, foo.get()); +} + +TEST(RefCountedPtr, ResetFromNonNullToNull) { + RefCountedPtr foo(New()); + EXPECT_NE(nullptr, foo.get()); + foo.reset(); + EXPECT_EQ(nullptr, foo.get()); +} + +TEST(RefCountedPtr, ResetFromNullToNull) { + RefCountedPtr foo; + EXPECT_EQ(nullptr, foo.get()); + foo.reset(nullptr); + EXPECT_EQ(nullptr, foo.get()); +} + +TEST(RefCountedPtr, DerefernceOperators) { + RefCountedPtr foo(New()); + foo->value(); + Foo& foo_ref = *foo; + foo_ref.value(); +} + +TEST(MakeRefCounted, NoArgs) { + RefCountedPtr foo = MakeRefCounted(); + EXPECT_EQ(0, foo->value()); +} + +TEST(MakeRefCounted, Args) { + RefCountedPtr foo = MakeRefCounted(3); + EXPECT_EQ(3, foo->value()); +} + +TraceFlag foo_tracer(true, "foo"); + +class FooWithTracing : public RefCountedWithTracing { + public: + FooWithTracing() : RefCountedWithTracing(&foo_tracer) {} +}; + +TEST(RefCountedPtr, RefCountedWithTracing) { + RefCountedPtr foo(New()); + foo->Ref(DEBUG_LOCATION, "foo"); + foo->Unref(DEBUG_LOCATION, "foo"); +} + +} // namespace +} // namespace testing +} // namespace grpc_core + +int main(int argc, char** argv) { + grpc_test_init(argc, argv); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/test/core/support/ref_counted_test.cc b/test/core/support/ref_counted_test.cc new file mode 100644 index 0000000000..be9b6ff7c2 --- /dev/null +++ b/test/core/support/ref_counted_test.cc @@ -0,0 +1,72 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "src/core/lib/support/ref_counted.h" + +#include + +#include "src/core/lib/support/memory.h" +#include "test/core/util/test_config.h" + +namespace grpc_core { +namespace testing { +namespace { + +class Foo : public RefCounted { + public: + Foo() {} +}; + +TEST(RefCounted, Basic) { + Foo* foo = New(); + foo->Unref(); +} + +TEST(RefCounted, ExtraRef) { + Foo* foo = New(); + foo->Ref(); + foo->Unref(); + foo->Unref(); +} + +TraceFlag foo_tracer(true, "foo"); + +class FooWithTracing : public RefCountedWithTracing { + public: + FooWithTracing() : RefCountedWithTracing(&foo_tracer) {} +}; + +TEST(RefCountedWithTracing, Basic) { + FooWithTracing* foo = New(); + foo->Ref(DEBUG_LOCATION, "extra_ref"); + foo->Unref(DEBUG_LOCATION, "extra_ref"); + // Can use the no-argument methods, too. + foo->Ref(); + foo->Unref(); + foo->Unref(DEBUG_LOCATION, "original_ref"); +} + +} // namespace +} // namespace testing +} // namespace grpc_core + +int main(int argc, char** argv) { + grpc_test_init(argc, argv); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/test/core/support/reference_counted_ptr_test.cc b/test/core/support/reference_counted_ptr_test.cc deleted file mode 100644 index 45c552a1bc..0000000000 --- a/test/core/support/reference_counted_ptr_test.cc +++ /dev/null @@ -1,174 +0,0 @@ -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "src/core/lib/support/reference_counted_ptr.h" - -#include - -#include - -#include "src/core/lib/support/memory.h" -#include "src/core/lib/support/reference_counted.h" -#include "test/core/util/test_config.h" - -namespace grpc_core { -namespace testing { -namespace { - -class Foo : public ReferenceCounted { - public: - Foo() : value_(0) {} - - explicit Foo(int value) : value_(value) {} - - int value() const { return value_; } - - private: - int value_; -}; - -TEST(ReferenceCountedPtr, DefaultConstructor) { ReferenceCountedPtr foo; } - -TEST(ReferenceCountedPtr, ExplicitConstructorEmpty) { - ReferenceCountedPtr foo(nullptr); -} - -TEST(ReferenceCountedPtr, ExplicitConstructor) { - ReferenceCountedPtr foo(New()); -} - -TEST(ReferenceCountedPtr, MoveConstructor) { - ReferenceCountedPtr foo(New()); - ReferenceCountedPtr foo2(std::move(foo)); - EXPECT_EQ(nullptr, foo.get()); - EXPECT_NE(nullptr, foo2.get()); -} - -TEST(ReferenceCountedPtr, MoveAssignment) { - ReferenceCountedPtr foo(New()); - ReferenceCountedPtr foo2 = std::move(foo); - EXPECT_EQ(nullptr, foo.get()); - EXPECT_NE(nullptr, foo2.get()); -} - -TEST(ReferenceCountedPtr, CopyConstructor) { - ReferenceCountedPtr foo(New()); - ReferenceCountedPtr foo2(foo); - EXPECT_NE(nullptr, foo.get()); - EXPECT_EQ(foo.get(), foo2.get()); -} - -TEST(ReferenceCountedPtr, CopyAssignment) { - ReferenceCountedPtr foo(New()); - ReferenceCountedPtr foo2 = foo; - EXPECT_NE(nullptr, foo.get()); - EXPECT_EQ(foo.get(), foo2.get()); -} - -TEST(ReferenceCountedPtr, CopyAssignmentWhenEmpty) { - ReferenceCountedPtr foo; - ReferenceCountedPtr foo2; - foo2 = foo; - EXPECT_EQ(nullptr, foo.get()); - EXPECT_EQ(nullptr, foo2.get()); -} - -TEST(ReferenceCountedPtr, CopyAssignmentToSelf) { - ReferenceCountedPtr foo(New()); - foo = foo; -} - -TEST(ReferenceCountedPtr, EnclosedScope) { - ReferenceCountedPtr foo(New()); - { - ReferenceCountedPtr foo2(std::move(foo)); - EXPECT_EQ(nullptr, foo.get()); - EXPECT_NE(nullptr, foo2.get()); - } - EXPECT_EQ(nullptr, foo.get()); -} - -TEST(ReferenceCountedPtr, ResetFromNullToNonNull) { - ReferenceCountedPtr foo; - EXPECT_EQ(nullptr, foo.get()); - foo.reset(New()); - EXPECT_NE(nullptr, foo.get()); -} - -TEST(ReferenceCountedPtr, ResetFromNonNullToNonNull) { - ReferenceCountedPtr foo(New()); - EXPECT_NE(nullptr, foo.get()); - Foo* original = foo.get(); - foo.reset(New()); - EXPECT_NE(nullptr, foo.get()); - EXPECT_NE(original, foo.get()); -} - -TEST(ReferenceCountedPtr, ResetFromNonNullToNull) { - ReferenceCountedPtr foo(New()); - EXPECT_NE(nullptr, foo.get()); - foo.reset(); - EXPECT_EQ(nullptr, foo.get()); -} - -TEST(ReferenceCountedPtr, ResetFromNullToNull) { - ReferenceCountedPtr foo; - EXPECT_EQ(nullptr, foo.get()); - foo.reset(nullptr); - EXPECT_EQ(nullptr, foo.get()); -} - -TEST(ReferenceCountedPtr, DerefernceOperators) { - ReferenceCountedPtr foo(New()); - foo->value(); - Foo& foo_ref = *foo; - foo_ref.value(); -} - -TEST(MakeReferenceCounted, NoArgs) { - ReferenceCountedPtr foo = MakeReferenceCounted(); - EXPECT_EQ(0, foo->value()); -} - -TEST(MakeReferenceCounted, Args) { - ReferenceCountedPtr foo = MakeReferenceCounted(3); - EXPECT_EQ(3, foo->value()); -} - -TraceFlag foo_tracer(true, "foo"); - -class FooWithTracing : public ReferenceCountedWithTracing { - public: - FooWithTracing() : ReferenceCountedWithTracing(&foo_tracer) {} -}; - -TEST(ReferenceCountedPtr, ReferenceCountedWithTracing) { - ReferenceCountedPtr foo(New()); - foo->Ref(DEBUG_LOCATION, "foo"); - foo->Unref(DEBUG_LOCATION, "foo"); -} - -} // namespace -} // namespace testing -} // namespace grpc_core - -int main(int argc, char** argv) { - grpc_test_init(argc, argv); - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/test/core/support/reference_counted_test.cc b/test/core/support/reference_counted_test.cc deleted file mode 100644 index 88ee5d8e55..0000000000 --- a/test/core/support/reference_counted_test.cc +++ /dev/null @@ -1,72 +0,0 @@ -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "src/core/lib/support/reference_counted.h" - -#include - -#include "src/core/lib/support/memory.h" -#include "test/core/util/test_config.h" - -namespace grpc_core { -namespace testing { -namespace { - -class Foo : public ReferenceCounted { - public: - Foo() {} -}; - -TEST(ReferenceCounted, Basic) { - Foo* foo = New(); - foo->Unref(); -} - -TEST(ReferenceCounted, ExtraRef) { - Foo* foo = New(); - foo->Ref(); - foo->Unref(); - foo->Unref(); -} - -TraceFlag foo_tracer(true, "foo"); - -class FooWithTracing : public ReferenceCountedWithTracing { - public: - FooWithTracing() : ReferenceCountedWithTracing(&foo_tracer) {} -}; - -TEST(ReferenceCountedWithTracing, Basic) { - FooWithTracing* foo = New(); - foo->Ref(DEBUG_LOCATION, "extra_ref"); - foo->Unref(DEBUG_LOCATION, "extra_ref"); - // Can use the no-argument methods, too. - foo->Ref(); - foo->Unref(); - foo->Unref(DEBUG_LOCATION, "original_ref"); -} - -} // namespace -} // namespace testing -} // namespace grpc_core - -int main(int argc, char** argv) { - grpc_test_init(argc, argv); - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index 078dd7bb87..a7979f87a8 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -1037,8 +1037,8 @@ src/core/lib/support/manual_constructor.h \ src/core/lib/support/memory.h \ src/core/lib/support/mpscq.h \ src/core/lib/support/murmur_hash.h \ -src/core/lib/support/reference_counted.h \ -src/core/lib/support/reference_counted_ptr.h \ +src/core/lib/support/ref_counted.h \ +src/core/lib/support/ref_counted_ptr.h \ src/core/lib/support/spinlock.h \ src/core/lib/support/stack_lockfree.h \ src/core/lib/support/string.h \ diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal index 8343f88a37..827b3382ab 100644 --- a/tools/doxygen/Doxyfile.core.internal +++ b/tools/doxygen/Doxyfile.core.internal @@ -1299,8 +1299,8 @@ src/core/lib/support/mpscq.cc \ src/core/lib/support/mpscq.h \ src/core/lib/support/murmur_hash.cc \ src/core/lib/support/murmur_hash.h \ -src/core/lib/support/reference_counted.h \ -src/core/lib/support/reference_counted_ptr.h \ +src/core/lib/support/ref_counted.h \ +src/core/lib/support/ref_counted_ptr.h \ src/core/lib/support/spinlock.h \ src/core/lib/support/stack_lockfree.cc \ src/core/lib/support/stack_lockfree.h \ diff --git a/tools/run_tests/generated/sources_and_headers.json b/tools/run_tests/generated/sources_and_headers.json index 091aa17141..9e70297825 100644 --- a/tools/run_tests/generated/sources_and_headers.json +++ b/tools/run_tests/generated/sources_and_headers.json @@ -3929,9 +3929,9 @@ "headers": [], "is_filegroup": false, "language": "c++", - "name": "reference_counted_ptr_test", + "name": "ref_counted_ptr_test", "src": [ - "test/core/support/reference_counted_ptr_test.cc" + "test/core/support/ref_counted_ptr_test.cc" ], "third_party": false, "type": "target" @@ -3948,9 +3948,9 @@ "headers": [], "is_filegroup": false, "language": "c++", - "name": "reference_counted_test", + "name": "ref_counted_test", "src": [ - "test/core/support/reference_counted_test.cc" + "test/core/support/ref_counted_test.cc" ], "third_party": false, "type": "target" @@ -8259,8 +8259,8 @@ "src/core/lib/slice/slice_internal.h", "src/core/lib/slice/slice_string_helpers.h", "src/core/lib/support/debug_location.h", - "src/core/lib/support/reference_counted.h", - "src/core/lib/support/reference_counted_ptr.h", + "src/core/lib/support/ref_counted.h", + "src/core/lib/support/ref_counted_ptr.h", "src/core/lib/support/vector.h", "src/core/lib/surface/alarm_internal.h", "src/core/lib/surface/api_trace.h", @@ -8398,8 +8398,8 @@ "src/core/lib/slice/slice_internal.h", "src/core/lib/slice/slice_string_helpers.h", "src/core/lib/support/debug_location.h", - "src/core/lib/support/reference_counted.h", - "src/core/lib/support/reference_counted_ptr.h", + "src/core/lib/support/ref_counted.h", + "src/core/lib/support/ref_counted_ptr.h", "src/core/lib/support/vector.h", "src/core/lib/surface/alarm_internal.h", "src/core/lib/surface/api_trace.h", diff --git a/tools/run_tests/generated/tests.json b/tools/run_tests/generated/tests.json index ba70d56085..0a49f3adf9 100644 --- a/tools/run_tests/generated/tests.json +++ b/tools/run_tests/generated/tests.json @@ -4138,7 +4138,7 @@ "flaky": false, "gtest": true, "language": "c++", - "name": "reference_counted_ptr_test", + "name": "ref_counted_ptr_test", "platforms": [ "linux", "mac", @@ -4162,7 +4162,7 @@ "flaky": false, "gtest": true, "language": "c++", - "name": "reference_counted_test", + "name": "ref_counted_test", "platforms": [ "linux", "mac", -- cgit v1.2.3