2020-06-16 21:19:26 +00:00
|
|
|
use crate::{LvError, LvResult};
|
|
|
|
use core::mem;
|
|
|
|
use core::ptr::NonNull;
|
|
|
|
|
|
|
|
/// Places `T` into LVGL memory.
|
2020-06-18 21:45:08 +00:00
|
|
|
pub(crate) struct Box<T>(NonNull<T>);
|
2020-06-16 21:19:26 +00:00
|
|
|
|
2020-06-17 06:38:35 +00:00
|
|
|
impl<T> Box<T> {
|
2020-06-16 21:19:26 +00:00
|
|
|
pub fn new(inner: T) -> LvResult<Box<T>> {
|
2020-06-18 21:45:08 +00:00
|
|
|
let size = mem::size_of::<T>();
|
2020-06-16 21:19:26 +00:00
|
|
|
let inner = unsafe {
|
2020-06-18 21:45:08 +00:00
|
|
|
let ptr = lvgl_sys::lv_mem_alloc(size as lvgl_sys::size_t) as *mut T;
|
|
|
|
|
2020-06-18 21:56:09 +00:00
|
|
|
// LVGL should align the memory address for us!
|
2020-06-18 21:45:08 +00:00
|
|
|
assert_eq!(
|
|
|
|
ptr as usize % mem::align_of::<T>(),
|
|
|
|
0,
|
|
|
|
"Memory address not aligned!"
|
|
|
|
);
|
|
|
|
|
2020-06-16 21:19:26 +00:00
|
|
|
match NonNull::new(ptr) {
|
|
|
|
Some(v) => {
|
2020-06-17 06:38:35 +00:00
|
|
|
// Move `T` to LVGL managed memory
|
2020-06-18 21:45:08 +00:00
|
|
|
// It will panic if LVGL memory is not aligned
|
|
|
|
ptr.write(inner);
|
2020-06-16 21:19:26 +00:00
|
|
|
Ok(v)
|
|
|
|
}
|
|
|
|
None => Err(LvError::LvOOMemory),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(Box(inner?))
|
|
|
|
}
|
|
|
|
|
2020-06-18 21:45:08 +00:00
|
|
|
pub fn into_raw(self) -> *mut T {
|
|
|
|
let b = mem::ManuallyDrop::new(self);
|
2020-06-16 21:19:26 +00:00
|
|
|
b.0.as_ptr()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 06:38:35 +00:00
|
|
|
impl<T> Drop for Box<T> {
|
2020-06-16 21:19:26 +00:00
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
lvgl_sys::lv_mem_free(self.0.as_ptr() as *const cty::c_void);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 06:38:35 +00:00
|
|
|
impl<T> AsMut<T> for Box<T> {
|
2020-06-16 21:19:26 +00:00
|
|
|
fn as_mut(&mut self) -> &mut T {
|
|
|
|
unsafe { self.0.as_mut() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
2020-06-18 21:45:08 +00:00
|
|
|
use core::mem::MaybeUninit;
|
|
|
|
use std::sync::Once;
|
|
|
|
|
|
|
|
static INIT_LVGL: Once = Once::new();
|
|
|
|
|
|
|
|
fn init() {
|
|
|
|
INIT_LVGL.call_once(|| {
|
|
|
|
unsafe {
|
|
|
|
lvgl_sys::lv_init();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2020-06-16 21:19:26 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn place_value_in_lv_mem() {
|
2020-06-18 21:45:08 +00:00
|
|
|
init();
|
|
|
|
|
2020-06-16 21:19:26 +00:00
|
|
|
let v = Box::new(5).unwrap();
|
|
|
|
drop(v);
|
|
|
|
let v = Box::new(10).unwrap();
|
|
|
|
drop(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn place_complex_value_in_lv_mem() {
|
2020-06-18 21:45:08 +00:00
|
|
|
init();
|
2020-06-16 21:19:26 +00:00
|
|
|
|
2020-06-18 21:45:08 +00:00
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Debug)]
|
2020-06-16 21:19:26 +00:00
|
|
|
struct Point {
|
|
|
|
x: u64,
|
2020-06-18 21:45:08 +00:00
|
|
|
y: i8,
|
|
|
|
t: i32,
|
2020-06-16 21:19:26 +00:00
|
|
|
disp: i32,
|
|
|
|
}
|
|
|
|
|
2020-06-18 21:45:08 +00:00
|
|
|
for i in 0..100 {
|
|
|
|
let p = Point {
|
|
|
|
x: i,
|
|
|
|
y: 42,
|
|
|
|
t: 0,
|
|
|
|
disp: -100,
|
|
|
|
};
|
|
|
|
|
|
|
|
println!("{:?}", p);
|
|
|
|
let mut b = Box::new(p).unwrap_or_else(|_| {
|
|
|
|
print_mem_info();
|
|
|
|
panic!("OOM");
|
|
|
|
});
|
|
|
|
|
|
|
|
println!("memory address is {:p}", b.as_mut());
|
2020-06-16 21:19:26 +00:00
|
|
|
|
2020-06-18 21:45:08 +00:00
|
|
|
let point = b.as_mut();
|
|
|
|
if point.x != i {
|
|
|
|
print_mem_info();
|
2020-06-16 21:19:26 +00:00
|
|
|
|
2020-06-18 21:45:08 +00:00
|
|
|
println!("{:?}", point);
|
|
|
|
}
|
|
|
|
assert_eq!(point.x, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_mem_info() {
|
|
|
|
let mut info = MaybeUninit::uninit();
|
|
|
|
unsafe {
|
|
|
|
lvgl_sys::lv_mem_monitor(info.as_mut_ptr());
|
|
|
|
}
|
|
|
|
if !info.as_ptr().is_null() {
|
|
|
|
let info = unsafe { info.assume_init() };
|
|
|
|
println!("mem info: {:?}", info);
|
|
|
|
}
|
2020-06-16 21:19:26 +00:00
|
|
|
}
|
|
|
|
}
|