aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/remmap/remmap.h
blob: 7cb738f7aeb3ac07289973976fb29d77f68b481f (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
#pragma once

#include <util/system/yassert.h>
#include <util/system/align.h>
#include <util/system/info.h>
#include <util/system/filemap.h>
#include <util/memory/alloc.h>
#include <util/generic/noncopyable.h>

class TRemmapAllocation : TNonCopyable {
public:
    TRemmapAllocation();
    TRemmapAllocation(size_t size, char* base = nullptr);

    ~TRemmapAllocation() {
        Dealloc();
    }

    char* Alloc(size_t size, char* base = nullptr);
    char* Realloc(size_t newsize);
    void Dealloc();
    char* FullRealloc(size_t newsize);

#if defined(_unix_)
private:
    inline char* CommonMMap(size_t size, char* base = nullptr);

    char* Ptr_;
    size_t Size_;

public:
    inline void* Ptr() const {
        return (void*)Ptr_;
    }
    inline char* Data(ui32 pos = 0) const {
        return Ptr_ + pos;
    }
    inline size_t Size() const {
        return Size_;
    }
    inline void swap(TRemmapAllocation& other) {
        DoSwap(Ptr_, other.Ptr_);
        DoSwap(Size_, other.Size_);
    }

#else
private:
    TMappedAllocation Allocation_;

public:
    inline void* Ptr() const {
        return Allocation_.Ptr();
    }
    inline char* Data(ui32 pos = 0) const {
        return Allocation_.Data(pos);
    }
    inline size_t Size() const {
        return Allocation_.MappedSize();
    }
    inline void swap(TRemmapAllocation& other) {
        Allocation_.swap(other.Allocation_);
    }
#endif
};