aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/memory/chunked_memory_allocator.cpp
blob: 3560f8096d91c56415481b38e919fc096d328128 (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
#include "chunked_memory_allocator.h"
#include "serialize.h"

namespace NYT {

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

const i64 TChunkedMemoryAllocator::DefaultChunkSize = 4096;
const double TChunkedMemoryAllocator::DefaultMaxSmallBlockSizeRatio = 0.25;

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

TChunkedMemoryAllocator::TChunkedMemoryAllocator(
    i64 chunkSize,
    double maxSmallBlockSizeRatio,
    TRefCountedTypeCookie tagCookie)
    : ChunkSize_(chunkSize)
    , MaxSmallBlockSize_(static_cast<i64>(ChunkSize_ * maxSmallBlockSizeRatio))
    , TagCookie_(tagCookie)
{  }

TSharedMutableRef TChunkedMemoryAllocator::AllocateUnalignedSlow(i64 size)
{
    auto large = AllocateSlowCore(size);
    if (large) {
        return large;
    }
    return AllocateUnaligned(size);
}

TSharedMutableRef TChunkedMemoryAllocator::AllocateAlignedSlow(i64 size, int align)
{
    // NB: Do not rely on any particular alignment of chunks.
    auto large = AllocateSlowCore(size + align);
    if (large) {
        auto* alignedBegin = AlignUp(large.Begin(), align);
        return large.Slice(alignedBegin, alignedBegin + size);
    }
    return AllocateAligned(size, align);
}

TSharedMutableRef TChunkedMemoryAllocator::AllocateSlowCore(i64 size)
{
    if (size > MaxSmallBlockSize_) {
        return TSharedMutableRef::Allocate(size, {.InitializeStorage = false}, TagCookie_);
    }

    Chunk_ = TSharedMutableRef::Allocate(ChunkSize_, {.InitializeStorage = false}, TagCookie_);
    FreeZoneBegin_ = Chunk_.Begin();
    FreeZoneEnd_ = Chunk_.End();

    return TSharedMutableRef();
}

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

} // namespace NYT