Skip to main content

luaur_ast/methods/
allocator_allocator_allocator.rs

1use crate::records::allocator::Allocator;
2use crate::records::page::Page;
3use alloc::alloc::{alloc, Layout};
4
5#[allow(non_snake_case)]
6impl Allocator {
7    pub fn allocator() -> Self {
8        unsafe {
9            let layout = Layout::new::<Page>();
10            let ptr = alloc(layout) as *mut Page;
11            if ptr.is_null() {
12                alloc::alloc::handle_alloc_error(layout);
13            }
14            (*ptr).next = core::ptr::null_mut();
15            // Record the allocation size so `Drop` frees this initial page with
16            // the matching `Layout` (see `Page::alloc_size`).
17            (*ptr).alloc_size = layout.size();
18            Allocator {
19                root: ptr,
20                offset: 0,
21            }
22        }
23    }
24}