PRCYCoin  2.0.0.7rc1
P2P Digital Currency
allocators.cpp
Go to the documentation of this file.
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 #ifndef NOMINMAX
9 #define NOMINMAX
10 #endif
11 #include <windows.h>
12 // This is used to attempt to keep keying material out of swap
13 // Note that VirtualLock does not provide this as a guarantee on Windows,
14 // but, in practice, memory that has been VirtualLock'd almost never gets written to
15 // the pagefile except in rare circumstances where memory is extremely low.
16 #else
17 #include <limits.h> // for PAGESIZE
18 #include <sys/mman.h>
19 #include <unistd.h> // for sysconf
20 #endif
21 
23 boost::once_flag LockedPageManager::init_flag = BOOST_ONCE_INIT;
24 
26 static inline size_t GetSystemPageSize()
27 {
28  size_t page_size;
29 #if defined(WIN32)
30  SYSTEM_INFO sSysInfo;
31  GetSystemInfo(&sSysInfo);
32  page_size = sSysInfo.dwPageSize;
33 #elif defined(PAGESIZE) // defined in limits.h
34  page_size = PAGESIZE;
35 #else // assume some POSIX OS
36  page_size = sysconf(_SC_PAGESIZE);
37 #endif
38  return page_size;
39 }
40 
41 bool MemoryPageLocker::Lock(const void* addr, size_t len)
42 {
43 #ifdef WIN32
44  return VirtualLock(const_cast<void*>(addr), len) != 0;
45 #else
46  return mlock(addr, len) == 0;
47 #endif
48 }
49 
50 bool MemoryPageLocker::Unlock(const void* addr, size_t len)
51 {
52 #ifdef WIN32
53  return VirtualUnlock(const_cast<void*>(addr), len) != 0;
54 #else
55  return munlock(addr, len) == 0;
56 #endif
57 }
58 
60 {
61 }
LockedPageManager::init_flag
static boost::once_flag init_flag
Definition: allocators.h:160
LockedPageManager
Singleton class to keep track of locked (ie, non-swappable) memory pages, for use in std::allocator t...
Definition: allocators.h:136
LockedPageManagerBase
Thread-safe class to keep track of locked (ie, non-swappable) memory pages.
Definition: allocators.h:31
MemoryPageLocker::Unlock
bool Unlock(const void *addr, size_t len)
Unlock memory pages.
Definition: allocators.cpp:50
MemoryPageLocker::Lock
bool Lock(const void *addr, size_t len)
Lock memory pages.
Definition: allocators.cpp:41
LockedPageManager::LockedPageManager
LockedPageManager()
Definition: allocators.cpp:59
allocators.h
LockedPageManager::_instance
static LockedPageManager * _instance
Definition: allocators.h:159
MemoryPageLocker
OS-dependent memory page locking/unlocking.
Definition: allocators.h:112