Line data Source code
1 : // Copyright (c) 2009-2013 The Bitcoin Core developers
2 : // Distributed under the MIT software license, see the accompanying
3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 :
5 : #include "support/pagelocker.h"
6 :
7 : #if defined(HAVE_CONFIG_H)
8 : #include "config/bitcoin-config.h"
9 : #endif
10 :
11 : #ifdef WIN32
12 : #ifdef _WIN32_WINNT
13 : #undef _WIN32_WINNT
14 : #endif
15 : #define _WIN32_WINNT 0x0501
16 : #define WIN32_LEAN_AND_MEAN 1
17 : #ifndef NOMINMAX
18 : #define NOMINMAX
19 : #endif
20 : #include <windows.h>
21 : // This is used to attempt to keep keying material out of swap
22 : // Note that VirtualLock does not provide this as a guarantee on Windows,
23 : // but, in practice, memory that has been VirtualLock'd almost never gets written to
24 : // the pagefile except in rare circumstances where memory is extremely low.
25 : #else
26 : #include <sys/mman.h>
27 : #include <limits.h> // for PAGESIZE
28 : #include <unistd.h> // for sysconf
29 : #endif
30 :
31 : LockedPageManager* LockedPageManager::_instance = NULL;
32 : boost::once_flag LockedPageManager::init_flag = BOOST_ONCE_INIT;
33 :
34 : /** Determine system page size in bytes */
35 : static inline size_t GetSystemPageSize()
36 : {
37 : size_t page_size;
38 : #if defined(WIN32)
39 : SYSTEM_INFO sSysInfo;
40 : GetSystemInfo(&sSysInfo);
41 : page_size = sSysInfo.dwPageSize;
42 : #elif defined(PAGESIZE) // defined in limits.h
43 : page_size = PAGESIZE;
44 : #else // assume some POSIX OS
45 96 : page_size = sysconf(_SC_PAGESIZE);
46 : #endif
47 : return page_size;
48 : }
49 :
50 9628 : bool MemoryPageLocker::Lock(const void* addr, size_t len)
51 : {
52 : #ifdef WIN32
53 : return VirtualLock(const_cast<void*>(addr), len) != 0;
54 : #else
55 9628 : return mlock(addr, len) == 0;
56 : #endif
57 : }
58 :
59 9628 : bool MemoryPageLocker::Unlock(const void* addr, size_t len)
60 : {
61 : #ifdef WIN32
62 : return VirtualUnlock(const_cast<void*>(addr), len) != 0;
63 : #else
64 9628 : return munlock(addr, len) == 0;
65 : #endif
66 : }
67 :
68 192 : LockedPageManager::LockedPageManager() : LockedPageManagerBase<MemoryPageLocker>(GetSystemPageSize())
69 : {
70 426 : }
|