Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Firwood-Software/AdvancedDLSupport
Browse files Browse the repository at this point in the history
  • Loading branch information
Nihlus committed Sep 27, 2019
2 parents 171bd23 + dd003ff commit feaa38f
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 144 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ addons:
packages:
- gcc-multilib

mono: latest
mono: 6.0.0
dotnet: 2.2.402

before_install:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
//

using System;
using System.Runtime.InteropServices;
using AdvancedDLSupport.Tests.TestBases;

#pragma warning disable SA1600, CS1591

Expand All @@ -29,5 +27,7 @@ public interface ISpanMarshallingTests
{
[return: NativeCollectionLength(10)]
Span<int> GetInt32ArrayZeroToNine();

void WriteToInt32Array(Span<int> span, int arrLen);
}
}
13 changes: 13 additions & 0 deletions AdvancedDLSupport.Tests/Tests/Integration/SpanMarshallingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ public void CanMarshalCollectionWithConstLength()
}
}

[Fact]
public void CanMarshalSpanAsPointer()
{
Span<int> span = stackalloc int[10];

Library.WriteToInt32Array(span, 10);

for (var i = 0; i < 10; i++)
{
Assert.True(span[i] == i);
}
}

[Fact]
public void ThrowsWhenSpanTypeIsReferenceType()
{
Expand Down
37 changes: 32 additions & 5 deletions AdvancedDLSupport.Tests/c/src/SpanMarshallingTests.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
#include "comp.h"
#include "TestStruct.h"

int32_t globalArray[10];
int isInitialized = 0;

void InitGlobals()
{
if (isInitialized == 0)
{
for (int i = 0; i < 10; ++i)
{
globalArray[i] = i;
}

isInitialized = 1;
}
}

//Rewriten so there will not be a memory leak
__declspec(dllexport) int32_t* GetInt32ArrayZeroToNine()
{
int32_t* arr = malloc(sizeof(int32_t) * 10);
InitGlobals();

return globalArray;
}

__declspec(dllexport) void WriteToInt32Array(int32_t* arr, int arrLen)
{
InitGlobals();

int len = arrLen < 10 ? arrLen : 10;

for (int i = 0; i < 10; i++)
arr[i] = i;
for (int i = 0; i < len; ++i)
{
arr[i] = globalArray[i];
}
}

return arr;
}
2 changes: 1 addition & 1 deletion AdvancedDLSupport/AdvancedDLSupport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<DefineConstants>$(DefineConstants);JETBRAINS_ANNOTATIONS</DefineConstants>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>7.1</LangVersion>
<LangVersion>7.3</LangVersion>
<Configurations>Debug;Release</Configurations>
<Platforms>AnyCPU;x86;x64</Platforms>
<DebugSymbols>true</DebugSymbols>
Expand Down
26 changes: 26 additions & 0 deletions AdvancedDLSupport/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ namespace AdvancedDLSupport.Extensions
/// </summary>
internal static class TypeExtensions
{
/// <summary>
/// A class used for testing whether a generic argument is blittable/unmanaged.
/// </summary>
private class UnmanagedTest<T>
where T : unmanaged
{
}

/// <summary>
/// Determines whether the given type is blittable.
/// </summary>
/// <param name="type">The type to check.</param>
/// <returns>True if the type is blittable.</returns>
public static bool IsUnmanaged([NotNull] this Type type)
{
try
{
typeof(UnmanagedTest<>).MakeGenericType(type);
return true;
}
catch
{
return false;
}
}

/// <summary>
/// Determines whether the given type is a delegate type.
/// </summary>
Expand Down
Loading

0 comments on commit feaa38f

Please sign in to comment.