From 71bb92a7a5533d9a288e381a96b68b66bbb10146 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Thu, 25 May 2023 23:57:14 +0300 Subject: [PATCH 01/14] Clean-up `NativeDelegate`. --- csharp/src/Apache.Arrow/C/NativeDelegate.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/csharp/src/Apache.Arrow/C/NativeDelegate.cs b/csharp/src/Apache.Arrow/C/NativeDelegate.cs index 48b37fbfe5582..ebc6330c9b951 100644 --- a/csharp/src/Apache.Arrow/C/NativeDelegate.cs +++ b/csharp/src/Apache.Arrow/C/NativeDelegate.cs @@ -20,17 +20,16 @@ namespace Apache.Arrow.C { - internal readonly struct NativeDelegate + internal readonly struct NativeDelegate where T : Delegate { private readonly T _managedDelegate; // For lifetime management - private readonly IntPtr _nativePointer; public NativeDelegate(T managedDelegate) { _managedDelegate = managedDelegate; - _nativePointer = Marshal.GetFunctionPointerForDelegate(managedDelegate); + Pointer = Marshal.GetFunctionPointerForDelegate(managedDelegate); } - public IntPtr Pointer { get { return _nativePointer; } } + public IntPtr Pointer { get; } } } From 5a7c8831bfae2a9633ead86021da418c75ebd041 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Fri, 26 May 2023 00:07:44 +0300 Subject: [PATCH 02/14] Specify the calling convention of the unmanaged delegates. --- csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs | 1 + csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs | 4 ++++ csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs | 1 + 3 files changed, 6 insertions(+) diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs b/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs index 4e11291cde4a8..b1250356c7b48 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs @@ -22,6 +22,7 @@ namespace Apache.Arrow.C { public static class CArrowArrayExporter { + [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate void ReleaseArrowArray(CArrowArray* cArray); private static unsafe readonly NativeDelegate s_releaseArray = new NativeDelegate(ReleaseArray); diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs b/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs index 2b4a5b216593b..28dca4107bc9a 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs @@ -22,12 +22,16 @@ namespace Apache.Arrow.C { public static class CArrowArrayStreamExporter { + [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate int GetSchemaArrayStream(CArrowArrayStream* cArrayStream, CArrowSchema* cSchema); private static unsafe NativeDelegate s_getSchemaArrayStream = new NativeDelegate(GetSchema); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate int GetNextArrayStream(CArrowArrayStream* cArrayStream, CArrowArray* cArray); private static unsafe NativeDelegate s_getNextArrayStream = new NativeDelegate(GetNext); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate byte* GetLastErrorArrayStream(CArrowArrayStream* cArrayStream); private static unsafe NativeDelegate s_getLastErrorArrayStream = new NativeDelegate(GetLastError); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate void ReleaseArrayStream(CArrowArrayStream* cArrayStream); private static unsafe NativeDelegate s_releaseArrayStream = new NativeDelegate(Release); diff --git a/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs b/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs index c2745c4cc4cc1..658722be4d08f 100644 --- a/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs @@ -24,6 +24,7 @@ namespace Apache.Arrow.C { public static class CArrowSchemaExporter { + [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate void ReleaseArrowSchema(CArrowSchema* cArray); private static unsafe readonly NativeDelegate s_releaseSchema = new NativeDelegate(ReleaseCArrowSchema); From f879fa84614f0ca2828b9430db2326bfe0541dfd Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Fri, 26 May 2023 00:32:56 +0300 Subject: [PATCH 03/14] Use`[UnmanagedCallersOnly]` on modern .NET. --- csharp/src/Apache.Arrow/Apache.Arrow.csproj | 4 +++ .../src/Apache.Arrow/C/CArrowArrayExporter.cs | 18 +++++++--- .../C/CArrowArrayStreamExporter.cs | 36 ++++++++++++++++--- .../Apache.Arrow/C/CArrowSchemaExporter.cs | 11 +++++- 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/csharp/src/Apache.Arrow/Apache.Arrow.csproj b/csharp/src/Apache.Arrow/Apache.Arrow.csproj index c57c8e48b9494..43d60ba0012ac 100644 --- a/csharp/src/Apache.Arrow/Apache.Arrow.csproj +++ b/csharp/src/Apache.Arrow/Apache.Arrow.csproj @@ -44,4 +44,8 @@ + + + + diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs b/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs index b1250356c7b48..01ad9004f6e71 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs @@ -15,6 +15,7 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Apache.Arrow.Memory; @@ -22,10 +23,14 @@ namespace Apache.Arrow.C { public static class CArrowArrayExporter { +#if NET5_0_OR_GREATER + private static unsafe delegate* unmanaged[Stdcall] ReleaseArrayPtr => &ReleaseArray; +#else [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate void ReleaseArrowArray(CArrowArray* cArray); private static unsafe readonly NativeDelegate s_releaseArray = new NativeDelegate(ReleaseArray); - + private static unsafe delegate* unmanaged[Stdcall] ReleaseArrayPtr => (delegate* unmanaged[Stdcall])s_releaseArray.Pointer; +#endif /// /// Export an to a . Whether or not the /// export succeeds, the original array becomes invalid. Clone an array to continue using it @@ -59,7 +64,7 @@ public static unsafe void ExportArray(IArrowArray array, CArrowArray* cArray) try { ConvertArray(allocationOwner, array.Data, cArray); - cArray->release = (delegate* unmanaged[Stdcall])(IntPtr)s_releaseArray.Pointer; + cArray->release = ReleaseArrayPtr; cArray->private_data = FromDisposable(allocationOwner); allocationOwner = null; } @@ -102,7 +107,7 @@ public static unsafe void ExportRecordBatch(RecordBatch batch, CArrowArray* cArr try { ConvertRecordBatch(allocationOwner, batch, cArray); - cArray->release = (delegate* unmanaged[Stdcall])s_releaseArray.Pointer; + cArray->release = ReleaseArrayPtr; cArray->private_data = FromDisposable(allocationOwner); allocationOwner = null; } @@ -117,7 +122,7 @@ private unsafe static void ConvertArray(ExportedAllocationOwner sharedOwner, Arr cArray->length = array.Length; cArray->offset = array.Offset; cArray->null_count = array.NullCount; - cArray->release = (delegate* unmanaged[Stdcall])s_releaseArray.Pointer; + cArray->release = ReleaseArrayPtr; cArray->private_data = null; cArray->n_buffers = array.Buffers?.Length ?? 0; @@ -162,7 +167,7 @@ private unsafe static void ConvertRecordBatch(ExportedAllocationOwner sharedOwne cArray->length = batch.Length; cArray->offset = 0; cArray->null_count = 0; - cArray->release = (delegate* unmanaged[Stdcall])s_releaseArray.Pointer; + cArray->release = ReleaseArrayPtr; cArray->private_data = null; cArray->n_buffers = 1; @@ -185,6 +190,9 @@ private unsafe static void ConvertRecordBatch(ExportedAllocationOwner sharedOwne cArray->dictionary = null; } +#if NET5_0_OR_GREATER + [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] +#endif private unsafe static void ReleaseArray(CArrowArray* cArray) { if (cArray->private_data != null) diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs b/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs index 28dca4107bc9a..ffebb5932c7d5 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs @@ -15,6 +15,7 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Apache.Arrow.Ipc; @@ -22,18 +23,33 @@ namespace Apache.Arrow.C { public static class CArrowArrayStreamExporter { +#if NET5_0_OR_GREATER + private static unsafe delegate* unmanaged[Stdcall] GetSchemaPtr => &GetSchema; + private static unsafe delegate* unmanaged[Stdcall] GetNextPtr => &GetNext; + private static unsafe delegate* unmanaged[Stdcall] GetLastErrorPtr => &GetLastError; + private static unsafe delegate* unmanaged[Stdcall] ReleasePtr => &Release; +#else [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate int GetSchemaArrayStream(CArrowArrayStream* cArrayStream, CArrowSchema* cSchema); private static unsafe NativeDelegate s_getSchemaArrayStream = new NativeDelegate(GetSchema); + private static unsafe delegate* unmanaged[Stdcall] GetSchemaPtr => + (delegate* unmanaged[Stdcall])s_getSchemaArrayStream.Pointer; [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate int GetNextArrayStream(CArrowArrayStream* cArrayStream, CArrowArray* cArray); private static unsafe NativeDelegate s_getNextArrayStream = new NativeDelegate(GetNext); + private static unsafe delegate* unmanaged[Stdcall] GetNextPtr => + (delegate* unmanaged[Stdcall])s_getNextArrayStream.Pointer; [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate byte* GetLastErrorArrayStream(CArrowArrayStream* cArrayStream); private static unsafe NativeDelegate s_getLastErrorArrayStream = new NativeDelegate(GetLastError); + private static unsafe delegate* unmanaged[Stdcall] GetLastErrorPtr => + (delegate* unmanaged[Stdcall])s_getLastErrorArrayStream.Pointer; [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate void ReleaseArrayStream(CArrowArrayStream* cArrayStream); private static unsafe NativeDelegate s_releaseArrayStream = new NativeDelegate(Release); + private static unsafe delegate* unmanaged[Stdcall] ReleasePtr => + (delegate* unmanaged[Stdcall])s_releaseArrayStream.Pointer; +#endif /// /// Export an to a . @@ -63,12 +79,15 @@ public static unsafe void ExportArrayStream(IArrowArrayStream arrayStream, CArro } cArrayStream->private_data = ExportedArrayStream.Export(arrayStream); - cArrayStream->get_schema = (delegate* unmanaged[Stdcall])s_getSchemaArrayStream.Pointer; - cArrayStream->get_next = (delegate* unmanaged[Stdcall])s_getNextArrayStream.Pointer; - cArrayStream->get_last_error = (delegate* unmanaged[Stdcall])s_getLastErrorArrayStream.Pointer; - cArrayStream->release = (delegate* unmanaged[Stdcall])s_releaseArrayStream.Pointer; + cArrayStream->get_schema = GetSchemaPtr; + cArrayStream->get_next = GetNextPtr; + cArrayStream->get_last_error = GetLastErrorPtr; + cArrayStream->release = ReleasePtr; } +#if NET5_0_OR_GREATER + [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] +#endif private unsafe static int GetSchema(CArrowArrayStream* cArrayStream, CArrowSchema* cSchema) { ExportedArrayStream arrayStream = null; @@ -84,6 +103,9 @@ private unsafe static int GetSchema(CArrowArrayStream* cArrayStream, CArrowSchem } } +#if NET5_0_OR_GREATER + [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] +#endif private unsafe static int GetNext(CArrowArrayStream* cArrayStream, CArrowArray* cArray) { ExportedArrayStream arrayStream = null; @@ -104,6 +126,9 @@ private unsafe static int GetNext(CArrowArrayStream* cArrayStream, CArrowArray* } } +#if NET5_0_OR_GREATER + [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] +#endif private unsafe static byte* GetLastError(CArrowArrayStream* cArrayStream) { try @@ -117,6 +142,9 @@ private unsafe static int GetNext(CArrowArrayStream* cArrayStream, CArrowArray* } } +#if NET5_0_OR_GREATER + [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] +#endif private unsafe static void Release(CArrowArrayStream* cArrayStream) { ExportedArrayStream arrayStream = ExportedArrayStream.FromPointer(cArrayStream->private_data); diff --git a/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs b/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs index 658722be4d08f..3a85d36794b3b 100644 --- a/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs @@ -17,6 +17,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Apache.Arrow.Types; @@ -24,9 +25,14 @@ namespace Apache.Arrow.C { public static class CArrowSchemaExporter { +#if NET5_0_OR_GREATER + private static unsafe delegate* unmanaged[Stdcall] ReleaseSchemaPtr => &ReleaseCArrowSchema; +#else [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate void ReleaseArrowSchema(CArrowSchema* cArray); private static unsafe readonly NativeDelegate s_releaseSchema = new NativeDelegate(ReleaseCArrowSchema); + private static unsafe delegate* unmanaged[Stdcall] ReleaseSchemaPtr => (delegate* unmanaged[Stdcall])s_releaseSchema.Pointer; +#endif /// /// Export a type to a . @@ -66,7 +72,7 @@ public static unsafe void ExportType(IArrowType datatype, CArrowSchema* schema) schema->dictionary = ConstructDictionary(datatype); - schema->release = (delegate* unmanaged[Stdcall])s_releaseSchema.Pointer; + schema->release = ReleaseSchemaPtr; schema->private_data = null; } @@ -244,6 +250,9 @@ private static long GetFlags(IArrowType datatype, bool nullable = true) } } +#if NET5_0_OR_GREATER + [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] +#endif private static unsafe void ReleaseCArrowSchema(CArrowSchema* schema) { if (schema == null) return; From ed68f434af77a28ae4d6e96415557d7d43f85540 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Fri, 26 May 2023 00:48:34 +0300 Subject: [PATCH 04/14] Optimize zeroing of the C structs. --- csharp/src/Apache.Arrow/C/CArrowArray.cs | 11 +---------- csharp/src/Apache.Arrow/C/CArrowArrayStream.cs | 6 +----- csharp/src/Apache.Arrow/C/CArrowSchema.cs | 10 +--------- 3 files changed, 3 insertions(+), 24 deletions(-) diff --git a/csharp/src/Apache.Arrow/C/CArrowArray.cs b/csharp/src/Apache.Arrow/C/CArrowArray.cs index 4aba95add0da4..63a79a9798d3d 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArray.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArray.cs @@ -51,16 +51,7 @@ public unsafe struct CArrowArray { var ptr = (CArrowArray*)Marshal.AllocHGlobal(sizeof(CArrowArray)); - ptr->length = 0; - ptr->n_buffers = 0; - ptr->offset = 0; - ptr->buffers = null; - ptr->n_children = 0; - ptr->children = null; - ptr->dictionary = null; - ptr->null_count = 0; - ptr->release = null; - ptr->private_data = null; + *ptr = default; return ptr; } diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayStream.cs b/csharp/src/Apache.Arrow/C/CArrowArrayStream.cs index bf1fcb39ef386..624895099170c 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayStream.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayStream.cs @@ -74,11 +74,7 @@ public unsafe struct CArrowArrayStream { var ptr = (CArrowArrayStream*)Marshal.AllocHGlobal(sizeof(CArrowArrayStream)); - ptr->get_schema = null; - ptr->get_next = null; - ptr->get_last_error = null; - ptr->release = null; - ptr->private_data = null; + *ptr = default; return ptr; } diff --git a/csharp/src/Apache.Arrow/C/CArrowSchema.cs b/csharp/src/Apache.Arrow/C/CArrowSchema.cs index af01247800655..5d6a14437358d 100644 --- a/csharp/src/Apache.Arrow/C/CArrowSchema.cs +++ b/csharp/src/Apache.Arrow/C/CArrowSchema.cs @@ -52,15 +52,7 @@ public unsafe struct CArrowSchema { var ptr = (CArrowSchema*)Marshal.AllocHGlobal(sizeof(CArrowSchema)); - ptr->format = null; - ptr->name = null; - ptr->metadata = null; - ptr->flags = 0; - ptr->n_children = 0; - ptr->children = null; - ptr->dictionary = null; - ptr->release = null; - ptr->private_data = null; + *ptr = default; return ptr; } From b0bbeaa2c1157b3c03aa82b078fb955869ade910 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Fri, 26 May 2023 01:23:21 +0300 Subject: [PATCH 05/14] Free the GCHandle when releasing the exported stream. This patches a memory leak. --- .../Apache.Arrow/C/CArrowArrayStreamExporter.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs b/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs index ffebb5932c7d5..515957486264f 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs @@ -147,8 +147,7 @@ private unsafe static int GetNext(CArrowArrayStream* cArrayStream, CArrowArray* #endif private unsafe static void Release(CArrowArrayStream* cArrayStream) { - ExportedArrayStream arrayStream = ExportedArrayStream.FromPointer(cArrayStream->private_data); - arrayStream.Dispose(); + ExportedArrayStream.Free(&cArrayStream->private_data); cArrayStream->release = null; } @@ -172,6 +171,18 @@ sealed unsafe class ExportedArrayStream : IDisposable return (void*)GCHandle.ToIntPtr(gch); } + public static void Free(void** ptr) + { + GCHandle gch = GCHandle.FromIntPtr((IntPtr)ptr); + if (!gch.IsAllocated) + { + return; + } + ((ExportedArrayStream)gch.Target).Dispose(); + gch.Free(); + *ptr = null; + } + public static ExportedArrayStream FromPointer(void* ptr) { GCHandle gch = GCHandle.FromIntPtr((IntPtr)ptr); From 1b477317ea21da0afed72d751d03a0e40053dfcb Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Fri, 26 May 2023 01:32:01 +0300 Subject: [PATCH 06/14] Refactor the release code of exported arrays. --- csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs b/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs index 01ad9004f6e71..0d3d641dd6d9d 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs @@ -195,11 +195,7 @@ private unsafe static void ConvertRecordBatch(ExportedAllocationOwner sharedOwne #endif private unsafe static void ReleaseArray(CArrowArray* cArray) { - if (cArray->private_data != null) - { - Dispose(&cArray->private_data); - } - cArray->private_data = null; + Dispose(&cArray->private_data); cArray->release = null; } @@ -212,6 +208,10 @@ private unsafe static void ReleaseArray(CArrowArray* cArray) private unsafe static void Dispose(void** ptr) { GCHandle gch = GCHandle.FromIntPtr((IntPtr)(*ptr)); + if (!gch.IsAllocated) + { + return; + } ((IDisposable)gch.Target).Dispose(); gch.Free(); *ptr = null; From a595a9a7732828abb523b5cd33c41a552e29827e Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Sun, 28 May 2023 14:46:45 +0300 Subject: [PATCH 07/14] Improve exception throwing in imported `ArrayStream`s. The last error message is included if it exists, and in `ReadNextRecordBatchAsync` the exception was returned with the `ValueTask`. --- .../Apache.Arrow/C/CArrowArrayStreamImporter.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayStreamImporter.cs b/csharp/src/Apache.Arrow/C/CArrowArrayStreamImporter.cs index 4a374388282a0..c3e7c9fb75e8b 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayStreamImporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayStreamImporter.cs @@ -1,4 +1,4 @@ -// Licensed to the Apache Software Foundation (ASF) under one +// Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file @@ -53,6 +53,16 @@ private sealed unsafe class ImportedArrowArrayStream : IArrowArrayStream private readonly Schema _schema; private bool _disposed; + internal static string GetLastError(CArrowArrayStream* arrayStream, int errno) + { + byte* error = arrayStream->get_last_error(arrayStream); + if (error == null) + { + return $"Array stream operation failed with no message. Error code: {errno}"; + } + return StringUtil.PtrToStringUtf8(error); + } + public ImportedArrowArrayStream(CArrowArrayStream* cArrayStream) { if (cArrayStream == null) @@ -71,7 +81,7 @@ public ImportedArrowArrayStream(CArrowArrayStream* cArrayStream) int errno = _cArrayStream->get_schema(_cArrayStream, cSchema); if (errno != 0) { - throw new Exception($"Unexpected error recieved from external stream. Errno: {errno}"); + throw new Exception(GetLastError(cArrayStream, errno)); } _schema = CArrowSchemaImporter.ImportSchema(cSchema); } @@ -105,7 +115,7 @@ public ValueTask ReadNextRecordBatchAsync(CancellationToken cancell int errno = _cArrayStream->get_next(_cArrayStream, cArray); if (errno != 0) { - throw new Exception($"Unexpected error recieved from external stream. Errno: {errno}"); + return new(Task.FromException(new Exception(GetLastError(_cArrayStream, errno)))); } if (cArray->release != null) { From f9f310afbae601b0b311d3224d9c99a96c51867d Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Sun, 28 May 2023 14:48:40 +0300 Subject: [PATCH 08/14] Honor the canellation token in `ImportedArrowArrayStream.ReadNextRecordBatchAsync`. --- csharp/src/Apache.Arrow/C/CArrowArrayStreamImporter.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayStreamImporter.cs b/csharp/src/Apache.Arrow/C/CArrowArrayStreamImporter.cs index c3e7c9fb75e8b..27a7d0632713a 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayStreamImporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayStreamImporter.cs @@ -1,4 +1,4 @@ -// Licensed to the Apache Software Foundation (ASF) under one +// Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file @@ -108,6 +108,11 @@ public ValueTask ReadNextRecordBatchAsync(CancellationToken cancell throw new ObjectDisposedException(typeof(ImportedArrowArrayStream).Name); } + if (cancellationToken.IsCancellationRequested) + { + return new(Task.FromCanceled(cancellationToken)); + } + RecordBatch result = null; CArrowArray* cArray = CArrowArray.Create(); try From 540c19083533884a76a999a99e1d400bd4abfc9a Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Sun, 28 May 2023 14:48:56 +0300 Subject: [PATCH 09/14] Use `Array.Empty`. --- csharp/src/Apache.Arrow/C/CArrowArrayImporter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayImporter.cs b/csharp/src/Apache.Arrow/C/CArrowArrayImporter.cs index db5181b56e859..e8943449bc2ee 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayImporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayImporter.cs @@ -151,7 +151,7 @@ private ArrayData GetAsArrayData(CArrowArray* cArray, IArrowType type) case ArrowTypeId.Map: break; case ArrowTypeId.Null: - buffers = new ArrowBuffer[0]; + buffers = System.Array.Empty(); break; case ArrowTypeId.Dictionary: DictionaryType dictionaryType = (DictionaryType)type; From 473a9be91d92ca230355d9e2966caf1e1b9776ec Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Sun, 28 May 2023 14:52:37 +0300 Subject: [PATCH 10/14] Use the correct memory freeing method. --- csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs b/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs index 515957486264f..4e2a7b10594ce 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs @@ -213,7 +213,7 @@ void ReleaseLastError() { if (LastError != null) { - Marshal.FreeCoTaskMem((IntPtr)LastError); + Marshal.FreeHGlobal((IntPtr)LastError); LastError = null; } } From 154b86c5c90ab31c524c3c7a3da52462253e82f8 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Mon, 29 May 2023 02:12:21 +0300 Subject: [PATCH 11/14] Remove `[UnmanagedFunctionPointer]` from the unmanaged delegates. There was a reason they were skipped, as explained in the PR that added them. --- csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs | 1 - csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs | 4 ---- csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs | 1 - 3 files changed, 6 deletions(-) diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs b/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs index 0d3d641dd6d9d..fe199506f277b 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs @@ -26,7 +26,6 @@ public static class CArrowArrayExporter #if NET5_0_OR_GREATER private static unsafe delegate* unmanaged[Stdcall] ReleaseArrayPtr => &ReleaseArray; #else - [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate void ReleaseArrowArray(CArrowArray* cArray); private static unsafe readonly NativeDelegate s_releaseArray = new NativeDelegate(ReleaseArray); private static unsafe delegate* unmanaged[Stdcall] ReleaseArrayPtr => (delegate* unmanaged[Stdcall])s_releaseArray.Pointer; diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs b/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs index 4e2a7b10594ce..7b3b6ef16d05e 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs @@ -29,22 +29,18 @@ public static class CArrowArrayStreamExporter private static unsafe delegate* unmanaged[Stdcall] GetLastErrorPtr => &GetLastError; private static unsafe delegate* unmanaged[Stdcall] ReleasePtr => &Release; #else - [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate int GetSchemaArrayStream(CArrowArrayStream* cArrayStream, CArrowSchema* cSchema); private static unsafe NativeDelegate s_getSchemaArrayStream = new NativeDelegate(GetSchema); private static unsafe delegate* unmanaged[Stdcall] GetSchemaPtr => (delegate* unmanaged[Stdcall])s_getSchemaArrayStream.Pointer; - [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate int GetNextArrayStream(CArrowArrayStream* cArrayStream, CArrowArray* cArray); private static unsafe NativeDelegate s_getNextArrayStream = new NativeDelegate(GetNext); private static unsafe delegate* unmanaged[Stdcall] GetNextPtr => (delegate* unmanaged[Stdcall])s_getNextArrayStream.Pointer; - [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate byte* GetLastErrorArrayStream(CArrowArrayStream* cArrayStream); private static unsafe NativeDelegate s_getLastErrorArrayStream = new NativeDelegate(GetLastError); private static unsafe delegate* unmanaged[Stdcall] GetLastErrorPtr => (delegate* unmanaged[Stdcall])s_getLastErrorArrayStream.Pointer; - [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate void ReleaseArrayStream(CArrowArrayStream* cArrayStream); private static unsafe NativeDelegate s_releaseArrayStream = new NativeDelegate(Release); private static unsafe delegate* unmanaged[Stdcall] ReleasePtr => diff --git a/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs b/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs index 3a85d36794b3b..f1cfe9aa3172f 100644 --- a/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs @@ -28,7 +28,6 @@ public static class CArrowSchemaExporter #if NET5_0_OR_GREATER private static unsafe delegate* unmanaged[Stdcall] ReleaseSchemaPtr => &ReleaseCArrowSchema; #else - [UnmanagedFunctionPointer(CallingConvention.StdCall)] private unsafe delegate void ReleaseArrowSchema(CArrowSchema* cArray); private static unsafe readonly NativeDelegate s_releaseSchema = new NativeDelegate(ReleaseCArrowSchema); private static unsafe delegate* unmanaged[Stdcall] ReleaseSchemaPtr => (delegate* unmanaged[Stdcall])s_releaseSchema.Pointer; From e9365fce770dca6228637f6e0ea72897bc09d130 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Sat, 10 Jun 2023 19:05:46 +0300 Subject: [PATCH 12/14] Make the function pointer fields in the C API structs internal. --- csharp/src/Apache.Arrow/C/CArrowArray.cs | 2 +- csharp/src/Apache.Arrow/C/CArrowArrayStream.cs | 8 ++++---- csharp/src/Apache.Arrow/C/CArrowSchema.cs | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/csharp/src/Apache.Arrow/C/CArrowArray.cs b/csharp/src/Apache.Arrow/C/CArrowArray.cs index 63a79a9798d3d..fac8509230042 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArray.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArray.cs @@ -38,7 +38,7 @@ public unsafe struct CArrowArray public byte** buffers; public CArrowArray** children; public CArrowArray* dictionary; - public delegate* unmanaged[Stdcall] release; + internal delegate* unmanaged[Stdcall] release; public void* private_data; /// diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayStream.cs b/csharp/src/Apache.Arrow/C/CArrowArrayStream.cs index 624895099170c..404bcf53a2dc3 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayStream.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayStream.cs @@ -35,7 +35,7 @@ public unsafe struct CArrowArrayStream /// /// Return value: 0 if successful, an `errno`-compatible error code otherwise. /// - public delegate* unmanaged[Stdcall] get_schema; + internal delegate* unmanaged[Stdcall] get_schema; /// /// Callback to get the next array. If no error and the array is released, the stream has ended. @@ -43,7 +43,7 @@ public unsafe struct CArrowArrayStream /// /// Return value: 0 if successful, an `errno`-compatible error code otherwise. /// - public delegate* unmanaged[Stdcall] get_next; + internal delegate* unmanaged[Stdcall] get_next; /// /// Callback to get optional detailed error information. This must only @@ -54,13 +54,13 @@ public unsafe struct CArrowArrayStream /// Return value: pointer to a null-terminated character array describing the last /// error, or NULL if no description is available. /// - public delegate* unmanaged[Stdcall] get_last_error; + internal delegate* unmanaged[Stdcall] get_last_error; /// /// Release callback: release the stream's own resources. Note that arrays returned by /// get_next must be individually released. /// - public delegate* unmanaged[Stdcall] release; + internal delegate* unmanaged[Stdcall] release; public void* private_data; diff --git a/csharp/src/Apache.Arrow/C/CArrowSchema.cs b/csharp/src/Apache.Arrow/C/CArrowSchema.cs index 5d6a14437358d..df389358eda62 100644 --- a/csharp/src/Apache.Arrow/C/CArrowSchema.cs +++ b/csharp/src/Apache.Arrow/C/CArrowSchema.cs @@ -39,7 +39,7 @@ public unsafe struct CArrowSchema public long n_children; public CArrowSchema** children; public CArrowSchema* dictionary; - public delegate* unmanaged[Stdcall] release; + internal delegate* unmanaged[Stdcall] release; public void* private_data; /// From d7d830fb255aad89e277798d49f976bf39b9d7ef Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Thu, 22 Jun 2023 01:23:39 +0300 Subject: [PATCH 13/14] Use the default unmanaged calling convention where supported. --- csharp/src/Apache.Arrow/C/CArrowArray.cs | 6 ++++- .../src/Apache.Arrow/C/CArrowArrayExporter.cs | 4 ++-- .../src/Apache.Arrow/C/CArrowArrayStream.cs | 24 +++++++++++++++---- .../C/CArrowArrayStreamExporter.cs | 16 ++++++------- csharp/src/Apache.Arrow/C/CArrowSchema.cs | 6 ++++- .../Apache.Arrow/C/CArrowSchemaExporter.cs | 4 ++-- .../CDataInterfaceDataTests.cs | 2 +- .../CDataInterfaceSchemaTests.cs | 2 +- 8 files changed, 44 insertions(+), 20 deletions(-) diff --git a/csharp/src/Apache.Arrow/C/CArrowArray.cs b/csharp/src/Apache.Arrow/C/CArrowArray.cs index fac8509230042..cd47028f026a8 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArray.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArray.cs @@ -38,7 +38,11 @@ public unsafe struct CArrowArray public byte** buffers; public CArrowArray** children; public CArrowArray* dictionary; - internal delegate* unmanaged[Stdcall] release; + internal delegate* unmanaged +#if !NET5_0_OR_GREATER + [Stdcall] +#endif + release; public void* private_data; /// diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs b/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs index b3765e85ee883..0985f76d2811f 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs @@ -24,7 +24,7 @@ namespace Apache.Arrow.C public static class CArrowArrayExporter { #if NET5_0_OR_GREATER - private static unsafe delegate* unmanaged[Stdcall] ReleaseArrayPtr => &ReleaseArray; + private static unsafe delegate* unmanaged ReleaseArrayPtr => &ReleaseArray; #else private unsafe delegate void ReleaseArrowArray(CArrowArray* cArray); private static unsafe readonly NativeDelegate s_releaseArray = new NativeDelegate(ReleaseArray); @@ -186,7 +186,7 @@ private unsafe static void ConvertRecordBatch(ExportedAllocationOwner sharedOwne } #if NET5_0_OR_GREATER - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly] #endif private unsafe static void ReleaseArray(CArrowArray* cArray) { diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayStream.cs b/csharp/src/Apache.Arrow/C/CArrowArrayStream.cs index 404bcf53a2dc3..238d4c32dac9f 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayStream.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayStream.cs @@ -35,7 +35,11 @@ public unsafe struct CArrowArrayStream /// /// Return value: 0 if successful, an `errno`-compatible error code otherwise. /// - internal delegate* unmanaged[Stdcall] get_schema; + internal delegate* unmanaged +#if !NET5_0_OR_GREATER + [Stdcall] +#endif + get_schema; /// /// Callback to get the next array. If no error and the array is released, the stream has ended. @@ -43,7 +47,11 @@ public unsafe struct CArrowArrayStream /// /// Return value: 0 if successful, an `errno`-compatible error code otherwise. /// - internal delegate* unmanaged[Stdcall] get_next; + internal delegate* unmanaged +#if !NET5_0_OR_GREATER + [Stdcall] +#endif + get_next; /// /// Callback to get optional detailed error information. This must only @@ -54,13 +62,21 @@ public unsafe struct CArrowArrayStream /// Return value: pointer to a null-terminated character array describing the last /// error, or NULL if no description is available. /// - internal delegate* unmanaged[Stdcall] get_last_error; + internal delegate* unmanaged +#if !NET5_0_OR_GREATER + [Stdcall] +#endif + get_last_error; /// /// Release callback: release the stream's own resources. Note that arrays returned by /// get_next must be individually released. /// - internal delegate* unmanaged[Stdcall] release; + internal delegate* unmanaged +#if !NET5_0_OR_GREATER + [Stdcall] +#endif + release; public void* private_data; diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs b/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs index 70d35fc8bf760..93878a76bc67d 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs @@ -24,10 +24,10 @@ namespace Apache.Arrow.C public static class CArrowArrayStreamExporter { #if NET5_0_OR_GREATER - private static unsafe delegate* unmanaged[Stdcall] GetSchemaPtr => &GetSchema; - private static unsafe delegate* unmanaged[Stdcall] GetNextPtr => &GetNext; - private static unsafe delegate* unmanaged[Stdcall] GetLastErrorPtr => &GetLastError; - private static unsafe delegate* unmanaged[Stdcall] ReleasePtr => &Release; + private static unsafe delegate* unmanaged GetSchemaPtr => &GetSchema; + private static unsafe delegate* unmanaged GetNextPtr => &GetNext; + private static unsafe delegate* unmanaged GetLastErrorPtr => &GetLastError; + private static unsafe delegate* unmanaged ReleasePtr => &Release; #else private unsafe delegate int GetSchemaArrayStream(CArrowArrayStream* cArrayStream, CArrowSchema* cSchema); private static unsafe NativeDelegate s_getSchemaArrayStream = new NativeDelegate(GetSchema); @@ -78,7 +78,7 @@ public static unsafe void ExportArrayStream(IArrowArrayStream arrayStream, CArro } #if NET5_0_OR_GREATER - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly] #endif private unsafe static int GetSchema(CArrowArrayStream* cArrayStream, CArrowSchema* cSchema) { @@ -96,7 +96,7 @@ private unsafe static int GetSchema(CArrowArrayStream* cArrayStream, CArrowSchem } #if NET5_0_OR_GREATER - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly] #endif private unsafe static int GetNext(CArrowArrayStream* cArrayStream, CArrowArray* cArray) { @@ -119,7 +119,7 @@ private unsafe static int GetNext(CArrowArrayStream* cArrayStream, CArrowArray* } #if NET5_0_OR_GREATER - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly] #endif private unsafe static byte* GetLastError(CArrowArrayStream* cArrayStream) { @@ -135,7 +135,7 @@ private unsafe static int GetNext(CArrowArrayStream* cArrayStream, CArrowArray* } #if NET5_0_OR_GREATER - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly] #endif private unsafe static void Release(CArrowArrayStream* cArrayStream) { diff --git a/csharp/src/Apache.Arrow/C/CArrowSchema.cs b/csharp/src/Apache.Arrow/C/CArrowSchema.cs index df389358eda62..2099f09b0bf67 100644 --- a/csharp/src/Apache.Arrow/C/CArrowSchema.cs +++ b/csharp/src/Apache.Arrow/C/CArrowSchema.cs @@ -39,7 +39,11 @@ public unsafe struct CArrowSchema public long n_children; public CArrowSchema** children; public CArrowSchema* dictionary; - internal delegate* unmanaged[Stdcall] release; + internal delegate* unmanaged +#if !NET5_0_OR_GREATER + [Stdcall] +#endif + release; public void* private_data; /// diff --git a/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs b/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs index 7d517c5736c24..39f4ecc26537d 100644 --- a/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs @@ -28,7 +28,7 @@ namespace Apache.Arrow.C public static class CArrowSchemaExporter { #if NET5_0_OR_GREATER - private static unsafe delegate* unmanaged[Stdcall] ReleaseSchemaPtr => &ReleaseCArrowSchema; + private static unsafe delegate* unmanaged ReleaseSchemaPtr => &ReleaseCArrowSchema; #else private unsafe delegate void ReleaseArrowSchema(CArrowSchema* cArray); private static unsafe readonly NativeDelegate s_releaseSchema = new NativeDelegate(ReleaseCArrowSchema); @@ -292,7 +292,7 @@ private unsafe static void WriteMetadataString(ref byte* ptr, int length, string } #if NET5_0_OR_GREATER - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly] #endif private static unsafe void ReleaseCArrowSchema(CArrowSchema* schema) { diff --git a/csharp/test/Apache.Arrow.Tests/CDataInterfaceDataTests.cs b/csharp/test/Apache.Arrow.Tests/CDataInterfaceDataTests.cs index 89a346f9f7a8a..a430e140cfc2a 100644 --- a/csharp/test/Apache.Arrow.Tests/CDataInterfaceDataTests.cs +++ b/csharp/test/Apache.Arrow.Tests/CDataInterfaceDataTests.cs @@ -77,7 +77,7 @@ public unsafe void CallsReleaseForInvalid() wasCalled = true; cArray->release = null; }; - cArray->release = (delegate* unmanaged[Stdcall])Marshal.GetFunctionPointerForDelegate( + cArray->release = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate( releaseCallback); Assert.Throws(() => diff --git a/csharp/test/Apache.Arrow.Tests/CDataInterfaceSchemaTests.cs b/csharp/test/Apache.Arrow.Tests/CDataInterfaceSchemaTests.cs index a75d777ad318e..dfd6f9912cd4d 100644 --- a/csharp/test/Apache.Arrow.Tests/CDataInterfaceSchemaTests.cs +++ b/csharp/test/Apache.Arrow.Tests/CDataInterfaceSchemaTests.cs @@ -105,7 +105,7 @@ public unsafe void CallsReleaseForInvalid() wasCalled = true; cSchema->release = null; }; - cSchema->release = (delegate* unmanaged[Stdcall])Marshal.GetFunctionPointerForDelegate( + cSchema->release = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate( releaseCallback); Assert.Throws(() => From 09e6a24272ee550c92c1759541b7d09b6496b403 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Thu, 6 Jul 2023 20:02:41 +0300 Subject: [PATCH 14/14] Replace `Stdcall` with `Cdecl`. --- csharp/src/Apache.Arrow/C/CArrowArray.cs | 2 +- csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs | 2 +- csharp/src/Apache.Arrow/C/CArrowArrayStream.cs | 8 ++++---- .../Apache.Arrow/C/CArrowArrayStreamExporter.cs | 16 ++++++++-------- csharp/src/Apache.Arrow/C/CArrowSchema.cs | 2 +- .../src/Apache.Arrow/C/CArrowSchemaExporter.cs | 2 +- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/csharp/src/Apache.Arrow/C/CArrowArray.cs b/csharp/src/Apache.Arrow/C/CArrowArray.cs index cd47028f026a8..a8a084d1d767d 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArray.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArray.cs @@ -40,7 +40,7 @@ public unsafe struct CArrowArray public CArrowArray* dictionary; internal delegate* unmanaged #if !NET5_0_OR_GREATER - [Stdcall] + [Cdecl] #endif release; public void* private_data; diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs b/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs index 0985f76d2811f..5a793c177e0a6 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs @@ -28,7 +28,7 @@ public static class CArrowArrayExporter #else private unsafe delegate void ReleaseArrowArray(CArrowArray* cArray); private static unsafe readonly NativeDelegate s_releaseArray = new NativeDelegate(ReleaseArray); - private static unsafe delegate* unmanaged[Stdcall] ReleaseArrayPtr => (delegate* unmanaged[Stdcall])s_releaseArray.Pointer; + private static unsafe delegate* unmanaged[Cdecl] ReleaseArrayPtr => (delegate* unmanaged[Cdecl])s_releaseArray.Pointer; #endif /// /// Export an to a . Whether or not the diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayStream.cs b/csharp/src/Apache.Arrow/C/CArrowArrayStream.cs index 238d4c32dac9f..a900a6895a097 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayStream.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayStream.cs @@ -37,7 +37,7 @@ public unsafe struct CArrowArrayStream /// internal delegate* unmanaged #if !NET5_0_OR_GREATER - [Stdcall] + [Cdecl] #endif get_schema; @@ -49,7 +49,7 @@ internal delegate* unmanaged /// internal delegate* unmanaged #if !NET5_0_OR_GREATER - [Stdcall] + [Cdecl] #endif get_next; @@ -64,7 +64,7 @@ internal delegate* unmanaged /// internal delegate* unmanaged #if !NET5_0_OR_GREATER - [Stdcall] + [Cdecl] #endif get_last_error; @@ -74,7 +74,7 @@ internal delegate* unmanaged /// internal delegate* unmanaged #if !NET5_0_OR_GREATER - [Stdcall] + [Cdecl] #endif release; diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs b/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs index 93878a76bc67d..56e0468f9415c 100644 --- a/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs @@ -31,20 +31,20 @@ public static class CArrowArrayStreamExporter #else private unsafe delegate int GetSchemaArrayStream(CArrowArrayStream* cArrayStream, CArrowSchema* cSchema); private static unsafe NativeDelegate s_getSchemaArrayStream = new NativeDelegate(GetSchema); - private static unsafe delegate* unmanaged[Stdcall] GetSchemaPtr => - (delegate* unmanaged[Stdcall])s_getSchemaArrayStream.Pointer; + private static unsafe delegate* unmanaged[Cdecl] GetSchemaPtr => + (delegate* unmanaged[Cdecl])s_getSchemaArrayStream.Pointer; private unsafe delegate int GetNextArrayStream(CArrowArrayStream* cArrayStream, CArrowArray* cArray); private static unsafe NativeDelegate s_getNextArrayStream = new NativeDelegate(GetNext); - private static unsafe delegate* unmanaged[Stdcall] GetNextPtr => - (delegate* unmanaged[Stdcall])s_getNextArrayStream.Pointer; + private static unsafe delegate* unmanaged[Cdecl] GetNextPtr => + (delegate* unmanaged[Cdecl])s_getNextArrayStream.Pointer; private unsafe delegate byte* GetLastErrorArrayStream(CArrowArrayStream* cArrayStream); private static unsafe NativeDelegate s_getLastErrorArrayStream = new NativeDelegate(GetLastError); - private static unsafe delegate* unmanaged[Stdcall] GetLastErrorPtr => - (delegate* unmanaged[Stdcall])s_getLastErrorArrayStream.Pointer; + private static unsafe delegate* unmanaged[Cdecl] GetLastErrorPtr => + (delegate* unmanaged[Cdecl])s_getLastErrorArrayStream.Pointer; private unsafe delegate void ReleaseArrayStream(CArrowArrayStream* cArrayStream); private static unsafe NativeDelegate s_releaseArrayStream = new NativeDelegate(Release); - private static unsafe delegate* unmanaged[Stdcall] ReleasePtr => - (delegate* unmanaged[Stdcall])s_releaseArrayStream.Pointer; + private static unsafe delegate* unmanaged[Cdecl] ReleasePtr => + (delegate* unmanaged[Cdecl])s_releaseArrayStream.Pointer; #endif /// diff --git a/csharp/src/Apache.Arrow/C/CArrowSchema.cs b/csharp/src/Apache.Arrow/C/CArrowSchema.cs index 2099f09b0bf67..64761dbd0d095 100644 --- a/csharp/src/Apache.Arrow/C/CArrowSchema.cs +++ b/csharp/src/Apache.Arrow/C/CArrowSchema.cs @@ -41,7 +41,7 @@ public unsafe struct CArrowSchema public CArrowSchema* dictionary; internal delegate* unmanaged #if !NET5_0_OR_GREATER - [Stdcall] + [Cdecl] #endif release; public void* private_data; diff --git a/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs b/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs index 39f4ecc26537d..9053e80664e31 100644 --- a/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs +++ b/csharp/src/Apache.Arrow/C/CArrowSchemaExporter.cs @@ -32,7 +32,7 @@ public static class CArrowSchemaExporter #else private unsafe delegate void ReleaseArrowSchema(CArrowSchema* cArray); private static unsafe readonly NativeDelegate s_releaseSchema = new NativeDelegate(ReleaseCArrowSchema); - private static unsafe delegate* unmanaged[Stdcall] ReleaseSchemaPtr => (delegate* unmanaged[Stdcall])s_releaseSchema.Pointer; + private static unsafe delegate* unmanaged[Cdecl] ReleaseSchemaPtr => (delegate* unmanaged[Cdecl])s_releaseSchema.Pointer; #endif ///