4 * Copyright 2009 Andrew Hill <ash77@reactos.org>
5 * Copyright 2016 Mark Jansen
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.
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.
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
27 static void* Allocate(_In_
size_t size
)
32 static void* Reallocate(_In_opt_
void* ptr
, _In_
size_t size
)
34 return realloc(ptr
, size
);
37 static void Free(_In_opt_
void* ptr
)
46 static void* Allocate(_In_
size_t size
)
48 return ::LocalAlloc(LMEM_FIXED
, size
);
51 static void* Reallocate(_In_opt_
void* ptr
, _In_
size_t size
)
54 return Allocate(size
);
60 return ::LocalReAlloc(ptr
, size
, 0);
63 static void Free(_In_opt_
void* ptr
)
69 class CGlobalAllocator
72 static void* Allocate(_In_
size_t size
)
74 return ::GlobalAlloc(GMEM_FIXED
, size
);
77 static void* Reallocate(_In_opt_
void* ptr
, _In_
size_t size
)
80 return Allocate(size
);
86 return ::GlobalReAlloc(ptr
, size
, 0);
89 static void Free(_In_opt_
void* ptr
)
96 template<class T
, class Allocator
= CCRTAllocator
>
105 explicit CHeapPtr(T
*lp
) :
110 explicit CHeapPtr(CHeapPtr
<T
, Allocator
> &lp
)
112 m_Data
= lp
.Detach();
120 CHeapPtr
<T
, Allocator
>& operator = (CHeapPtr
<T
, Allocator
> &lp
)
122 if (lp
.m_Data
!= m_Data
)
127 bool AllocateBytes(_In_
size_t nBytes
)
129 ATLASSERT(m_Data
== NULL
);
130 m_Data
= static_cast<T
*>(Allocator::Allocate(nBytes
));
131 return m_Data
!= NULL
;
134 bool ReallocateBytes(_In_
size_t nBytes
)
136 T
* newData
= static_cast<T
*>(Allocator::Reallocate(m_Data
, nBytes
));
143 bool Allocate(_In_
size_t nElements
= 1)
145 return AllocateBytes(nElements
* sizeof(T
));
148 bool Reallocate(_In_
size_t nElements
)
150 return ReallocateBytes(nElements
* sizeof(T
));
157 Allocator::Free(m_Data
);
164 Allocator::Free(m_Data
);
177 ATLASSERT(m_Data
== NULL
);
186 T
* operator->() const