1 // Copyright (c) 2009-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "allocators.h"
8 #if (_WIN32_WINNT != _WIN32_WINNT_WIN7)
9 #define _WIN32_WINNT 0x601
11 #define WIN32_LEAN_AND_MEAN 1
16 // This is used to attempt to keep keying material out of swap
17 // Note that VirtualLock does not provide this as a guarantee on Windows,
18 // but, in practice, memory that has been VirtualLock'd almost never gets written to
19 // the pagefile except in rare circumstances where memory is extremely low.
22 #include <climits> // for PAGESIZE
23 #include <unistd.h> // for sysconf
26 LockedPageManager* LockedPageManager::_instance = nullptr;
27 std::once_flag LockedPageManager::init_flag{};
29 /** Determine system page size in bytes */
30 static inline size_t GetSystemPageSize()
35 GetSystemInfo(&sSysInfo);
36 page_size = sSysInfo.dwPageSize;
37 #elif defined(PAGESIZE) // defined in limits.h
39 #else // assume some POSIX OS
40 page_size = sysconf(_SC_PAGESIZE);
45 bool MemoryPageLocker::Lock(const void *addr, size_t len)
48 return VirtualLock(const_cast<void*>(addr), len) != 0;
50 return mlock(addr, len) == 0;
54 bool MemoryPageLocker::Unlock(const void *addr, size_t len)
57 return VirtualUnlock(const_cast<void*>(addr), len) != 0;
59 return munlock(addr, len) == 0;
63 LockedPageManager::LockedPageManager(): LockedPageManagerBase<MemoryPageLocker>(GetSystemPageSize())