diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/system/mlock.cpp | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/system/mlock.cpp')
-rw-r--r-- | util/system/mlock.cpp | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/util/system/mlock.cpp b/util/system/mlock.cpp new file mode 100644 index 0000000000..435338c98f --- /dev/null +++ b/util/system/mlock.cpp @@ -0,0 +1,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 +} |