mirror of
https://github.com/danbulant/Cosmos
synced 2026-05-22 05:48:37 +00:00
heap
This commit is contained in:
parent
d1447458af
commit
7ed55bdbea
8 changed files with 75 additions and 21 deletions
|
|
@ -45,12 +45,18 @@ inline increases block size, but makes it faster to find metadata and keeps it u
|
|||
Could have a single pointer to another record elsewhere, but increases complexity and does not provide
|
||||
major benefit.
|
||||
-Data (handle pointer points here)
|
||||
-64 Ref count
|
||||
-64 Ptr to first ref
|
||||
-Optional - Stacked and single only
|
||||
-64 Size (always need, cant interpolate even from slotted as may be smaller than slot)
|
||||
-Always allocate bigger to word align (or page align for large?). Wont bother .NET, it never needs size from heap.
|
||||
-If need actual size - could add 3 and mask lower 2 bits to round up.
|
||||
-32/64 Ptr to first ref
|
||||
using 32 below saves space on small/med objects. slightly increases access time, but access to these fields is not time critical
|
||||
-32 (64 to align on large) Ref count
|
||||
-32 (64 for large) Size (always need, cant interpolate even from slotted as may be smaller than slot)
|
||||
-Always allocate bigger to word align (or page align for large?). Wont bother .NET, it never needs size from heap.
|
||||
-If need align - could add 3 and mask lower 2 bits to round up.
|
||||
-Optional
|
||||
-32/64 Size allocated - may be bigger and not needed for slotted etc.
|
||||
|
||||
-Tiny
|
||||
If needed can make tiny types too with only ref and ptr, no need for size. But check heap and
|
||||
see if such small ones are common. Probably not, likely only for boxing etc.
|
||||
|
||||
-Small (Tables)
|
||||
Fixed sizes, max size one page.
|
||||
|
|
|
|||
|
|
@ -52,6 +52,9 @@
|
|||
</Choose>
|
||||
<ItemGroup>
|
||||
<Compile Include="Heap.cs" />
|
||||
<Compile Include="HeapItemLarge.cs" />
|
||||
<Compile Include="HeapItemMedium.cs" />
|
||||
<Compile Include="HeapItemSmall.cs" />
|
||||
<Compile Include="RAT.cs" />
|
||||
<Compile Include="UnitTest1.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
|
|
|||
|
|
@ -9,23 +9,29 @@ namespace Cosmos.Core.Memory.Test {
|
|||
|
||||
unsafe static public class Heap {
|
||||
static public void Init() {
|
||||
|
||||
}
|
||||
|
||||
static public void* New(Native aSize) {
|
||||
return null;
|
||||
}
|
||||
|
||||
static private void* NewBlock(int aSize) {
|
||||
// size is inclusive? final sizse important when we get to vm
|
||||
static private void* NewBlock(Native aSize) {
|
||||
return NewBlockLarge(aSize);
|
||||
}
|
||||
|
||||
// Block Status - 1 byte of 4
|
||||
// -Has Data
|
||||
// -Empty (Can be removed or merged)
|
||||
// Next Block - Pointer to data. 0 if this is current last.
|
||||
// Data Size - Native - Size of data, not including header.
|
||||
// Data
|
||||
return null;
|
||||
static private void* NewBlockLarge(Native aSize) {
|
||||
const Native xPrefixWordsLarge = 4;
|
||||
const Native xPrefixSizeLarge = xPrefixWordsLarge * sizeof(Native);
|
||||
|
||||
Native xPages = (Native)((aSize + xPrefixSizeLarge) / RAT.PageSize);
|
||||
var xPtr = (Native*)RAT.Alloc(RAT.PageType.HeapLarge, xPages);
|
||||
|
||||
xPtr[0] = xPages * RAT.PageSize - xPrefixSizeLarge; // Allocated data size
|
||||
xPtr[1] = aSize; // Actual data size
|
||||
xPtr[2] = 0; // Ref count
|
||||
xPtr[3] = 0; // Ptr to first
|
||||
|
||||
return xPtr + xPrefixWordsLarge;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
10
source/Cosmos.Core.Memory.Test/HeapItemLarge.cs
Normal file
10
source/Cosmos.Core.Memory.Test/HeapItemLarge.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Cosmos.Core.Memory.Test {
|
||||
static public class HeapItemLarge {
|
||||
}
|
||||
}
|
||||
10
source/Cosmos.Core.Memory.Test/HeapItemMedium.cs
Normal file
10
source/Cosmos.Core.Memory.Test/HeapItemMedium.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Cosmos.Core.Memory.Test {
|
||||
static public class HeapItemMedium {
|
||||
}
|
||||
}
|
||||
10
source/Cosmos.Core.Memory.Test/HeapItemSmall.cs
Normal file
10
source/Cosmos.Core.Memory.Test/HeapItemSmall.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Cosmos.Core.Memory.Test {
|
||||
static public class HeapItemSmall {
|
||||
}
|
||||
}
|
||||
|
|
@ -9,8 +9,16 @@ namespace Cosmos.Core.Memory.Test {
|
|||
unsafe static public class RAT {
|
||||
static public class PageType {
|
||||
public const byte Empty = 0;
|
||||
public const byte RAT = 1;
|
||||
|
||||
// Data Types from 1, special meanings from 255 down.
|
||||
public const byte RAT = 1;
|
||||
public const byte HeapSmall = 2;
|
||||
public const byte HeapMedium = 3;
|
||||
public const byte HeapLarge = 4;
|
||||
// Code
|
||||
// Stack
|
||||
// Disk Cache
|
||||
|
||||
// Extension of previous page.
|
||||
public const byte Extension = 255;
|
||||
}
|
||||
|
|
@ -80,7 +88,7 @@ namespace Cosmos.Core.Memory.Test {
|
|||
return xResult;
|
||||
}
|
||||
|
||||
static private byte* Alloc(byte aType, Native aCount = 1) {
|
||||
static public byte* Alloc(byte aType, Native aPageCount = 1) {
|
||||
Native? xPos = null;
|
||||
|
||||
// Could combine with an external method or delegate, but will slow things down
|
||||
|
|
@ -88,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 (aCount == 1) {
|
||||
if (aPageCount == 1) {
|
||||
for (Native i = 0; i < mPageCount; i++) {
|
||||
if (mRAT[i] == PageType.Empty) {
|
||||
xCount++;
|
||||
if (xCount == aCount) {
|
||||
if (xCount == aPageCount) {
|
||||
xPos = i - xCount - 1;
|
||||
break;
|
||||
}
|
||||
|
|
@ -104,7 +112,7 @@ namespace Cosmos.Core.Memory.Test {
|
|||
for (Native i = mPageCount - 1; i >= 0; i--) {
|
||||
if (mRAT[i] == PageType.Empty) {
|
||||
xCount++;
|
||||
if (xCount == aCount) {
|
||||
if (xCount == aPageCount) {
|
||||
xPos = i;
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ namespace Cosmos.Core.Memory.Test {
|
|||
Assert.IsTrue(xRatPages > 0);
|
||||
|
||||
Native xFreePages = RAT.GetPageCount(RAT.PageType.Empty);
|
||||
Assert.IsTrue(xFreePages > 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue