Skip to content

Commit

Permalink
Platform.Memory does not require Platform.WindowsAPI anymore. Refacto…
Browse files Browse the repository at this point in the history
…ring of Platform.Helpers and Platform.Tests (added some tests to ensure refactorings does not break anything).
  • Loading branch information
Konard committed Apr 14, 2016
1 parent 6f468f0 commit 71165d4
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 50 deletions.
4 changes: 0 additions & 4 deletions Platform/Platform.Helpers/ILGeneratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ public static void EmitJumpTo(this ILGenerator il, Label label)
public static void EmitCall(this ILGenerator il, MethodInfo method)
{
if (method.IsFinal || !method.IsVirtual)
{
il.EmitCall(OpCodes.Call, method, null);
}
else
{
il.EmitCall(OpCodes.Callvirt, method, null);
}
}

public static void EmitLiteralLoad(this ILGenerator il, int value)
Expand Down
12 changes: 1 addition & 11 deletions Platform/Platform.Helpers/RandomExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,7 @@ namespace Platform.Helpers
{
public static class RandomExtensions
{
public static ulong NextUInt64(this Random rnd)
{
return rnd.NextUInt64(UInt64.MaxValue);
}

public static ulong NextUInt64(this Random rnd, ulong maxValue)
{
return rnd.NextUInt64(UInt64.MinValue, maxValue);
}

public static ulong NextUInt64(this Random rnd, ulong minValue, ulong maxValue)
public static ulong NextUInt64(this Random rnd, ulong minValue = ulong.MinValue, ulong maxValue = ulong.MaxValue)
{
if (minValue >= maxValue)
return minValue;
Expand Down
17 changes: 4 additions & 13 deletions Platform/Platform.Helpers/SerializationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,22 @@ static XmlSerializer GetOrAddXmlSerializer<T>()

public static T DeserializeFromXml<T>(string xmlString)
{
var serializer = GetOrAddXmlSerializer<T>();
using (var reader = new StringReader(xmlString))
return (T)serializer.Deserialize(reader);
return (T)GetOrAddXmlSerializer<T>().Deserialize(reader);
}

public static void SerializeToFile<T>(string path, T obj)
{
var serializer = GetOrAddXmlSerializer<T>();
using (var fileStream = File.Open(path, FileMode.Create))
{
serializer.Serialize(fileStream, obj);
fileStream.Flush();
}
GetOrAddXmlSerializer<T>().Serialize(fileStream, obj);
}

public static string SerializeAsXmlString<T>(T obj)
{
var serializer = GetOrAddXmlSerializer<T>();
var sb = new StringBuilder();
using (var writer = new StringWriter(sb))
{
serializer.Serialize(writer, obj);
writer.Flush();
return sb.ToString();
}
GetOrAddXmlSerializer<T>().Serialize(writer, obj);
return sb.ToString();
}
}
}
2 changes: 1 addition & 1 deletion Platform/Platform.Helpers/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.7-*",
"version": "0.0.8-*",
"description": "LinksPlatform's Platform.Helpers Class Library",
"authors": [ "Konstantin Diachenko" ],
"tags": [ "Helpers", "Utilities", "Extensions" ],
Expand Down
11 changes: 7 additions & 4 deletions Platform/Platform.Memory/HeapResizableDirectMemory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using Platform.WindowsAPI;
using System.Runtime.InteropServices;

namespace Platform.Memory
{
Expand All @@ -8,7 +8,7 @@ namespace Platform.Memory
/// </summary>
/// <remarks>
/// TODO: Реализовать вариант с Virtual Memory
/// TODO: После использования WinApi подумать над реализацией под Mono
/// TODO: Test on Unix
/// </remarks>
public unsafe class HeapResizableDirectMemory : ResizableDirectMemoryBase
{
Expand All @@ -30,7 +30,10 @@ public HeapResizableDirectMemory()

protected override void OnReservedCapacityChanged(long oldReservedCapacity, long newReservedCapacity)
{
Pointer = Pointer == null ? Kernel32.HeapAlloc(newReservedCapacity) : Kernel32.HeapReAlloc(Pointer, newReservedCapacity);
//
Pointer = Pointer == null
? Marshal.AllocHGlobal(new IntPtr(newReservedCapacity)).ToPointer()
: Marshal.ReAllocHGlobal(new IntPtr(Pointer), new IntPtr(newReservedCapacity)).ToPointer();
}

#endregion
Expand All @@ -39,7 +42,7 @@ protected override void OnReservedCapacityChanged(long oldReservedCapacity, long

protected override void DisposePointer(void* pointer, long size)
{
Kernel32.HeapFree(pointer);
Marshal.FreeHGlobal(new IntPtr(pointer));
}

protected override void EnsureNotDisposed()
Expand Down
5 changes: 2 additions & 3 deletions Platform/Platform.Memory/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.8-*",
"version": "0.0.9-*",
"description": "LinksPlatform's Platform.Memory Class Library",
"authors": [ "Konstantin Diachenko" ],
"tags": [ "Memory Mapped Files", "Storage", "Memory" ],
Expand All @@ -11,8 +11,7 @@
"compilationOptions": { "allowUnsafe": true },

"dependencies": {
"Platform.Helpers": "0.0.7-*",
"Platform.WindowsAPI": "0.0.5-*"
"Platform.Helpers": "0.0.8-*"
},

"frameworks": {
Expand Down
3 changes: 3 additions & 0 deletions Platform/Platform.Sandbox/NetParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace Platform.Sandbox
{
/// <remarks>
/// Use PEG instead.
/// </remarks>
public class NetParser
{
const string StatementTermPart = @"((?<term>\w+)|""(?<termstring>[^""]+)""):";
Expand Down
22 changes: 11 additions & 11 deletions Platform/Platform.Tests/Data.Core/LinksMemoryManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@

namespace Platform.Tests.Data.Core
{
public class LinksMemoryManagerTests
public static class LinksMemoryManagerTests
{
[Fact]
public void BasicMemoryTest()
public static void BasicFileMappedMemoryTest()
{
var tempFilename = Path.GetTempFileName();

using (var memoryManager = new LinksMemoryManager(tempFilename))
{
var link = memoryManager.AllocateLink();
memoryManager.FreeLink(link);
}
memoryManager.TestBasicMemoryOperations();

File.Delete(tempFilename);
}

[Fact]
public void HeapMemorySupportTest()
public static void BasicHeapMemoryTest()
{
using (var memory = new HeapResizableDirectMemory(LinksMemoryManager.DefaultLinksSizeStep))
using (var memoryManager = new LinksMemoryManager(memory, LinksMemoryManager.DefaultLinksSizeStep))
{
var link = memoryManager.AllocateLink();
memoryManager.FreeLink(link);
}
memoryManager.TestBasicMemoryOperations();
}

private static void TestBasicMemoryOperations(this ILinksMemoryManager<ulong> memoryManager)
{
var link = memoryManager.AllocateLink();
memoryManager.FreeLink(link);
}
}
}
18 changes: 18 additions & 0 deletions Platform/Platform.Tests/Helpers/MathHelpersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Platform.Helpers;
using Xunit;

namespace Platform.Tests.Helpers
{
public class MathHelpersTests
{
[Theory]
[InlineData(00, -1)] // 0000 0000 (none, -1)
[InlineData(01, 00)] // 0000 0001 (first, 0)
[InlineData(08, 03)] // 0000 1000 (forth, 3)
[InlineData(88, 03)] // 0101 1000 (forth, 3)
public void GetLowestBitPosition(ulong value, int expectedPosition)
{
Assert.True(MathHelpers.GetLowestBitPosition(value) == expectedPosition);
}
}
}
28 changes: 28 additions & 0 deletions Platform/Platform.Tests/Helpers/SerializationHelpersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.IO;
using Platform.Helpers;
using Xunit;

namespace Platform.Tests.Helpers
{
public class SerializationHelpersTests
{
[Fact]
public void SerializeToFileTest()
{
var tempFilename = Path.GetTempFileName();

SerializationHelpers.SerializeToFile(tempFilename, new object());

Assert.True(File.ReadAllText(tempFilename) == "<?xml version=\"1.0\"?>\r\n<anyType xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" />");

File.Delete(tempFilename);
}

[Fact]
public void SerializeAsXmlStringTest()
{
var serializedObject = SerializationHelpers.SerializeAsXmlString(new object());
Assert.True(serializedObject == "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<anyType xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" />");
}
}
}
4 changes: 2 additions & 2 deletions Platform/Platform.Tests/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.8-*",
"version": "0.0.9-*",
"description": "LinksPlatform's Platform.Tests Class Library",
"authors": [ "Konstantin Diachenko" ],
"tags": [ "LinksPlatform", "Tests" ],
Expand All @@ -19,7 +19,7 @@
"xunit": "2.1.0",
"xunit.runner.dnx": "2.1.0-rc1-build204",
"Platform.Data.Core": "0.0.7-*",
"Platform.Helpers": "0.0.7-*"
"Platform.Helpers": "0.0.8-*"
},

"frameworks": {
Expand Down
3 changes: 2 additions & 1 deletion Platform/Platform.dotnet.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CLI/@EntryIndexedValue">CLI</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CSV/@EntryIndexedValue">CSV</s:String></wpf:ResourceDictionary>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CSV/@EntryIndexedValue">CSV</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IL/@EntryIndexedValue">IL</s:String></wpf:ResourceDictionary>

0 comments on commit 71165d4

Please sign in to comment.