Skip to content

Commit

Permalink
heap
Browse files Browse the repository at this point in the history
  • Loading branch information
Kudzu committed Jun 14, 2016
1 parent d144745 commit 7ed55bd
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 21 deletions.
18 changes: 12 additions & 6 deletions Docs/Kernel/Memory.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions source/Cosmos.Core.Memory.Test/Cosmos.Core.Memory.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
26 changes: 16 additions & 10 deletions source/Cosmos.Core.Memory.Test/Heap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 10 additions & 0 deletions source/Cosmos.Core.Memory.Test/HeapItemLarge.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions source/Cosmos.Core.Memory.Test/HeapItemMedium.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions source/Cosmos.Core.Memory.Test/HeapItemSmall.cs
Original file line number Diff line number Diff line change
@@ -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 {
}
}
18 changes: 13 additions & 5 deletions source/Cosmos.Core.Memory.Test/RAT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -80,19 +88,19 @@ static public Native GetPageCount(byte aType = 0) {
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
// unless we can force it to be inlined.
//
// 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;
}
Expand All @@ -104,7 +112,7 @@ static public Native GetPageCount(byte aType = 0) {
for (Native i = mPageCount - 1; i >= 0; i--) {
if (mRAT[i] == PageType.Empty) {
xCount++;
if (xCount == aCount) {
if (xCount == aPageCount) {
xPos = i;
break;
}
Expand Down
1 change: 1 addition & 0 deletions source/Cosmos.Core.Memory.Test/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ unsafe public void TestMethod1() {
Assert.IsTrue(xRatPages > 0);

Native xFreePages = RAT.GetPageCount(RAT.PageType.Empty);
Assert.IsTrue(xFreePages > 0);
}
}
}
Expand Down

0 comments on commit 7ed55bd

Please sign in to comment.