| 20 | #include <limits> |
| 21 | |
| 22 | void LockMemory(const void* addr, size_t len) { |
| 23 | #if defined(_unix_) |
| 24 | if (0 == len) { |
| 25 | return; |
| 26 | } |
| 27 | Y_ASSERT(static_cast<ssize_t>(len) > 0); |
| 28 | const size_t pageSize = NSystemInfo::GetPageSize(); |
| 29 | const char* begin = AlignDown(static_cast<const char*>(addr), pageSize); |
| 30 | const char* end = AlignUp(static_cast<const char*>(addr) + len, pageSize); |
| 31 | if (mlock(begin, end - begin)) { |
| 32 | ythrow yexception() << LastSystemErrorText(); |
| 33 | } |
| 34 | #elif defined(_win_) |
| 35 | HANDLE hndl = GetCurrentProcess(); |
| 36 | SIZE_T min, max; |
| 37 | if (!GetProcessWorkingSetSize(hndl, &min, &max)) { |
| 38 | ythrow yexception() << LastSystemErrorText(); |
| 39 | } |
| 40 | if (!SetProcessWorkingSetSize(hndl, min + len, max + len)) { |
| 41 | ythrow yexception() << LastSystemErrorText(); |
| 42 | } |
| 43 | if (!VirtualLock((LPVOID)addr, len)) { |
| 44 | ythrow yexception() << LastSystemErrorText(); |
| 45 | } |
| 46 | #endif |
| 47 | } |
| 48 | |
| 49 | void UnlockMemory(const void* addr, size_t len) { |
| 50 | #if defined(_unix_) |
no test coverage detected