aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2018-03-09 07:13:07 -0500
committerJack Lloyd <[email protected]>2018-03-09 07:13:07 -0500
commit869909163d08144e69c7f2b11e8053d68a07d390 (patch)
treef6f82e2815d585c137a9ce9e59e1cbdc4b27a6af
parent5185c2aaa8bf9556556e4507869042a71eaba6c0 (diff)
Add OS::system_page_size
-rw-r--r--src/lib/utils/os_utils.cpp23
-rw-r--r--src/lib/utils/os_utils.h6
2 files changed, 25 insertions, 4 deletions
diff --git a/src/lib/utils/os_utils.cpp b/src/lib/utils/os_utils.cpp
index 769615543..f2813b323 100644
--- a/src/lib/utils/os_utils.cpp
+++ b/src/lib/utils/os_utils.cpp
@@ -197,6 +197,24 @@ uint64_t OS::get_system_timestamp_ns()
return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
}
+size_t OS::system_page_size()
+ {
+#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
+ long p = ::sysconf(_SC_PAGESIZE);
+ if(p > 1)
+ return static_cast<size_t>(p);
+ else
+ return 4096;
+#elif defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK)
+ SYSTEM_INFO sys_info;
+ ::GetSystemInfo(&sys_info);
+ return sys_info.dwPageSize;
+#endif
+
+ // default value
+ return 4096;
+ }
+
size_t OS::get_memory_locking_limit()
{
#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
@@ -255,16 +273,13 @@ size_t OS::get_memory_locking_limit()
return 0;
}
- SYSTEM_INFO sSysInfo;
- ::GetSystemInfo(&sSysInfo);
-
// According to Microsoft MSDN:
// The maximum number of pages that a process can lock is equal to the number of pages in its minimum working set minus a small overhead
// In the book "Windows Internals Part 2": the maximum lockable pages are minimum working set size - 8 pages
// But the information in the book seems to be inaccurate/outdated
// I've tested this on Windows 8.1 x64, Windows 10 x64 and Windows 7 x86
// On all three OS the value is 11 instead of 8
- size_t overhead = sSysInfo.dwPageSize * 11ULL;
+ size_t overhead = OS::system_page_size() * 11ULL;
if(working_min > overhead)
{
size_t lockable_bytes = working_min - overhead;
diff --git a/src/lib/utils/os_utils.h b/src/lib/utils/os_utils.h
index feccdbe73..5210b2523 100644
--- a/src/lib/utils/os_utils.h
+++ b/src/lib/utils/os_utils.h
@@ -68,6 +68,12 @@ uint64_t BOTAN_TEST_API get_system_timestamp_ns();
size_t get_memory_locking_limit();
/**
+* Return the size of a memory page, if that can be derived on the
+* current system. Otherwise returns some default value (eg 4096)
+*/
+size_t system_page_size();
+
+/**
* Request so many bytes of page-aligned RAM locked into memory using
* mlock, VirtualLock, or similar. Returns null on failure. The memory
* returned is zeroed. Free it with free_locked_pages.