aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/memory/ref_tracked.h
blob: 75c1eb59850bea0d665e48fec8e7842169e5bfcc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#pragma once

#include <library/cpp/yt/misc/port.h>
#include <library/cpp/yt/misc/source_location.h>

#include <util/system/defaults.h>

#include <atomic>
#include <typeinfo>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

using TRefCountedTypeCookie = int;
const int NullRefCountedTypeCookie = -1;

using TRefCountedTypeKey = const void*;

////////////////////////////////////////////////////////////////////////////////

// Used to avoid including heavy ref_counted_tracker.h
class TRefCountedTrackerFacade
{
public:
    static TRefCountedTypeCookie GetCookie(
        TRefCountedTypeKey typeKey,
        size_t instanceSize,
        const NYT::TSourceLocation& location);

    static void AllocateInstance(TRefCountedTypeCookie cookie);
    static void FreeInstance(TRefCountedTypeCookie cookie);

    static void AllocateTagInstance(TRefCountedTypeCookie cookie);
    static void FreeTagInstance(TRefCountedTypeCookie cookie);

    static void AllocateSpace(TRefCountedTypeCookie cookie, size_t size);
    static void FreeSpace(TRefCountedTypeCookie cookie, size_t size);

    // Typically invoked from GDB console.
    // Dumps the ref-counted statistics sorted by "bytes alive".
    static void Dump();
};

////////////////////////////////////////////////////////////////////////////////

namespace {

//! A per-translation unit tag type.
struct TCurrentTranslationUnitTag
{ };

} // namespace

template <class T>
TRefCountedTypeKey GetRefCountedTypeKey();

template <class T>
TRefCountedTypeCookie GetRefCountedTypeCookie();

template <class T, class TTag, int Counter>
TRefCountedTypeCookie GetRefCountedTypeCookieWithLocation(
    const TSourceLocation& location);

////////////////////////////////////////////////////////////////////////////////

//! A lightweight mix-in that integrates any class into TRefCountedTracker statistics.
/*!
 *  |T| must be the actual derived type.
 *
 *  This mix-in provides statistical tracking only, |T| is responsible for implementing
 *  lifetime management on its own.
 */
template <class T>
class TRefTracked
{
public:
#ifdef YT_ENABLE_REF_COUNTED_TRACKING
    TRefTracked()
    {
        auto cookie = GetRefCountedTypeCookie<T>();
        TRefCountedTrackerFacade::AllocateInstance(cookie);
    }

    TRefTracked(const TRefTracked&)
    {
        auto cookie = GetRefCountedTypeCookie<T>();
        TRefCountedTrackerFacade::AllocateInstance(cookie);
    }

    TRefTracked(TRefTracked&&)
    {
        auto cookie = GetRefCountedTypeCookie<T>();
        TRefCountedTrackerFacade::AllocateInstance(cookie);
    }

    ~TRefTracked()
    {
        auto cookie = GetRefCountedTypeCookie<T>();
        TRefCountedTrackerFacade::FreeInstance(cookie);
    }
#endif
};

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT

#define REF_TRACKED_INL_H_
#include "ref_tracked-inl.h"
#undef REF_TRACKED_INL_H_