Add ReferenceCountedPtr class.

pull/13357/head
Mark D. Roth 7 years ago
parent abadc6c516
commit cf9ca843eb
  1. 6
      BUILD
  2. 41
      CMakeLists.txt
  3. 48
      Makefile
  4. 15
      build.yaml
  5. 2
      gRPC-Core.podspec
  6. 1
      grpc.gemspec
  7. 1
      package.xml
  8. 81
      src/core/lib/support/reference_counted_ptr.h
  9. 14
      test/core/support/BUILD
  10. 135
      test/core/support/reference_counted_ptr_test.cc
  11. 1
      tools/doxygen/Doxyfile.c++.internal
  12. 1
      tools/doxygen/Doxyfile.core.internal
  13. 21
      tools/run_tests/generated/sources_and_headers.json
  14. 24
      tools/run_tests/generated/tests.json

@ -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 = [

@ -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

@ -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 \

@ -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

@ -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',

@ -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 )

@ -108,6 +108,7 @@
<file baseinstalldir="/" name="src/core/lib/support/mpscq.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/murmur_hash.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/reference_counted.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/reference_counted_ptr.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/spinlock.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/stack_lockfree.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/string.h" role="src" />

@ -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<typename T>
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

@ -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",
],
)

@ -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 <gtest/gtest.h>
#include <grpc/support/log.h>
#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> foo;
}
TEST(ReferenceCountedPtr, ExplicitConstructorEmpty) {
ReferenceCountedPtr<Foo> foo(nullptr);
}
TEST(ReferenceCountedPtr, ExplicitConstructor) {
ReferenceCountedPtr<Foo> foo(New<Foo>());
}
TEST(ReferenceCountedPtr, MoveConstructor) {
ReferenceCountedPtr<Foo> foo(New<Foo>());
ReferenceCountedPtr<Foo> foo2(std::move(foo));
EXPECT_EQ(nullptr, foo.get());
EXPECT_NE(nullptr, foo2.get());
}
TEST(ReferenceCountedPtr, MoveAssignment) {
ReferenceCountedPtr<Foo> foo(New<Foo>());
ReferenceCountedPtr<Foo> foo2 = std::move(foo);
EXPECT_EQ(nullptr, foo.get());
EXPECT_NE(nullptr, foo2.get());
}
TEST(ReferenceCountedPtr, CopyConstructor) {
ReferenceCountedPtr<Foo> foo(New<Foo>());
ReferenceCountedPtr<Foo> foo2(foo);
EXPECT_NE(nullptr, foo.get());
EXPECT_EQ(foo.get(), foo2.get());
}
TEST(ReferenceCountedPtr, CopyAssignment) {
ReferenceCountedPtr<Foo> foo(New<Foo>());
ReferenceCountedPtr<Foo> foo2 = foo;
EXPECT_NE(nullptr, foo.get());
EXPECT_EQ(foo.get(), foo2.get());
}
TEST(ReferenceCountedPtr, EnclosedScope) {
ReferenceCountedPtr<Foo> foo(New<Foo>());
{
ReferenceCountedPtr<Foo> foo2(std::move(foo));
EXPECT_EQ(nullptr, foo.get());
EXPECT_NE(nullptr, foo2.get());
}
EXPECT_EQ(nullptr, foo.get());
}
TEST(ReferenceCountedPtr, ResetFromNullToNonNull) {
ReferenceCountedPtr<Foo> foo;
EXPECT_EQ(nullptr, foo.get());
foo.reset(New<Foo>());
EXPECT_NE(nullptr, foo.get());
}
TEST(ReferenceCountedPtr, ResetFromNonNullToNonNull) {
ReferenceCountedPtr<Foo> foo(New<Foo>());
EXPECT_NE(nullptr, foo.get());
Foo* original = foo.get();
foo.reset(New<Foo>());
EXPECT_NE(nullptr, foo.get());
EXPECT_NE(original, foo.get());
}
TEST(ReferenceCountedPtr, ResetFromNonNullToNull) {
ReferenceCountedPtr<Foo> foo(New<Foo>());
EXPECT_NE(nullptr, foo.get());
foo.reset();
EXPECT_EQ(nullptr, foo.get());
}
TEST(ReferenceCountedPtr, ResetFromNullToNull) {
ReferenceCountedPtr<Foo> foo;
EXPECT_EQ(nullptr, foo.get());
foo.reset(nullptr);
EXPECT_EQ(nullptr, foo.get());
}
TEST(ReferenceCountedPtr, DerefernceOperators) {
ReferenceCountedPtr<Foo> foo(New<Foo>());
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();
}

@ -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 \

@ -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 \

@ -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",

@ -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,

Loading…
Cancel
Save