aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/memory_log/mmap.cpp
blob: 1fe734235eaf8fc942be584b37e2a043380fc1b6 (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
#include "memlog.h"

#if defined(_unix_)
#include <sys/mman.h>
#elif defined(_win_)
#include <util/system/winint.h>
#else
#error NO IMPLEMENTATION FOR THE PLATFORM
#endif

void TMemoryLog::TMMapArea::MMap(size_t amount) {
    Y_ABORT_UNLESS(amount > 0);

#if defined(_unix_)
    constexpr int mmapProt = PROT_READ | PROT_WRITE;
#if defined(_linux_)
    constexpr int mmapFlags = MAP_PRIVATE | MAP_ANON | MAP_POPULATE;
#else
    constexpr int mmapFlags = MAP_PRIVATE | MAP_ANON;
#endif

    BufPtr = ::mmap(nullptr, amount, mmapProt, mmapFlags, -1, 0);
    if (BufPtr == MAP_FAILED) {
        throw std::bad_alloc();
    }

#elif defined(_win_)
    Mapping = ::CreateFileMapping(
        (HANDLE)-1, nullptr, PAGE_READWRITE, 0, amount, nullptr);
    if (Mapping == NULL) {
        throw std::bad_alloc();
    }
    BufPtr = ::MapViewOfFile(Mapping, FILE_MAP_WRITE, 0, 0, amount);
    if (BufPtr == NULL) {
        throw std::bad_alloc();
    }
#endif

    Size = amount;
}

void TMemoryLog::TMMapArea::MUnmap() {
    if (BufPtr == nullptr) {
        return;
    }

#if defined(_unix_)
    int result = ::munmap(BufPtr, Size);
    Y_ABORT_UNLESS(result == 0);

#elif defined(_win_)
    BOOL result = ::UnmapViewOfFile(BufPtr);
    Y_ABORT_UNLESS(result != 0);

    result = ::CloseHandle(Mapping);
    Y_ABORT_UNLESS(result != 0);

    Mapping = 0;
#endif

    BufPtr = nullptr;
    Size = 0;
}