Master Core  v0.0.9 - 49a5c0d97abf09ef2911ddfe8d9551df59f9efd3-dirty
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 #ifdef _WIN32_WINNT
9 #undef _WIN32_WINNT
10 #endif
11 #define _WIN32_WINNT 0x0501
12 #define WIN32_LEAN_AND_MEAN 1
13 #ifndef NOMINMAX
14 #define NOMINMAX
15 #endif
16 #include <windows.h>
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.
21 #else
22 #include <sys/mman.h>
23 #include <limits.h> // for PAGESIZE
24 #include <unistd.h> // for sysconf
25 #endif
26 
28 boost::once_flag LockedPageManager::init_flag = BOOST_ONCE_INIT;
29 
31 static inline size_t GetSystemPageSize()
32 {
33  size_t page_size;
34 #if defined(WIN32)
35  SYSTEM_INFO sSysInfo;
36  GetSystemInfo(&sSysInfo);
37  page_size = sSysInfo.dwPageSize;
38 #elif defined(PAGESIZE) // defined in limits.h
39  page_size = PAGESIZE;
40 #else // assume some POSIX OS
41  page_size = sysconf(_SC_PAGESIZE);
42 #endif
43  return page_size;
44 }
45 
46 bool MemoryPageLocker::Lock(const void *addr, size_t len)
47 {
48 #ifdef WIN32
49  return VirtualLock(const_cast<void*>(addr), len);
50 #else
51  return mlock(addr, len) == 0;
52 #endif
53 }
54 
55 bool MemoryPageLocker::Unlock(const void *addr, size_t len)
56 {
57 #ifdef WIN32
58  return VirtualUnlock(const_cast<void*>(addr), len);
59 #else
60  return munlock(addr, len) == 0;
61 #endif
62 }
63 
65 {
66 }
67 
Thread-safe class to keep track of locked (ie, non-swappable) memory pages.
Definition: allocators.h:28
static boost::once_flag init_flag
Definition: allocators.h:160
bool Unlock(const void *addr, size_t len)
Unlock memory pages.
Definition: allocators.cpp:55
static size_t GetSystemPageSize()
Determine system page size in bytes.
Definition: allocators.cpp:31
Singleton class to keep track of locked (ie, non-swappable) memory pages, for use in std::allocator t...
Definition: allocators.h:136
OS-dependent memory page locking/unlocking.
Definition: allocators.h:112
static LockedPageManager * _instance
Definition: allocators.h:159
bool Lock(const void *addr, size_t len)
Lock memory pages.
Definition: allocators.cpp:46