Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cMemPage.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cMemPage_H
7 #define _INC_cMemPage_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cSingleton.h"
13 #include "cArraySortRef.h"
14 #include "cSystemInfo.h"
15 
16 namespace Gray
17 {
18  class cMemPage : public cRefBase
19  {
23 
24  friend class cMemPageMgr;
25 
26  public:
27  cMemPage(UINT_PTR nPageStart, size_t nPageSize)
28  : m_nPageStart(nPageStart)
29  , m_nPageSize(nPageSize)
31  , m_nRefCount2(1)
32  {
33  ASSERT(get_SortValue() != 0);
34  ASSERT((get_SortValue() % m_nPageSize) == 0);
35  }
36  virtual ~cMemPage() noexcept
37  {
38  }
39 
40  UINT_PTR get_SortValue() const noexcept
41  {
42  return m_nPageStart;
43  }
44  bool IsOverlapped(UINT_PTR p, size_t n) const noexcept
45  {
46  if ((p + n) <= m_nPageStart)
47  return false;
48  if ((m_nPageStart + m_nPageSize) <= p)
49  return false;
50  return true;
51  }
52  bool SetProtect(bool bProtect) noexcept
53  {
55 #ifdef __linux__
56  ::mprotect((void*)m_nPageStart, m_nPageSize - 1, PROT_READ | PROT_WRITE | PROT_EXEC);
57 #else
58  DWORD dwNewProtectionFlags = bProtect ? m_dwOldProtectionFlags : PAGE_EXECUTE_READWRITE;
59  return ::VirtualProtect((void*)m_nPageStart, m_nPageSize - 1, dwNewProtectionFlags, &m_dwOldProtectionFlags);
60 #endif
61  }
62 
63  public:
64  UINT_PTR m_nPageStart;
65  size_t m_nPageSize;
68  };
70 
71  class cMemPageMgr : public cSingleton<cMemPageMgr>
72  {
76 
77  public:
79  : cSingleton<cMemPageMgr>(this, typeid(cMemPageMgr))
80  , m_dwPageSize(0)
81  {
82  }
83  virtual ~cMemPageMgr()
84  {
85  // Make sure this stuff doesnt get destroyed too early.
86  }
87 
88  HRESULT ProtectPages(const void* p, size_t nSize, bool bProtect)
89  {
91  if (m_dwPageSize == 0)
92  {
93  m_dwPageSize = cSystemInfo::I().m_SystemInfo.dwPageSize;
95  }
96 
97  const UINT_PTR nStart = ((UINT_PTR)p);
98  const UINT_PTR nEnd = nStart + nSize;
99  const UINT_PTR nPageOver = nStart % m_dwPageSize;
100  UINT_PTR nPageStart = nStart - nPageOver;
101  for (; nPageStart < nEnd; nPageStart += m_dwPageSize)
102  {
103  cMemPagePtr pPage = m_aPages.FindArgForKey(nPageStart);
104  if (bProtect)
105  {
106  if (pPage == nullptr)
107  {
108  // odd
109  // DEBUG_ERR(("ProtectPages bProtect = nullptr"));
110  continue;
111  }
112  if (--pPage->m_nRefCount2)
113  continue;
114  m_aPages.RemoveArg(pPage);
115  if (!pPage->SetProtect(true))
116  {
117  // DEBUG_ERR(("ProtectPages SetProtect true"));
118  return E_FAIL;
119  }
120  }
121  else
122  {
123  if (pPage == nullptr)
124  {
125  pPage = new cMemPage(nPageStart, m_dwPageSize);
126  ASSERT(pPage);
127  if (!pPage->SetProtect(false))
128  {
129  // DEBUG_ERR(("ProtectPages SetProtect false"));
130  return E_FAIL;
131  }
132  m_aPages.Add(pPage);
133  }
134  else
135  {
136  pPage->m_nRefCount2++;
137  }
138  }
139  }
140  return S_OK;
141  }
142 
145  };
146 }
147 
148 #endif
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
Definition: cArraySortRef.h:113
Definition: cMemPage.h:72
cArraySortValue< cMemPage, UINT_PTR > m_aPages
Definition: cMemPage.h:144
virtual ~cMemPageMgr()
Definition: cMemPage.h:83
cMemPageMgr()
Definition: cMemPage.h:78
DWORD m_dwPageSize
Definition: cMemPage.h:143
HRESULT ProtectPages(const void *p, size_t nSize, bool bProtect)
Definition: cMemPage.h:88
Definition: cMemPage.h:19
int m_nRefCount2
ProtectPages count.
Definition: cMemPage.h:67
virtual ~cMemPage() noexcept
Definition: cMemPage.h:36
bool SetProtect(bool bProtect) noexcept
Definition: cMemPage.h:52
cMemPage(UINT_PTR nPageStart, size_t nPageSize)
Definition: cMemPage.h:27
DWORD m_dwOldProtectionFlags
original flags used/returned by _WIN32 VirtualProtect()
Definition: cMemPage.h:66
UINT_PTR get_SortValue() const noexcept
Definition: cMemPage.h:40
bool IsOverlapped(UINT_PTR p, size_t n) const noexcept
Definition: cMemPage.h:44
UINT_PTR m_nPageStart
Always aligned to dwPageSize.
Definition: cMemPage.h:64
size_t m_nPageSize
SystemInfo::dwPageSize.
Definition: cMemPage.h:65
Definition: cRefPtr.h:22
Definition: cRefPtr.h:225
Definition: cSingleton.h:127
static cSystemInfo &__stdcall I()
Definition: cSingleton.h:199
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
cRefPtr< cMemPage > cMemPagePtr
Definition: cMemPage.h:69