eerhardt commented on code in PR #35496: URL: https://github.com/apache/arrow/pull/35496#discussion_r1190139307
########## csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs: ########## @@ -0,0 +1,210 @@ +// 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 to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +using System; +using System.Runtime.InteropServices; +using Apache.Arrow.Memory; + +namespace Apache.Arrow.C +{ + public static class CArrowArrayExporter + { + private unsafe delegate void ReleaseArrowArray(CArrowArray* cArray); + + /// <summary> + /// Export an <see cref="IArrowArray"/> to a <see cref="CArrowArray"/>. Whether or not the + /// export succeeds, the original array becomes invalid. Clone an array to continue using it + /// after a copy has been exported. + /// </summary> + /// <param name="array">The array to export</param> + /// <param name="cArray">An allocated but uninitialized CArrowArray pointer.</param> + /// <example> + /// <code> + /// CArrowArray* exportPtr = CArrowArray.Create(); + /// CArrowArrayExporter.ExportArray(array, exportPtr); + /// foreign_import_function(exportPtr); + /// </code> + /// </example> + public static unsafe void ExportArray(IArrowArray array, CArrowArray* cArray) + { + if (array == null) + { + throw new ArgumentNullException(nameof(array)); + } + if (cArray == null) + { + throw new ArgumentNullException(nameof(cArray)); + } + if (cArray->release != null) + { + throw new ArgumentException("Cannot export array to a struct that is already initialized.", nameof(cArray)); + } + + ExportedAllocationOwner allocationOwner = new ExportedAllocationOwner(); + try + { + ConvertArray(allocationOwner, array.Data, cArray); + cArray->release = (delegate* unmanaged[Stdcall]<CArrowArray*, void>)Marshal.GetFunctionPointerForDelegate<ReleaseArrowArray>(ReleaseArray); Review Comment: > It would still work correctly if an array were exported via the C API to another bit of managed code which consumed it (via the C API)? Yes because it is the `release` pointer is an unmanaged function pointer, and not a managed delegate. It is a bit hard to explain. But even though the caller is "managed" (i.e. .NET code), the way the method is invoked is through an unmanged function pointer. So it is still an "UnmanagedCaller". -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
