aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/memory/leaky_singleton-inl.h
blob: d8662a4b07f4a4b4409333f9b5ffbf99c90f6adf (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
#ifndef LEAKY_SINGLETON_INL_H_
#error "Direct inclusion of this file is not allowed, include leaky_singleton.h"
// For the sake of sane code completion.
#include "leaky_singleton.h"
#endif

#ifdef _asan_enabled_
#include <sanitizer/lsan_interface.h>
#endif

#include <utility>

namespace NYT {

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

template <class T>
template <class... TArgs>
TLeakyStorage<T>::TLeakyStorage(TArgs&&... args)
{
#ifdef _asan_enabled_
    __lsan_disable();
#endif
    new (Get()) T(std::forward<TArgs>(args)...);
#ifdef _asan_enabled_
    __lsan_enable();
#endif
}

template <class T>
T* TLeakyStorage<T>::Get()
{
    return reinterpret_cast<T*>(Buffer_);
}

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

template <class T, class... TArgs>
T* LeakySingleton(TArgs&&... args)
{
    static TLeakyStorage<T> Storage(std::forward<TArgs>(args)...);
    return Storage.Get();
}

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

} // namespace NYT