Skip to main content

luaur_reduce_cli/methods/
reducer_reallocate_statements.rs

1use alloc::vec::Vec;
2
3use luaur_ast::records::ast_stat::AstStat;
4
5use crate::records::reducer::Reducer;
6
7impl Reducer {
8    /// Move new body data into allocator-managed storage so that it's safe to keep around
9    /// longterm. C++ (`CLI/src/Reduce.cpp:233-239`):
10    /// ```cpp
11    /// AstStat** newData = static_cast<AstStat**>(allocator.allocate(sizeof(AstStat*) * statements.size()));
12    /// std::copy(statements.data(), statements.data() + statements.size(), newData);
13    /// return newData;
14    /// ```
15    /// The pointers are copied into arena storage owned by `self.allocator`, which outlives
16    /// the `Reducer`, so the returned pointer stays valid for the rest of the run.
17    pub fn reallocate_statements(&mut self, statements: &Vec<*mut AstStat>) -> *mut *mut AstStat {
18        let count = statements.len();
19        if count == 0 {
20            return core::ptr::null_mut();
21        }
22
23        let bytes = core::mem::size_of::<*mut AstStat>() * count;
24        let new_data = self.allocator.allocate(bytes) as *mut *mut AstStat;
25
26        unsafe {
27            core::ptr::copy_nonoverlapping(statements.as_ptr(), new_data, count);
28        }
29
30        new_data
31    }
32}
33
34pub fn reducer_reallocate_statements(
35    this: &mut Reducer,
36    statements: &Vec<*mut AstStat>,
37) -> *mut *mut AstStat {
38    this.reallocate_statements(statements)
39}