3 * Copyright 2017 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #ifndef GRPC_CORE_LIB_GPRPP_ORPHANABLE_H
20 #define GRPC_CORE_LIB_GPRPP_ORPHANABLE_H
22 #include <grpc/support/port_platform.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/sync.h>
30 #include "src/core/lib/debug/trace.h"
31 #include "src/core/lib/gprpp/abstract.h"
32 #include "src/core/lib/gprpp/debug_location.h"
33 #include "src/core/lib/gprpp/memory.h"
34 #include "src/core/lib/gprpp/ref_counted.h"
35 #include "src/core/lib/gprpp/ref_counted_ptr.h"
39 // A base class for orphanable objects, which have one external owner
40 // but are not necessarily destroyed immediately when the external owner
41 // gives up ownership. Instead, the owner calls the object's Orphan()
42 // method, and the object then takes responsibility for its own cleanup
46 // Gives up ownership of the object. The implementation must arrange
47 // to eventually destroy the object without further interaction from the
49 virtual void Orphan() GRPC_ABSTRACT;
51 // Not copyable or movable.
52 Orphanable(const Orphanable&) = delete;
53 Orphanable& operator=(const Orphanable&) = delete;
55 GRPC_ABSTRACT_BASE_CLASS
59 virtual ~Orphanable() {}
63 class OrphanableDelete {
65 void operator()(T* p) { p->Orphan(); }
68 template <typename T, typename Deleter = OrphanableDelete<T>>
69 using OrphanablePtr = std::unique_ptr<T, Deleter>;
71 template <typename T, typename... Args>
72 inline OrphanablePtr<T> MakeOrphanable(Args&&... args) {
73 return OrphanablePtr<T>(New<T>(std::forward<Args>(args)...));
76 // A type of Orphanable with internal ref-counting.
77 template <typename Child>
78 class InternallyRefCounted : public Orphanable {
80 // Not copyable nor movable.
81 InternallyRefCounted(const InternallyRefCounted&) = delete;
82 InternallyRefCounted& operator=(const InternallyRefCounted&) = delete;
84 GRPC_ABSTRACT_BASE_CLASS
87 GRPC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE
89 // Allow RefCountedPtr<> to access Unref() and IncrementRefCount().
91 friend class RefCountedPtr;
93 // TraceFlagT is defined to accept both DebugOnlyTraceFlag and TraceFlag.
94 // Note: RefCount tracing is only enabled on debug builds, even when a
96 template <typename TraceFlagT = TraceFlag>
97 explicit InternallyRefCounted(TraceFlagT* trace_flag = nullptr,
98 intptr_t initial_refcount = 1)
99 : refs_(initial_refcount, trace_flag) {}
100 virtual ~InternallyRefCounted() = default;
102 RefCountedPtr<Child> Ref() GRPC_MUST_USE_RESULT {
104 return RefCountedPtr<Child>(static_cast<Child*>(this));
106 RefCountedPtr<Child> Ref(const DebugLocation& location,
107 const char* reason) GRPC_MUST_USE_RESULT {
108 IncrementRefCount(location, reason);
109 return RefCountedPtr<Child>(static_cast<Child*>(this));
113 if (GPR_UNLIKELY(refs_.Unref())) {
114 Delete(static_cast<Child*>(this));
117 void Unref(const DebugLocation& location, const char* reason) {
118 if (GPR_UNLIKELY(refs_.Unref(location, reason))) {
119 Delete(static_cast<Child*>(this));
124 void IncrementRefCount() { refs_.Ref(); }
125 void IncrementRefCount(const DebugLocation& location, const char* reason) {
126 refs_.Ref(location, reason);
129 grpc_core::RefCount refs_;
132 } // namespace grpc_core
134 #endif /* GRPC_CORE_LIB_GPRPP_ORPHANABLE_H */