Skip to main content

arrow_buffer/alloc/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! Defines the low-level [`Allocation`] API for shared memory regions
6
7use std::alloc::Layout;
8use std::fmt::{Debug, Formatter};
9use std::panic::RefUnwindSafe;
10use std::sync::Arc;
11
12mod alignment;
13
14pub use alignment::ALIGNMENT;
15
16/// The owner of an allocation.
17/// The trait implementation is responsible for dropping the allocations once no more references exist.
18pub trait Allocation: RefUnwindSafe + Send + Sync {}
19
20impl<T: RefUnwindSafe + Send + Sync> Allocation for T {}
21
22/// Mode of deallocating memory regions
23pub(crate) enum Deallocation {
24    /// An allocation using [`std::alloc`]
25    Standard(Layout),
26    /// An allocation from an external source like the FFI interface
27    /// Deallocation will happen on `Allocation::drop`
28    /// The size of the allocation is tracked here separately only
29    /// for memory usage reporting via `Array::get_buffer_memory_size`
30    Custom(Arc<dyn Allocation>, usize),
31}
32
33impl Debug for Deallocation {
34    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
35        match self {
36            Deallocation::Standard(layout) => {
37                write!(f, "Deallocation::Standard {layout:?}")
38            }
39            Deallocation::Custom(_, size) => {
40                write!(f, "Deallocation::Custom {{ capacity: {size} }}")
41            }
42        }
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use crate::alloc::Deallocation;
49
50    #[test]
51    fn test_size_of_deallocation() {
52        assert_eq!(
53            std::mem::size_of::<Deallocation>(),
54            3 * std::mem::size_of::<usize>()
55        );
56    }
57}