1 //
2 //
3 // Copyright 2017 gRPC authors.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 //
18
19 #ifndef GRPC_SRC_CORE_LIB_GPRPP_ORPHANABLE_H
20 #define GRPC_SRC_CORE_LIB_GPRPP_ORPHANABLE_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <cinttypes>
25 #include <memory>
26 #include <utility>
27
28 #include "src/core/lib/gprpp/debug_location.h"
29 #include "src/core/lib/gprpp/ref_counted.h"
30 #include "src/core/lib/gprpp/ref_counted_ptr.h"
31
32 namespace grpc_core {
33
34 // A base class for orphanable objects, which have one external owner
35 // but are not necessarily destroyed immediately when the external owner
36 // gives up ownership. Instead, the owner calls the object's Orphan()
37 // method, and the object then takes responsibility for its own cleanup
38 // and destruction.
39 class Orphanable {
40 public:
41 // Gives up ownership of the object. The implementation must arrange
42 // to eventually destroy the object without further interaction from the
43 // caller.
44 virtual void Orphan() = 0;
45
46 // Not copyable or movable.
47 Orphanable(const Orphanable&) = delete;
48 Orphanable& operator=(const Orphanable&) = delete;
49
50 protected:
Orphanable()51 Orphanable() {}
~Orphanable()52 virtual ~Orphanable() {}
53 };
54
55 class OrphanableDelete {
56 public:
57 template <typename T>
operator()58 void operator()(T* p) {
59 p->Orphan();
60 }
61 };
62
63 template <typename T, typename Deleter = OrphanableDelete>
64 using OrphanablePtr = std::unique_ptr<T, Deleter>;
65
66 template <typename T, typename... Args>
MakeOrphanable(Args &&...args)67 inline OrphanablePtr<T> MakeOrphanable(Args&&... args) {
68 return OrphanablePtr<T>(new T(std::forward<Args>(args)...));
69 }
70
71 // A type of Orphanable with internal ref-counting.
72 template <typename Child, typename UnrefBehavior = UnrefDelete>
73 class InternallyRefCounted : public Orphanable {
74 public:
75 // Not copyable nor movable.
76 InternallyRefCounted(const InternallyRefCounted&) = delete;
77 InternallyRefCounted& operator=(const InternallyRefCounted&) = delete;
78
79 protected:
80 // Allow RefCountedPtr<> to access Unref() and IncrementRefCount().
81 template <typename T>
82 friend class RefCountedPtr;
83
84 // Note: Tracing is a no-op on non-debug builds.
85 explicit InternallyRefCounted(const char* trace = nullptr,
86 intptr_t initial_refcount = 1)
refs_(initial_refcount,trace)87 : refs_(initial_refcount, trace) {}
88 ~InternallyRefCounted() override = default;
89
Ref()90 RefCountedPtr<Child> Ref() GRPC_MUST_USE_RESULT {
91 IncrementRefCount();
92 return RefCountedPtr<Child>(static_cast<Child*>(this));
93 }
Ref(const DebugLocation & location,const char * reason)94 RefCountedPtr<Child> Ref(const DebugLocation& location,
95 const char* reason) GRPC_MUST_USE_RESULT {
96 IncrementRefCount(location, reason);
97 return RefCountedPtr<Child>(static_cast<Child*>(this));
98 }
99
Unref()100 void Unref() {
101 if (GPR_UNLIKELY(refs_.Unref())) {
102 unref_behavior_(static_cast<Child*>(this));
103 }
104 }
Unref(const DebugLocation & location,const char * reason)105 void Unref(const DebugLocation& location, const char* reason) {
106 if (GPR_UNLIKELY(refs_.Unref(location, reason))) {
107 unref_behavior_(static_cast<Child*>(this));
108 }
109 }
110
111 private:
IncrementRefCount()112 void IncrementRefCount() { refs_.Ref(); }
IncrementRefCount(const DebugLocation & location,const char * reason)113 void IncrementRefCount(const DebugLocation& location, const char* reason) {
114 refs_.Ref(location, reason);
115 }
116
117 RefCount refs_;
118 GPR_NO_UNIQUE_ADDRESS UnrefBehavior unref_behavior_;
119 };
120
121 } // namespace grpc_core
122
123 #endif // GRPC_SRC_CORE_LIB_GPRPP_ORPHANABLE_H
124