Update CMakeLists.txt - play with openssl
[novacoin.git] / src / allocators.cpp
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.
4
5 #include "allocators.h"
6
7 #ifdef WIN32
8 #if (_WIN32_WINNT != _WIN32_WINNT_WIN7)
9 #define _WIN32_WINNT 0x601
10 #endif
11 #define WIN32_LEAN_AND_MEAN 1
12 #ifndef NOMINMAX
13 #define NOMINMAX
14 #endif
15 #include <windows.h>
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.
20 #else
21 #include <sys/mman.h>
22 #include <climits> // for PAGESIZE
23 #include <unistd.h> // for sysconf
24 #endif
25
26 LockedPageManager* LockedPageManager::_instance = nullptr;
27 std::once_flag LockedPageManager::init_flag{};
28
29 /** Determine system page size in bytes */
30 static inline size_t GetSystemPageSize()
31 {
32     size_t page_size;
33 #if defined(WIN32)
34     SYSTEM_INFO sSysInfo;
35     GetSystemInfo(&sSysInfo);
36     page_size = sSysInfo.dwPageSize;
37 #elif defined(PAGESIZE) // defined in limits.h
38     page_size = PAGESIZE;
39 #else // assume some POSIX OS
40     page_size = sysconf(_SC_PAGESIZE);
41 #endif
42     return page_size;
43 }
44
45 bool MemoryPageLocker::Lock(const void *addr, size_t len)
46 {
47 #ifdef WIN32
48     return VirtualLock(const_cast<void*>(addr), len) != 0;
49 #else
50     return mlock(addr, len) == 0;
51 #endif
52 }
53
54 bool MemoryPageLocker::Unlock(const void *addr, size_t len)
55 {
56 #ifdef WIN32
57     return VirtualUnlock(const_cast<void*>(addr), len) != 0;
58 #else
59     return munlock(addr, len) == 0;
60 #endif
61 }
62
63 LockedPageManager::LockedPageManager(): LockedPageManagerBase<MemoryPageLocker>(GetSystemPageSize())
64 {
65 }