Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cWinHeap.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cWinHeap_H
7 #define _INC_cWinHeap_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cOSHandle.h"
13 #include "cUnitTestDecl.h"
14 
15 #if defined(_WIN32) && ! defined(UNDER_CE)
16 
17 namespace Gray
18 {
19  class GRAYCORE_LINK cWinHeap
20  {
23 
24  private:
25  HANDLE m_hHeap;
26  bool m_bManaged;
27 
28  public:
29  cWinHeap() noexcept
30  : m_hHeap(::GetProcessHeap())
31  , m_bManaged(false) // This belongs to the process. leave it.
32  {
34  }
35  cWinHeap(HANDLE hHeap, bool bManaged = true) noexcept
36  : m_hHeap(hHeap)
37  , m_bManaged(bManaged)
38  {
40  }
41  cWinHeap(DWORD flOptions, SIZE_T dwInitialSize = 0, SIZE_T dwMaximumSize = 0) noexcept
42  : m_hHeap(::HeapCreate(flOptions, dwInitialSize, dwMaximumSize))
43  , m_bManaged(true)
44  {
46  }
47  ~cWinHeap() noexcept
48  {
49  if (m_bManaged)
50  {
51  ::HeapDestroy(m_hHeap);
52  }
53  }
54 
55  HANDLE get_Handle() const noexcept
56  {
57  return m_hHeap;
58  }
59  bool isValidHeap() const noexcept
60  {
62  return m_hHeap != HANDLE_NULL;
63  }
64  SIZE_T Compact(DWORD dwFlags = 0) const noexcept
65  {
68  return ::HeapCompact(m_hHeap, dwFlags);
69  }
70 
71 #ifndef UNDER_CE
72  bool Lock()
73  {
75  return ::HeapLock(m_hHeap);
76  }
77  bool Unlock()
78  {
80  return ::HeapUnlock(m_hHeap);
81  }
82  bool QueryInformation(HEAP_INFORMATION_CLASS HeapInformationClass, void* pData, SIZE_T* pnSizeData) noexcept
83  {
84  // HeapInformationClass = HeapEnableTerminationOnCorruption, HeapCompatibilityInformation
85  return ::HeapQueryInformation(m_hHeap, HeapInformationClass, pData, *pnSizeData, pnSizeData);
86  }
87  bool SetInformation(HEAP_INFORMATION_CLASS HeapInformationClass, const void* pData, SIZE_T nSizeData) noexcept
88  {
89  // HeapInformationClass = HeapEnableTerminationOnCorruption, HeapCompatibilityInformation
90  return ::HeapSetInformation(m_hHeap, HeapInformationClass, const_cast<void*>(pData), nSizeData);
91  }
92 #endif
93 
94 #if 0
95  // HeapWalk
96 #endif
97 
98  bool IsValidHeapPtr(void* pMem = nullptr, DWORD dwFlags = 0) const noexcept
99  {
102  return ::HeapValidate(m_hHeap, dwFlags, pMem);
103  }
104 
105  void* AllocPtr(SIZE_T dwBytes, DWORD dwFlags = 0)
106  {
108  return ::HeapAlloc(m_hHeap, dwFlags, dwBytes);
109  }
110  void* ReAllocPtr(void* pMem, SIZE_T dwBytes, DWORD dwFlags = 0)
111  {
113  return ::HeapReAlloc(m_hHeap, dwFlags, pMem, dwBytes);
114  }
115  bool FreePtr(void* pMem, DWORD dwFlags = 0)
116  {
118  return ::HeapFree(m_hHeap, dwFlags, pMem);
119  }
120  SIZE_T GetAllocPtrSize(const void* pMem, DWORD dwFlags = 0) const noexcept
121  {
124  return ::HeapSize(m_hHeap, dwFlags, pMem);
125  }
126 
127  UNITTEST_FRIEND(cWinHeap);
128  };
129 }
130 
131 #endif
132 #endif // _INC_cWinHeap_H
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define HANDLE_NULL
Invalid OS handle for _WIN32. Not invalid OS handle for linux.
Definition: cOSHandle.h:21
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14