commonlibsse_ng\re\b\BSTArray/
allocator.rs

1mod scrap;
2
3pub use self::scrap::BSScrapArrayAllocator;
4
5use core::alloc::Layout;
6use core::ffi::c_void;
7
8/// A trait representing a custom memory allocator.
9///
10/// # Safety
11///
12/// Implementors must ensure all allocation and deallocation operations are safe
13/// and consistent with Rust’s memory safety guarantees.
14pub unsafe trait Allocator {
15    /// Creates a new instance of the allocator.
16    fn new() -> Self;
17
18    /// Returns a memory layout for a given size using pointer alignment.
19    ///
20    /// Panics if the layout is invalid, which should never happen for valid inputs.
21    #[inline]
22    fn ptr_layout(size: usize) -> Layout {
23        const PTR_ALIGN_SIZE: usize = align_of::<*mut c_void>();
24        Layout::from_size_align(size, PTR_ALIGN_SIZE).expect("Valid layout")
25    }
26
27    /// Returns an immutable raw pointer to the allocator's memory region.
28    fn as_ptr(&self) -> *const c_void;
29
30    /// Returns a mutable raw pointer to the allocator's memory region.
31    fn as_mut_ptr(&mut self) -> *mut c_void;
32
33    /// Returns the total capacity of the allocator in bytes.
34    fn capacity(&self) -> u32;
35
36    /// Allocates memory using the given layout and returns a zero-initialized pointer.
37    ///
38    /// Returns a null pointer on allocation failure.
39    ///
40    /// # Safety
41    ///
42    /// The caller must ensure the layout is valid. The returned pointer must be used in
43    /// accordance with Rust's aliasing and alignment rules.
44    unsafe fn allocate(&mut self, layout: Layout) -> *mut c_void;
45
46    /// Deallocates the memory at the given pointer.
47    ///
48    /// # Safety
49    ///
50    /// The pointer must have been previously allocated by this allocator and must
51    /// match the layout used during allocation.
52    unsafe fn deallocate(&mut self, ptr: *mut c_void);
53
54    /// Sets the internal state of the allocator.
55    ///
56    /// - `data`: Pointer to the start of the memory region.
57    /// - `capacity`: Total number of bytes the allocator can manage.
58    /// - `type_size`: Size of the type to be allocated in this allocator.
59    fn set_allocator_traits(&mut self, data: *mut c_void, capacity: u32, type_size: usize);
60}