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"
11 #define _WIN32_WINNT 0x0501
12 #define WIN32_LEAN_AND_MEAN 1
17 // This is used to attempt to keep keying material out of swap
18 // Note that VirtualLock does not provide this as a guarantee on Windows,
19 // but, in practice, memory that has been VirtualLock'd almost never gets written to
20 // the pagefile except in rare circumstances where memory is extremely low.
23 #include <limits.h> // for PAGESIZE
24 #include <unistd.h> // for sysconf
27 LockedPageManager* LockedPageManager::_instance = NULL;
28 boost::once_flag LockedPageManager::init_flag = BOOST_ONCE_INIT;
30 /** Determine system page size in bytes */
31 static inline size_t GetSystemPageSize()
36 GetSystemInfo(&sSysInfo);
37 page_size = sSysInfo.dwPageSize;
38 #elif defined(PAGESIZE) // defined in limits.h
40 #else // assume some POSIX OS
41 page_size = sysconf(_SC_PAGESIZE);
46 bool MemoryPageLocker::Lock(const void *addr, size_t len)
49 return VirtualLock(const_cast<void*>(addr), len);
51 return mlock(addr, len) == 0;
55 bool MemoryPageLocker::Unlock(const void *addr, size_t len)
58 return VirtualUnlock(const_cast<void*>(addr), len);
60 return munlock(addr, len) == 0;
64 LockedPageManager::LockedPageManager() : LockedPageManagerBase<MemoryPageLocker>(GetSystemPageSize())