Skip to main content

luaur_ast/methods/
allocator_allocator_allocator_alt_c.rs

1use crate::records::allocator::Allocator;
2use crate::records::page::Page;
3use alloc::alloc::{dealloc, Layout};
4
5#[allow(non_snake_case)]
6impl Allocator {
7    /// Destructor logic for Allocator.
8    /// Note: In Rust this is typically handled by a Drop implementation,
9    /// but we provide the requested method for compatibility.
10    pub fn allocator_allocator_dtor(&mut self) {
11        unsafe {
12            let mut page = self.root;
13            while !page.is_null() {
14                let next = (*page).next;
15
16                // Free with the *exact* layout `allocate` used: a page can be
17                // over-sized for a single large allocation, so a fixed
18                // `Layout::new::<Page>()` would mismatch (dealloc with the wrong
19                // size is UB). `alloc_size` was recorded at allocation time.
20                let layout = Layout::from_size_align_unchecked(
21                    (*page).alloc_size,
22                    core::mem::align_of::<Page>(),
23                );
24                dealloc(page as *mut u8, layout);
25
26                page = next;
27            }
28            self.root = core::ptr::null_mut();
29        }
30    }
31}
32
33/// Frees the page list on drop. Without this every parser leaks its arena pages
34/// — caught by the fuzz suite's LeakSanitizer (repeated 8200-byte `Page` leaks
35/// from the `compile` target).
36impl Drop for Allocator {
37    fn drop(&mut self) {
38        self.allocator_allocator_dtor();
39    }
40}