Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding span overloads for most of the interop extensions #136

Merged
merged 1 commit into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions sources/LLVMSharp/Internals/MarshaledArray`1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

using System;
using System.Buffers;

namespace LLVMSharp
{
internal unsafe struct MarshaledArray<T, U> : IDisposable
{
public MarshaledArray(ReadOnlySpan<T> inputs, Func<T, U> marshal)
{
if (inputs.IsEmpty)
{
Count = 0;
Values = null;
}
else
{
Count = inputs.Length;
Values = ArrayPool<U>.Shared.Rent(Count);

for (int i = 0; i < Count; i++)
{
Values[i] = marshal(inputs[i]);
}
}
}

public int Count { get; private set; }

public U[] Values { get; private set; }

public static implicit operator ReadOnlySpan<U>(in MarshaledArray<T, U> value)
{
return value.Values;
}

public void Dispose()
{
if (Values != null)
{
ArrayPool<U>.Shared.Return(Values);
}
}
}
}
6 changes: 3 additions & 3 deletions sources/LLVMSharp/Internals/MarshaledString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace LLVMSharp
{
internal unsafe struct MarshaledString : IDisposable
{
public MarshaledString(string input)
public MarshaledString(ReadOnlySpan<char> input)
{
if ((input is null) || (input.Length == 0))
if (input.IsEmpty)
{
var value = Marshal.AllocHGlobal(1);
Marshal.WriteByte(value, 0, 0);
Expand All @@ -20,7 +20,7 @@ public MarshaledString(string input)
}
else
{
var valueBytes = Encoding.UTF8.GetBytes(input);
var valueBytes = Encoding.UTF8.GetBytes(input.ToString());
var length = valueBytes.Length;
var value = Marshal.AllocHGlobal(length + 1);
Marshal.Copy(valueBytes, 0, value, length);
Expand Down
6 changes: 3 additions & 3 deletions sources/LLVMSharp/Internals/MarshaledStringArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace LLVMSharp
{
internal unsafe struct MarshaledStringArray : IDisposable
{
public MarshaledStringArray(string[] inputs)
public MarshaledStringArray(ReadOnlySpan<string> inputs)
{
if ((inputs is null) || (inputs.Length == 0))
if (inputs.IsEmpty)
{
Count = 0;
Values = null;
Expand All @@ -20,7 +20,7 @@ public MarshaledStringArray(string[] inputs)

for (int i = 0; i < Count; i++)
{
Values[i] = new MarshaledString(inputs[i]);
Values[i] = new MarshaledString(inputs[i].AsSpan());
}
}
}
Expand Down
28 changes: 27 additions & 1 deletion sources/LLVMSharp/Interop.Extensions/LLVMBasicBlockRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ public static implicit operator LLVMBasicBlockRef(LLVMOpaqueBasicBlock* value)

public static bool operator !=(LLVMBasicBlockRef left, LLVMBasicBlockRef right) => !(left == right);

public static LLVMBasicBlockRef AppendInContext(LLVMContextRef C, LLVMValueRef Fn, string Name) => AppendInContext(C, Fn, Name.AsSpan());

public static LLVMBasicBlockRef AppendInContext(LLVMContextRef C, LLVMValueRef Fn, ReadOnlySpan<char> Name)
{
using var marshaledName = new MarshaledString(Name);
return LLVM.AppendBasicBlockInContext(C, Fn, marshaledName);
}

public static LLVMBasicBlockRef CreateInContext(LLVMContextRef C, string Name) => CreateInContext(C, Name.AsSpan());

public static LLVMBasicBlockRef CreateInContext(LLVMContextRef C, ReadOnlySpan<char> Name)
{
using var marshaledName = new MarshaledString(Name);
return LLVM.CreateBasicBlockInContext(C, marshaledName);
}

public static LLVMBasicBlockRef InsertInContext(LLVMContextRef C, LLVMBasicBlockRef BB, string Name) => InsertInContext(C, BB, Name.AsSpan());

public static LLVMBasicBlockRef InsertInContext(LLVMContextRef C, LLVMBasicBlockRef BB, ReadOnlySpan<char> Name)
{
using var marshaledName = new MarshaledString(Name);
return LLVM.InsertBasicBlockInContext(C, BB, marshaledName);
}

public LLVMValueRef AsValue() => LLVM.BasicBlockAsValue(this);

public void Delete() => LLVM.DeleteBasicBlock(this);
Expand All @@ -61,7 +85,9 @@ public static implicit operator LLVMBasicBlockRef(LLVMOpaqueBasicBlock* value)

public override int GetHashCode() => Handle.GetHashCode();

public LLVMBasicBlockRef InsertBasicBlock(string Name)
public LLVMBasicBlockRef InsertBasicBlock(string Name) => InsertBasicBlock(Name.AsSpan());

public LLVMBasicBlockRef InsertBasicBlock(ReadOnlySpan<char> Name)
{
using var marshaledName = new MarshaledString(Name);
return LLVM.InsertBasicBlock(this, marshaledName);
Expand Down
Loading