-
Notifications
You must be signed in to change notification settings - Fork 64
/
mem.rs
155 lines (130 loc) · 3.61 KB
/
mem.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use core::mem;
use core::ops::{Deref, DerefMut};
use core::ptr::NonNull;
/// Places a sized `T` into LVGL memory.
///
/// This is useful for cases when we need to allocate memory on Rust side
/// and handover the management of that memory to LVGL. May also be used in cases we
/// want to use dynamic memory in the Rust side.
pub(crate) struct Box<T>(NonNull<T>);
impl<T> Box<T> {
/// Allocate memory using LVGL memory API and place `T` in the LVGL tracked memory.
pub fn new(value: T) -> Box<T> {
let size = mem::size_of::<T>();
let inner = unsafe {
let ptr = lvgl_sys::lv_mem_alloc(size as lvgl_sys::size_t) as *mut T;
// LVGL should align the memory address for us!
assert_eq!(
ptr as usize % mem::align_of::<T>(),
0,
"Memory address not aligned!"
);
NonNull::new(ptr)
.map(|p| {
p.as_ptr().write(value);
p
})
.unwrap_or_else(|| {
panic!("Could not allocate memory {} bytes", size);
})
};
Box(inner)
}
pub fn into_raw(self) -> *mut T {
let b = mem::ManuallyDrop::new(self);
b.0.as_ptr()
}
}
impl<T> Drop for Box<T> {
fn drop(&mut self) {
unsafe {
lvgl_sys::lv_mem_free(self.0.as_ptr() as *const cty::c_void);
}
}
}
impl<T> DerefMut for Box<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut()
}
}
impl<T> Deref for Box<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
unsafe { self.0.as_ref() }
}
}
impl<T> AsMut<T> for Box<T> {
fn as_mut(&mut self) -> &mut T {
unsafe { self.0.as_mut() }
}
}
#[cfg(test)]
mod test {
use super::*;
use std::vec::Vec;
#[test]
fn place_value_in_lv_mem() {
crate::lvgl_init();
let v = Box::new(5);
drop(v);
let v = Box::new(10);
drop(v);
}
#[test]
fn place_complex_value_in_lv_mem() {
crate::lvgl_init();
#[repr(C)]
#[derive(Debug)]
struct Point {
x: u64,
y: i8,
t: i32,
disp: i32,
}
let initial_mem_info = mem_info();
let mut keep = Vec::new();
for i in 0..100 {
let p = Point {
x: i,
y: 42,
t: 0,
disp: -100,
};
println!("{:?}", p);
let mut b = Box::new(p);
println!("memory address is {:p}", b.as_mut());
let point = b.as_mut();
if point.x != i {
println!("{:?}", point);
}
assert_eq!(point.x, i);
let info = mem_info();
println!("mem info: {:?}", &info);
keep.push(b);
}
drop(keep);
unsafe {
lvgl_sys::lv_mem_defrag();
}
let final_info = mem_info();
println!("mem info: {:?}", &final_info);
// If this fails, we are leaking memory! BOOM! \o/
assert_eq!(initial_mem_info.free_size, final_info.free_size)
}
fn mem_info() -> lvgl_sys::lv_mem_monitor_t {
let mut info = lvgl_sys::lv_mem_monitor_t {
total_size: 0,
free_cnt: 0,
free_size: 0,
free_biggest_size: 0,
used_cnt: 0,
max_used: 0,
used_pct: 0,
frag_pct: 0,
};
unsafe {
lvgl_sys::lv_mem_monitor(&mut info as *mut _);
}
info
}
}