Anoncoin  0.9.4
P2P Digital Currency
allocators.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2013 The Bitcoin developers
2 // Copyright (c) 2013-2014 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "allocators.h"
7 
8 #ifdef WIN32
9 #ifdef _WIN32_WINNT
10 #undef _WIN32_WINNT
11 #endif
12 #define _WIN32_WINNT 0x0501
13 #define WIN32_LEAN_AND_MEAN 1
14 #ifndef NOMINMAX
15 #define NOMINMAX
16 #endif
17 #include <windows.h>
18 // This is used to attempt to keep keying material out of swap
19 // Note that VirtualLock does not provide this as a guarantee on Windows,
20 // but, in practice, memory that has been VirtualLock'd almost never gets written to
21 // the pagefile except in rare circumstances where memory is extremely low.
22 #else
23 #include <sys/mman.h>
24 #include <limits.h> // for PAGESIZE
25 #include <unistd.h> // for sysconf
26 #endif
27 
29 boost::once_flag LockedPageManager::init_flag = BOOST_ONCE_INIT;
30 
32 static inline size_t GetSystemPageSize()
33 {
34  size_t page_size;
35 #if defined(WIN32)
36  SYSTEM_INFO sSysInfo;
37  GetSystemInfo(&sSysInfo);
38  page_size = sSysInfo.dwPageSize;
39 #elif defined(PAGESIZE) // defined in limits.h
40  page_size = PAGESIZE;
41 #else // assume some POSIX OS
42  page_size = sysconf(_SC_PAGESIZE);
43 #endif
44  return page_size;
45 }
46 
47 bool MemoryPageLocker::Lock(const void *addr, size_t len)
48 {
49 #ifdef WIN32
50  return VirtualLock(const_cast<void*>(addr), len);
51 #else
52  return mlock(addr, len) == 0;
53 #endif
54 }
55 
56 bool MemoryPageLocker::Unlock(const void *addr, size_t len)
57 {
58 #ifdef WIN32
59  return VirtualUnlock(const_cast<void*>(addr), len);
60 #else
61  return munlock(addr, len) == 0;
62 #endif
63 }
64 
66 {
67 }
68 
Thread-safe class to keep track of locked (ie, non-swappable) memory pages.
Definition: allocators.h:29
static boost::once_flag init_flag
Definition: allocators.h:161
bool Unlock(const void *addr, size_t len)
Unlock memory pages.
Definition: allocators.cpp:56
Singleton class to keep track of locked (ie, non-swappable) memory pages, for use in std::allocator t...
Definition: allocators.h:137
OS-dependent memory page locking/unlocking.
Definition: allocators.h:113
static LockedPageManager * _instance
Definition: allocators.h:160
bool Lock(const void *addr, size_t len)
Lock memory pages.
Definition: allocators.cpp:47