aboutsummaryrefslogtreecommitdiffstats
path: root/yt/yt/core/misc/pool_allocator-inl.h
blob: 4d33816df49a67e6c27bf2cd1b15f665106bd02d (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
112
#ifndef POOL_ALLOCATOR_INL_H_
#error "Direct inclusion of this file is not allowed, include pool_allocator.h"
// For the sake of sane code completion.
#include "pool_allocator.h"
#endif

#include <library/cpp/yt/misc/tls.h>

#include <util/system/align.h>

namespace NYT {

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

struct TPoolAllocator::TAllocatedBlockHeader
{
    explicit TAllocatedBlockHeader(TPoolAllocator* pool)
        : Pool(pool)
    { }

    TPoolAllocator* Pool;
};

struct TPoolAllocator::TFreeBlockHeader
{
    explicit TFreeBlockHeader(TFreeBlockHeader* next)
        : Next(next)
    { }

    TFreeBlockHeader* Next;
};

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

inline TPoolAllocator::TPoolAllocator(
    size_t blockSize,
    size_t blockAlignment,
    size_t chunkSize,
    TRefCountedTypeCookie cookie)
    : BlockSize_(blockSize)
    , BlockAlignment_(blockAlignment)
    , ChunkSize_(chunkSize)
    , Cookie_(cookie)
{ }

inline void* TPoolAllocator::Allocate() noexcept
{
    VERIFY_THREAD_AFFINITY(HomeThread);

    if (Y_UNLIKELY(!FirstFree_)) {
        AllocateChunk();
    }

    auto* freeHeader = FirstFree_;
    FirstFree_ = freeHeader->Next;

    auto* ptr = reinterpret_cast<char*>(freeHeader + 1);
    auto* allocatedHeader = reinterpret_cast<TAllocatedBlockHeader*>(ptr) - 1;
    new(allocatedHeader) TAllocatedBlockHeader(this);
    return ptr;
}

inline void TPoolAllocator::Free(void* ptr) noexcept
{
    auto* header = static_cast<TAllocatedBlockHeader*>(ptr) - 1;
    header->Pool->DoFree(ptr);
}

template <std::derived_from<TPoolAllocator::TObjectBase> T, class... TArgs>
std::unique_ptr<T> TPoolAllocator::New(TArgs&&... args)
{
    struct TChunkTag
    { };
    constexpr auto ChunkSize = 64_KB;
    YT_THREAD_LOCAL(TPoolAllocator) Allocator(
        sizeof(T),
        alignof(T),
        ChunkSize,
        GetRefCountedTypeCookie<TChunkTag>());

    return std::unique_ptr<T>(new(&GetTlsRef(Allocator)) T(std::forward<TArgs>(args)...));
}

inline void TPoolAllocator::DoFree(void* ptr)
{
    VERIFY_THREAD_AFFINITY(HomeThread);

    auto* header = static_cast<TFreeBlockHeader*>(ptr) - 1;
    new(header) TFreeBlockHeader(FirstFree_);
    FirstFree_ = header;
}

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

inline void* TPoolAllocator::TObjectBase::operator new(size_t /*size*/, TPoolAllocator* allocator) noexcept
{
    return allocator->Allocate();
}

inline void* TPoolAllocator::TObjectBase::operator new(size_t /*size*/, void* where) noexcept
{
    return where;
}

inline void TPoolAllocator::TObjectBase::operator delete(void* ptr) noexcept
{
    TPoolAllocator::Free(ptr);
}

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

} // namespace NYT