You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "paleolimbot (via GitHub)" <gi...@apache.org> on 2023/06/18 17:11:56 UTC

[GitHub] [arrow] paleolimbot commented on a diff in pull request #35496: GH-33856: [C#] Implement C Data Interface for C#

paleolimbot commented on code in PR #35496:
URL: https://github.com/apache/arrow/pull/35496#discussion_r1233342323


##########
csharp/src/Apache.Arrow/C/CArrowArrayExporter.cs:
##########
@@ -0,0 +1,211 @@
+// 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);
+        private static unsafe readonly NativeDelegate<ReleaseArrowArray> s_releaseArray = new NativeDelegate<ReleaseArrowArray>(ReleaseArray);
+
+        /// <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));
+            }

Review Comment:
   I'm sorry I missed this and I see that it's been solved. In nanoarrow we definitely assume that pointer output arguments point to uninitialized memory (and strive to not touch that memory until failure is impossible). There are a few places where we do something like 
   
   ```c
   struct ArrowArray tmp;
   tmp.release = NULL;
   // stuff with tmp that might fail
   if (had_error) {
     if (tmp.release != NULL) {
       tmp.release(&tmp);
     }
   
     return;
   }
   
   ArrowArrayMove(&tmp, out);
   return;
   ```
   
   ...to simplify (to the extent that anything in C is simple) the error handling.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org