Cosmos/source/Cosmos.Core.Memory.Test/HeapLarge.cs
2016-06-15 15:12:48 -04:00

31 lines
980 B
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Native = System.UInt32;
namespace Cosmos.Core.Memory.Test {
unsafe static public class HeapLarge {
public const Native PrefixBytes = 4 * sizeof(Native);
static public byte* Alloc(Native aSize) {
Native xPages = (Native)((aSize + PrefixBytes) / RAT.PageSize) + 1;
var xPtr = (Native*)RAT.Alloc(RAT.PageType.HeapLarge, xPages);
xPtr[0] = xPages * RAT.PageSize - PrefixBytes; // Allocated data size
xPtr[1] = aSize; // Actual data size
xPtr[2] = 0; // Ref count
xPtr[3] = 0; // Ptr to first
return (byte*)xPtr + PrefixBytes;
}
static public void Free(void* aPtr) {
// TODO - Should check the page type before freeing to make sure it is a Large?
// or just trust the caller to avoid adding overhead?
var xPageIdx = RAT.GetFirstRAT(aPtr);
RAT.Free(xPageIdx);
}
}
}