unify heap allocation handling
This commit is contained in:
parent
63dfecdbf5
commit
fd6f9198f3
10 changed files with 101 additions and 110 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use crate::ByteSlice;
|
||||
use crate::{heap_drop, heap_move, heap_move_nonnull, ByteSlice};
|
||||
use servicepoint::{CharGrid, Grid};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
|
|
@ -19,8 +19,7 @@ pub unsafe extern "C" fn sp_char_grid_new(
|
|||
width: usize,
|
||||
height: usize,
|
||||
) -> NonNull<CharGrid> {
|
||||
let result = Box::new(CharGrid::new(width, height));
|
||||
NonNull::from(Box::leak(result))
|
||||
heap_move_nonnull(CharGrid::new(width, height))
|
||||
}
|
||||
|
||||
/// Loads a [CharGrid] with the specified dimensions from the provided data.
|
||||
|
|
@ -34,7 +33,7 @@ pub unsafe extern "C" fn sp_char_grid_load(
|
|||
) -> *mut CharGrid {
|
||||
let data = unsafe { data.as_slice() };
|
||||
if let Ok(grid) = CharGrid::load_utf8(width, height, data.to_vec()) {
|
||||
Box::leak(Box::new(grid))
|
||||
heap_move(grid)
|
||||
} else {
|
||||
std::ptr::null_mut()
|
||||
}
|
||||
|
|
@ -45,14 +44,13 @@ pub unsafe extern "C" fn sp_char_grid_load(
|
|||
pub unsafe extern "C" fn sp_char_grid_clone(
|
||||
char_grid: NonNull<CharGrid>,
|
||||
) -> NonNull<CharGrid> {
|
||||
let result = unsafe { char_grid.as_ref().clone() };
|
||||
NonNull::from(Box::leak(Box::new(result)))
|
||||
heap_move_nonnull(unsafe { char_grid.as_ref().clone() })
|
||||
}
|
||||
|
||||
/// Deallocates a [CharGrid].
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp_char_grid_free(char_grid: NonNull<CharGrid>) {
|
||||
_ = unsafe { Box::from_raw(char_grid.as_ptr()) };
|
||||
unsafe { heap_drop(char_grid) }
|
||||
}
|
||||
|
||||
/// Returns the current value at the specified position.
|
||||
|
|
|
|||
Loading…
Reference in a new issue