ReactOS  0.4.14-dev-384-g5b37caa
atlalloc.h
Go to the documentation of this file.
1 /*
2  * ReactOS ATL
3  *
4  * Copyright 2009 Andrew Hill <ash77@reactos.org>
5  * Copyright 2016 Mark Jansen
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #pragma once
23 
25 {
26 public:
27  static void* Allocate(_In_ size_t size)
28  {
29  return malloc(size);
30  }
31 
32  static void* Reallocate(_In_opt_ void* ptr, _In_ size_t size)
33  {
34  return realloc(ptr, size);
35  }
36 
37  static void Free(_In_opt_ void* ptr)
38  {
39  free(ptr);
40  }
41 };
42 
44 {
45 public:
46  static void* Allocate(_In_ size_t size)
47  {
49  }
50 
51  static void* Reallocate(_In_opt_ void* ptr, _In_ size_t size)
52  {
53  if (!ptr)
54  return Allocate(size);
55  if (size == 0)
56  {
57  Free(ptr);
58  return NULL;
59  }
61  }
62 
63  static void Free(_In_opt_ void* ptr)
64  {
66  }
67 };
68 
70 {
71 public:
72  static void* Allocate(_In_ size_t size)
73  {
75  }
76 
77  static void* Reallocate(_In_opt_ void* ptr, _In_ size_t size)
78  {
79  if (!ptr)
80  return Allocate(size);
81  if (size == 0)
82  {
83  Free(ptr);
84  return NULL;
85  }
87  }
88 
89  static void Free(_In_opt_ void* ptr)
90  {
91  GlobalFree(ptr);
92  }
93 };
94 
95 
96 template<class T, class Allocator = CCRTAllocator>
97 class CHeapPtr
98 {
99 public:
101  m_Data(NULL)
102  {
103  }
104 
105  explicit CHeapPtr(T *lp) :
106  m_Data(lp)
107  {
108  }
109 
111  {
112  m_Data = lp.Detach();
113  }
114 
116  {
117  Free();
118  }
119 
121  {
122  if (lp.m_Data != m_Data)
123  Attach(lp.Detach());
124  return *this;
125  }
126 
127  bool AllocateBytes(_In_ size_t nBytes)
128  {
129  ATLASSERT(m_Data == NULL);
130  m_Data = static_cast<T*>(Allocator::Allocate(nBytes));
131  return m_Data != NULL;
132  }
133 
134  bool ReallocateBytes(_In_ size_t nBytes)
135  {
136  T* newData = static_cast<T*>(Allocator::Reallocate(m_Data, nBytes));
137  if (newData == NULL)
138  return false;
139  m_Data = newData;
140  return true;
141  }
142 
143  bool Allocate(_In_ size_t nElements = 1)
144  {
145  return AllocateBytes(nElements * sizeof(T));
146  }
147 
148  bool Reallocate(_In_ size_t nElements)
149  {
150  return ReallocateBytes(nElements * sizeof(T));
151  }
152 
153  void Free()
154  {
155  if (m_Data)
156  {
158  m_Data = NULL;
159  }
160  }
161 
162  void Attach(T *lp)
163  {
165  m_Data = lp;
166  }
167 
169  {
170  T *saveP = m_Data;
171  m_Data = NULL;
172  return saveP;
173  }
174 
176  {
177  ATLASSERT(m_Data == NULL);
178  return &m_Data;
179  }
180 
181  operator T* () const
182  {
183  return m_Data;
184  }
185 
186  T* operator->() const
187  {
188  return m_Data;
189  }
190 
191 protected:
193 };
194 
#define realloc
Definition: debug_ros.c:6
HGLOBAL NTAPI GlobalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:368
CHeapPtr(T *lp)
Definition: atlalloc.h:105
HGLOBAL NTAPI GlobalReAlloc(HGLOBAL hMem, SIZE_T dwBytes, UINT uFlags)
Definition: heapmem.c:825
#define ATLASSERT(x)
Definition: CComVariant.cpp:9
T * operator->() const
Definition: atlalloc.h:186
#define free
Definition: debug_ros.c:5
CHeapPtr< T, Allocator > & operator=(CHeapPtr< T, Allocator > &lp)
Definition: atlalloc.h:120
static void Free(_In_opt_ void *ptr)
Definition: atlalloc.h:37
static void * Allocate(_In_ size_t size)
Definition: atlalloc.h:46
bool AllocateBytes(_In_ size_t nBytes)
Definition: atlalloc.h:127
T * Detach()
Definition: atlalloc.h:168
~CHeapPtr()
Definition: atlalloc.h:115
CHeapPtr(CHeapPtr< T, Allocator > &lp)
Definition: atlalloc.h:110
#define T
Definition: mbstring.h:31
_In_opt_ PALLOCATE_FUNCTION Allocate
Definition: exfuncs.h:656
#define _In_opt_
Definition: no_sal2.h:213
_In_opt_ PALLOCATE_FUNCTION _In_opt_ PFREE_FUNCTION Free
Definition: exfuncs.h:656
bool Allocate(_In_ size_t nElements=1)
Definition: atlalloc.h:143
static void * Reallocate(_In_opt_ void *ptr, _In_ size_t size)
Definition: atlalloc.h:32
static PVOID ptr
Definition: dispmode.c:27
smooth NULL
Definition: ftsmooth.c:416
#define LMEM_FIXED
Definition: winbase.h:349
static void Free(_In_opt_ void *ptr)
Definition: atlalloc.h:63
GLsizeiptr size
Definition: glext.h:5919
static void * Allocate(_In_ size_t size)
Definition: atlalloc.h:72
static void * Allocate(_In_ size_t size)
Definition: atlalloc.h:27
HGLOBAL NTAPI GlobalFree(HGLOBAL hMem)
Definition: heapmem.c:611
T ** operator &()
Definition: atlalloc.h:175
CHeapPtr()
Definition: atlalloc.h:100
#define GMEM_FIXED
Definition: winbase.h:290
#define _In_
Definition: no_sal2.h:204
static void * Reallocate(_In_opt_ void *ptr, _In_ size_t size)
Definition: atlalloc.h:77
bool Reallocate(_In_ size_t nElements)
Definition: atlalloc.h:148
static void * Reallocate(_In_opt_ void *ptr, _In_ size_t size)
Definition: atlalloc.h:51
HLOCAL NTAPI LocalReAlloc(HLOCAL hMem, SIZE_T dwBytes, UINT uFlags)
Definition: heapmem.c:1608
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
bool ReallocateBytes(_In_ size_t nBytes)
Definition: atlalloc.h:134
T * m_Data
Definition: atlalloc.h:192
#define malloc
Definition: debug_ros.c:4
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
void Attach(T *lp)
Definition: atlalloc.h:162
void Free()
Definition: atlalloc.h:153
static void Free(_In_opt_ void *ptr)
Definition: atlalloc.h:89