pitrou commented on code in PR #39091:
URL: https://github.com/apache/arrow/pull/39091#discussion_r1481451781


##########
swift/Arrow/Sources/Arrow/ArrowCExporter.swift:
##########
@@ -0,0 +1,100 @@
+// 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.
+
+import Foundation
+import ArrowC
+
+extension String {
+    var cstring: UnsafePointer<CChar> {
+        (self as NSString).cString(using: String.Encoding.utf8.rawValue)!
+    }
+}
+
+public class ArrowCExporter {
+    private class ExportData {
+        let id: String
+        var cArray = ArrowC.ArrowArray()
+        private let arrowData: ArrowData
+        private(set) var data = [UnsafeRawPointer?]()
+        private(set) var buffers: UnsafeMutablePointer<UnsafeRawPointer?>
+        init(_ arrowData: ArrowData) {
+            id = UUID().uuidString
+            // keep a reference to the ArrowData
+            // obj so the memory doesn't get
+            // deallocated
+            self.arrowData = arrowData
+            for arrowBuffer in arrowData.buffers {
+                data.append(arrowBuffer.rawPointer)
+            }
+
+            self.buffers = UnsafeMutablePointer(mutating: data)
+            ArrowCExporter.exportedData[id.hashValue] = self
+        }
+
+        func release() {
+            // the data associated with this export data
+            // does not need to be released as they are
+            // still associated with the ArrowBuffer
+            // and it will deallocate this memory
+            ArrowCExporter.exportedData.removeValue(forKey: id.hashValue)
+        }
+    }
+
+    private static var exportedData = [Int: ExportData]()
+
+    public init() {}
+
+    public func exportType(_ cSchema: inout ArrowC.ArrowSchema, arrowType: 
ArrowType, name: String = "") ->
+        Result<Bool, ArrowError> {
+        do {
+            cSchema.format = try arrowType.cDataFormatId.cstring
+            cSchema.name = name.cstring
+            cSchema.release = {data in
+                data?.pointee.release = nil
+            }
+        } catch {
+            return .failure(.unknownError("\(error)"))
+        }
+        return .success(true)
+    }
+
+    public func exportField(_ schema: inout ArrowC.ArrowSchema, field: 
ArrowField) ->
+        Result<Bool, ArrowError> {
+            return exportType(&schema, arrowType: field.type, name: field.name)
+    }
+
+    public func exportArray(_ cArray: inout ArrowC.ArrowArray, arrowData: 
ArrowData) {
+        let exportData = ExportData(arrowData)
+        cArray.buffers = exportData.buffers
+        cArray.length = Int64(arrowData.length)
+        cArray.null_count = Int64(arrowData.nullCount)
+        cArray.n_buffers = Int64(arrowData.buffers.count)
+        cArray.n_children = 0
+        cArray.children = nil
+        cArray.dictionary = nil
+        cArray.private_data =
+            UnsafeMutableRawPointer(mutating: UnsafeRawPointer(bitPattern: 
exportData.id.hashValue))
+        cArray.release = {data in
+            var arrayData = data?.pointee
+            let hashValue = Int(bitPattern: arrayData?.private_data)
+            if let exportData = ArrowCExporter.exportedData[hashValue] {

Review Comment:
   The lookup should ideally never fail, I would suggest printing a warning (or 
even aborting the process?) if it does. Passing silently may hide an 
implementation error and memory leak.



-- 
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]

Reply via email to