Skip to content

Commit

Permalink
Fix linux memory marshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
farost committed Mar 21, 2024
1 parent 749a483 commit fa0ef06
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions c/swig/typedb_driver_csharp.swg
Original file line number Diff line number Diff line change
Expand Up @@ -618,17 +618,41 @@

%typemap(ctype) char ** "char**"
%typemap(cstype) char ** "string[]"
%typemap(imtype) char ** "global::System.IntPtr"

%typemap(
imtype,
inattributes = "[
global::System.Runtime.InteropServices.In,
global::System.Runtime.InteropServices.Out,
global::System.Runtime.InteropServices.MarshalAs(
global::System.Runtime.InteropServices.UnmanagedType.LPArray,
ArraySubType=global::System.Runtime.InteropServices.UnmanagedType.LPStr)]"
) char ** "string[]"

%typemap(csin) char ** "$csinput"
csin,
pre="
int arraySize = $csinput.Length;
global::System.IntPtr unmanaged$csinput = System.Runtime.InteropServices.Marshal.AllocHGlobal(
(arraySize + 1) * System.Runtime.InteropServices.Marshal.SizeOf<global::System.IntPtr>());

unsafe
{
global::System.IntPtr* arrayPtr = (global::System.IntPtr*)unmanaged$csinput.ToPointer();

int i = 0;
for (; i < arraySize; i++)
{
arrayPtr[i] = global::System.Runtime.InteropServices.Marshal.StringToCoTaskMemAnsi($csinput[i]);
}

arrayPtr[i] = global::System.IntPtr.Zero;
}",
post="
unsafe
{
global::System.IntPtr* arrayPtr = (global::System.IntPtr*)unmanaged$csinput.ToPointer();

for (int i = 0; i < arraySize; i++)
{
global::System.Runtime.InteropServices.Marshal.FreeHGlobal(arrayPtr[i]);
}
}

global::System.Runtime.InteropServices.Marshal.FreeHGlobal(unmanaged$csinput);"
) char ** "unmanaged$csinput"

%typemap(csout) char **
{
return $imcall;
Expand Down

0 comments on commit fa0ef06

Please sign in to comment.