orphanable.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // Allow RefCountedPtr<> to access Unref() and IncrementRefCount().
  71. template <typename T>
  72. friend class RefCountedPtr;
  73. // TraceFlagT is defined to accept both DebugOnlyTraceFlag and TraceFlag.
  74. // Note: RefCount tracing is only enabled on debug builds, even when a
  75. // TraceFlag is used.
  76. template <typename TraceFlagT = TraceFlag>
  77. explicit InternallyRefCounted(TraceFlagT* trace_flag = nullptr,
  78. intptr_t initial_refcount = 1)
  79. : refs_(initial_refcount, trace_flag) {}
  80. virtual ~InternallyRefCounted() = default;
  81. RefCountedPtr<Child> Ref() GRPC_MUST_USE_RESULT {
  82. IncrementRefCount();
  83. return RefCountedPtr<Child>(static_cast<Child*>(this));
  84. }
  85. RefCountedPtr<Child> Ref(const DebugLocation& location,
  86. const char* reason) GRPC_MUST_USE_RESULT {
  87. IncrementRefCount(location, reason);
  88. return RefCountedPtr<Child>(static_cast<Child*>(this));
  89. }
  90. void Unref() {
  91. if (GPR_UNLIKELY(refs_.Unref())) {
  92. delete this;
  93. }
  94. }
  95. void Unref(const DebugLocation& location, const char* reason) {
  96. if (GPR_UNLIKELY(refs_.Unref(location, reason))) {
  97. delete this;
  98. }
  99. }
  100. private:
  101. void IncrementRefCount() { refs_.Ref(); }
  102. void IncrementRefCount(const DebugLocation& location, const char* reason) {
  103. refs_.Ref(location, reason);
  104. }
  105. grpc_core::RefCount refs_;
  106. };
  107. } // namespace grpc_core
  108. #endif /* GRPC_CORE_LIB_GPRPP_ORPHANABLE_H */