diff --git a/BUILD b/BUILD index 5289023ef5a..9c93a5c0990 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 ca59c84e500..7e18465c926 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 0e8437a05cd..848182d1bd1 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 2903b866dcf..9ac86eb430b 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 71e13633ae8..f623f26831e 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 900f4afe7a4..da1b6c0aeff 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 c0d9b498286..92eab40b87b 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 00000000000..48a763ebe0f --- /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 ee20ef86925..1a8d2a13592 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 00000000000..7347ef5e4b8 --- /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 1d84dcba336..078dd7bb87e 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 5b289e7bc4d..37ffff6b79d 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 8ec96198748..ed5128d9c12 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 a6b58d8ba65..ba70d56085a 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,