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>
27 #include <grpc/support/log.h>
28 #include <grpc/support/sync.h>
30 #include "src/core/lib/gprpp/debug_location.h"
31 #include "src/core/lib/gprpp/memory.h"
32 #include "src/core/lib/gprpp/ref_counted.h"
33 #include "src/core/lib/gprpp/ref_counted_ptr.h"
37 // A base class for orphanable objects, which have one external owner
38 // but are not necessarily destroyed immediately when the external owner
39 // gives up ownership. Instead, the owner calls the object's Orphan()
40 // method, and the object then takes responsibility for its own cleanup
44 // Gives up ownership of the object. The implementation must arrange
45 // to eventually destroy the object without further interaction from the
47 virtual void Orphan() = 0;
49 // Not copyable or movable.
50 Orphanable(const Orphanable&) = delete;
51 Orphanable& operator=(const Orphanable&) = delete;
55 virtual ~Orphanable() {}
58 class OrphanableDelete {
61 void operator()(T* p) {
66 template <typename T, typename Deleter = OrphanableDelete>
67 using OrphanablePtr = std::unique_ptr<T, Deleter>;
69 template <typename T, typename... Args>
70 inline OrphanablePtr<T> MakeOrphanable(Args&&... args) {
71 return OrphanablePtr<T>(new T(std::forward<Args>(args)...));
74 // A type of Orphanable with internal ref-counting.
75 template <typename Child, UnrefBehavior UnrefBehaviorArg = kUnrefDelete>
76 class InternallyRefCounted : public Orphanable {
78 // Not copyable nor movable.
79 InternallyRefCounted(const InternallyRefCounted&) = delete;
80 InternallyRefCounted& operator=(const InternallyRefCounted&) = delete;
83 // Allow RefCountedPtr<> to access Unref() and IncrementRefCount().
85 friend class RefCountedPtr;
87 // Note: Tracing is a no-op on non-debug builds.
88 explicit InternallyRefCounted(const char* trace = nullptr,
89 intptr_t initial_refcount = 1)
90 : refs_(initial_refcount, trace) {}
91 ~InternallyRefCounted() override = default;
93 RefCountedPtr<Child> Ref() GRPC_MUST_USE_RESULT {
95 return RefCountedPtr<Child>(static_cast<Child*>(this));
97 RefCountedPtr<Child> Ref(const DebugLocation& location,
98 const char* reason) GRPC_MUST_USE_RESULT {
99 IncrementRefCount(location, reason);
100 return RefCountedPtr<Child>(static_cast<Child*>(this));
104 if (GPR_UNLIKELY(refs_.Unref())) {
105 internal::Delete<Child, UnrefBehaviorArg>(static_cast<Child*>(this));
108 void Unref(const DebugLocation& location, const char* reason) {
109 if (GPR_UNLIKELY(refs_.Unref(location, reason))) {
110 internal::Delete<Child, UnrefBehaviorArg>(static_cast<Child*>(this));
115 void IncrementRefCount() { refs_.Ref(); }
116 void IncrementRefCount(const DebugLocation& location, const char* reason) {
117 refs_.Ref(location, reason);
120 grpc_core::RefCount refs_;
123 } // namespace grpc_core
125 #endif /* GRPC_CORE_LIB_GPRPP_ORPHANABLE_H */