Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cHeapObject.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cHeapObject_H
7 #define _INC_cHeapObject_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cHeap.h" // cHeap
13 
14 #if defined(_DEBUG) || defined(_DEBUG_FAST)
15 #define USE_HEAPSIG
16 #endif
17 
18 namespace Gray
19 {
21  {
26 
28 
29  virtual const void* get_HeapPtr() const noexcept = 0;
30 
31  // Add this to each IHeapObject rooted object to get the base heap allocation pointer. Avoids problems with multiple inheritance and heap allocated objects.
32 #define CHEAPOBJECT_IMPL const void* get_HeapPtr() const noexcept override { return this; }
33  };
34 
35  //*************************************************
36 
38  {
45 
46  protected:
47  // Get the top level malloc() pointer in the case of multiple inheritance.
49 #ifdef USE_HEAPSIG
50  cMemSignature<> m_Sig;
51 #endif
52 
53  public:
55  {
57  }
58  virtual ~cHeapObject()
59  {
62  }
63  bool IsValidInsideN(INT_PTR index) const
64  {
66 #ifdef USE_HEAPSIG
67  if (!m_Sig.isValidSignature())
68  return false;
69 #endif
70  const void* pBase = get_HeapPtr();
71  return cHeapAlign::IsValidInside(pBase, index);
72  }
73  bool IsValidInsidePtr(void const* pTest) const
74  {
76  if (pTest == nullptr)
77  return false;
78 #ifdef USE_HEAPSIG
79  if (!m_Sig.isValidSignature())
80  return false;
81 #endif
82  const void* pBase = get_HeapPtr();
83  return cHeapAlign::IsValidInside(pBase, cMem::Diff(pTest, pBase));
84  }
85  virtual size_t GetHeapStatsThis(OUT ITERATE_t& iAllocCount) const
86  {
88 #ifdef USE_HEAPSIG
89  ASSERT(m_Sig.isValidSignature());
90 #endif
91  iAllocCount++;
92  return cHeapAlign::GetSize(get_HeapPtr());
93  }
94  virtual bool isValidCheck() const noexcept
95  {
97  if (!cMem::IsValidApp(this)) // NOT be based on nullptr ? sanity check.
98  return false;
99 #ifdef USE_HEAPSIG
100  if (!m_Sig.isValidSignature())
101  return false;
102 #endif
103  if (!cHeapAlign::IsValidHeap(get_HeapPtr())) // might be aligned.
104  return false;
105  return true;
106  }
107  };
108 
109  // Create an ^2 aligned pool for allocation of these objects.
110 #define DECLARE_HEAP_ALIGNED_ALLOCN(_CLASS,_IALIGN) public: \
111  static void* operator new( size_t nCount) \
112  { return cHeapAlign::AllocPtr( nCount, _IALIGN ); } \
113  static void operator delete(void* pData) \
114  { cHeapAlign::FreePtr(pData); }
115 
116 #define DECLARE_HEAP_ALIGNED_ALLOC(_CLASS) DECLARE_HEAP_ALIGNED_ALLOCN(_CLASS,__alignof( _CLASS ))
117 };
118 
119 #endif // _INC_cHeapObject_H
#define IGNORE_WARN_INTERFACE(c)
Definition: GrayCore.h:79
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define CHEAPOBJECT_IMPL
Definition: cHeapObject.h:32
Definition: cHeapObject.h:38
virtual bool isValidCheck() const noexcept
Definition: cHeapObject.h:94
bool IsValidInsidePtr(void const *pTest) const
Definition: cHeapObject.h:73
cHeapObject()
Definition: cHeapObject.h:54
bool IsValidInsideN(INT_PTR index) const
Definition: cHeapObject.h:63
virtual size_t GetHeapStatsThis(OUT ITERATE_t &iAllocCount) const
Definition: cHeapObject.h:85
virtual ~cHeapObject()
Definition: cHeapObject.h:58
Definition: cMem.h:249
bool isValidSignature() const noexcept
Definition: cMem.h:272
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
DECLARE_INTERFACE(IRandomNoise)
int ITERATE_t
like size_t but signed
Definition: Index.h:28
uint16 index
Definition: sample3.cpp:29
Definition: cHeapObject.h:21
virtual const void * get_HeapPtr() const noexcept=0
Get the top level (outermost, freeable) class pointer. I can delete get_HeapPtr().
static bool IsValidInside(const void *pData, INT_PTR index) noexcept
Definition: cHeap.h:138
static bool IsValidHeap(const void *pData) noexcept
Definition: cHeap.h:130
static size_t GetSize(const void *pData) noexcept
Definition: cHeap.h:134
static bool IsValidApp(const void *pData) noexcept
Definition: cMem.h:42
static ptrdiff_t Diff(const void *pEnd, const void *pStart) noexcept
Definition: cMem.h:34