This commit is contained in:
Matthijs ter Woord 2016-06-16 13:41:40 -04:00
commit 33da131344
2 changed files with 23 additions and 24 deletions

View file

@ -8,9 +8,7 @@ using Native = System.UInt32;
namespace Cosmos.Core.Memory.Test {
unsafe static public class Heap {
static public void Init() {
}
static public byte* Alloc(Native aSize) {
//TODO - Dont use medium if its close to the page size - does'nt make sense to make a medium page
// with only enough free space for something that would be small anyway.

View file

@ -59,22 +59,21 @@ namespace Cosmos.Core.Memory.Test {
Native xRatPageCount = (mPageCount - 1) / PageSize + 1;
Native xRatPageBytes = xRatPageCount * PageSize;
mRAT = mRamStart + mRamSize - xRatPageBytes;
for (Native i = 0; i < xRatPageBytes - xRatPageCount; i++) {
mRAT[i] = PageType.Empty;
for (byte* p = mRAT; p < mRAT + xRatPageBytes - xRatPageCount; p++) {
*p = PageType.Empty;
}
for (Native i = xRatPageBytes - xRatPageCount; i < xRatPageBytes; i++) {
mRAT[i] = PageType.RAT;
for (byte* p = mRAT + xRatPageBytes - xRatPageCount; p < mRAT + xRatPageBytes; p++) {
*p = PageType.RAT;
}
Heap.Init();
}
static public Native GetPageCount(byte aType = 0) {
Native xResult = 0;
byte xType = 0; // Could us nullable type instead of this + xCounting, but this is faster.
bool xCounting = false;
for (Native i = 0; i < mPageCount; i++) {
byte xType = mRAT[i];
if (xType == aType) {
for (byte* p = mRAT; p < mRAT + mPageCount; p++) {
if (*p == aType) {
xType = *p;
xResult++;
xCounting = true;
} else if (xCounting) {
@ -89,7 +88,7 @@ namespace Cosmos.Core.Memory.Test {
}
static public byte* Alloc(byte aType, Native aPageCount = 1) {
Native? xPos = null;
byte* xPos = null;
// Could combine with an external method or delegate, but will slow things down
// unless we can force it to be inlined.
@ -97,11 +96,11 @@ namespace Cosmos.Core.Memory.Test {
// Alloc single blocks at bottom, larger blocks at top to help reduce fragmentation.
Native xCount = 0;
if (aPageCount == 1) {
for (Native i = 0; i < mPageCount; i++) {
if (mRAT[i] == PageType.Empty) {
for (byte* p = mRAT; p < mRAT + mPageCount; p++) {
if (*p == PageType.Empty) {
xCount++;
if (xCount == aPageCount) {
xPos = i - xCount + 1;
xPos = p - xCount + 1;
break;
}
} else {
@ -109,11 +108,13 @@ namespace Cosmos.Core.Memory.Test {
}
}
} else {
for (Native i = mPageCount - 1; i != Native.MaxValue; i--) {
if (mRAT[i] == PageType.Empty) {
// This loop will FAIL if mRAT is ever 0. This should be impossible though
// so we don't bother to account for such a case. xPos would also have issues.
for (byte* p = mRAT + mPageCount - 1; p >= mRAT; p--) {
if (*p == PageType.Empty) {
xCount++;
if (xCount == aPageCount) {
xPos = i;
xPos = p;
break;
}
} else {
@ -123,11 +124,11 @@ namespace Cosmos.Core.Memory.Test {
}
// If we found enough space, mark it as used.
if (xPos.HasValue) {
byte* xResult = mRamStart + xPos.Value * PageSize;
mRAT[xPos.Value] = aType;
for (Native i = xPos.Value + 1; i < xPos.Value + xCount; i++) {
mRAT[i] = PageType.Extension;
if (xPos != null) {
byte* xResult = mRamStart + (xPos - mRAT) * PageSize;
*xPos = aType;
for (byte* p = xPos + 1; p < xPos + xCount; p++) {
*p = PageType.Extension;
}
return xResult;
}