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

#include "serialize.h"

#include <util/system/align.h>

namespace NYT {

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

inline TSharedMutableRef TChunkedMemoryAllocator::AllocateUnaligned(i64 size)
{
    // Fast path.
    if (FreeZoneEnd_ >= FreeZoneBegin_ + size) {
        FreeZoneEnd_ -= size;
        return Chunk_.Slice(FreeZoneEnd_, FreeZoneEnd_ + size);
    }

    // Slow path.
    return AllocateUnalignedSlow(size);
}

inline TSharedMutableRef TChunkedMemoryAllocator::AllocateAligned(i64 size, int align)
{
    // NB: This can lead to FreeZoneBegin_ >= FreeZoneEnd_ in which case the chunk is full.
    FreeZoneBegin_ = AlignUp(FreeZoneBegin_, align);

    // Fast path.
    if (FreeZoneBegin_ + size <= FreeZoneEnd_) {
        FreeZoneBegin_ += size;
        return Chunk_.Slice(FreeZoneBegin_ - size, FreeZoneBegin_);
    }

    // Slow path.
    return AllocateAlignedSlow(size, align);
}

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

} // namespace NYT