Untitled


SUBMITTED BY: Guest

DATE: June 8, 2014, 8:34 p.m.

FORMAT: C++

SIZE: 2.0 kB

HITS: 1012

  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. #include "allocators.h"
  5. #ifdef WIN32
  6. #ifdef _WIN32_WINNT
  7. #undef _WIN32_WINNT
  8. #endif
  9. #define _WIN32_WINNT 0x0501
  10. #define WIN32_LEAN_AND_MEAN 1
  11. #ifndef NOMINMAX
  12. #define NOMINMAX
  13. #endif
  14. #include <windows.h>
  15. // This is used to attempt to keep keying material out of swap
  16. // Note that VirtualLock does not provide this as a guarantee on Windows,
  17. // but, in practice, memory that has been VirtualLock'd almost never gets written to
  18. // the pagefile except in rare circumstances where memory is extremely low.
  19. #else
  20. #include <sys/mman.h>
  21. #include <limits.h> // for PAGESIZE
  22. #include <unistd.h> // for sysconf
  23. #endif
  24. LockedPageManager* LockedPageManager::_instance = NULL;
  25. boost::once_flag LockedPageManager::init_flag = BOOST_ONCE_INIT;
  26. /** Determine system page size in bytes */
  27. static inline size_t GetSystemPageSize()
  28. {
  29. size_t page_size;
  30. #if defined(WIN32)
  31. SYSTEM_INFO sSysInfo;
  32. GetSystemInfo(&sSysInfo);
  33. page_size = sSysInfo.dwPageSize;
  34. #elif defined(PAGESIZE) // defined in limits.h
  35. page_size = PAGESIZE;
  36. #else // assume some POSIX OS
  37. page_size = sysconf(_SC_PAGESIZE);
  38. #endif
  39. return page_size;
  40. }
  41. bool MemoryPageLocker::Lock(const void *addr, size_t len)
  42. {
  43. #ifdef WIN32
  44. return VirtualLock(const_cast<void*>(addr), len);
  45. #else
  46. return mlock(addr, len) == 0;
  47. #endif
  48. }
  49. bool MemoryPageLocker::Unlock(const void *addr, size_t len)
  50. {
  51. #ifdef WIN32
  52. return VirtualUnlock(const_cast<void*>(addr), len);
  53. #else
  54. return munlock(addr, len) == 0;
  55. #endif
  56. }
  57. LockedPageManager::LockedPageManager() : LockedPageManagerBase<MemoryPageLocker>(GetSystemPageSize())
  58. {
  59. }

comments powered by Disqus