aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/mlock.cpp
blob: 03e68d8f5096c7c3e40c0c83d1654ba4f70772b0 (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
#include <util/generic/yexception.h>
#include "align.h"
#include "error.h"
#include "info.h"
#include "mlock.h"

#if defined(_unix_)
    #include <sys/mman.h> 
    #if !defined(MCL_ONFAULT) && defined(MCL_FUTURE) // Old glibc. 
        #define MCL_ONFAULT (MCL_FUTURE << 1) 
    #endif 
    #if defined(_android_) 
        #include <sys/syscall.h> 
        #define munlockall() syscall(__NR_munlockall) 
    #endif 
#else
    #include "winint.h" 
#endif

void LockMemory(const void* addr, size_t len) { 
#if defined(_unix_)
    const size_t pageSize = NSystemInfo::GetPageSize();
    if (mlock(AlignDown(addr, pageSize), AlignUp(len, pageSize))) { 
        ythrow yexception() << LastSystemErrorText();
    } 
#elif defined(_win_)
    HANDLE hndl = GetCurrentProcess();
    SIZE_T min, max;
    if (!GetProcessWorkingSetSize(hndl, &min, &max))
        ythrow yexception() << LastSystemErrorText();
    if (!SetProcessWorkingSetSize(hndl, min + len, max + len))
        ythrow yexception() << LastSystemErrorText();
    if (!VirtualLock((LPVOID)addr, len))
        ythrow yexception() << LastSystemErrorText();
#endif
}

void UnlockMemory(const void* addr, size_t len) { 
#if defined(_unix_)
    if (munlock(addr, len)) { 
        ythrow yexception() << LastSystemErrorText();
    } 
#elif defined(_win_)
    HANDLE hndl = GetCurrentProcess();
    SIZE_T min, max;
    if (!GetProcessWorkingSetSize(hndl, &min, &max))
        ythrow yexception() << LastSystemErrorText();
    if (!SetProcessWorkingSetSize(hndl, min - len, max - len))
        ythrow yexception() << LastSystemErrorText();
    if (!VirtualUnlock((LPVOID)addr, len))
        ythrow yexception() << LastSystemErrorText();
#endif
}

void LockAllMemory(ELockAllMemoryFlags flags) {
    Y_UNUSED(flags);
#if defined(_android_)
// unimplemented 
#elif defined(_cygwin_)
// unimplemented 
#elif defined(_unix_)
    int sys_flags = 0;
    if (flags & LockCurrentMemory) {
        sys_flags |= MCL_CURRENT;
    }
    if (flags & LockFutureMemory) {
        sys_flags |= MCL_FUTURE;
    }
    if (flags & LockMemoryOnFault) {
        sys_flags |= MCL_ONFAULT;
    }
    if (mlockall(sys_flags)) {
        ythrow yexception() << LastSystemErrorText();
    }
#endif
}

void UnlockAllMemory() { 
#if defined(_cygwin_) 
// unimplemented 
#elif defined(_unix_) 
    if (munlockall()) { 
        ythrow yexception() << LastSystemErrorText();
    } 
#endif
}