diff --git a/BUILD b/BUILD
index ddcb0cd0d7d..e645f8364b4 100644
--- a/BUILD
+++ b/BUILD
@@ -558,6 +558,16 @@ grpc_cc_library(
public_hdrs = ["src/core/lib/support/debug_location.h"],
)
+grpc_cc_library(
+ name = "orphanable",
+ public_hdrs = ["src/core/lib/support/orphanable.h"],
+ language = "c++",
+ deps = [
+ "grpc_trace",
+ "debug_location",
+ ],
+)
+
grpc_cc_library(
name = "ref_counted",
language = "c++",
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 93954b412d7..38bb92a5a87 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -563,6 +563,7 @@ add_dependencies(buildtests_cxx memory_test)
add_dependencies(buildtests_cxx metrics_client)
add_dependencies(buildtests_cxx mock_test)
add_dependencies(buildtests_cxx noop-benchmark)
+add_dependencies(buildtests_cxx orphanable_test)
add_dependencies(buildtests_cxx proto_server_reflection_test)
add_dependencies(buildtests_cxx proto_utils_test)
if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_POSIX)
@@ -10913,6 +10914,45 @@ target_link_libraries(noop-benchmark
endif (gRPC_BUILD_TESTS)
if (gRPC_BUILD_TESTS)
+add_executable(orphanable_test
+ test/core/support/orphanable_test.cc
+ third_party/googletest/googletest/src/gtest-all.cc
+ third_party/googletest/googlemock/src/gmock-all.cc
+)
+
+
+target_include_directories(orphanable_test
+ PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
+ PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
+ PRIVATE ${_gRPC_SSL_INCLUDE_DIR}
+ 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(orphanable_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(proto_server_reflection_test
test/cpp/end2end/proto_server_reflection_test.cc
third_party/googletest/googletest/src/gtest-all.cc
diff --git a/Makefile b/Makefile
index 8a38cc8da0e..3265cea362e 100644
--- a/Makefile
+++ b/Makefile
@@ -1160,6 +1160,7 @@ memory_test: $(BINDIR)/$(CONFIG)/memory_test
metrics_client: $(BINDIR)/$(CONFIG)/metrics_client
mock_test: $(BINDIR)/$(CONFIG)/mock_test
noop-benchmark: $(BINDIR)/$(CONFIG)/noop-benchmark
+orphanable_test: $(BINDIR)/$(CONFIG)/orphanable_test
proto_server_reflection_test: $(BINDIR)/$(CONFIG)/proto_server_reflection_test
proto_utils_test: $(BINDIR)/$(CONFIG)/proto_utils_test
qps_interarrival_test: $(BINDIR)/$(CONFIG)/qps_interarrival_test
@@ -1602,6 +1603,7 @@ buildtests_cxx: privatelibs_cxx \
$(BINDIR)/$(CONFIG)/metrics_client \
$(BINDIR)/$(CONFIG)/mock_test \
$(BINDIR)/$(CONFIG)/noop-benchmark \
+ $(BINDIR)/$(CONFIG)/orphanable_test \
$(BINDIR)/$(CONFIG)/proto_server_reflection_test \
$(BINDIR)/$(CONFIG)/proto_utils_test \
$(BINDIR)/$(CONFIG)/qps_interarrival_test \
@@ -1733,6 +1735,7 @@ buildtests_cxx: privatelibs_cxx \
$(BINDIR)/$(CONFIG)/metrics_client \
$(BINDIR)/$(CONFIG)/mock_test \
$(BINDIR)/$(CONFIG)/noop-benchmark \
+ $(BINDIR)/$(CONFIG)/orphanable_test \
$(BINDIR)/$(CONFIG)/proto_server_reflection_test \
$(BINDIR)/$(CONFIG)/proto_utils_test \
$(BINDIR)/$(CONFIG)/qps_interarrival_test \
@@ -2142,6 +2145,8 @@ test_cxx: buildtests_cxx
$(Q) $(BINDIR)/$(CONFIG)/mock_test || ( echo test mock_test failed ; exit 1 )
$(E) "[RUN] Testing noop-benchmark"
$(Q) $(BINDIR)/$(CONFIG)/noop-benchmark || ( echo test noop-benchmark failed ; exit 1 )
+ $(E) "[RUN] Testing orphanable_test"
+ $(Q) $(BINDIR)/$(CONFIG)/orphanable_test || ( echo test orphanable_test failed ; exit 1 )
$(E) "[RUN] Testing proto_server_reflection_test"
$(Q) $(BINDIR)/$(CONFIG)/proto_server_reflection_test || ( echo test proto_server_reflection_test failed ; exit 1 )
$(E) "[RUN] Testing proto_utils_test"
@@ -16141,6 +16146,49 @@ endif
endif
+ORPHANABLE_TEST_SRC = \
+ test/core/support/orphanable_test.cc \
+
+ORPHANABLE_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(ORPHANABLE_TEST_SRC))))
+ifeq ($(NO_SECURE),true)
+
+# You can't build secure targets if you don't have OpenSSL.
+
+$(BINDIR)/$(CONFIG)/orphanable_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)/orphanable_test: protobuf_dep_error
+
+else
+
+$(BINDIR)/$(CONFIG)/orphanable_test: $(PROTOBUF_DEP) $(ORPHANABLE_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) $(ORPHANABLE_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)/orphanable_test
+
+endif
+
+endif
+
+$(OBJDIR)/$(CONFIG)/test/core/support/orphanable_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_orphanable_test: $(ORPHANABLE_TEST_OBJS:.o=.dep)
+
+ifneq ($(NO_SECURE),true)
+ifneq ($(NO_DEPS),true)
+-include $(ORPHANABLE_TEST_OBJS:.o=.dep)
+endif
+endif
+
+
PROTO_SERVER_REFLECTION_TEST_SRC = \
test/cpp/end2end/proto_server_reflection_test.cc \
diff --git a/build.yaml b/build.yaml
index c3d54f1e8ad..c9adc9b44e7 100644
--- a/build.yaml
+++ b/build.yaml
@@ -397,6 +397,7 @@ 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/orphanable.h
- src/core/lib/support/ref_counted.h
- src/core/lib/support/ref_counted_ptr.h
- src/core/lib/support/vector.h
@@ -4403,6 +4404,20 @@ targets:
deps:
- benchmark
defaults: benchmark
+- name: orphanable_test
+ gtest: true
+ build: test
+ language: c++
+ src:
+ - test/core/support/orphanable_test.cc
+ deps:
+ - grpc_test_util
+ - grpc++
+ - grpc
+ - gpr_test_util
+ - gpr
+ uses:
+ - grpc++_test
- name: proto_server_reflection_test
gtest: true
build: test
diff --git a/gRPC-Core.podspec b/gRPC-Core.podspec
index 358fad3d983..d064ac80ae6 100644
--- a/gRPC-Core.podspec
+++ b/gRPC-Core.podspec
@@ -420,6 +420,7 @@ 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/orphanable.h',
'src/core/lib/support/ref_counted.h',
'src/core/lib/support/ref_counted_ptr.h',
'src/core/lib/support/vector.h',
@@ -901,6 +902,7 @@ 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/orphanable.h',
'src/core/lib/support/ref_counted.h',
'src/core/lib/support/ref_counted_ptr.h',
'src/core/lib/support/vector.h',
diff --git a/grpc.gemspec b/grpc.gemspec
index 7547bc85ded..f8afaa5803c 100644
--- a/grpc.gemspec
+++ b/grpc.gemspec
@@ -346,6 +346,7 @@ 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/orphanable.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 )
diff --git a/package.xml b/package.xml
index ff3d0797abe..8c590348b6c 100644
--- a/package.xml
+++ b/package.xml
@@ -358,6 +358,7 @@
+
diff --git a/src/core/lib/support/abstract.h b/src/core/lib/support/abstract.h
index 5498769a7d9..1dffa301282 100644
--- a/src/core/lib/support/abstract.h
+++ b/src/core/lib/support/abstract.h
@@ -26,4 +26,9 @@
#define GRPC_ABSTRACT_BASE_CLASS \
static void operator delete(void* p) { abort(); }
+// gRPC currently can't depend on libstdc++, so we can't use "= 0" for
+// pure virtual methods. Instead, we use this macro.
+#define GRPC_ABSTRACT \
+ { GPR_ASSERT(false); }
+
#endif /* GRPC_CORE_LIB_SUPPORT_ABSTRACT_H */
diff --git a/src/core/lib/support/orphanable.h b/src/core/lib/support/orphanable.h
new file mode 100644
index 00000000000..2f537573fd4
--- /dev/null
+++ b/src/core/lib/support/orphanable.h
@@ -0,0 +1,171 @@
+/*
+ *
+ * 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_ORPHANABLE_H
+#define GRPC_CORE_LIB_SUPPORT_ORPHANABLE_H
+
+#include
+#include
+
+#include
+
+#include "src/core/lib/debug/trace.h"
+#include "src/core/lib/support/abstract.h"
+#include "src/core/lib/support/debug_location.h"
+#include "src/core/lib/support/memory.h"
+
+namespace grpc_core {
+
+// A base class for orphanable objects, which have one external owner
+// but are not necessarily destroyed immediately when the external owner
+// gives up ownership. Instead, the owner calls the object's Orphan()
+// method, and the object then takes responsibility for its own cleanup
+// and destruction.
+class Orphanable {
+ public:
+ // Gives up ownership of the object. The implementation must arrange
+ // to eventually destroy the object without further interaction from the
+ // caller.
+ virtual void Orphan() GRPC_ABSTRACT;
+
+ // Not copyable or movable.
+ Orphanable(const Orphanable&) = delete;
+ Orphanable& operator=(const Orphanable&) = delete;
+
+ GRPC_ABSTRACT_BASE_CLASS
+
+ protected:
+ Orphanable() {}
+ virtual ~Orphanable() {}
+};
+
+template
+class OrphanableDelete {
+ public:
+ void operator()(T* p) { p->Orphan(); }
+};
+
+template >
+using OrphanablePtr = std::unique_ptr;
+
+template
+inline OrphanablePtr MakeOrphanable(Args&&... args) {
+ return OrphanablePtr(New(std::forward(args)...));
+}
+
+// A type of Orphanable with internal ref-counting.
+class InternallyRefCounted : public Orphanable {
+ public:
+ // Not copyable nor movable.
+ InternallyRefCounted(const InternallyRefCounted&) = delete;
+ InternallyRefCounted& operator=(const InternallyRefCounted&) = delete;
+
+ GRPC_ABSTRACT_BASE_CLASS
+
+ protected:
+ InternallyRefCounted() { gpr_ref_init(&refs_, 1); }
+ virtual ~InternallyRefCounted() {}
+
+ void Ref() { gpr_ref(&refs_); }
+
+ void Unref() {
+ if (gpr_unref(&refs_)) {
+ Delete(this);
+ }
+ }
+
+ // Allow Delete() to access destructor.
+ template
+ friend void Delete(T*);
+
+ private:
+ gpr_refcount refs_;
+};
+
+// An alternative version of the InternallyRefCounted 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 InternallyRefCountedWithTracing : public Orphanable {
+ public:
+ // Not copyable nor movable.
+ InternallyRefCountedWithTracing(const InternallyRefCountedWithTracing&) =
+ delete;
+ InternallyRefCountedWithTracing& operator=(
+ const InternallyRefCountedWithTracing&) = delete;
+
+ GRPC_ABSTRACT_BASE_CLASS
+
+ protected:
+ // Allow Delete() to access destructor.
+ template
+ friend void Delete(T*);
+
+ InternallyRefCountedWithTracing()
+ : InternallyRefCountedWithTracing(static_cast(nullptr)) {}
+
+ explicit InternallyRefCountedWithTracing(TraceFlag* trace_flag)
+ : trace_flag_(trace_flag) {
+ gpr_ref_init(&refs_, 1);
+ }
+
+#ifdef NDEBUG
+ explicit InternallyRefCountedWithTracing(DebugOnlyTraceFlag* trace_flag)
+ : InternallyRefCountedWithTracing() {}
+#endif
+
+ virtual ~InternallyRefCountedWithTracing() {}
+
+ 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();
+ }
+
+ private:
+ TraceFlag* trace_flag_ = nullptr;
+ gpr_refcount refs_;
+};
+
+} // namespace grpc_core
+
+#endif /* GRPC_CORE_LIB_SUPPORT_ORPHANABLE_H */
diff --git a/src/core/lib/support/ref_counted.h b/src/core/lib/support/ref_counted.h
index 4c662f91190..48c11f7bbfe 100644
--- a/src/core/lib/support/ref_counted.h
+++ b/src/core/lib/support/ref_counted.h
@@ -23,6 +23,7 @@
#include
#include "src/core/lib/debug/trace.h"
+#include "src/core/lib/support/abstract.h"
#include "src/core/lib/support/debug_location.h"
#include "src/core/lib/support/memory.h"
@@ -45,6 +46,8 @@ class RefCounted {
RefCounted(const RefCounted&) = delete;
RefCounted& operator=(const RefCounted&) = delete;
+ GRPC_ABSTRACT_BASE_CLASS
+
protected:
// Allow Delete() to access destructor.
template
@@ -98,18 +101,26 @@ class RefCountedWithTracing {
RefCountedWithTracing(const RefCountedWithTracing&) = delete;
RefCountedWithTracing& operator=(const RefCountedWithTracing&) = delete;
+ GRPC_ABSTRACT_BASE_CLASS
+
protected:
// Allow Delete() to access destructor.
template
friend void Delete(T*);
- RefCountedWithTracing() : RefCountedWithTracing(nullptr) {}
+ RefCountedWithTracing()
+ : RefCountedWithTracing(static_cast(nullptr)) {}
explicit RefCountedWithTracing(TraceFlag* trace_flag)
: trace_flag_(trace_flag) {
gpr_ref_init(&refs_, 1);
}
+#ifdef NDEBUG
+ explicit RefCountedWithTracing(DebugOnlyTraceFlag* trace_flag)
+ : RefCountedWithTracing() {}
+#endif
+
virtual ~RefCountedWithTracing() {}
private:
diff --git a/src/core/lib/support/ref_counted_ptr.h b/src/core/lib/support/ref_counted_ptr.h
index dc2385e369c..76ff0bba668 100644
--- a/src/core/lib/support/ref_counted_ptr.h
+++ b/src/core/lib/support/ref_counted_ptr.h
@@ -76,6 +76,15 @@ class RefCountedPtr {
T& operator*() const { return *value_; }
T* operator->() const { return value_; }
+ bool operator==(const RefCountedPtr& other) const {
+ return value_ == other.value_;
+ }
+ bool operator==(const T* other) const { return value_ == other; }
+ bool operator!=(const RefCountedPtr& other) const {
+ return value_ != other.value_;
+ }
+ bool operator!=(const T* other) const { return value_ != other; }
+
private:
T* value_ = nullptr;
};
diff --git a/test/core/support/BUILD b/test/core/support/BUILD
index 4372b49b545..c8fa046da1d 100644
--- a/test/core/support/BUILD
+++ b/test/core/support/BUILD
@@ -214,6 +214,19 @@ grpc_cc_test(
],
)
+grpc_cc_test(
+ name = "orphanable_test",
+ srcs = ["orphanable_test.cc"],
+ language = "C++",
+ deps = [
+ "//:orphanable",
+ "//test/core/util:gpr_test_util",
+ ],
+ external_deps = [
+ "gtest",
+ ],
+)
+
grpc_cc_test(
name = "ref_counted_test",
srcs = ["ref_counted_test.cc"],
diff --git a/test/core/support/orphanable_test.cc b/test/core/support/orphanable_test.cc
new file mode 100644
index 00000000000..e07017ab1ed
--- /dev/null
+++ b/test/core/support/orphanable_test.cc
@@ -0,0 +1,114 @@
+/*
+ *
+ * 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/orphanable.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 Orphanable {
+ public:
+ Foo() : Foo(0) {}
+ explicit Foo(int value) : value_(value) {}
+ void Orphan() override { Delete(this); }
+ int value() const { return value_; }
+
+ private:
+ int value_;
+};
+
+TEST(Orphanable, Basic) {
+ Foo* foo = New();
+ foo->Orphan();
+}
+
+TEST(OrphanablePtr, Basic) {
+ OrphanablePtr foo(New());
+ EXPECT_EQ(0, foo->value());
+}
+
+TEST(MakeOrphanable, DefaultConstructor) {
+ auto foo = MakeOrphanable();
+ EXPECT_EQ(0, foo->value());
+}
+
+TEST(MakeOrphanable, WithParameters) {
+ auto foo = MakeOrphanable(5);
+ EXPECT_EQ(5, foo->value());
+}
+
+class Bar : public InternallyRefCounted {
+ public:
+ Bar() : Bar(0) {}
+ explicit Bar(int value) : value_(value) {}
+ void Orphan() override { Unref(); }
+ int value() const { return value_; }
+
+ void StartWork() { Ref(); }
+ void FinishWork() { Unref(); }
+
+ private:
+ int value_;
+};
+
+TEST(OrphanablePtr, InternallyRefCounted) {
+ auto bar = MakeOrphanable();
+ bar->StartWork();
+ bar->FinishWork();
+}
+
+// Note: We use DebugOnlyTraceFlag instead of TraceFlag to ensure that
+// things build properly in both debug and non-debug cases.
+DebugOnlyTraceFlag baz_tracer(true, "baz");
+
+class Baz : public InternallyRefCountedWithTracing {
+ public:
+ Baz() : Baz(0) {}
+ explicit Baz(int value)
+ : InternallyRefCountedWithTracing(&baz_tracer), value_(value) {}
+ void Orphan() override { Unref(); }
+ int value() const { return value_; }
+
+ void StartWork() { Ref(DEBUG_LOCATION, "work"); }
+ void FinishWork() { Unref(DEBUG_LOCATION, "work"); }
+
+ private:
+ int value_;
+};
+
+TEST(OrphanablePtr, InternallyRefCountedWithTracing) {
+ auto baz = MakeOrphanable();
+ baz->StartWork();
+ baz->FinishWork();
+}
+
+} // 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_ptr_test.cc b/test/core/support/ref_counted_ptr_test.cc
index 1830edc4e59..ce4975d3477 100644
--- a/test/core/support/ref_counted_ptr_test.cc
+++ b/test/core/support/ref_counted_ptr_test.cc
@@ -138,6 +138,19 @@ TEST(RefCountedPtr, DerefernceOperators) {
foo_ref.value();
}
+TEST(RefCountedPtr, EqualityOperators) {
+ RefCountedPtr foo(New());
+ RefCountedPtr bar = foo;
+ RefCountedPtr empty;
+ // Test equality between RefCountedPtrs.
+ EXPECT_EQ(foo, bar);
+ EXPECT_NE(foo, empty);
+ // Test equality with bare pointers.
+ EXPECT_EQ(foo, foo.get());
+ EXPECT_EQ(empty, nullptr);
+ EXPECT_NE(foo, nullptr);
+}
+
TEST(MakeRefCounted, NoArgs) {
RefCountedPtr foo = MakeRefCounted();
EXPECT_EQ(0, foo->value());
diff --git a/test/core/support/ref_counted_test.cc b/test/core/support/ref_counted_test.cc
index be9b6ff7c2d..0629e3ff5fd 100644
--- a/test/core/support/ref_counted_test.cc
+++ b/test/core/support/ref_counted_test.cc
@@ -44,7 +44,9 @@ TEST(RefCounted, ExtraRef) {
foo->Unref();
}
-TraceFlag foo_tracer(true, "foo");
+// Note: We use DebugOnlyTraceFlag instead of TraceFlag to ensure that
+// things build properly in both debug and non-debug cases.
+DebugOnlyTraceFlag foo_tracer(true, "foo");
class FooWithTracing : public RefCountedWithTracing {
public:
diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal
index d9184f49a22..85bbeed088e 100644
--- a/tools/doxygen/Doxyfile.c++.internal
+++ b/tools/doxygen/Doxyfile.c++.internal
@@ -1038,6 +1038,7 @@ 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/orphanable.h \
src/core/lib/support/ref_counted.h \
src/core/lib/support/ref_counted_ptr.h \
src/core/lib/support/spinlock.h \
diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal
index 3d3c6711d0c..4bf0fc74d1d 100644
--- a/tools/doxygen/Doxyfile.core.internal
+++ b/tools/doxygen/Doxyfile.core.internal
@@ -1304,6 +1304,7 @@ 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/orphanable.h \
src/core/lib/support/ref_counted.h \
src/core/lib/support/ref_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 c934155ecc7..df45489d5e8 100644
--- a/tools/run_tests/generated/sources_and_headers.json
+++ b/tools/run_tests/generated/sources_and_headers.json
@@ -3707,6 +3707,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": "orphanable_test",
+ "src": [
+ "test/core/support/orphanable_test.cc"
+ ],
+ "third_party": false,
+ "type": "target"
+ },
{
"deps": [
"gpr",
@@ -8286,6 +8305,7 @@
"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/orphanable.h",
"src/core/lib/support/ref_counted.h",
"src/core/lib/support/ref_counted_ptr.h",
"src/core/lib/support/vector.h",
@@ -8426,6 +8446,7 @@
"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/orphanable.h",
"src/core/lib/support/ref_counted.h",
"src/core/lib/support/ref_counted_ptr.h",
"src/core/lib/support/vector.h",
diff --git a/tools/run_tests/generated/tests.json b/tools/run_tests/generated/tests.json
index 6f36dff8208..dc2b39eb217 100644
--- a/tools/run_tests/generated/tests.json
+++ b/tools/run_tests/generated/tests.json
@@ -4077,6 +4077,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": "orphanable_test",
+ "platforms": [
+ "linux",
+ "mac",
+ "posix",
+ "windows"
+ ],
+ "uses_polling": true
+ },
{
"args": [],
"benchmark": false,