Merge pull request #13357 from markdroth/reference_counted

Add ReferenceCounted base class.
pull/13555/merge
Mark D. Roth 7 years ago committed by GitHub
commit 5f662537de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      BUILD
  2. 80
      CMakeLists.txt
  3. 96
      Makefile
  4. 31
      build.yaml
  5. 6
      gRPC-Core.podspec
  6. 3
      grpc.gemspec
  7. 3
      package.xml
  8. 52
      src/core/lib/support/debug_location.h
  9. 122
      src/core/lib/support/ref_counted.h
  10. 90
      src/core/lib/support/ref_counted_ptr.h
  11. 27
      test/core/support/BUILD
  12. 172
      test/core/support/ref_counted_ptr_test.cc
  13. 72
      test/core/support/ref_counted_test.cc
  14. 3
      tools/doxygen/Doxyfile.c++.internal
  15. 3
      tools/doxygen/Doxyfile.core.internal
  16. 44
      tools/run_tests/generated/sources_and_headers.json
  17. 48
      tools/run_tests/generated/tests.json

22
BUILD

@ -538,6 +538,28 @@ grpc_cc_library(
],
)
grpc_cc_library(
name = "debug_location",
public_hdrs = ["src/core/lib/support/debug_location.h"],
language = "c++",
)
grpc_cc_library(
name = "ref_counted",
public_hdrs = ["src/core/lib/support/ref_counted.h"],
language = "c++",
deps = [
"grpc_trace",
"debug_location",
],
)
grpc_cc_library(
name = "ref_counted_ptr",
public_hdrs = ["src/core/lib/support/ref_counted_ptr.h"],
language = "c++",
)
grpc_cc_library(
name = "grpc_base_c",
srcs = [

@ -738,6 +738,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 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)
@ -12018,6 +12020,84 @@ target_link_libraries(reconnect_interop_server
endif (gRPC_BUILD_TESTS)
if (gRPC_BUILD_TESTS)
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(ref_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 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(ref_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(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(ref_counted_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 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(ref_counted_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(secure_auth_context_test
test/cpp/common/secure_auth_context_test.cc
third_party/googletest/googletest/src/gtest-all.cc

@ -1164,6 +1164,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
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
@ -1598,6 +1600,8 @@ buildtests_cxx: privatelibs_cxx \
$(BINDIR)/$(CONFIG)/qps_worker \
$(BINDIR)/$(CONFIG)/reconnect_interop_client \
$(BINDIR)/$(CONFIG)/reconnect_interop_server \
$(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 \
@ -1723,6 +1727,8 @@ buildtests_cxx: privatelibs_cxx \
$(BINDIR)/$(CONFIG)/qps_worker \
$(BINDIR)/$(CONFIG)/reconnect_interop_client \
$(BINDIR)/$(CONFIG)/reconnect_interop_server \
$(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 \
@ -2123,6 +2129,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 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"
@ -16482,6 +16492,92 @@ 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
REF_COUNTED_PTR_TEST_SRC = \
test/core/support/ref_counted_ptr_test.cc \
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)/ref_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)/ref_counted_ptr_test: protobuf_dep_error
else
$(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) $(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/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_ref_counted_ptr_test: $(REF_COUNTED_PTR_TEST_OBJS:.o=.dep)
ifneq ($(NO_SECURE),true)
ifneq ($(NO_DEPS),true)
-include $(REF_COUNTED_PTR_TEST_OBJS:.o=.dep)
endif
endif
REF_COUNTED_TEST_SRC = \
test/core/support/ref_counted_test.cc \
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)/ref_counted_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)/ref_counted_test: protobuf_dep_error
else
$(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) $(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/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_ref_counted_test: $(REF_COUNTED_TEST_OBJS:.o=.dep)
ifneq ($(NO_SECURE),true)
ifneq ($(NO_DEPS),true)
-include $(REF_COUNTED_TEST_OBJS:.o=.dep)
endif
endif
SECURE_AUTH_CONTEXT_TEST_SRC = \
test/cpp/common/secure_auth_context_test.cc \

@ -398,6 +398,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/ref_counted.h
- src/core/lib/support/ref_counted_ptr.h
- src/core/lib/surface/alarm_internal.h
- src/core/lib/surface/api_trace.h
- src/core/lib/surface/call.h
@ -4535,6 +4538,34 @@ targets:
- gpr_test_util
- gpr
- grpc++_test_config
- name: ref_counted_ptr_test
gtest: true
build: test
language: c++
src:
- test/core/support/ref_counted_ptr_test.cc
deps:
- grpc_test_util
- grpc++
- grpc
- gpr_test_util
- gpr
uses:
- grpc++_test
- name: ref_counted_test
gtest: true
build: test
language: c++
src:
- test/core/support/ref_counted_test.cc
deps:
- grpc_test_util
- grpc++
- grpc
- gpr_test_util
- gpr
uses:
- grpc++_test
- name: secure_auth_context_test
gtest: true
build: test

@ -418,6 +418,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/ref_counted.h',
'src/core/lib/support/ref_counted_ptr.h',
'src/core/lib/surface/alarm_internal.h',
'src/core/lib/surface/api_trace.h',
'src/core/lib/surface/call.h',
@ -895,6 +898,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/ref_counted.h',
'src/core/lib/support/ref_counted_ptr.h',
'src/core/lib/surface/alarm_internal.h',
'src/core/lib/surface/api_trace.h',
'src/core/lib/surface/call.h',

@ -348,6 +348,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/ref_counted.h )
s.files += %w( src/core/lib/support/ref_counted_ptr.h )
s.files += %w( src/core/lib/surface/alarm_internal.h )
s.files += %w( src/core/lib/surface/api_trace.h )
s.files += %w( src/core/lib/surface/call.h )

@ -360,6 +360,9 @@
<file baseinstalldir="/" name="src/core/lib/slice/slice_hash_table.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/slice/slice_internal.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/slice/slice_string_helpers.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/debug_location.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/ref_counted.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/ref_counted_ptr.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/surface/alarm_internal.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/surface/api_trace.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/surface/call.h" role="src" />

@ -0,0 +1,52 @@
/*
*
* 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_DEBUG_LOCATION_H
#define GRPC_CORE_LIB_SUPPORT_DEBUG_LOCATION_H
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:
DebugLocation(const char* file, int line) : file_(file), line_(line) {}
bool Log() const { return true; }
const char* file() const { return file_; }
int line() const { return line_; }
private:
const char* file_;
const int line_;
};
#define DEBUG_LOCATION DebugLocation(__FILE__, __LINE__)
#else
class DebugLocation {
public:
bool Log() const { return false; }
const char* file() const { return nullptr; }
int line() const { return -1; }
};
#define DEBUG_LOCATION DebugLocation()
#endif
} // namespace grpc_core
#endif /* GRPC_CORE_LIB_SUPPORT_DEBUG_LOCATION_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 <grpc/support/log.h>
#include <grpc/support/sync.h>
#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 <typename T>
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 <typename T>
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 */

@ -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 <utility>
#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 <typename T>
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 <typename T, typename... Args>
inline RefCountedPtr<T> MakeRefCounted(Args&&... args) {
return RefCountedPtr<T>(New<T>(std::forward<Args>(args)...));
}
} // namespace grpc_core
#endif /* GRPC_CORE_LIB_SUPPORT_REF_COUNTED_PTR_H */

@ -223,3 +223,30 @@ grpc_cc_test(
"//test/core/util:gpr_test_util",
],
)
grpc_cc_test(
name = "ref_counted_test",
srcs = ["ref_counted_test.cc"],
language = "C++",
deps = [
"//:ref_counted",
"//test/core/util:gpr_test_util",
],
external_deps = [
"gtest",
],
)
grpc_cc_test(
name = "ref_counted_ptr_test",
srcs = ["ref_counted_ptr_test.cc"],
language = "C++",
deps = [
"//:ref_counted",
"//:ref_counted_ptr",
"//test/core/util:gpr_test_util",
],
external_deps = [
"gtest",
],
)

@ -0,0 +1,172 @@
/*
*
* 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 <gtest/gtest.h>
#include <grpc/support/log.h>
#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> foo; }
TEST(RefCountedPtr, ExplicitConstructorEmpty) {
RefCountedPtr<Foo> foo(nullptr);
}
TEST(RefCountedPtr, ExplicitConstructor) { RefCountedPtr<Foo> foo(New<Foo>()); }
TEST(RefCountedPtr, MoveConstructor) {
RefCountedPtr<Foo> foo(New<Foo>());
RefCountedPtr<Foo> foo2(std::move(foo));
EXPECT_EQ(nullptr, foo.get());
EXPECT_NE(nullptr, foo2.get());
}
TEST(RefCountedPtr, MoveAssignment) {
RefCountedPtr<Foo> foo(New<Foo>());
RefCountedPtr<Foo> foo2 = std::move(foo);
EXPECT_EQ(nullptr, foo.get());
EXPECT_NE(nullptr, foo2.get());
}
TEST(RefCountedPtr, CopyConstructor) {
RefCountedPtr<Foo> foo(New<Foo>());
RefCountedPtr<Foo> foo2(foo);
EXPECT_NE(nullptr, foo.get());
EXPECT_EQ(foo.get(), foo2.get());
}
TEST(RefCountedPtr, CopyAssignment) {
RefCountedPtr<Foo> foo(New<Foo>());
RefCountedPtr<Foo> foo2 = foo;
EXPECT_NE(nullptr, foo.get());
EXPECT_EQ(foo.get(), foo2.get());
}
TEST(RefCountedPtr, CopyAssignmentWhenEmpty) {
RefCountedPtr<Foo> foo;
RefCountedPtr<Foo> foo2;
foo2 = foo;
EXPECT_EQ(nullptr, foo.get());
EXPECT_EQ(nullptr, foo2.get());
}
TEST(RefCountedPtr, CopyAssignmentToSelf) {
RefCountedPtr<Foo> foo(New<Foo>());
foo = foo;
}
TEST(RefCountedPtr, EnclosedScope) {
RefCountedPtr<Foo> foo(New<Foo>());
{
RefCountedPtr<Foo> foo2(std::move(foo));
EXPECT_EQ(nullptr, foo.get());
EXPECT_NE(nullptr, foo2.get());
}
EXPECT_EQ(nullptr, foo.get());
}
TEST(RefCountedPtr, ResetFromNullToNonNull) {
RefCountedPtr<Foo> foo;
EXPECT_EQ(nullptr, foo.get());
foo.reset(New<Foo>());
EXPECT_NE(nullptr, foo.get());
}
TEST(RefCountedPtr, ResetFromNonNullToNonNull) {
RefCountedPtr<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(RefCountedPtr, ResetFromNonNullToNull) {
RefCountedPtr<Foo> foo(New<Foo>());
EXPECT_NE(nullptr, foo.get());
foo.reset();
EXPECT_EQ(nullptr, foo.get());
}
TEST(RefCountedPtr, ResetFromNullToNull) {
RefCountedPtr<Foo> foo;
EXPECT_EQ(nullptr, foo.get());
foo.reset(nullptr);
EXPECT_EQ(nullptr, foo.get());
}
TEST(RefCountedPtr, DerefernceOperators) {
RefCountedPtr<Foo> foo(New<Foo>());
foo->value();
Foo& foo_ref = *foo;
foo_ref.value();
}
TEST(MakeRefCounted, NoArgs) {
RefCountedPtr<Foo> foo = MakeRefCounted<Foo>();
EXPECT_EQ(0, foo->value());
}
TEST(MakeRefCounted, Args) {
RefCountedPtr<Foo> foo = MakeRefCounted<Foo>(3);
EXPECT_EQ(3, foo->value());
}
TraceFlag foo_tracer(true, "foo");
class FooWithTracing : public RefCountedWithTracing {
public:
FooWithTracing() : RefCountedWithTracing(&foo_tracer) {}
};
TEST(RefCountedPtr, RefCountedWithTracing) {
RefCountedPtr<FooWithTracing> foo(New<FooWithTracing>());
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();
}

@ -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 <gtest/gtest.h>
#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>();
foo->Unref();
}
TEST(RefCounted, ExtraRef) {
Foo* foo = New<Foo>();
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<FooWithTracing>();
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();
}

@ -1033,12 +1033,15 @@ src/core/lib/support/arena.h \
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/fork.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/ref_counted.h \
src/core/lib/support/ref_counted_ptr.h \
src/core/lib/support/spinlock.h \
src/core/lib/support/string.h \
src/core/lib/support/string_windows.h \

@ -1287,6 +1287,7 @@ src/core/lib/support/cpu_iphone.cc \
src/core/lib/support/cpu_linux.cc \
src/core/lib/support/cpu_posix.cc \
src/core/lib/support/cpu_windows.cc \
src/core/lib/support/debug_location.h \
src/core/lib/support/env.h \
src/core/lib/support/env_linux.cc \
src/core/lib/support/env_posix.cc \
@ -1306,6 +1307,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/ref_counted.h \
src/core/lib/support/ref_counted_ptr.h \
src/core/lib/support/spinlock.h \
src/core/lib/support/string.cc \
src/core/lib/support/string.h \

@ -3902,6 +3902,44 @@
"third_party": false,
"type": "target"
},
{
"deps": [
"gpr",
"gpr_test_util",
"grpc",
"grpc++",
"grpc++_test",
"grpc_test_util"
],
"headers": [],
"is_filegroup": false,
"language": "c++",
"name": "ref_counted_ptr_test",
"src": [
"test/core/support/ref_counted_ptr_test.cc"
],
"third_party": false,
"type": "target"
},
{
"deps": [
"gpr",
"gpr_test_util",
"grpc",
"grpc++",
"grpc++_test",
"grpc_test_util"
],
"headers": [],
"is_filegroup": false,
"language": "c++",
"name": "ref_counted_test",
"src": [
"test/core/support/ref_counted_test.cc"
],
"third_party": false,
"type": "target"
},
{
"deps": [
"gpr",
@ -8193,6 +8231,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/ref_counted.h",
"src/core/lib/support/ref_counted_ptr.h",
"src/core/lib/surface/alarm_internal.h",
"src/core/lib/surface/api_trace.h",
"src/core/lib/surface/call.h",
@ -8329,6 +8370,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/ref_counted.h",
"src/core/lib/support/ref_counted_ptr.h",
"src/core/lib/surface/alarm_internal.h",
"src/core/lib/surface/api_trace.h",
"src/core/lib/surface/call.h",

@ -4099,6 +4099,54 @@
],
"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": "ref_counted_ptr_test",
"platforms": [
"linux",
"mac",
"posix",
"windows"
],
"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": "ref_counted_test",
"platforms": [
"linux",
"mac",
"posix",
"windows"
],
"uses_polling": true
},
{
"args": [],
"benchmark": false,

Loading…
Cancel
Save