1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-29 12:55:59 +02:00
Beef/BeefLibs/corlib/src/Allocator.bf

53 lines
808 B
Beef
Raw Normal View History

2022-02-11 08:12:30 -05:00
using System.Reflection;
using System.Threading;
2020-06-16 10:33:52 -07:00
namespace System
{
interface IRawAllocator
{
void* Alloc(int size, int align);
void Free(void* ptr);
}
struct StdAllocator : IRawAllocator
{
public void* Alloc(int size, int align)
{
return Internal.StdMalloc(size);
}
public void Free(void* ptr)
{
Internal.StdFree(ptr);
}
}
2022-02-11 08:12:30 -05:00
struct AllocWrapper<T> where T : new, delete
{
alloctype(T) mVal;
public alloctype(T) Val
{
get mut
{
if (mVal != default)
return mVal;
var newVal = new T();
let prevValue = Interlocked.CompareExchange(ref mVal, default, newVal);
if (prevValue != default)
{
delete newVal;
return prevValue;
}
return newVal;
}
}
public void Dispose() mut
{
delete mVal;
mVal = default;
}
}
2020-06-16 10:33:52 -07:00
}
2022-02-11 08:12:30 -05:00