mirror of
https://github.com/penpot/penpot.git
synced 2025-08-01 08:48:28 +02:00
91 lines
2.4 KiB
Rust
91 lines
2.4 KiB
Rust
use std::alloc::{alloc, Layout};
|
|
use std::ptr;
|
|
use std::sync::Mutex;
|
|
|
|
const LAYOUT_ALIGN: usize = 4;
|
|
|
|
static BUFFERU8: Mutex<Option<Box<Vec<u8>>>> = Mutex::new(None);
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn alloc_bytes(len: usize) -> *mut u8 {
|
|
let mut guard = BUFFERU8.lock().unwrap();
|
|
|
|
if guard.is_some() {
|
|
panic!("Bytes already allocated");
|
|
}
|
|
|
|
unsafe {
|
|
let layout = Layout::from_size_align_unchecked(len, LAYOUT_ALIGN);
|
|
let ptr = alloc(layout) as *mut u8;
|
|
if ptr.is_null() {
|
|
panic!("Allocation failed");
|
|
}
|
|
// TODO: Maybe this could be removed.
|
|
ptr::write_bytes(ptr, 0, len);
|
|
*guard = Some(Box::new(Vec::from_raw_parts(ptr, len, len)));
|
|
ptr
|
|
}
|
|
}
|
|
|
|
pub fn write_bytes(bytes: Vec<u8>) -> *mut u8 {
|
|
let mut guard = BUFFERU8.lock().unwrap();
|
|
|
|
if guard.is_some() {
|
|
panic!("Bytes already allocated");
|
|
}
|
|
|
|
let mut new_buffer = Box::new(bytes);
|
|
let ptr = new_buffer.as_mut_ptr();
|
|
|
|
*guard = Some(new_buffer);
|
|
ptr
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn free_bytes() {
|
|
let mut guard = BUFFERU8.lock().unwrap();
|
|
*guard = None;
|
|
std::mem::drop(guard);
|
|
}
|
|
|
|
pub fn bytes() -> Vec<u8> {
|
|
let mut guard = BUFFERU8.lock().unwrap();
|
|
|
|
guard
|
|
.take()
|
|
.map_or_else(|| panic!("Buffer is not initialized"), |buffer| *buffer)
|
|
}
|
|
|
|
pub fn bytes_or_empty() -> Vec<u8> {
|
|
let mut guard = BUFFERU8.lock().unwrap();
|
|
|
|
guard.take().map_or_else(|| Vec::new(), |buffer| *buffer)
|
|
}
|
|
|
|
pub trait SerializableResult {
|
|
type BytesType;
|
|
fn from_bytes(bytes: Self::BytesType) -> Self;
|
|
fn as_bytes(&self) -> Self::BytesType;
|
|
fn clone_to_slice(&self, slice: &mut [u8]);
|
|
}
|
|
|
|
/*
|
|
Returns an array in the heap. The first 4 bytes is always the size
|
|
of the array. Then the items are serialized one after the other
|
|
by the implementation of SerializableResult trait
|
|
*/
|
|
pub fn write_vec<T: SerializableResult>(result: Vec<T>) -> *mut u8 {
|
|
let elem_size = size_of::<T::BytesType>();
|
|
let bytes_len = 4 + result.len() * elem_size;
|
|
let mut result_bytes = Vec::<u8>::with_capacity(bytes_len);
|
|
|
|
result_bytes.resize(bytes_len, 0);
|
|
result_bytes[0..4].clone_from_slice(&result.len().to_le_bytes());
|
|
|
|
for i in 0..result.len() {
|
|
let base = 4 + i * elem_size;
|
|
result[i].clone_to_slice(&mut result_bytes[base..base + elem_size]);
|
|
}
|
|
|
|
write_bytes(result_bytes)
|
|
}
|