ref_tracked-inl.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef REF_TRACKED_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include ref_tracked.h"
  3. // For the sake of sane code completion.
  4. #include "ref_tracked.h"
  5. #endif
  6. namespace NYT {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. template <class T>
  9. TRefCountedTypeKey GetRefCountedTypeKey()
  10. {
  11. return TRefCountedTypeKey(&typeid(T));
  12. }
  13. template <class T>
  14. Y_FORCE_INLINE TRefCountedTypeCookie GetRefCountedTypeCookie()
  15. {
  16. static std::atomic<TRefCountedTypeCookie> cookie{NullRefCountedTypeCookie};
  17. auto cookieValue = cookie.load(std::memory_order::relaxed);
  18. if (Y_UNLIKELY(cookieValue == NullRefCountedTypeCookie)) {
  19. cookieValue = TRefCountedTrackerFacade::GetCookie(
  20. GetRefCountedTypeKey<T>(),
  21. sizeof(T),
  22. NYT::TSourceLocation());
  23. cookie.store(cookieValue, std::memory_order::relaxed);
  24. }
  25. return cookieValue;
  26. }
  27. template <class T, class TTag, int Counter>
  28. Y_FORCE_INLINE TRefCountedTypeCookie GetRefCountedTypeCookieWithLocation(const TSourceLocation& location)
  29. {
  30. static std::atomic<TRefCountedTypeCookie> cookie{NullRefCountedTypeCookie};
  31. auto cookieValue = cookie.load(std::memory_order::relaxed);
  32. if (Y_UNLIKELY(cookieValue == NullRefCountedTypeCookie)) {
  33. cookieValue = TRefCountedTrackerFacade::GetCookie(
  34. GetRefCountedTypeKey<T>(),
  35. sizeof(T),
  36. location);
  37. cookie.store(cookieValue, std::memory_order::relaxed);
  38. }
  39. return cookieValue;
  40. }
  41. ////////////////////////////////////////////////////////////////////////////////
  42. #ifdef YT_ENABLE_REF_COUNTED_TRACKING
  43. template <class T>
  44. TRefTracked<T>::TRefTracked()
  45. {
  46. auto cookie = GetRefCountedTypeCookie<T>();
  47. TRefCountedTrackerFacade::AllocateInstance(cookie);
  48. }
  49. template <class T>
  50. TRefTracked<T>::TRefTracked(const TRefTracked&)
  51. {
  52. auto cookie = GetRefCountedTypeCookie<T>();
  53. TRefCountedTrackerFacade::AllocateInstance(cookie);
  54. }
  55. template <class T>
  56. TRefTracked<T>::TRefTracked(TRefTracked&&)
  57. {
  58. auto cookie = GetRefCountedTypeCookie<T>();
  59. TRefCountedTrackerFacade::AllocateInstance(cookie);
  60. }
  61. template <class T>
  62. TRefTracked<T>::~TRefTracked()
  63. {
  64. auto cookie = GetRefCountedTypeCookie<T>();
  65. TRefCountedTrackerFacade::FreeInstance(cookie);
  66. }
  67. #endif
  68. ////////////////////////////////////////////////////////////////////////////////
  69. } // namespace NYT