Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/CosmosOS/Cosmos
Browse files Browse the repository at this point in the history
  • Loading branch information
mterwoord committed Jun 15, 2016
2 parents f5e3ea6 + 257b9cf commit 452cad9
Show file tree
Hide file tree
Showing 25 changed files with 318 additions and 133 deletions.
22 changes: 14 additions & 8 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 Expand Up @@ -104,15 +110,15 @@ Is portable and simple. Can even be used without VirtMem, but increases time dur

```
MM API
-Allocate new item
+Allocate new item
-Add/remove ref
-Lock/unlock an item
-Force a compact
-Resize a heap item - now way to integrate .NET as it doesnt resize ever (copies instead), but may be useful internally.
Could also plug stringbuilder resize, etc. Possibly even special strings when used internally?
Implicit
-Get pointer
+Get pointer
Internal
-Compact
Expand Down
2 changes: 1 addition & 1 deletion Tests/Cosmos.Common.Tests/Cosmos.Common.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion Tests/Cosmos.Compiler.Tests/Cosmos.Compiler.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
Expand Down
3 changes: 2 additions & 1 deletion Tests/Cosmos.TestRunner.Core/DefaultEngineConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void Apply(Engine engine)
}

// Sets the time before an error is registered. For example if set to 60 then if a kernel runs for more than 60 seconds then
// that kernel will be marked as a failiure and terminated
// that kernel will be marked as a failure and terminated
engine.AllowedSecondsInKernel = 1800;

// If you want to test only specific platforms, add them to the list, like next line. By default, all platforms are run.
Expand Down Expand Up @@ -43,6 +43,7 @@ public static void Apply(Engine engine)
//engine.AddKernel(typeof(SimpleStructsAndArraysTest.Kernel).Assembly.Location);
//engine.AddKernel(typeof(Cosmos.Compiler.Tests.Exceptions.Kernel).Assembly.Location);
//engine.AddKernel(typeof(Cosmos.Compiler.Tests.MethodTests.Kernel).Assembly.Location);
//engine.AddKernel(typeof(Cosmos.Kernel.Tests.Fat.Kernel).Assembly.Location);

// Known bugs, therefore disabled for now:
//engine.AddKernel(typeof(BoxingTests.Kernel).Assembly.Location);
Expand Down
2 changes: 1 addition & 1 deletion Users/Matthijs/DebugCompiler/DebugCompiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<PlatformTarget>x86</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
Expand Down
18 changes: 18 additions & 0 deletions source/Cosmos.Build.Common.Tests/Cosmos.Build.Common.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
Expand Down
23 changes: 23 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 @@ -26,6 +26,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -35,6 +36,25 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
Expand All @@ -52,6 +72,9 @@
</Choose>
<ItemGroup>
<Compile Include="Heap.cs" />
<Compile Include="HeapLarge.cs" />
<Compile Include="HeapMedium.cs" />
<Compile Include="HeapSmall.cs" />
<Compile Include="RAT.cs" />
<Compile Include="UnitTest1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
22 changes: 8 additions & 14 deletions source/Cosmos.Core.Memory.Test/Heap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,17 @@ namespace Cosmos.Core.Memory.Test {

unsafe static public class Heap {
static public void Init() {

}

static public void* New(Native aSize) {
return null;
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);
} else {
return HeapLarge.Alloc(aSize);
}
}

static private void* NewBlock(int aSize) {
// size is inclusive? final sizse important when we get to vm

// 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;
}
}
}
24 changes: 24 additions & 0 deletions source/Cosmos.Core.Memory.Test/HeapLarge.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
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 HeapLarge {
public const Native PrefixBytes = 4 * sizeof(Native);

static public byte* Alloc(Native aSize) {
Native xPages = (Native)((aSize + PrefixBytes) / RAT.PageSize) + 1;
var xPtr = (Native*)RAT.Alloc(RAT.PageType.HeapLarge, xPages);

xPtr[0] = xPages * RAT.PageSize - PrefixBytes; // Allocated data size
xPtr[1] = aSize; // Actual data size
xPtr[2] = 0; // Ref count
xPtr[3] = 0; // Ptr to first

return (byte*)xPtr + PrefixBytes;
}
}
}
12 changes: 12 additions & 0 deletions source/Cosmos.Core.Memory.Test/HeapMedium.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
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 HeapMedium {
public const Native PrefixBytes = 4 * sizeof(Native);
}
}
10 changes: 10 additions & 0 deletions source/Cosmos.Core.Memory.Test/HeapSmall.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 {
unsafe static public class HeapSmall {
}
}
40 changes: 24 additions & 16 deletions source/Cosmos.Core.Memory.Test/RAT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@ 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;
}

// Used to bypass certain checks that will fail during tests and debugging.
static public bool Debug = false;

static private Native PtrSize = sizeof(Native);
// Native Intel page size
// x86 Page Size: 4k, 2m (PAE only), 4m
// x64 Page Size: 4k, 2m
Expand Down Expand Up @@ -46,17 +53,18 @@ static public void Init(byte* aStartPtr, Native aSize) {
mRamSize = aSize;
mPageCount = aSize / PageSize;

mRAT = mRamStart;
// Clear RAT
for (Native i = 0; i < mPageCount; i++) {
mRAT[i] = PageType.Empty;
}

// We need one status byte for each block.
// Intel blocks are 4k (10 bits). So for 4GB, this means
// 32 - 12 = 20 bits, 1 MB for a RAT for 4GB. 0.025%
Native xRatPageCount = mPageCount / PageSize;
Alloc(PageType.RAT, xRatPageCount);
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 (Native i = xRatPageBytes - xRatPageCount; i < xRatPageBytes; i++) {
mRAT[i] = PageType.RAT;
}

Heap.Init();
}
Expand All @@ -80,31 +88,31 @@ 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) {
xPos = i - xCount - 1;
if (xCount == aPageCount) {
xPos = i - xCount + 1;
break;
}
} else {
xCount = 0;
}
}
} else {
for (Native i = mPageCount - 1; i >= 0; i--) {
for (Native i = mPageCount - 1; i != Native.MaxValue; i--) {
if (mRAT[i] == PageType.Empty) {
xCount++;
if (xCount == aCount) {
if (xCount == aPageCount) {
xPos = i;
break;
}
Expand Down
9 changes: 7 additions & 2 deletions source/Cosmos.Core.Memory.Test/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ unsafe public void TestMethod1() {
xRAM[0] = 1;
fixed (byte* xPtr = xRAM) {
RAT.Debug = true;
RAT.Init(xPtr, (UInt32)xRAM.LongLength);
RAT.Init(xPtr, (Native)xRAM.LongLength);

Native xRatPages = RAT.GetPageCount(RAT.PageType.RAT);
Assert.IsTrue(xRatPages > 0);

Native xFreePages = RAT.GetPageCount(RAT.PageType.Empty);
var xFreePages = RAT.GetPageCount(RAT.PageType.Empty);
Assert.IsTrue(xFreePages > 0);

var x1 = (Int32*)Heap.Alloc(sizeof(Int32));
var xFreePages2 = RAT.GetPageCount(RAT.PageType.Empty);
Assert.IsTrue(xFreePages - xFreePages2 == 1);
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions source/Cosmos.Core.Memory/Cosmos.Core.Memory.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@
<PropertyGroup>
<AssemblyOriginatorKeyFile>..\Cosmos.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.Core.Tests/Cosmos.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<DefineConstants>TRACE;DEBUG</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DebugType>full</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.System.Plugs/System/IO/DirectoryImpl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//define COSMOSDEBUG
//#define COSMOSDEBUG

using System;
using System.IO;
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.System.Plugs/System/IO/DirectoryInfoImpl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//define COSMOSDEBUG
//#define COSMOSDEBUG

using System;
using System.Collections.Generic;
Expand Down
Loading

0 comments on commit 452cad9

Please sign in to comment.