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