mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-08 11:38:21 +02:00
Added ITypedAllocator, SingleAllocator
This commit is contained in:
parent
3aacd09199
commit
3b711932ec
2 changed files with 81 additions and 0 deletions
|
@ -9,6 +9,11 @@ namespace System
|
||||||
void Free(void* ptr);
|
void Free(void* ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ITypedAllocator : IRawAllocator
|
||||||
|
{
|
||||||
|
void* AllocTyped(Type type, int size, int align);
|
||||||
|
}
|
||||||
|
|
||||||
struct StdAllocator : IRawAllocator
|
struct StdAllocator : IRawAllocator
|
||||||
{
|
{
|
||||||
public void* Alloc(int size, int align)
|
public void* Alloc(int size, int align)
|
||||||
|
@ -22,6 +27,76 @@ namespace System
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class SingleAllocator : ITypedAllocator
|
||||||
|
{
|
||||||
|
void* mPtr;
|
||||||
|
int mSize;
|
||||||
|
Type mType;
|
||||||
|
|
||||||
|
[AllowAppend]
|
||||||
|
public this(int size)
|
||||||
|
{
|
||||||
|
void* ptr = append uint8[size]*(?);
|
||||||
|
mPtr = ptr;
|
||||||
|
mSize = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ~this()
|
||||||
|
{
|
||||||
|
if ((mType != null) && (mType.IsObject))
|
||||||
|
{
|
||||||
|
Object obj = Internal.UnsafeCastToObject(mPtr);
|
||||||
|
delete:null obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mSize < 0)
|
||||||
|
delete mPtr;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void* AllocLarge(int size, int align)
|
||||||
|
{
|
||||||
|
return new uint8[size]*;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void FreeLarge(void* ptr)
|
||||||
|
{
|
||||||
|
delete ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void* Alloc(int size, int align)
|
||||||
|
{
|
||||||
|
return AllocTyped(typeof(void), size, align);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Free(void* ptr)
|
||||||
|
{
|
||||||
|
Runtime.Assert(ptr == mPtr);
|
||||||
|
mType = typeof(void);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Optimize]
|
||||||
|
public void* AllocTyped(Type type, int size, int align)
|
||||||
|
{
|
||||||
|
Runtime.Assert(mType == null, "SingleAllocator has been used for multiple allocations");
|
||||||
|
|
||||||
|
mType = type;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
if (size > size)
|
||||||
|
break;
|
||||||
|
void* usePtr = (void*)(int)Math.Align((int)mPtr, align);
|
||||||
|
if ((uint8*)usePtr + size > (uint8*)mPtr + mSize)
|
||||||
|
break;
|
||||||
|
mPtr = usePtr;
|
||||||
|
mSize = 0;
|
||||||
|
return mPtr;
|
||||||
|
}
|
||||||
|
mSize = -1;
|
||||||
|
mPtr = AllocLarge(size, align);
|
||||||
|
return mPtr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct AllocWrapper<T> where T : new, delete
|
struct AllocWrapper<T> where T : new, delete
|
||||||
{
|
{
|
||||||
alloctype(T) mVal;
|
alloctype(T) mVal;
|
||||||
|
|
|
@ -1004,6 +1004,12 @@ void BfAutoComplete::AddTypeMembers(BfTypeInstance* typeInst, bool addStatic, bo
|
||||||
AddTypeMembers(mModule->mContext->mBfObjectType, addStatic, addNonStatic, filter, startType, false, allowImplicitThis, false);
|
AddTypeMembers(mModule->mContext->mBfObjectType, addStatic, addNonStatic, filter, startType, false, allowImplicitThis, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeInst->IsInterface())
|
||||||
|
{
|
||||||
|
for (auto interface : typeInst->mInterfaces)
|
||||||
|
AddTypeMembers(interface.mInterfaceType, addStatic, addNonStatic, filter, startType, false, allowImplicitThis, false);
|
||||||
|
}
|
||||||
|
|
||||||
if ((addStatic) && (allowImplicitThis) && (checkOuterType))
|
if ((addStatic) && (allowImplicitThis) && (checkOuterType))
|
||||||
{
|
{
|
||||||
auto outerType = mModule->GetOuterType(typeInst);
|
auto outerType = mModule->GetOuterType(typeInst);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue