From f06d9d29a59111d67bd4b3a05b23d7e37c73caf2 Mon Sep 17 00:00:00 2001 From: Raphael Taylor-Davies Date: Thu, 12 Jan 2023 14:23:37 +0100 Subject: [PATCH 1/2] Add Global deallocation variant (#3516) --- arrow-buffer/src/alloc/mod.rs | 11 ++++++++--- arrow-buffer/src/bytes.rs | 4 ++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/arrow-buffer/src/alloc/mod.rs b/arrow-buffer/src/alloc/mod.rs index a7ce80600462..5e4477f77b59 100644 --- a/arrow-buffer/src/alloc/mod.rs +++ b/arrow-buffer/src/alloc/mod.rs @@ -135,8 +135,10 @@ pub(crate) enum Deallocation { /// An allocation of the given capacity that needs to be deallocated using arrows's cache aligned allocator. /// See [allocate_aligned] and [free_aligned]. Arrow(usize), - /// An allocation from an external source like the FFI interface or a Rust Vec. - /// Deallocation will happen + /// An allocation using the system's default global allocator and the provided layout + Global(Layout), + /// An allocation from an external source like the FFI interface + /// Deallocation should happen when `Allocation` is dropped Custom(Arc), } @@ -144,11 +146,14 @@ impl Debug for Deallocation { fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { match self { Deallocation::Arrow(capacity) => { - write!(f, "Deallocation::Arrow {{ capacity: {} }}", capacity) + write!(f, "Deallocation::Arrow {{ capacity: {capacity} }}") } Deallocation::Custom(_) => { write!(f, "Deallocation::Custom {{ capacity: unknown }}") } + Deallocation::Global(layout) => { + write!(f, "Deallocation::Global {{ alignment: {layout:?} }}") + } } } } diff --git a/arrow-buffer/src/bytes.rs b/arrow-buffer/src/bytes.rs index fea04ad0d50b..c75a445d8621 100644 --- a/arrow-buffer/src/bytes.rs +++ b/arrow-buffer/src/bytes.rs @@ -94,6 +94,7 @@ impl Bytes { pub fn capacity(&self) -> usize { match self.deallocation { Deallocation::Arrow(capacity) => capacity, + Deallocation::Global(l) => l.size(), // we cannot determine this in general, // and thus we state that this is externally-owned memory Deallocation::Custom(_) => 0, @@ -118,6 +119,9 @@ impl Drop for Bytes { Deallocation::Arrow(capacity) => { unsafe { alloc::free_aligned(self.ptr, *capacity) }; } + Deallocation::Global(layout) => { + unsafe { std::alloc::dealloc(self.ptr.as_ptr(), *layout) }; + } // The automatic drop implementation will free the memory once the reference count reaches zero Deallocation::Custom(_allocation) => (), } From 4a0d35feea36b003e147cddfe6673b96671270d8 Mon Sep 17 00:00:00 2001 From: Raphael Taylor-Davies Date: Thu, 12 Jan 2023 16:12:15 +0100 Subject: [PATCH 2/2] Clippy --- arrow-buffer/src/alloc/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/arrow-buffer/src/alloc/mod.rs b/arrow-buffer/src/alloc/mod.rs index 5e4477f77b59..3dbc1362e5c0 100644 --- a/arrow-buffer/src/alloc/mod.rs +++ b/arrow-buffer/src/alloc/mod.rs @@ -136,6 +136,7 @@ pub(crate) enum Deallocation { /// See [allocate_aligned] and [free_aligned]. Arrow(usize), /// An allocation using the system's default global allocator and the provided layout + #[allow(unused)] Global(Layout), /// An allocation from an external source like the FFI interface /// Deallocation should happen when `Allocation` is dropped