2016-03-17 18:05:44 +00:00
|
|
|
/*
|
|
|
|
* ReactOS ATL
|
|
|
|
*
|
|
|
|
* Copyright 2009 Andrew Hill <ash77@reactos.org>
|
|
|
|
* Copyright 2016 Mark Jansen
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
class CCRTAllocator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void* Allocate(_In_ size_t size)
|
|
|
|
{
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* Reallocate(_In_opt_ void* ptr, _In_ size_t size)
|
|
|
|
{
|
|
|
|
return realloc(ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Free(_In_opt_ void* ptr)
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CLocalAllocator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void* Allocate(_In_ size_t size)
|
|
|
|
{
|
|
|
|
return ::LocalAlloc(LMEM_FIXED, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* Reallocate(_In_opt_ void* ptr, _In_ size_t size)
|
|
|
|
{
|
|
|
|
if (!ptr)
|
|
|
|
return Allocate(size);
|
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
Free(ptr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ::LocalReAlloc(ptr, size, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Free(_In_opt_ void* ptr)
|
|
|
|
{
|
|
|
|
::LocalFree(ptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CGlobalAllocator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void* Allocate(_In_ size_t size)
|
|
|
|
{
|
|
|
|
return ::GlobalAlloc(GMEM_FIXED, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* Reallocate(_In_opt_ void* ptr, _In_ size_t size)
|
|
|
|
{
|
|
|
|
if (!ptr)
|
|
|
|
return Allocate(size);
|
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
Free(ptr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ::GlobalReAlloc(ptr, size, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Free(_In_opt_ void* ptr)
|
|
|
|
{
|
|
|
|
GlobalFree(ptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<class T, class Allocator = CCRTAllocator>
|
|
|
|
class CHeapPtr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CHeapPtr() :
|
2020-01-11 13:38:08 +00:00
|
|
|
m_pData(NULL)
|
2016-03-17 18:05:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit CHeapPtr(T *lp) :
|
2020-01-11 13:38:08 +00:00
|
|
|
m_pData(lp)
|
2016-03-17 18:05:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit CHeapPtr(CHeapPtr<T, Allocator> &lp)
|
|
|
|
{
|
2020-01-11 13:38:08 +00:00
|
|
|
m_pData = lp.Detach();
|
2016-03-17 18:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~CHeapPtr()
|
|
|
|
{
|
|
|
|
Free();
|
|
|
|
}
|
|
|
|
|
|
|
|
CHeapPtr<T, Allocator>& operator = (CHeapPtr<T, Allocator> &lp)
|
|
|
|
{
|
2020-01-11 13:38:08 +00:00
|
|
|
if (lp.m_pData != m_pData)
|
2016-03-17 18:05:44 +00:00
|
|
|
Attach(lp.Detach());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AllocateBytes(_In_ size_t nBytes)
|
|
|
|
{
|
2020-01-11 13:38:08 +00:00
|
|
|
ATLASSERT(m_pData == NULL);
|
|
|
|
m_pData = static_cast<T*>(Allocator::Allocate(nBytes));
|
|
|
|
return m_pData != NULL;
|
2016-03-17 18:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ReallocateBytes(_In_ size_t nBytes)
|
|
|
|
{
|
2020-01-11 13:38:08 +00:00
|
|
|
T* newData = static_cast<T*>(Allocator::Reallocate(m_pData, nBytes));
|
2016-03-17 18:05:44 +00:00
|
|
|
if (newData == NULL)
|
|
|
|
return false;
|
2020-01-11 13:38:08 +00:00
|
|
|
m_pData = newData;
|
2016-03-17 18:05:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Allocate(_In_ size_t nElements = 1)
|
|
|
|
{
|
|
|
|
return AllocateBytes(nElements * sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Reallocate(_In_ size_t nElements)
|
|
|
|
{
|
|
|
|
return ReallocateBytes(nElements * sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Free()
|
|
|
|
{
|
2020-01-11 13:38:08 +00:00
|
|
|
if (m_pData)
|
2016-03-17 18:05:44 +00:00
|
|
|
{
|
2020-01-11 13:38:08 +00:00
|
|
|
Allocator::Free(m_pData);
|
|
|
|
m_pData = NULL;
|
2016-03-17 18:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Attach(T *lp)
|
|
|
|
{
|
2020-01-11 13:38:08 +00:00
|
|
|
Allocator::Free(m_pData);
|
|
|
|
m_pData = lp;
|
2016-03-17 18:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
T *Detach()
|
|
|
|
{
|
2020-01-11 13:38:08 +00:00
|
|
|
T *saveP = m_pData;
|
|
|
|
m_pData = NULL;
|
2016-03-17 18:05:44 +00:00
|
|
|
return saveP;
|
|
|
|
}
|
|
|
|
|
|
|
|
T **operator &()
|
|
|
|
{
|
2020-01-11 13:38:08 +00:00
|
|
|
ATLASSERT(m_pData == NULL);
|
|
|
|
return &m_pData;
|
2016-03-17 18:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
operator T* () const
|
|
|
|
{
|
2020-01-11 13:38:08 +00:00
|
|
|
return m_pData;
|
2016-03-17 18:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
T* operator->() const
|
|
|
|
{
|
2020-01-11 13:38:08 +00:00
|
|
|
return m_pData;
|
2016-03-17 18:05:44 +00:00
|
|
|
}
|
|
|
|
|
2020-01-11 13:38:08 +00:00
|
|
|
public:
|
|
|
|
T *m_pData;
|
2016-03-17 18:05:44 +00:00
|
|
|
};
|
|
|
|
|