From d1ed1caafbb69d197bc49eb93e94df32d727d3e8 Mon Sep 17 00:00:00 2001 From: Kudzu Date: Thu, 16 Jun 2016 14:50:37 -0400 Subject: [PATCH] heap --- source/Cosmos.Core.Memory.Test/Heap.cs | 10 +++++----- source/Cosmos.Core.Memory.Test/HeapMedium.cs | 13 +++++++++++++ source/Cosmos.Core.Memory.Test/HeapSmall.cs | 11 +++++++++++ source/Cosmos.Core.Memory.Test/RAT.cs | 2 +- source/Cosmos.Core.Memory.Test/UnitTest1.cs | 2 +- 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/source/Cosmos.Core.Memory.Test/Heap.cs b/source/Cosmos.Core.Memory.Test/Heap.cs index 71c9801f8..e92f0b43b 100644 --- a/source/Cosmos.Core.Memory.Test/Heap.cs +++ b/source/Cosmos.Core.Memory.Test/Heap.cs @@ -8,12 +8,12 @@ using Native = System.UInt32; namespace Cosmos.Core.Memory.Test { unsafe static public class Heap { - + 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. - if (aSize <= RAT.PageSize - HeapMedium.PrefixBytes) { - return HeapLarge.Alloc(aSize); + if (aSize <= HeapSmall.MaxItemSize) { + return HeapSmall.Alloc(aSize); + } else if (aSize <= HeapMedium.MaxItemSize) { + return HeapMedium.Alloc(aSize); } else { return HeapLarge.Alloc(aSize); } diff --git a/source/Cosmos.Core.Memory.Test/HeapMedium.cs b/source/Cosmos.Core.Memory.Test/HeapMedium.cs index fb74a9a95..d45fde61c 100644 --- a/source/Cosmos.Core.Memory.Test/HeapMedium.cs +++ b/source/Cosmos.Core.Memory.Test/HeapMedium.cs @@ -8,5 +8,18 @@ using Native = System.UInt32; namespace Cosmos.Core.Memory.Test { unsafe static public class HeapMedium { public const Native PrefixBytes = 4 * sizeof(Native); + // TODO Adjust when page size changes from 4k to 2/4mb + // Also adjust according to heap stats and final adjustments to small heap. ie the -1024 should be at least size of + // max small heap else it will never get used; + // HeapMedium may be of limited use with 4k pages depending on the final sizes of the small heap. + public const Native MaxItemSize = RAT.PageSize - 1024; + + static public byte* Alloc(Native aSize) { + return HeapLarge.Alloc(aSize); + } + + static public void Free(void* aPtr) { + HeapLarge.Free(aPtr); + } } } diff --git a/source/Cosmos.Core.Memory.Test/HeapSmall.cs b/source/Cosmos.Core.Memory.Test/HeapSmall.cs index 996c5fa6c..3c4827c3c 100644 --- a/source/Cosmos.Core.Memory.Test/HeapSmall.cs +++ b/source/Cosmos.Core.Memory.Test/HeapSmall.cs @@ -3,8 +3,19 @@ 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 HeapSmall { + public const Native PrefixBytes = 4 * sizeof(Native); + public const Native MaxItemSize = 1024 - PrefixBytes; + + static public byte* Alloc(Native aSize) { + return HeapLarge.Alloc(aSize); + } + + static public void Free(void* aPtr) { + HeapLarge.Free(aPtr); + } } } diff --git a/source/Cosmos.Core.Memory.Test/RAT.cs b/source/Cosmos.Core.Memory.Test/RAT.cs index 8ea7cc2b9..bf0586483 100644 --- a/source/Cosmos.Core.Memory.Test/RAT.cs +++ b/source/Cosmos.Core.Memory.Test/RAT.cs @@ -29,7 +29,7 @@ namespace Cosmos.Core.Memory.Test { // Native Intel page size // x86 Page Size: 4k, 2m (PAE only), 4m // x64 Page Size: 4k, 2m - static public readonly Native PageSize = 4096; + public const Native PageSize = 4096; // Start of area usable for heap, and also start of heap. static private byte* mRamStart; diff --git a/source/Cosmos.Core.Memory.Test/UnitTest1.cs b/source/Cosmos.Core.Memory.Test/UnitTest1.cs index bfa06bbfa..cee9b0e60 100644 --- a/source/Cosmos.Core.Memory.Test/UnitTest1.cs +++ b/source/Cosmos.Core.Memory.Test/UnitTest1.cs @@ -7,7 +7,7 @@ namespace Cosmos.Core.Memory.Test { public class UnitTest1 { [TestMethod] unsafe public void TestMethod1() { - var xRAM = new byte[128 * 1024 * 1024]; + var xRAM = new byte[128 * 1024 * 1024]; // 128 MB xRAM[0] = 1; fixed (byte* xPtr = xRAM) { RAT.Debug = true;