ref_tracked.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include <library/cpp/yt/misc/port.h>
  3. #include <library/cpp/yt/misc/source_location.h>
  4. #include <library/cpp/yt/misc/strong_typedef.h>
  5. #include <util/system/defaults.h>
  6. #include <atomic>
  7. #include <typeinfo>
  8. namespace NYT {
  9. ////////////////////////////////////////////////////////////////////////////////
  10. YT_DEFINE_STRONG_TYPEDEF(TRefCountedTypeCookie, int)
  11. constexpr TRefCountedTypeCookie NullRefCountedTypeCookie{-1};
  12. YT_DEFINE_STRONG_TYPEDEF(TRefCountedTypeKey, const std::type_info*)
  13. ////////////////////////////////////////////////////////////////////////////////
  14. // Used to avoid including heavy ref_counted_tracker.h
  15. class TRefCountedTrackerFacade
  16. {
  17. public:
  18. static TRefCountedTypeCookie GetCookie(
  19. TRefCountedTypeKey typeKey,
  20. size_t instanceSize,
  21. const NYT::TSourceLocation& location);
  22. static void AllocateInstance(TRefCountedTypeCookie cookie);
  23. static void FreeInstance(TRefCountedTypeCookie cookie);
  24. static void AllocateTagInstance(TRefCountedTypeCookie cookie);
  25. static void FreeTagInstance(TRefCountedTypeCookie cookie);
  26. static void AllocateSpace(TRefCountedTypeCookie cookie, size_t size);
  27. static void FreeSpace(TRefCountedTypeCookie cookie, size_t size);
  28. // Typically invoked from GDB console.
  29. // Dumps the ref-counted statistics sorted by "bytes alive".
  30. static void Dump();
  31. };
  32. ////////////////////////////////////////////////////////////////////////////////
  33. namespace {
  34. //! A per-translation unit tag type.
  35. struct TCurrentTranslationUnitTag
  36. { };
  37. } // namespace
  38. template <class T>
  39. TRefCountedTypeKey GetRefCountedTypeKey();
  40. template <class T>
  41. TRefCountedTypeCookie GetRefCountedTypeCookie();
  42. template <class T, class TTag, int Counter>
  43. TRefCountedTypeCookie GetRefCountedTypeCookieWithLocation(
  44. const TSourceLocation& location);
  45. ////////////////////////////////////////////////////////////////////////////////
  46. //! A lightweight mix-in that integrates any class into TRefCountedTracker statistics.
  47. /*!
  48. * |T| must be the actual derived type.
  49. *
  50. * This mix-in provides statistical tracking only, |T| is responsible for implementing
  51. * lifetime management on its own.
  52. */
  53. template <class T>
  54. class TRefTracked
  55. {
  56. public:
  57. #ifdef YT_ENABLE_REF_COUNTED_TRACKING
  58. TRefTracked();
  59. TRefTracked(const TRefTracked&);
  60. TRefTracked(TRefTracked&&);
  61. ~TRefTracked();
  62. #endif
  63. };
  64. ////////////////////////////////////////////////////////////////////////////////
  65. } // namespace NYT
  66. #define REF_TRACKED_INL_H_
  67. #include "ref_tracked-inl.h"
  68. #undef REF_TRACKED_INL_H_