diff options
author | yurial <yurial@yandex-team.com> | 2022-08-30 14:22:15 +0300 |
---|---|---|
committer | yurial <yurial@yandex-team.com> | 2022-08-30 14:22:15 +0300 |
commit | 810fc0836b07bb8591a0e8a693e600eab2795a8b (patch) | |
tree | bb23850c4fb38ddc821d08a5342603198a9c64d6 /util/system/mlock.cpp | |
parent | 76dc9dab1ef4e4b4fcae17e5ae188e1c3c9774ce (diff) | |
download | ydb-810fc0836b07bb8591a0e8a693e600eab2795a8b.tar.gz |
fixup size of aligned memory region to mlock []
Если мы сдвигаем addr в меньшую сторону, то len мы должны увеличить на столько же. В противном случае, мы можем залочить меньше, чем нужно.
Diffstat (limited to 'util/system/mlock.cpp')
-rw-r--r-- | util/system/mlock.cpp | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/util/system/mlock.cpp b/util/system/mlock.cpp index 435338c98f..27895bbacc 100644 --- a/util/system/mlock.cpp +++ b/util/system/mlock.cpp @@ -17,10 +17,18 @@ #include "winint.h" #endif +#include <limits> + void LockMemory(const void* addr, size_t len) { #if defined(_unix_) + if (0 == len) { + return; + } + Y_ASSERT(static_cast<ssize_t>(len) > 0); const size_t pageSize = NSystemInfo::GetPageSize(); - if (mlock(AlignDown(addr, pageSize), AlignUp(len, pageSize))) { + const char* begin = AlignDown(static_cast<const char*>(addr), pageSize); + const char* end = AlignUp(static_cast<const char*>(addr) + len, pageSize); + if (mlock(begin, end - begin)) { ythrow yexception() << LastSystemErrorText(); } #elif defined(_win_) @@ -37,7 +45,14 @@ void LockMemory(const void* addr, size_t len) { void UnlockMemory(const void* addr, size_t len) { #if defined(_unix_) - if (munlock(addr, len)) { + if (0 == len) { + return; + } + Y_ASSERT(static_cast<ssize_t>(len) > 0); + const size_t pageSize = NSystemInfo::GetPageSize(); + const char* begin = AlignDown(static_cast<const char*>(addr), pageSize); + const char* end = AlignUp(static_cast<const char*>(addr) + len, pageSize); + if (munlock(begin, end - begin)) { ythrow yexception() << LastSystemErrorText(); } #elif defined(_win_) |