Skip to main content

luaur_code_gen/functions/
allocate_pages_impl_code_allocator.rs

1use crate::macros::codegen_assert::CODEGEN_ASSERT;
2use crate::records::code_allocator::CodeAllocator;
3
4pub fn allocate_pages_impl(size: usize) -> *mut u8 {
5    CODEGEN_ASSERT!(size == CodeAllocator::align_to_page_size(size));
6
7    #[cfg(target_os = "windows")]
8    {
9        use core::ffi::c_void;
10        use windows_sys::Win32::System::Memory::{
11            VirtualAlloc, MEM_COMMIT, MEM_RESERVE, PAGE_READWRITE,
12        };
13
14        unsafe {
15            VirtualAlloc(
16                core::ptr::null::<c_void>(),
17                size,
18                MEM_RESERVE | MEM_COMMIT,
19                PAGE_READWRITE,
20            ) as *mut u8
21        }
22    }
23
24    #[cfg(not(target_os = "windows"))]
25    {
26        crate::functions::allocate_pages_impl_code_allocator_alt_b::allocate_pages_impl(size)
27    }
28}