Skip to content

Apache DataFusion SQL Query Engine

ARROW-2481: [Rust] Move all calls to free() into memory.rs

Andy Grovecf04ce63d1b3
diff --git a/rust/src/buffer.rs b/rust/src/buffer.rs
index df62a3da5d..cdfbfc9954 100644
--- a/rust/src/buffer.rs
+++ b/rust/src/buffer.rs
@@ -22,12 +22,6 @@ use std::slice;
use super::memory::*;
-#[cfg(windows)]
-#[link(name = "msvcrt")]
-extern "C" {
- fn _aligned_free(prt: *const u8);
-}
-
/// Buffer<T> is essentially just a Vec<T> for fixed-width primitive types and the start of the
/// memory region is aligned at a 64-byte boundary
pub struct Buffer<T> {
@@ -82,19 +76,10 @@ impl<T> Buffer<T> {
}
impl<T> Drop for Buffer<T> {
- #[cfg(windows)]
fn drop(&mut self) {
unsafe {
let p = mem::transmute::<*const T, *const u8>(self.data);
- _aligned_free(p);
- }
- }
-
- #[cfg(not(windows))]
- fn drop(&mut self) {
- unsafe {
- let p = mem::transmute::<*const T, *mut libc::c_void>(self.data);
- libc::free(p);
+ free_aligned(p);
}
}
}
diff --git a/rust/src/builder.rs b/rust/src/builder.rs
index 69884ccacf..bb18d2e029 100644
--- a/rust/src/builder.rs
+++ b/rust/src/builder.rs
@@ -24,12 +24,6 @@ use std::slice;
use super::buffer::*;
use super::memory::*;
-#[cfg(windows)]
-#[link(name = "msvcrt")]
-extern "C" {
- fn _aligned_free(prt: *const u8);
-}
-
/// Buffer builder with zero-copy build method
pub struct Builder<T> {
data: *mut T,
@@ -143,22 +137,11 @@ impl<T> Builder<T> {
}
impl<T> Drop for Builder<T> {
- #[cfg(windows)]
fn drop(&mut self) {
if !self.data.is_null() {
unsafe {
let p = mem::transmute::<*const T, *const u8>(self.data);
- _aligned_free(p);
- }
- }
- }
-
- #[cfg(not(windows))]
- fn drop(&mut self) {
- if !self.data.is_null() {
- unsafe {
- let p = mem::transmute::<*const T, *mut libc::c_void>(self.data);
- libc::free(p);
+ free_aligned(p);
}
}
}
diff --git a/rust/src/memory.rs b/rust/src/memory.rs
index 0fc2fda3b2..a62fcb20f0 100644
--- a/rust/src/memory.rs
+++ b/rust/src/memory.rs
@@ -26,6 +26,7 @@ const ALIGNMENT: usize = 64;
#[link(name = "msvcrt")]
extern "C" {
fn _aligned_malloc(size: libc::size_t, alignment: libc::size_t) -> libc::size_t;
+ fn _aligned_free(prt: *const u8);
}
#[cfg(windows)]
@@ -53,6 +54,20 @@ pub fn allocate_aligned(size: i64) -> Result<*const u8, ArrowError> {
}
}
+#[cfg(windows)]
+pub fn free_aligned(p: *const u8) {
+ unsafe {
+ _aligned_free(p);
+ }
+}
+
+#[cfg(not(windows))]
+pub fn free_aligned(p: *const u8) {
+ unsafe {
+ libc::free(mem::transmute::<*const u8, *mut libc::c_void>(p));
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;