Initial commit

This commit is contained in:
alexpete
2021-03-05 11:26:34 -08:00
commit a10351f38d
27091 changed files with 5521199 additions and 0 deletions
@@ -0,0 +1,207 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
// Original file Copyright Crytek GMBH or its affiliates, used under license.
#ifndef CRYINCLUDE_CRYPOOL_ALLOCATOR_H
#define CRYINCLUDE_CRYPOOL_ALLOCATOR_H
#pragma once
namespace NCryPoolAlloc
{
template<class TPool, class TItem>
class CFirstFit
: public TPool
{
public:
ILINE CFirstFit()
{
}
template<class T>
ILINE T Allocate(size_t Size, size_t Align = 1)
{
//fastpath?
if (TPool::m_pEmpty && TPool::m_pEmpty->Available(Size, Align))
{
TItem* pItem = TPool::Split(TPool::m_pEmpty, Size, Align);
if (!pItem)
{
return 0;
}
pItem->InUse(Align);
TPool::AllocatedMemory(pItem->MemSize());
//not fully occupied empty space?
TPool::m_pEmpty = pItem != TPool::m_pEmpty ? TPool::m_pEmpty : 0;
return TPool::Handle(pItem);
}
TItem* pBestItem;
for (pBestItem = TPool::m_Items.First(); pBestItem; pBestItem = pBestItem->Next())
{
if (pBestItem->Available(Size, Align)) // && (!pBestItem || pItem->MemSize()<pBestItem->MemSize()))
{
break;
}
}
if (!pBestItem)
{
return 0; //out of mem
}
TItem* pItem = TPool::Split(pBestItem, Size, Align);
if (!pItem) //no free node
{
return 0;
}
pItem->InUse(Align);
TPool::AllocatedMemory(pItem->MemSize());
//not fully occupied empty space?
TPool::m_pEmpty = pItem != pBestItem ? pBestItem : 0;
return TPool::Handle(pItem);
}
template<class T>
ILINE bool Free(T Handle, bool ForceBoundsCheck = false)
{
return Handle ? TPool::Free(Handle, ForceBoundsCheck) : false;
}
};
template<class TPool, class TItem>
class CWorstFit
: public TPool
{
public:
ILINE CWorstFit()
{
}
template<class T>
ILINE T Allocate(size_t Size, size_t Align = 1)
{
TItem* pBestItem = 0;
for (TItem* pItem = TPool::m_Items.First(); pItem; pItem = pItem->Next())
{
if (pItem->IsFree() && (!pBestItem || pItem->MemSize() > pBestItem->MemSize()))
{
pBestItem = pItem;
}
}
if (!pBestItem || !pBestItem->Available(Size, Align))
{
return 0; //out of mem
}
TItem* pItem = Split(pBestItem, Size, Align);
if (!pItem) //no free node
{
return 0;
}
pItem->InUse(Align);
AllocatedMemory(pItem->MemSize());
return Handle(pItem);
}
};
template<class TPool, class TItem>
class CBestFit
: public TPool
{
public:
ILINE CBestFit()
{
}
template<class T>
ILINE T Allocate(size_t Size, size_t Align = 1)
{
TItem* pBestItem = 0;
for (TItem* pItem = TPool::m_Items.First(); pItem; pItem = pItem->Next())
{
if ((!pBestItem || pItem->MemSize() < pBestItem->MemSize()) && pItem->Available(Size, Align))
{
if (pItem->MemSize() == Size)
{
pItem->InUse(Align);
AllocatedMemory(pItem->MemSize());
return (T)Handle(pItem);
}
pBestItem = pItem;
}
}
if (!pBestItem)
{
return 0; //out of mem
}
TItem* pItem = Split(pBestItem, Size, Align);
if (!pItem) //no free node
{
return 0;
}
pItem->InUse(Align);
AllocatedMemory(pItem->MemSize());
return (T)Handle(pItem);
}
};
template<class TAllocator>
class CReallocator
: public TAllocator
{
public:
template<class T>
ILINE bool Reallocate(T* pData, size_t Size, size_t Alignment)
{
//special cases
if (!Size) //just free?
{
TAllocator::Free(*pData);
*pData = 0;
return true;
}
if (!*pData) //just alloc?
{
*pData = TAllocator::template Allocate<T>(Size, Alignment);
return *pData != 0;
}
//same size, nothing to do at all?
if (TAllocator::Item(*pData)->MemSize() == Size)
{
return true;
}
if (TAllocator::ReSize(pData, Size))
{
return true;
}
T pNewData = TAllocator::template Allocate<T>(Size, Alignment);
if (!pNewData)
{
return false;
}
memcpy(TAllocator::template Resolve<uint8*>(pNewData),
TAllocator::template Resolve<uint8*>(*pData), min(TAllocator::Item(*pData)->MemSize(), Size));
TAllocator::template Free(*pData);
*pData = pNewData;
return true;
}
};
}
#endif // CRYINCLUDE_CRYPOOL_ALLOCATOR_H
@@ -0,0 +1,655 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
// Original file Copyright Crytek GMBH or its affiliates, used under license.
#ifndef CRYINCLUDE_CRYPOOL_CONTAINER_H
#define CRYINCLUDE_CRYPOOL_CONTAINER_H
#pragma once
namespace NCryPoolAlloc
{
template<size_t TElementCount, class TElement>
class CPool
: public CMemoryStatic<TElementCount* sizeof(TElement)>
{
class CPoolNode;
class CPoolNode
: public CListItem<CPoolNode>
{
};
CList<CPoolNode> m_List;
public:
ILINE CPool()
{
CPoolNode* pPrev = 0;
CPoolNode* pNode = 0;
for (size_t a = 1; a < TElementCount; a++) //skip first element as it would be counted as zero ptr
{
uint8* pData = &CMemoryStatic<TElementCount* sizeof(TElement)>::Data()[a * sizeof(TElement)];
pNode = reinterpret_cast<CPoolNode*>(pData);
pNode->Prev(pPrev);
if (pPrev)
{
pPrev->Next(pNode);
}
else
{
m_List.First(pNode);
}
pPrev = pNode;
// m_List.AddLast(pNode);
}
if (pPrev)
{
pPrev->Next(0);
m_List.Last(pPrev);
}
}
ILINE uint8* Allocate([[maybe_unused]] size_t Size, [[maybe_unused]] size_t Align = 1)
{
CPoolNode* pNode = m_List.PopFirst();
return reinterpret_cast<uint8*>(pNode);
}
template<class T>
ILINE void Free(T* pData)
{
if (pData)
{
CPoolNode* pNode = reinterpret_cast<CPoolNode*>(pData);
m_List.AddLast(pNode);
}
}
ILINE TElement& operator[](uint32 Idx)
{
uint8* pData = &CMemoryStatic<TElementCount* sizeof(TElement)>::Data()[Idx * sizeof(TElement)];
return *reinterpret_cast<TElement*>(pData);
}
ILINE const TElement& operator[](uint32 Idx) const
{
const uint8* pData = &CMemoryStatic<TElementCount* sizeof(TElement)>::Data()[Idx * sizeof(TElement)];
return *reinterpret_cast<const TElement*>(pData);
}
};
template<class TMemory, bool BoundsCheck = false>
class CInPlace
: public TMemory
{
protected:
CList<CListItemInPlace> m_Items;
size_t m_Allocated;
CListItemInPlace* m_pEmpty;
ILINE void AllocatedMemory(size_t S)
{
m_Allocated += S + sizeof(CListItemInPlace);
}
ILINE void FreedMemory(size_t S)
{
m_Allocated -= S + sizeof(CListItemInPlace);
}
ILINE void Stack(CListItemInPlace* pItem)
{
}
public:
ILINE CInPlace()
: m_Allocated(0)
{
}
ILINE void InitMem(const size_t S = 0, uint8* pData = 0)
{
TMemory::InitMem(S, pData);
if (!TMemory::MemSize())
{
return;
}
pData = TMemory::Data();
CListItemInPlace* pFirst = reinterpret_cast<CListItemInPlace*>(pData);
CListItemInPlace* pFree = pFirst + 1;
CListItemInPlace* pLast = reinterpret_cast<CListItemInPlace*>(pData + TMemory::MemSize()) - 1;
m_Items.~CList<CListItemInPlace>();
new (&m_Items)CList<CListItemInPlace>();
m_Items.AddLast(pFirst);
m_Items.AddLast(pFree);
m_Items.AddLast(pLast);
pFirst->InUse(0); //static first item
pFree->Free();
pLast->InUse(0); //static last item
m_pEmpty = pFree;
m_Allocated = 0;
}
ILINE size_t FragmentCount() const
{
return m_Items.Count();
}
ILINE CListItemInPlace* Split(CListItemInPlace* pItem, size_t Size, size_t Align)
{
size_t Offset = reinterpret_cast<size_t>(pItem->Data());
Offset += pItem->MemSize(); //ptr to end
Offset -= Size; //minus size
Size += Offset & (Align - 1); //adjust size to fit required alignment
Offset -= Offset & (Align - 1);
size_t TSize = sizeof(CListItemInPlace);
Offset -= TSize; //header
if (Offset <= reinterpret_cast<size_t>(pItem + 1)) //not enough space for splitting?
{
return pItem;
}
CListItemInPlace* pItemNext = reinterpret_cast<CListItemInPlace*>(Offset);
const size_t Offset2 = reinterpret_cast<size_t>(pItemNext->Data());
CPA_ASSERT(!(Offset2 & (Align - 1)));
m_Items.AddBehind(pItemNext, pItem);
//pItemNext->Prev(pItem);
//pItemNext->Next(pItem->Next());
// if(pItem->Next())
// pItem->Next()->Prev(pItemNext);
// pItem->Next(pItemNext);
pItemNext->Free();
return pItemNext;
}
ILINE void Merge(CListItemInPlace* pItem)
{
//merge with next if possible
CListItemInPlace* pItemNext = pItem->Next();
if (pItemNext->IsFree())
{
if (m_pEmpty == pItemNext)
{
m_pEmpty = pItem;
}
m_Items.Remove(pItemNext);
//pItem->Next(pItemNext->Next());
//pItem->Next()->Prev(pItem);
}
//merge with prev if possible
CListItemInPlace* pItemPrev = pItem->Prev();
if (pItemPrev->IsFree())
{
if (m_pEmpty == pItem)
{
m_pEmpty = pItemPrev;
}
m_Items.Remove(pItem);
//pItemPrev->Next(pItem->Next());
//pItem->Next()->Prev(pItemPrev);
pItem = pItemPrev;
}
}
template<class T>
ILINE T Resolve(void* rItem) const
{
return reinterpret_cast<T>(rItem);
}
template<class T>
ILINE size_t Size(const T* pData) const
{
const CListItemInPlace* pItem = Item(pData);
return pItem->MemSize();
}
bool InBounds(const void* pData, const bool Check) const
{
return !Check || (
reinterpret_cast<size_t>(pData) >= reinterpret_cast<size_t>(TMemory::Data()) &&
reinterpret_cast<size_t>(pData) < reinterpret_cast<size_t>(TMemory::Data()) + TMemory::MemSize());
}
template<class T>
ILINE bool Free(T* pData, bool ForceBoundsCheck = false)
{
if (pData && InBounds(pData, BoundsCheck | ForceBoundsCheck))
{
CListItemInPlace* pItem = Item(pData);
FreedMemory(pItem->MemSize());
pItem->Free();
Merge(pItem);
return true;
}
return false;
}
ILINE bool Beat(){return false; }//dummy beat in case no defragmentator is wraping
ILINE size_t MemFree() const{return TMemory::MemSize() - m_Allocated; }
ILINE size_t MemSize() const{return TMemory::MemSize(); }
ILINE uint8* Handle(CListItemInPlace* pItem) const
{
return pItem->Data();
}
template<class T>
ILINE CListItemInPlace* Item(T* pData)
{
return reinterpret_cast<CListItemInPlace*>(pData) - 1;
}
template<class T>
ILINE const CListItemInPlace* Item(const T* pData) const
{
return reinterpret_cast<const CListItemInPlace*>(pData) - 1;
}
ILINE static bool Defragmentable(){return false; }
template<class T>
ILINE bool ReSize(T* pData, size_t SizeNew)
{
//special cases
CListItemInPlace* pItem = Item(*pData);
const size_t SizeOld = pItem->MemSize();
//reduction
if (SizeOld > SizeNew)
{
if (pItem->Next()->IsFree())
{
CListItemInPlace* pNextNext = pItem->Next()->Next();
size_t Offset = reinterpret_cast<size_t>(pItem->Data());
Offset += SizeNew; //Offset to next
CListItemInPlace* pItemNext = reinterpret_cast<CListItemInPlace*>(Offset);
pItem->Next(pItemNext);
pNextNext->Prev(pItemNext);
pItemNext->Prev(pItem);
pItemNext->Next(pNextNext);
pItemNext->Free();
return true;
}
if (SizeOld - SizeNew <= sizeof(CListItemInPlace))
{
return true; //header is bigger than the amount of freed memory
}
//split
size_t Offset = reinterpret_cast<size_t>(pItem->Data());
Offset += SizeNew; //Offset to next
CListItemInPlace* pItemNext = reinterpret_cast<CListItemInPlace*>(Offset);
m_Items.AddBehind(pItemNext, pItem);
pItemNext->Free();
return true;
}
//SizeOld<SizeNew grow
CListItemInPlace* pNext = pItem->Next();
CListItemInPlace* pNextNext = pNext->Next();
const size_t SizeNext = pNext->IsFree() ? pNext->MemSize() + sizeof(CListItemInPlace) : 0;
if (SizeNew <= SizeNext + SizeOld)
{
if (SizeNew + sizeof(CListItemInPlace) + 1 < SizeNext + SizeOld)
{
size_t Offset = reinterpret_cast<size_t>(pItem->Data());
Offset += SizeNew; //Offset to next
CListItemInPlace* pItemNext = reinterpret_cast<CListItemInPlace*>(Offset);
pItem->Next(pItemNext);
pNextNext->Prev(pItemNext);
pItemNext->Prev(pItem);
pItemNext->Next(pNextNext);
pItemNext->Free();
}
else
{
pItem->Next(pNextNext);
pNextNext->Prev(pItem);
}
return true;
}
return false; //no further in-place realloc possible
}
};
template<class TMemory, size_t TNodeCount, bool BoundsCheck = false>
class CReferenced
: public TMemory
{
typedef CPool<TNodeCount, CListItemReference> tdNodePool;
protected:
tdNodePool m_NodePool;
CList<CListItemReference> m_Items;
size_t m_Allocated;
CListItemReference* m_pEmpty;
ILINE void AllocatedMemory(size_t S)
{
m_Allocated += S;
}
ILINE void FreedMemory(size_t S)
{
m_Allocated -= S;
}
ILINE void Stack(CListItemReference* pItem)
{
m_Items.Validate(pItem);
CListItemReference* pItem2 = 0;
CListItemReference* pNext = pItem->Next();
uint8* pData = pItem->Data(pNext->Align());
if (pData != pItem->Data()) //needs splitting 'cause of alignment?
{
pItem2 = reinterpret_cast<CListItemReference*>(m_NodePool.Allocate(1, 1));
if (!pItem2) //no free node found for splitting?
{
return; //failed to stack -> return
}
}
memmove(pData, pNext->Data(), pNext->MemSize());
if (pItem2) //was not aligned?
{
//then keep the current ITem
const size_t SizeItem = pItem->MemSize();
const size_t SizeNext = pNext->MemSize();
m_Items.AddBehind(pItem2, pNext);
pItem2->Data(pData + SizeNext);
pNext->Data(pData);
pItem2->MemSize(pItem2->Next()->Data() - pItem2->Data());
pNext->MemSize(SizeNext);
pItem->MemSize(pNext->Data() - pItem->Data());
m_Items.Validate(pItem);
m_Items.Validate(pItem2);
m_Items.Validate(pNext);
}
else
{
const size_t SizeItem = pItem->MemSize();
const size_t SizeNext = pNext->MemSize();
m_Items.Remove(pItem);
m_Items.AddBehind(pItem, pNext);
pItem->Data(pNext->Data());
pNext->Data(pData);
pNext->MemSize(SizeItem);
pItem->MemSize(SizeNext);
m_Items.Validate(pItem);
m_Items.Validate(pNext);
}
}
public:
ILINE CReferenced()
: m_Allocated(0)
{
}
ILINE void InitMem(const size_t S = 0, uint8* pData = 0)
{
TMemory::InitMem(S, pData);
if (!TMemory::MemSize())
{
return;
}
pData = TMemory::Data();
CListItemReference* pItem = reinterpret_cast<CListItemReference*>(m_NodePool.Allocate(1, 1));
CListItemReference* pLast = reinterpret_cast<CListItemReference*>(m_NodePool.Allocate(1, 1));
m_Items.AddFirst(pItem);
m_Items.AddLast(pLast);
pLast->Init(pData + TMemory::MemSize(), 0, pItem, 0);
pLast->InUse(0);
pItem->Init(pData, TMemory::MemSize(), 0, pLast);
pItem->Free();
m_pEmpty = pItem;
m_Allocated = 0;
}
ILINE size_t FragmentCount() const
{
return m_Items.Count();
}
ILINE CListItemReference* Split(CListItemReference* pItem, size_t Size, size_t Align)
{
size_t Offset = reinterpret_cast<size_t>(pItem->Data());
if (!(Offset & (Align - 1))) //perfectly aligned?
{
if (pItem->MemSize() != Size) //not perfectly fitting?
{ //then split
CListItemReference* pItemPrev = reinterpret_cast<CListItemReference*>(m_NodePool.Allocate(1, 1));
if (!pItemPrev)
{
return 0;
}
const size_t OrgSize = pItem->MemSize();
m_Items.AddBefore(pItemPrev, pItem);
pItemPrev->Data(pItem->Data());
pItem->Data(pItem->Data() + Size);
pItem->MemSize(OrgSize - Size);
pItemPrev->MemSize(Size);
pItem = pItemPrev;
}
return pItem;
}
//not aligned to block start
//then lets try to align to block end
Offset += pItem->MemSize(); //ptr to end
Offset -= Size; //minus size
if (!(Offset & (Align - 1))) //perfectly aligned?
{
CListItemReference* pItemPrev = reinterpret_cast<CListItemReference*>(m_NodePool.Allocate(1, 1));
if (!pItemPrev)
{
return 0;
}
const size_t OrgSize = pItem->MemSize();
m_Items.AddBefore(pItemPrev, pItem);
pItemPrev->Data(pItem->Data());
pItem->Data(reinterpret_cast<uint8*>(Offset));
pItemPrev->MemSize(OrgSize - Size);
pItem->MemSize(Size);
pItemPrev->Free();
return pItem;
}
//last resort, fragment it into 3 parts
//Size +=Offset&(Align-1); //adjust size to fit required alignment
Offset -= Offset & (Align - 1);
CListItemReference* pItemPrev = reinterpret_cast<CListItemReference*>(m_NodePool.Allocate(1, 1));
CListItemReference* pItemNext = reinterpret_cast<CListItemReference*>(m_NodePool.Allocate(1, 1));
if (!pItemPrev || !pItemNext)
{
return 0;
}
const size_t OrgSize = pItem->MemSize();
m_Items.AddBefore(pItemPrev, pItem);
m_Items.AddBehind(pItemNext, pItem);
pItemPrev->Data(pItem->Data());
pItem->Data(reinterpret_cast<uint8*>(Offset));
pItemNext->Data(pItem->Data() + Size);
pItemPrev->MemSize(pItem->Data() - pItemPrev->Data());
pItemNext->MemSize(OrgSize - pItemPrev->MemSize() - Size);
pItem->MemSize(Size);
pItemPrev->Free();
pItemNext->Free();
return pItem;
}
ILINE void Merge(CListItemReference* pItem)
{
m_Items.Validate(pItem);
//merge with next if possible
CListItemReference* pItemNext = pItem->Next();
if (pItemNext && pItemNext->IsFree())
{
if (m_pEmpty == pItemNext)
{
m_pEmpty = pItem;
}
const size_t OrgSize = pItem->MemSize();
const size_t NextSize = pItemNext->MemSize();
m_Items.Remove(pItemNext);
pItem->MemSize(OrgSize + NextSize);
m_NodePool.Free(pItemNext);
}
//merge with prev if possible
CListItemReference* pItemPrev = pItem->Prev();
if (pItemPrev && pItemPrev->IsFree())
{
if (m_pEmpty == pItem)
{
m_pEmpty = pItemPrev;
}
const size_t OrgSize = pItem->MemSize();
const size_t PrevSize = pItemPrev->MemSize();
m_Items.Remove(pItem);
pItemPrev->MemSize(PrevSize + OrgSize);
m_NodePool.Free(pItem);
}
}
template<class T>
ILINE T Resolve(const uint32 ID)
{
CPA_ASSERT(ID); //0 is invalid
return reinterpret_cast<T>(Item(ID)->Data());
}
ILINE uint32 AddressToHandle(void* pData)
{
for (CListItemReference* pItem = m_Items.First(); pItem; pItem = pItem->Next())
{
if (pItem->Data() == pData)
{
return Handle(pItem);
}
}
return 0;
}
template<class T>
ILINE size_t Size(T ID) const
{
CPA_ASSERT(ID); //0 is invalid
return Item(ID)->MemSize();
}
template<class T>
bool InBounds([[maybe_unused]] T ID, [[maybe_unused]] const bool Check) const
{
//boundscheck doesn't work for Referenced containers
return true;
}
template<class T>
ILINE bool Free(T ID, bool ForceBoundsCheck = false)
{
IF (!ID, false)
{
return true;
}
IF (!InBounds(ID, BoundsCheck | ForceBoundsCheck), false)
{
return false;
}
CListItemReference* pItem = Item(ID);
FreedMemory(pItem->MemSize());
pItem->Free();
Merge(pItem);
return true;
}
ILINE bool Beat(){return false; }//dummy beat in case no defragmentator is wraping
ILINE size_t MemFree() const{return TMemory::MemSize() - m_Allocated; }
ILINE size_t MemSize() const{return TMemory::MemSize(); }
ILINE uint32 Handle(CListItemReference* pItem) const
{
return static_cast<uint32>(pItem - &m_NodePool[0]);
}
ILINE CListItemReference* Item(uint32 ID)
{
return &m_NodePool[ID];
}
ILINE const CListItemReference* Item(uint32 ID) const
{
return &m_NodePool[ID];
}
ILINE static bool Defragmentable(){return true; }
template<class T>
ILINE bool ReSize(T* pData, size_t SizeNew)
{
CListItemReference* pItem = Item(*pData);
const size_t SizeOld = pItem->MemSize();
//reduction
if (SizeOld > SizeNew)
{
if (pItem->Next()->IsFree())
{
CListItemReference* pNext = pItem->Next();
const size_t NextSize = pNext->MemSize();
pNext->Data(pNext->Data() + SizeNew - SizeOld);
pNext->MemSize(NextSize - SizeNew + SizeOld);
pItem->MemSize(SizeNew);
return true;
}
//split
CListItemReference* pItemNext = reinterpret_cast<CListItemReference*>(m_NodePool.Allocate(1, 1));
m_Items.AddBehind(pItemNext, pItem);
pItemNext->Data(pItem->Data() + SizeNew);
pItem->MemSize(SizeNew);
pItemNext->MemSize(SizeOld - SizeNew);
pItemNext->Free();
return true;
}
//SizeOld<SizeNew grow
CListItemReference* pNext = pItem->Next();
const size_t SizeNext = pNext->IsFree() ? pNext->MemSize() : 0;
if (SizeNew <= SizeNext + SizeOld)
{
if (SizeNew == SizeNext + SizeOld)
{
m_Items.Remove(pNext);
m_NodePool.Free(pNext);
}
else
{
pNext->Data(pNext->Data() + SizeNew - SizeOld);
pNext->MemSize(SizeNext - SizeNew + SizeOld);
}
pItem->MemSize(SizeNew);
return true;
}
return false; //no further in-place realloc possible
}
};
}
#endif // CRYINCLUDE_CRYPOOL_CONTAINER_H
+65
View File
@@ -0,0 +1,65 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
// Original file Copyright Crytek GMBH or its affiliates, used under license.
#ifndef CRYINCLUDE_CRYPOOL_DEFRAG_H
#define CRYINCLUDE_CRYPOOL_DEFRAG_H
#pragma once
namespace NCryPoolAlloc
{
template<class T>
class CDefragStacked
: public T
{
template<class TItem>
ILINE bool DefragElement(TItem* pItem)
{
T::m_Items.Validate();
if (pItem)
{
for (; pItem->Next(); pItem = pItem->Next())
{
if (!pItem->IsFree())
{
continue;
}
if (pItem->Next()->Locked())
{
continue;
}
if (!pItem->Available(pItem->Next()->Align(), pItem->Next()->Align()))
{
continue;
}
T::m_Items.Validate(pItem);
Stack(pItem);
T::m_Items.Validate(pItem);
Merge(pItem);
T::m_Items.Validate();
return true;
}
}
return false;
}
public:
ILINE bool Beat()
{
return T::Defragmentable() && DefragElement(T::m_Items.First());
};
};
}
#endif // CRYINCLUDE_CRYPOOL_DEFRAG_H
@@ -0,0 +1,81 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
// Original file Copyright Crytek GMBH or its affiliates, used under license.
#ifndef CRYINCLUDE_CRYPOOL_FALLBACK_H
#define CRYINCLUDE_CRYPOOL_FALLBACK_H
#pragma once
namespace NCryPoolAlloc
{
enum EFallbackMode
{
EFM_DISABLED,
EFM_ENABLED,
EFM_ALWAYS
};
template<class TAllocator>
class CFallback
: public TAllocator
{
EFallbackMode m_Fallback;
public:
ILINE CFallback()
: m_Fallback(EFM_DISABLED)
{
}
template<class T>
ILINE T Allocate(size_t Size, size_t Align = 1)
{
if (EFM_ALWAYS == m_Fallback)
{
return reinterpret_cast<T>(CPA_ALLOC(Align, Size));
}
T pRet = TAllocator::template Allocate<T>(Size, Align);
if (!pRet && EFM_ENABLED == m_Fallback)
{
return reinterpret_cast<T>(CPA_ALLOC(Align, Size));
}
return pRet;
}
template<class T>
ILINE bool Free(T Handle)
{
if (!Handle)
{
return true;
}
if (EFM_ALWAYS == m_Fallback)
{
CPA_FREE(Handle);
return true;
}
if (EFM_ENABLED == m_Fallback && TAllocator::InBounds(Handle, true))
{
CPA_FREE(Handle);
return true;
}
return TAllocator::template Free<T>(Handle);
}
void FallbackMode(EFallbackMode M){m_Fallback = M; }
EFallbackMode FallbaclMode() const{return m_Fallback; }
};
}
#endif // CRYINCLUDE_CRYPOOL_FALLBACK_H
@@ -0,0 +1,203 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
// Original file Copyright Crytek GMBH or its affiliates, used under license.
#ifndef CRYINCLUDE_CRYPOOL_INSPECTOR_H
#define CRYINCLUDE_CRYPOOL_INSPECTOR_H
#pragma once
namespace NCryPoolAlloc
{
template<class TAllocator>
class CInspector
: public TAllocator
{
enum
{
EITableSize = 30
};
size_t m_Allocations[EITableSize];
size_t m_Alignment[EITableSize];
char m_LogFileName[1024];
size_t m_AllocCount;
size_t m_FreeCount;
size_t m_ResizeCount;
size_t m_FailAllocCount;
size_t m_FailFreeCount;
size_t m_FailResizeCount;
void WriteOut(const char* pFileName, uint32 Stack, const char* pFormat, ...) const
{
/*
if(!pFileName)
{
if(!*m_LogFileName)
return;
pFileName = m_LogFileName;
}
FILE* File = fopen(pFileName,"a");
if(File)
{
char Buffer[1024];
for(uint32 a=0;a<Stack;a++)
Buffer[a]=' ';
va_list args;
va_start(args,pFormat);
vsprintf(Buffer+Stack,pFormat,args);
fwrite(Buffer,1,strlen(Buffer),File);
fclose(File);
va_end(args);
}
*/
}
size_t Bit(size_t C) const
{
size_t Count = 0;
C >>= 1;
while (C)
{
Count++;
C >>= 1;
}
return Count >= EITableSize ? EITableSize - 1 : Count;
}
public:
CInspector()
{
for (size_t a = 0; a < EITableSize; a++)
{
m_Allocations[a] = m_Alignment[a] = 0;
}
m_LogFileName[0] = 0;
m_AllocCount = 0;
m_FreeCount = 0;
m_ResizeCount = 0;
m_FailAllocCount = 0;
m_FailFreeCount = 0;
m_FailResizeCount = 0;
}
bool LogFileName(const char* pFileName)
{
const size_t Size = strlen(pFileName) + 1;
if (Size > sizeof(m_LogFileName))
{
m_LogFileName[0] = 0;
return false;
}
memcpy(m_LogFileName, pFileName, Size);
WriteOut(0, "[log start]\n");
return true;
}
void SaveStats(const char* pFileName) const
{
WriteOut(pFileName, 0, "stats:\n");
WriteOut(pFileName, 1, "Counter calls|fails\n");
WriteOut(pFileName, 2, "Alloc: %6d|%6d\n", m_AllocCount, m_FailAllocCount);
WriteOut(pFileName, 2, "Free: %6d|%6d\n", m_FreeCount, m_FailFreeCount);
WriteOut(pFileName, 2, "Resize:%6d|%6d\n", m_ResizeCount, m_FailResizeCount);
WriteOut(pFileName, 1, "Allocations:\n");
for (size_t a = 0; a < EITableSize; a++)
{
WriteOut(pFileName, 2, "%9dByte: %8d\n", 1 << a, m_Allocations[a]);
}
WriteOut(pFileName, 1, "Alignment:\n");
for (size_t a = 0; a < EITableSize; a++)
{
WriteOut(pFileName, 2, "%9dByte: %8d\n", 1 << a, m_Alignment[a]);
}
}
template<class T>
ILINE T Allocate(size_t Size, size_t Align = 1)
{
m_AllocCount++;
m_Allocations[Bit(Size)]++;
m_Alignment[Bit(Align)]++;
T pData = TAllocator::template Allocate<T>(Size, Align);
WriteOut(0, 0, "[A|%d|%d|%d]", (int)pData, Size, Align);
if (!pData)
{
m_FailAllocCount++;
WriteOut(0, 0, "[failed]", Size, Align);
}
return pData;
}
template<class T>
ILINE bool Free(T pData, bool ForceBoundsCheck = false)
{
m_FreeCount++;
const bool Ret = TAllocator::Free(pData, ForceBoundsCheck);
WriteOut(0, 0, "[F|%d|%d|%d]", (int)pData, (int)ForceBoundsCheck, (int)Ret);
m_FailFreeCount += !Ret;
return Ret;
}
//template<class T>
//ILINE bool Free(T pData)
// {
// m_FreeCount++;
// const bool Ret = TAllocator::Free(pData);
// WriteOut(0,0,"[F|%d|%d|%d]",(int)pData,(int)-1,(int)Ret);
// m_FailFreeCount+=!Ret;
// return Ret;
// }
template<class T>
ILINE bool Resize(T** pData, size_t Size, size_t Alignment)
{
m_ResizeCount++;
const bool Ret = TAllocator::Resize(pData, Size, Alignment);
WriteOut(0, 0, "[R|%d|%d|%d]", (int)*pData, (int)-1, (int)Ret);
m_FailResizeCount += !Ret;
return Ret;
}
template<class T>
ILINE size_t FindBiggest(const T* pItem)
{
size_t Biggest = 0;
while (pItem)
{
if (pItem->IsFree() && pItem->MemSize() > Biggest)
{
Biggest = pItem->MemSize();
}
pItem = pItem->Next();
}
return Biggest;
}
ILINE size_t BiggestFreeBlock()
{
return FindBiggest(TAllocator::m_Items.First());
}
ILINE uint8* FirstItem()
{
return TAllocator::m_Items.First()->Data();
}
};
}
#endif // CRYINCLUDE_CRYPOOL_INSPECTOR_H
+366
View File
@@ -0,0 +1,366 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
// Original file Copyright Crytek GMBH or its affiliates, used under license.
#ifndef CRYINCLUDE_CRYPOOL_LIST_H
#define CRYINCLUDE_CRYPOOL_LIST_H
#pragma once
namespace NCryPoolAlloc
{
class CListItemInPlace;
class CListItemReference;
template<typename TItem>
class CListItem
{
TItem* m_pPrev;
TItem* m_pNext;
public:
ILINE TItem* Prev(){return m_pPrev; }
ILINE TItem* Next(){return m_pNext; }
ILINE const TItem* Prev() const{return m_pPrev; }
ILINE const TItem* Next() const{return m_pNext; }
ILINE void Prev(TItem* pPrev){ m_pPrev = pPrev; }
ILINE void Next(TItem* pNext){ m_pNext = pNext; }
//debugging
void Validate();
};
template<typename TItem>
class CListItemFlagged
: public CListItem<TItem>
{
enum
{
ELIF_INUSE = (1 << 0),
ELIF_LOCKED = (1 << 1),
};
uint32 m_Flags : 8;
uint32 m_Align : 24;
public:
ILINE CListItemFlagged()
: m_Flags(0)
{
}
ILINE bool IsFree() const{return (m_Flags & ELIF_INUSE) != ELIF_INUSE; }
ILINE void Free(){m_Flags &= ~ELIF_INUSE; }
ILINE void InUse(uint32 A){m_Flags |= ELIF_INUSE; m_Align = A; }
ILINE bool Locked() const{return ELIF_LOCKED == (m_Flags & ELIF_LOCKED); }
ILINE void Lock(){m_Flags |= ELIF_LOCKED; }
ILINE void Unlock(){m_Flags &= ~ELIF_LOCKED; }
ILINE uint32 Align() const{return m_Align; }
};
class CListItemInPlace
: public CListItemFlagged<CListItemInPlace>
{
public:
ILINE void Init([[maybe_unused]] uint8* pData, [[maybe_unused]] size_t Size, CListItemInPlace* pPrev, CListItemInPlace* pNext)
{
Prev(pPrev);
Next(pNext);
CPA_ASSERT(Size == MemSize());
}
ILINE bool Available(size_t Size, size_t Align) const
{
size_t Offset = reinterpret_cast<size_t>(Data());
if (Offset & (Align - 1)) //not aligned?
{
Size += sizeof(CListItemInPlace) + Align - 1; //then an intermedian node needs to fit
}
return Size <= MemSize() && IsFree();
}
ILINE uint8* Data(){return reinterpret_cast<uint8*>(this) + sizeof(CListItemInPlace); }
ILINE const uint8* Data() const{return reinterpret_cast<const uint8*>(this) + sizeof(CListItemInPlace); }
ILINE size_t MemSize() const
{
const uint8* pNext = reinterpret_cast<const uint8*>(Next());
const uint8* pThis = reinterpret_cast<const uint8*>(this);
const size_t ESize = sizeof(CListItemInPlace);
size_t Delta = pNext - pThis;
Delta -= ESize;
return Delta;
}
};
class CListItemReference
: public CListItemFlagged<CListItemReference>
{
uint8* m_pData;
// size_t m_Size;
public:
ILINE void Init(uint8* pData, size_t Size, CListItemReference* pPrev, CListItemReference* pNext)
{
Data(pData);
Prev(pPrev);
Next(pNext);
MemSize(Size);
}
ILINE bool Available(size_t Size, size_t Align) const
{
size_t Offset = reinterpret_cast<size_t>(Data());
if ((Offset & (Align - 1)))
{
Size += Align - (Offset & (Align - 1));
}
return Size <= MemSize() && IsFree();
}
ILINE void Data(uint8* pData){m_pData = pData; }
ILINE uint8* Data(size_t Align)
{
Align--;
size_t Offset = reinterpret_cast<size_t>(m_pData);
Offset = (Offset + Align) & ~Align;
return reinterpret_cast<uint8*>(Offset);
}
ILINE uint8* Data(){return m_pData; }
ILINE const uint8* Data() const{return m_pData; }
ILINE void MemSize([[maybe_unused]] size_t Size) { }
ILINE size_t MemSize() const
{
const size_t T = reinterpret_cast<size_t>(Data());
const size_t N = Next() ? reinterpret_cast<size_t>(Next()->Data()) : T;
return N - T;
}
//ILINE void MemSize(size_t Size){m_Size=Size;}
//ILINE size_t MemSize()const{return m_Size;}
};
template<class TItem, bool VALIDATE = false>
class CList
{
TItem* m_pFirst;
TItem* m_pLast;
size_t m_Count;
public:
ILINE CList()
: m_pFirst(0)
, m_pLast(0)
, m_Count(0)
{
}
ILINE void First(TItem* pItem){m_pFirst = pItem; }
ILINE TItem* First(){return m_pFirst; }
ILINE void Last(TItem* pItem){m_pLast = pItem; }
ILINE TItem* Last(){return m_pLast; }
ILINE bool Empty() const{return m_pFirst == 0; }
ILINE TItem* PopFirst()
{
Validate();
if (!m_pFirst)
{
return 0;
}
TItem* pRet = m_pFirst;
m_pFirst = m_pFirst->Next();
if (m_pFirst) //if any element exists
{
m_pFirst->Prev(0); //set prev ptr of this element to 0
}
else
{
m_pLast = 0; //set ptr to last element to 0 if ptr to first is zero as well
}
Validate();
m_Count--;
return pRet;
}
ILINE TItem* PopLast()
{
Validate();
if (!m_pLast)
{
return 0;
}
TItem* pRet = m_pLast;
m_pLast = m_pLast->Prev();
if (m_pLast) //if any element exists
{
m_pLast->Next(0); //set prev ptr of this element to 0
}
else
{
m_pFirst = 0; //set ptr to last element to 0 if ptr to first is zero as well
}
Validate();
m_Count--;
return pRet;
}
ILINE void AddFirst(TItem* pItem)
{
CPA_ASSERT(pItem); //ERROR AddFirst got 0 pointer
Validate();
pItem->Prev(0);
pItem->Next(m_pFirst);
if (!m_pFirst)
{
m_pLast = pItem;
}
else
{
m_pFirst->Prev(pItem);
}
m_pFirst = pItem;
m_Count++;
Validate();
}
ILINE void AddLast(TItem* pItem)
{
CPA_ASSERT(pItem); //ERROR AddLast got 0 pointer
Validate();
pItem->Prev(m_pLast);
pItem->Next(0);
if (!m_pLast)
{
m_pFirst = pItem;
}
else
{
m_pLast->Next(pItem);
}
m_pLast = pItem;
m_Count++;
Validate();
}
ILINE void AddBefore(TItem* pItem, TItem* pItemSuccessor)
{
CPA_ASSERT(pItem);
CPA_ASSERT(pItemSuccessor);
Validate();
pItem->Next(pItemSuccessor);
pItem->Prev(pItemSuccessor->Prev());
pItemSuccessor->Prev(pItem);
if (pItemSuccessor == m_pFirst)
{
m_pFirst = pItem;
}
else
{
pItem->Prev()->Next(pItem);
}
m_Count++;
Validate();
}
ILINE void AddBehind(TItem* pItem, TItem* pItemPredecessor)
{
CPA_ASSERT(pItem);
CPA_ASSERT(pItemPredecessor);
Validate();
pItem->Next(pItemPredecessor->Next());
pItem->Prev(pItemPredecessor);
pItemPredecessor->Next(pItem);
if (pItemPredecessor == m_pLast)
{
m_pLast = pItem;
}
else
{
pItem->Next()->Prev(pItem);
}
m_Count++;
Validate();
}
ILINE void Remove(TItem* pItem)
{
CPA_ASSERT(pItem); //ERROR releasing empty item
if (pItem == m_pFirst)
{
PopFirst();
return;
}
if (pItem == m_pLast)
{
PopLast();
return;
}
Validate(pItem);
pItem->Prev()->Next(pItem->Next());
pItem->Next()->Prev(pItem->Prev());
m_Count--;
Validate();
}
//debug
ILINE void Validate(TItem* pReferenceItem = 0)
{
if (!VALIDATE)
{
return;
}
//one-sided empty?
CPA_ASSERT((!First() && !Last()) || (First() && Last())); //ERROR validating item-list, just one end is 0
// endles linking?
TItem* pPrev = 0;
TItem* pItem = First();
while (pItem)
{
if (pReferenceItem == pItem)
{
pReferenceItem = 0;
}
CPA_ASSERT(pPrev == pItem->Prev()); //ERROR validating item-list, endless linking NULL
pPrev = pItem;
pItem = pItem->Next();
}
CPA_ASSERT(pPrev == Last()); //ERROR validating item-list, broken list, does not end at specified Last item
CPA_ASSERT(!pReferenceItem); //ERROR reference item not found in the item-list
}
ILINE size_t Count() const{return m_Count; }
};
}
#endif // CRYINCLUDE_CRYPOOL_LIST_H
+70
View File
@@ -0,0 +1,70 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
// Original file Copyright Crytek GMBH or its affiliates, used under license.
#ifndef CRYINCLUDE_CRYPOOL_MEMORY_H
#define CRYINCLUDE_CRYPOOL_MEMORY_H
#pragma once
namespace NCryPoolAlloc
{
class CMemoryDynamic
{
size_t m_Size;
uint8* m_pData;
protected:
ILINE CMemoryDynamic()
: m_Size(0)
, m_pData(0){}
public:
ILINE void InitMem(const size_t S, uint8* pData)
{
m_Size = S;
m_pData = pData;
CPA_ASSERT(S);
CPA_ASSERT(pData);
}
ILINE size_t MemSize() const{return m_Size; }
ILINE uint8* Data(){return m_pData; }
ILINE const uint8* Data() const{return m_pData; }
};
template<size_t TSize>
class CMemoryStatic
{
uint8 m_Data[TSize];
protected:
ILINE CMemoryStatic()
{
}
public:
ILINE void InitMem(const size_t S, uint8* pData)
{
}
ILINE size_t MemSize() const{return TSize; }
ILINE uint8* Data(){return m_Data; }
ILINE const uint8* Data() const{return m_Data; }
};
}
#endif // CRYINCLUDE_CRYPOOL_MEMORY_H
@@ -0,0 +1,55 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
// Original file Copyright Crytek GMBH or its affiliates, used under license.
#pragma once
#if defined(POOLALLOCTESTSUIT)
//cheat just for unit testing on windows
#include "BaseTypes.h"
#define ILINE inline
#endif
// Traits
#if defined(AZ_RESTRICTED_PLATFORM)
#include AZ_RESTRICTED_FILE(CryPool/PoolAlloc_h)
#elif defined(APPLE) || defined(LINUX)
#define POOLALLOC_H_TRAIT_USE_MEMALIGN 1
#endif
#if POOLALLOC_H_TRAIT_USE_MEMALIGN
#define CPA_ALLOC memalign
#define CPA_FREE free
#else
#define CPA_ALLOC _aligned_malloc
#define CPA_FREE _aligned_free
#endif
#define CPA_ASSERT assert
#define CPA_ASSERT_STATIC(X) {uint8 assertdata[(X) ? 0 : 1]; }
#define CPA_BREAK __debugbreak()
#include "List.h"
#include "Memory.h"
#include "Container.h"
#include "Allocator.h"
#include "Defrag.h"
#include "STLWrapper.h"
#include "Inspector.h"
#include "Fallback.h"
#if !defined(POOLALLOCTESTSUIT)
#include "ThreadSafe.h"
#endif
#undef CPA_ASSERT
#undef CPA_ASSERT_STATIC
#undef CPA_BREAK
@@ -0,0 +1,148 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
// Original file Copyright Crytek GMBH or its affiliates, used under license.
#ifndef CRYINCLUDE_CRYPOOL_STLWRAPPER_H
#define CRYINCLUDE_CRYPOOL_STLWRAPPER_H
#pragma once
namespace NCryPoolAlloc
{
//namespace CSTLPoolAllocWrapperHelper
//{
// inline void destruct(char *) {}
// inline void destruct(wchar_t*) {}
// template <typename T>
// inline void destruct(T *t) {t->~T();}
//}
//template <size_t S, class L, size_t A, typename T>
//struct CSTLPoolAllocWrapperStatic
//{
// static PoolAllocator<S, L, A> * allocator;
//};
//template <class T, class L, size_t A>
//struct CSTLPoolAllocWrapperKungFu : public CSTLPoolAllocWrapperStatic<sizeof(T),L,A,T>
//{
//};
template <class T, class TCont>
class CSTLPoolAllocWrapper
{
private:
static TCont* m_pContainer;
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
static TCont* Container(){return m_pContainer; }
static void Container(TCont* pContainer){m_pContainer = pContainer; }
template <class U>
struct rebind
{
typedef CSTLPoolAllocWrapper<T, TCont> other;
};
CSTLPoolAllocWrapper() throw()
{
}
CSTLPoolAllocWrapper(const CSTLPoolAllocWrapper&) throw()
{
}
template <class TTemp, class TTempCont>
CSTLPoolAllocWrapper(const CSTLPoolAllocWrapper<TTemp, TTempCont>&) throw()
{
}
~CSTLPoolAllocWrapper() throw()
{
}
pointer address(reference x) const
{
return &x;
}
const_pointer address(const_reference x) const
{
return &x;
}
pointer allocate(size_type n = 1, const_pointer hint = 0)
{
TCont* pContainer = Container();
uint8* pData = pContainer->TCont::template Allocate<uint8*>(n * sizeof(T), sizeof(T));
return pContainer->TCont::template Resolve<pointer>(pData);
// return Container()?Container()->Allocate<void*>(n*sizeof(T),sizeof(T)):0
}
void deallocate(pointer p, size_type n = 1)
{
if (Container())
{
Container()->Free(p);
}
}
size_type max_size() const throw()
{
return Container() ? Container()->MemSize() : 0;
}
void construct(pointer p, const T& val)
{
new(static_cast<void*>(p))T(val);
}
void construct(pointer p)
{
new(static_cast<void*>(p))T();
}
void destroy(pointer p)
{
p->~T();
}
pointer new_pointer()
{
return new(allocate())T();
}
pointer new_pointer(const T& val)
{
return new(allocate())T(val);
}
void delete_pointer(pointer p)
{
p->~T();
deallocate(p);
}
bool operator==(const CSTLPoolAllocWrapper&) {return true; }
bool operator!=(const CSTLPoolAllocWrapper&) {return false; }
};
}
#endif // CRYINCLUDE_CRYPOOL_STLWRAPPER_H
@@ -0,0 +1,58 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
// Original file Copyright Crytek GMBH or its affiliates, used under license.
#ifndef CRYINCLUDE_CRYPOOL_THREADSAFE_H
#define CRYINCLUDE_CRYPOOL_THREADSAFE_H
#pragma once
#include <CryThread.h>
namespace NCryPoolAlloc
{
template<class TAllocator>
class CThreadSafe
: public TAllocator
{
CryCriticalSection m_Mutex;
public:
template<class T>
ILINE T Allocate(size_t Size, size_t Align = 1)
{
CryAutoLock<CryCriticalSection> lock(m_Mutex);
return TAllocator::template Allocate<T>(Size, Align);
}
template<class T>
ILINE bool Free(T pData, bool ForceBoundsCheck = false)
{
CryAutoLock<CryCriticalSection> lock(m_Mutex);
return TAllocator::Free(pData, ForceBoundsCheck);
}
template<class T>
ILINE bool Resize(T** pData, size_t Size, size_t Alignment)
{
CryAutoLock<CryCriticalSection> lock(m_Mutex);
return TAllocator::Resize(pData, Size, Alignment);
}
};
}
#endif // CRYINCLUDE_CRYPOOL_THREADSAFE_H
+287
View File
@@ -0,0 +1,287 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
// Original file Copyright Crytek GMBH or its affiliates, used under license.
#ifndef CRYINCLUDE_CRYPOOL_EXAMPLE_H
#define CRYINCLUDE_CRYPOOL_EXAMPLE_H
#pragma once
//The documentation is split up into 3 main parts, so strg+f for
// -Theory
// -Building blocks
// -Usage
// -FAQ
// -Realloc/Resize
/////////////////////////////////////////////////////////////////////////
// -Theory
/////////////////////////////////////////////////////////////////////////
//this includes the 3 major parts of the allocate suite
//1. the memory location templates
//2. container types
//3. some allocator version
//addtional you get
//4. a simple stack based defragmentation template
//5. helper
//1. memory location templates
// There are two types of them, static and dynamic
//1.1 CMemoryStatic<size> allows you do define on compile time what size
// it should have, suitable for pool you know that they won't grow or
// shrink
//1.2 CMemoryDynamic, this one has no template parameter, it has just one
// indirection via ptr to the memory location and size, that you will
// set during initialization.
//2. Container types
// We have also two container types, one so called "In Place"
// and one "Referenced".
//2.1 "In Place" means that a header is placed above every allocation,
// this is the usual way most allocators work.
//2.2 "Referenced", has an extra pool of headers that point to the actual
// memory. This is suitable for
// - external memory locations that are not directly accessable by the
// cpu. E.g. pools on disk, networks, rsx memory..
// - defragmentation, because you don't save a ptr to the real memory
// location, just a "handle" of the referencing item.
// - big alignments, having 4kb of alignment would waste also
// - 4kb for ever "In Place" header, you might not want that.
//3. Allocators
// This time we have 3 of them, "BestFit", "WorstFit" and "FirstFit"
//3.1 FirstFit just seeks for any location big enought to fit your
// requested size of memory. Internally it also saves the last used
// free memory area to speed up allocations.
// Use this also if you have just one particular allocation size.
//3.2 WorstFit, although it might sound illogical, WorstFit can reduce
// memory fragmentation in a cases with very random allocation sizes,
// because it gives smaller free blocks the chance to concatenate to
// bigger free blocks again while filling up those previously
// generated big blocks. The bad side is that it takes quite some time
// to find the biggest block as this needs to be done every time you
// allocate, so use this just when having a low amount of allocations
// or you're really desperately looking for mem.
//3.3 BestFit, it's best used if you don't have just one allocation size,
// but still very few varying sizes. Previously released blocks of
// the currently allocating sizes will be seeked and reused, this
// strongly helps to reduce fragmentation. While this might be slow
// in some cases, it can save you from doing any defragmentation.
//4. Defragmentation
// At the moment just one defragmentation algorithm is implemented:
// "Stack defragmentator"
// If you don't want some block to be moved, "Lock" it using your
// memory handle.
//4.1 Stack based
// To reduce fragmentation, holes are filled up with the next used,
// memory area. This defragmentation sheme is useful when you have
// some long living locations as well as very short living ones.
// At some point all long live memory will end up at the bottom of
// the stack, while leaving empty memory areas at the top for short
// living allocations.
//5. Helper
// this should be filled up with some handy helper tools for this
// pool suite.
// The first tool is a wrapper for the usage with stl
//5.1 Wrapper for STL
// As you know, you can pass your own allocator as the last
// parameter of stl containers, with this helper you can use a pool
// created with this suite and wrap it for the stl.
/////////////////////////////////////////////////////////////////////////
// -Building blocks
/////////////////////////////////////////////////////////////////////////
//That's the theory, so how does it work?
//It's pretty simple, you compose the pool of your dreams by cascading
//templates.
//Lets start with an exmaple
//Per level you want to allocate a fixed amount of memory for your
//textures.
CMemoryDynamic
//- They are placed in some memory you can access directly with the cpu:
CInPlace
//- and you don't want to defragmentate, so you prefer an allocation
// sheme that reduces fragmentation.
CBestFit
//now you combine them
typedef CBestFit<CInPlace<CMemoryDynamic>, CListItemInPlace> TMyOwnPool;
//Yes, it's that simple.
//ok, ok, texture memory is usually nothing you want to access directly
//with your cpu, so let's create a referencing pool. Therefor you need
//to also specify how many nodes that can reference your pool will have.
//We won't have more than 4000 textures, so let's start with
{
enum TEXTURE_NODE_COUNT = 4096
};
//and now our referencing pool
typedef CBestFit < CReferenced<CMemoryDynamic, TEXTURE_NODE_COUNT> TMyOwnPool;
//But yeah, you're right, texture memory has also a fixed size, lets
//assume it's 128MB.
{
enum TEXTURE_MEMORY_SIZE = 128 * 1024 * 1024
};
//and our fixed sized memory pool
typedef CBestFit < CReferenced<CMemoryStatic<TEXTURE_MEMORY_SIZE>, TEXTURE_NODE_COUNT> TMyOwnPool;
//ok, but you don't trust the best fit allocator in all cases, you prefer
//a fast one and you accept the slow down for defragmentation incase the
//allocation fails.
//So lets created a straight First Fit allocator with defragmentation:
typedef CDefragStacked < CFirstFit<CReferenced<CMemoryStatic<TEXTURE_MEMORY_SIZE>, TEXTURE_NODE_COUNT> > TMyOwnPool;
//here you see how simple you can add defragmentation, but be careful, it
//works of course just on Reference based memory containers, if you have
//Direct pointers to In Place allocation, we cannot shuffle them around.
/////////////////////////////////////////////////////////////////////////
// -Usage
/////////////////////////////////////////////////////////////////////////
//it all starts by including the meain header
#include "PoolAlloc.h"
//Define your dream allocator, preferably using a typedef (or macro)
typedef CBestFit<CInPlace<CMemoryDynamic>, CListItemInPlace> TMyOwnPool;
//also typedef (or macro) your handle
typedef uint8* TMyHandle; //in case of "In Place" allocations
typedef uint32 TMyHandle; //in case of "Referenced"
//Instantiate it
TMyOwnPool g_MyMemory;
//now you need to initialize it,
g_MyMemory.InitMem(pMemoryArea, MemorySize); //in case you use "CMemoryDynamic"
g_MyMemory.InitMem(); //in case you use "CmemoryStatic,
//altough you could pass the same
//parameters, they'd be ignored.
//Use this also to flush the pool
//quickly
//now allocate
TMyHandle MemID = g_MyMemory.Allocate<TMyHandle>(Size);
//optionally alignment can be passed as 2nd parameter
TMyHandle MemID = g_MyMemory.Allocate<TMyHandle>(Size, Align);
//free it simply by calling
g_Memory.Free(MemID);
//you might want to call the beat function to defragment the memory
//on regular base
g_Memory.Beat();
//you might also want to call it just when an allocation failed to
//defragmentate the memory as good as possible
if (!(MemID = g_Memoery.Allocate<TMyHandle>(Size)))
{
while (g_Memory.Beat())
{
;
}
MemID = g_Memoery.Allocate<TMyHandle>(Size);
}
//To acquire the pointer to your data, you need to resolve the handle
MyObject* pObject = g_Memory.Resolve<MyOBject*>(MemID);
/////////////////////////////////////////////////////////////////////////
// -Realloc/Resize
/////////////////////////////////////////////////////////////////////////
// The Containers provide a "resize" function. This one does nothing else
// than the name suggest, it is freeing some memory at the end of your
// allocation or, if free memory is available, allocates some memory to
// the end of your buffer. But it may also fail, if not enough memory
// available to allocate.
// "Realloc" on the other side requires an extra template that you wrap
// around your existing one like:
typedef CReallocator<TMyOwnPool> TMyOwnPoolWithReallocation;
// This one will first try to use resize, but in case it fails, it will
// allocate a seperate memory area, copy the data and free the old one.
//
// But this may fail as well, therefor the result is not a pointer to the
// allocation, but true/false.
// There for you need to pass a pointer to your pointer to the memory area
// or handle you deal with.
Handle = rMemory.Allocate<TPtr>(10, 1);
if (!rMemory.Reallocate<TPtr>(&Handles, 11, 1))
{
//handle realloc failure
}
/////////////////////////////////////////////////////////////////////////
// -FAQ
/////////////////////////////////////////////////////////////////////////
//"DO I HAVE TO ALWAYS RESOLVE?"
//if you use "In Place" memory, not at all, all resolve does is to
//cast your handle to your object ptr and returns it.
//if you use "Referenced" memory and you don't defragmentate, you
//can do it once and keep the ptr, but you also need to keep the
//handle to free the memory later on.
//"any reason I should resolve?"
//Yes, first of all, it makes it very easy to switch between various
//pool configuration for testing, you simply change some params of
//your typedef (or macro) and it should work out of the box.
//second, for defragmentation it's the only way to go and for future
//things it might be needed as well
//"but isn't resolving just overhead?"
//in case of "In Place": no, the resolve function just returns the
//pointer, casting to your wanted type
//in case of "Referenced": it cost you one indirection.
//"How do I flush the whole pool without freeing all items?"
g_Memory.InitMem()
//yes, you can call "InitMem" once again, you need to pass the mem
//ptr and size if using CMemoryDynamic e.g.
g_Memory.Init(g_Memory.Size(), g_Memory.Data());
//"How do I lock the allocated memory to avoid any reallocation"
g_Memory.Item(ptr)->Lock();
//"How do I get the size of a memory block?"
g_Memory.Item(ptr)->MemSize();
//"Is there any example?"
//for a real life example check PAUnitTest.cpp used to validate all
//functions of this pool.
//bug reports? questions? support?
//just ask me :) (michael kopietz)
#endif // CRYINCLUDE_CRYPOOL_EXAMPLE_H