Skip to content

Commit e2e2dd3

Browse files
committed
Manual fixups to the generated LLVMSharp code
1 parent b4f378e commit e2e2dd3

File tree

4 files changed

+82
-15
lines changed

4 files changed

+82
-15
lines changed

src/Generated.Custom.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ public size_t(IntPtr Pointer)
1313
public IntPtr Pointer;
1414
}
1515

16+
public partial struct off_t
17+
{
18+
public off_t(int value)
19+
{
20+
this.Value = value;
21+
}
22+
23+
public int Value;
24+
}
25+
1626
public static partial class LLVM
1727
{
1828
[DllImport(libraryPath, EntryPoint = "LLVMAddFunctionAttr2", CallingConvention = CallingConvention.Cdecl)]

src/Generated.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,6 @@ public partial struct LLVMOpaquePassManagerBuilder
161161
{
162162
}
163163

164-
public partial struct LLVMAttributeIndex
165-
{
166-
public LLVMAttributeIndex(uint value)
167-
{
168-
Value = value;
169-
}
170-
171-
public uint Value;
172-
}
173-
174164
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
175165
public delegate void LLVMDiagnosticHandler(LLVMDiagnosticInfoRef param0, IntPtr param1);
176166

@@ -841,7 +831,7 @@ public enum LLVMDiagnosticSeverity
841831
LLVMDSNote = 3,
842832
}
843833

844-
public enum (anonymous enum at E:\Users\tagoo\Repos\llvm\include/llvm-c/Core.h:342:1)
834+
public enum LLVMAttributeIndex
845835
{
846836
LLVMAttributeReturnIndex = 0,
847837
LLVMAttributeFunctionIndex = -1,
@@ -1008,9 +998,6 @@ public static partial class LLVM
1008998
[DllImport(libraryPath, EntryPoint = "LLVMWriteBitcodeToMemoryBuffer", CallingConvention = CallingConvention.Cdecl)]
1009999
public static extern LLVMMemoryBufferRef WriteBitcodeToMemoryBuffer(LLVMModuleRef M);
10101000

1011-
[DllImport(libraryPath, EntryPoint = "LLVMInitializeCore", CallingConvention = CallingConvention.Cdecl)]
1012-
public static extern void InitializeCore(LLVMPassRegistryRef R);
1013-
10141001
[DllImport(libraryPath, EntryPoint = "LLVMShutdown", CallingConvention = CallingConvention.Cdecl)]
10151002
public static extern void Shutdown();
10161003

src/LLVMSharp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard1.1</TargetFramework>
54
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
5+
<TargetFramework>netstandard2.0</TargetFramework>
66
</PropertyGroup>
77

88
</Project>

src/StringMarshaler.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
namespace LLVMSharp
2+
{
3+
using System;
4+
using System.IO;
5+
using System.Runtime.InteropServices;
6+
using System.Text;
7+
8+
internal class StringMarshaler : ICustomMarshaler
9+
{
10+
private static readonly StringMarshaler staticInstance = new StringMarshaler();
11+
12+
public static ICustomMarshaler GetInstance(string cookie)
13+
{
14+
return staticInstance;
15+
}
16+
17+
public object MarshalNativeToManaged(IntPtr pNativeData)
18+
{
19+
// We don't know when the string is done, so we have to read byte-by-byte :(
20+
// Start at 128 bytes capacity to avoid a crazy number of allocs...
21+
var data = new MemoryStream(128);
22+
var offset = 0;
23+
while (true)
24+
{
25+
var b = Marshal.ReadByte(pNativeData, offset);
26+
if (b == 0)
27+
{
28+
break;
29+
}
30+
31+
data.WriteByte(b);
32+
offset++;
33+
}
34+
35+
if (data.Length >= int.MaxValue)
36+
{
37+
throw new Exception("Crazy-long string!");
38+
}
39+
40+
return Encoding.UTF8.GetString(data.GetBuffer(), 0, (int)data.Length);
41+
}
42+
43+
public IntPtr MarshalManagedToNative(object managedObj)
44+
{
45+
var str = (string)managedObj;
46+
var bytes = Encoding.UTF8.GetBytes(str);
47+
48+
var ptr = Marshal.AllocCoTaskMem(bytes.Length + 1);
49+
Marshal.Copy(bytes, 0, ptr, bytes.Length); // utf-8 bytes
50+
Marshal.WriteByte(ptr, bytes.Length, 0); // null terminator
51+
52+
return ptr;
53+
}
54+
55+
public void CleanUpNativeData(IntPtr pNativeData)
56+
{
57+
// Nothing to do
58+
}
59+
60+
public void CleanUpManagedData(object managedObj)
61+
{
62+
// Nothing to do
63+
}
64+
65+
public int GetNativeDataSize()
66+
{
67+
return -1;
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)