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


##########
swift/Arrow/Sources/Arrow/ArrowCExporter.swift:
##########
@@ -0,0 +1,133 @@
+// 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
+import Atomics
+
+// The memory used by UnsafeAtomic is not automatically
+// reclaimed. Since this value is initialized once
+// and used until the program/app is closed it's
+// memory will be released on program/app exit
+let exportDataCounter: UnsafeAtomic<Int> = .create(0)
+
+public class ArrowCExporter {
+    private class ExportData {
+        let id: Int
+        init() {
+            id = exportDataCounter.loadThenWrappingIncrement(ordering: 
.relaxed)
+            ArrowCExporter.exportedData[id] = self
+        }
+    }
+
+    private class ExportSchema: ExportData {
+        public let arrowTypeName: UnsafePointer<CChar>
+        public let nameCstr: UnsafePointer<CChar>
+        private let arrowType: ArrowType
+        private let name: String
+        init(_ arrowType: ArrowType, name: String = "") throws {
+            self.arrowType = arrowType
+            // keeping the name str to ensure the cstring buffer remains valid
+            self.name = name
+            self.arrowTypeName = (try arrowType.cDataFormatId as 
NSString).utf8String!
+            self.nameCstr = (name as NSString).utf8String!
+            super.init()
+        }
+    }
+
+    private class ExportArray: ExportData {
+        private let arrowData: ArrowData
+        private(set) var data = [UnsafeRawPointer?]()
+        private(set) var buffers: UnsafeMutablePointer<UnsafeRawPointer?>
+        init(_ arrowData: ArrowData) {
+            // 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)
+            super.init()
+        }
+    }
+
+    private static var exportedData = [Int: ExportData]()

Review Comment:
   Yes, my question was more along the lines of: what happens if the release 
callback is called _after_ finalization of Swift internals has started? 
Granted, we might not be able to do much about it, but it's probably good to 
keep this in mind (and/or add a comment about it).



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