mirror of
https://github.com/danbulant/Cosmos
synced 2026-05-24 12:35:31 +00:00
Merge branch 'master' of https://github.com/CosmosOS/Cosmos
This commit is contained in:
commit
33da131344
2 changed files with 23 additions and 24 deletions
|
|
@ -8,8 +8,6 @@ using Native = System.UInt32;
|
||||||
namespace Cosmos.Core.Memory.Test {
|
namespace Cosmos.Core.Memory.Test {
|
||||||
|
|
||||||
unsafe static public class Heap {
|
unsafe static public class Heap {
|
||||||
static public void Init() {
|
|
||||||
}
|
|
||||||
|
|
||||||
static public byte* Alloc(Native aSize) {
|
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
|
//TODO - Dont use medium if its close to the page size - does'nt make sense to make a medium page
|
||||||
|
|
|
||||||
|
|
@ -59,22 +59,21 @@ namespace Cosmos.Core.Memory.Test {
|
||||||
Native xRatPageCount = (mPageCount - 1) / PageSize + 1;
|
Native xRatPageCount = (mPageCount - 1) / PageSize + 1;
|
||||||
Native xRatPageBytes = xRatPageCount * PageSize;
|
Native xRatPageBytes = xRatPageCount * PageSize;
|
||||||
mRAT = mRamStart + mRamSize - xRatPageBytes;
|
mRAT = mRamStart + mRamSize - xRatPageBytes;
|
||||||
for (Native i = 0; i < xRatPageBytes - xRatPageCount; i++) {
|
for (byte* p = mRAT; p < mRAT + xRatPageBytes - xRatPageCount; p++) {
|
||||||
mRAT[i] = PageType.Empty;
|
*p = PageType.Empty;
|
||||||
}
|
}
|
||||||
for (Native i = xRatPageBytes - xRatPageCount; i < xRatPageBytes; i++) {
|
for (byte* p = mRAT + xRatPageBytes - xRatPageCount; p < mRAT + xRatPageBytes; p++) {
|
||||||
mRAT[i] = PageType.RAT;
|
*p = PageType.RAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
Heap.Init();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static public Native GetPageCount(byte aType = 0) {
|
static public Native GetPageCount(byte aType = 0) {
|
||||||
Native xResult = 0;
|
Native xResult = 0;
|
||||||
|
byte xType = 0; // Could us nullable type instead of this + xCounting, but this is faster.
|
||||||
bool xCounting = false;
|
bool xCounting = false;
|
||||||
for (Native i = 0; i < mPageCount; i++) {
|
for (byte* p = mRAT; p < mRAT + mPageCount; p++) {
|
||||||
byte xType = mRAT[i];
|
if (*p == aType) {
|
||||||
if (xType == aType) {
|
xType = *p;
|
||||||
xResult++;
|
xResult++;
|
||||||
xCounting = true;
|
xCounting = true;
|
||||||
} else if (xCounting) {
|
} else if (xCounting) {
|
||||||
|
|
@ -89,7 +88,7 @@ namespace Cosmos.Core.Memory.Test {
|
||||||
}
|
}
|
||||||
|
|
||||||
static public byte* Alloc(byte aType, Native aPageCount = 1) {
|
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
|
// Could combine with an external method or delegate, but will slow things down
|
||||||
// unless we can force it to be inlined.
|
// 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.
|
// Alloc single blocks at bottom, larger blocks at top to help reduce fragmentation.
|
||||||
Native xCount = 0;
|
Native xCount = 0;
|
||||||
if (aPageCount == 1) {
|
if (aPageCount == 1) {
|
||||||
for (Native i = 0; i < mPageCount; i++) {
|
for (byte* p = mRAT; p < mRAT + mPageCount; p++) {
|
||||||
if (mRAT[i] == PageType.Empty) {
|
if (*p == PageType.Empty) {
|
||||||
xCount++;
|
xCount++;
|
||||||
if (xCount == aPageCount) {
|
if (xCount == aPageCount) {
|
||||||
xPos = i - xCount + 1;
|
xPos = p - xCount + 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -109,11 +108,13 @@ namespace Cosmos.Core.Memory.Test {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (Native i = mPageCount - 1; i != Native.MaxValue; i--) {
|
// This loop will FAIL if mRAT is ever 0. This should be impossible though
|
||||||
if (mRAT[i] == PageType.Empty) {
|
// 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++;
|
xCount++;
|
||||||
if (xCount == aPageCount) {
|
if (xCount == aPageCount) {
|
||||||
xPos = i;
|
xPos = p;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -123,11 +124,11 @@ namespace Cosmos.Core.Memory.Test {
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we found enough space, mark it as used.
|
// If we found enough space, mark it as used.
|
||||||
if (xPos.HasValue) {
|
if (xPos != null) {
|
||||||
byte* xResult = mRamStart + xPos.Value * PageSize;
|
byte* xResult = mRamStart + (xPos - mRAT) * PageSize;
|
||||||
mRAT[xPos.Value] = aType;
|
*xPos = aType;
|
||||||
for (Native i = xPos.Value + 1; i < xPos.Value + xCount; i++) {
|
for (byte* p = xPos + 1; p < xPos + xCount; p++) {
|
||||||
mRAT[i] = PageType.Extension;
|
*p = PageType.Extension;
|
||||||
}
|
}
|
||||||
return xResult;
|
return xResult;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue