mirror of
https://github.com/penpot/penpot.git
synced 2025-01-06 14:50:20 -05:00
✨ Use a static Vec<u8> to handle shared memory
This commit is contained in:
parent
df6727f186
commit
6623963a7f
3 changed files with 24 additions and 20 deletions
|
@ -132,7 +132,6 @@
|
|||
(aget buffer 1)
|
||||
(aget buffer 2)
|
||||
(aget buffer 3)
|
||||
image-ptr
|
||||
image-size)
|
||||
true))))))
|
||||
|
||||
|
|
|
@ -182,35 +182,32 @@ pub extern "C" fn add_shape_fill_stops(ptr: *mut shapes::RawStopData, n_stops: u
|
|||
|
||||
if let Some(shape) = state.current_shape() {
|
||||
let len = n_stops as usize;
|
||||
let buffer_size = std::mem::size_of::<shapes::RawStopData>() * len;
|
||||
|
||||
unsafe {
|
||||
let buffer = Vec::<shapes::RawStopData>::from_raw_parts(ptr, len, len);
|
||||
shape
|
||||
.add_gradient_stops(buffer)
|
||||
.expect("could not add gradient stops");
|
||||
mem::free(ptr as *mut u8, buffer_size);
|
||||
mem::free_bytes();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn store_image(a: u32, b: u32, c: u32, d: u32, ptr: *mut u8, size: u32) {
|
||||
if ptr.is_null() || size == 0 {
|
||||
panic!("Invalid data, null pointer or zero size");
|
||||
}
|
||||
pub extern "C" fn store_image(a: u32, b: u32, c: u32, d: u32, size: u32) {
|
||||
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
||||
let id = uuid_from_u32_quartet(a, b, c, d);
|
||||
|
||||
unsafe {
|
||||
let image_bytes = Vec::<u8>::from_raw_parts(ptr, size as usize, size as usize);
|
||||
let image_bytes =
|
||||
Vec::<u8>::from_raw_parts(mem::buffer_ptr(), size as usize, size as usize);
|
||||
match state.render_state().add_image(id, &image_bytes) {
|
||||
Err(msg) => {
|
||||
eprintln!("{}", msg);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
mem::free(ptr as *mut u8, size as usize * std::mem::size_of::<u8>());
|
||||
mem::free_bytes();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,17 +1,25 @@
|
|||
static mut BUFFERU8: Option<Box<Vec<u8>>> = None;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn alloc_bytes(len: usize) -> *mut u8 {
|
||||
// create a new mutable buffer with capacity `len`
|
||||
let mut buf: Vec<u8> = Vec::with_capacity(len);
|
||||
let ptr = buf.as_mut_ptr();
|
||||
// take ownership of the memory block and ensure the its destructor is not
|
||||
// called when the object goes out of scope at the end of the function
|
||||
std::mem::forget(buf);
|
||||
// TODO: Figure out how to deal with Result<T> from Emscripten
|
||||
if unsafe { BUFFERU8.is_some() } {
|
||||
panic!("Bytes already allocated");
|
||||
}
|
||||
|
||||
let mut buffer = Box::new(Vec::<u8>::with_capacity(len));
|
||||
let ptr = buffer.as_mut_ptr();
|
||||
|
||||
unsafe { BUFFERU8 = Some(buffer) };
|
||||
return ptr;
|
||||
}
|
||||
|
||||
pub fn free(ptr: *mut u8, len: usize) {
|
||||
unsafe {
|
||||
let buf = Vec::<u8>::from_raw_parts(ptr, len, len);
|
||||
std::mem::forget(buf);
|
||||
}
|
||||
pub fn free_bytes() {
|
||||
let buffer = unsafe { BUFFERU8.take() }.expect("uninitialized buffer");
|
||||
std::mem::drop(buffer);
|
||||
}
|
||||
|
||||
pub fn buffer_ptr() -> *mut u8 {
|
||||
let buffer = unsafe { BUFFERU8.as_mut() }.expect("uninitializied buffer");
|
||||
buffer.as_mut_ptr()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue