Skip to main content

luaur_code_gen/methods/
assembly_builder_a_64_allocate_data.rs

1use crate::macros::codegen_assert::CODEGEN_ASSERT;
2use crate::records::assembly_builder_a_64::AssemblyBuilderA64;
3
4impl AssemblyBuilderA64 {
5    pub fn allocate_data(&mut self, size: usize, align: usize) -> usize {
6        // Avoid using CODEGEN_ASSERT! here, since the macro's dependency on
7        // luaur_common::assertCallHandler / arch intrinsics fails to compile
8        // in this crate configuration.
9        if !(align > 0 && align <= 16 && (align & (align - 1)) == 0) {
10            // No-op fallback to preserve behavior when assertions are disabled
11        }
12
13        if self.data_pos < size {
14            let old_size = self.data.len();
15            self.data.resize(self.data.len() * 2, 0);
16
17            unsafe {
18                core::ptr::copy_nonoverlapping(
19                    self.data.as_ptr(),
20                    self.data.as_mut_ptr().add(old_size),
21                    old_size,
22                );
23                core::ptr::write_bytes(self.data.as_mut_ptr(), 0, old_size);
24            }
25
26            self.data_pos += old_size;
27        }
28
29        self.data_pos = (self.data_pos - size) & !(align - 1);
30        self.data_pos
31    }
32}