Kontinuation commented on code in PR #678:
URL: https://github.com/apache/sedona-db/pull/678#discussion_r2878728228


##########
c/sedona-gdal/src/vsi.rs:
##########
@@ -0,0 +1,177 @@
+// 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.
+
+//! GDAL Virtual File System (VSI) wrappers.
+
+use std::ffi::CString;
+
+use crate::errors::{GdalError, Result};
+use crate::gdal_api::{call_gdal_api, GdalApi};
+
+/// Creates a new VSI in-memory file from a given buffer.
+///
+/// The data is copied into GDAL-allocated memory (via `VSIMalloc`) so that
+/// GDAL can safely free it with `VSIFree` when ownership is taken.
+pub fn create_mem_file(api: &'static GdalApi, file_name: &str, data: Vec<u8>) 
-> Result<()> {
+    let c_file_name = CString::new(file_name)?;
+    let len = data.len();
+
+    // Allocate via GDAL's allocator so GDAL can safely free it.
+    let gdal_buf = unsafe { call_gdal_api!(api, VSIMalloc, len) } as *mut u8;
+    if gdal_buf.is_null() {
+        return Err(GdalError::NullPointer {
+            method_name: "VSIMalloc",
+            msg: format!("failed to allocate {len} bytes"),
+        });
+    }
+
+    // Copy data into GDAL-allocated buffer
+    unsafe {
+        std::ptr::copy_nonoverlapping(data.as_ptr(), gdal_buf, len);
+    }
+    // Rust Vec is dropped here, freeing the Rust-allocated memory.
+
+    let handle = unsafe {
+        call_gdal_api!(
+            api,
+            VSIFileFromMemBuffer,
+            c_file_name.as_ptr(),
+            gdal_buf,
+            len as i64,
+            1 // bTakeOwnership = true — GDAL will VSIFree gdal_buf
+        )

Review Comment:
   This is not the case. VSIMalloc will return non-null pointer even when the 
nSize is 0.



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