orphanable.h 3.9 KB

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