1use core::ffi::{c_char, c_int, c_void};
2
3#[cfg(lvgl_system_heap)]
4use c_support::{memory_allocate_core, memory_deallocate, memory_reallocate};
5use c_support::{
6 memory_compare, memory_copy, memory_move, memory_set, string_compare, string_compare_bounded,
7 string_concatenate, string_copy, string_copy_bounded, string_get_length,
8};
9#[cfg(lvgl_system_heap)]
10use c_support::{string_duplicate, string_duplicate_bounded};
11
12#[unsafe(no_mangle)]
17pub unsafe extern "C" fn lv_memset(destination: *mut c_void, value: u8, size: usize) {
18 unsafe { memory_set(destination, value as c_int, size) };
19}
20
21const _: () = assert!(
31 (crate::lvgl::LV_USE_STDLIB_MALLOC == crate::lvgl::LV_STDLIB_CUSTOM) == cfg!(lvgl_system_heap),
32 "lv_conf.h and build.rs disagree on LVGL's allocator (XILA_LVGL_MEM_POOL)"
33);
34
35#[cfg(lvgl_system_heap)]
40#[unsafe(no_mangle)]
41pub unsafe extern "C" fn lv_mem_deinit() {
42 log::information!("Deinitializing memory manager");
43 }
46
47#[cfg(lvgl_system_heap)]
52#[unsafe(no_mangle)]
53pub unsafe extern "C" fn lv_mem_init() {
54 log::information!("Initializing memory manager");
55 }
58
59#[cfg(lvgl_system_heap)]
64#[unsafe(no_mangle)]
65pub unsafe extern "C" fn lv_malloc_core(size: usize) -> *mut c_void {
66 unsafe { memory_allocate_core(size) }
67}
68
69#[cfg(lvgl_system_heap)]
74#[unsafe(no_mangle)]
75pub unsafe extern "C" fn lv_free_core(pointer: *mut c_void) {
76 unsafe { memory_deallocate(pointer) }
77}
78
79#[cfg(lvgl_system_heap)]
84#[unsafe(no_mangle)]
85pub unsafe extern "C" fn lv_realloc_core(pointer: *mut c_void, new_size: usize) -> *mut c_void {
86 unsafe { memory_reallocate(pointer, new_size) }
87}
88
89#[cfg(lvgl_system_heap)]
99#[unsafe(no_mangle)]
100pub unsafe extern "C" fn lv_mem_monitor_core(monitor: *mut crate::lvgl::lv_mem_monitor_t) {
101 let Some(monitor) = (unsafe { monitor.as_mut() }) else {
102 return;
103 };
104 let manager = memory::global_manager();
105 let free = manager.get_free();
106 let total = manager.get_total_size().max(free);
107 monitor.total_size = total;
108 monitor.free_size = free;
109 monitor.used_pct = ((total - free) as u64 * 100 / total.max(1) as u64) as u8;
110}
111
112#[cfg(lvgl_system_heap)]
117#[unsafe(no_mangle)]
118pub unsafe extern "C" fn lv_mem_test_core() -> crate::lvgl::lv_result_t {
119 crate::lvgl::lv_result_t_LV_RESULT_OK
120}
121
122#[unsafe(no_mangle)]
127pub unsafe extern "C" fn lv_memcpy(
128 destination: *mut c_void,
129 source: *const c_void,
130 size: usize,
131) -> *mut c_void {
132 unsafe { memory_copy(destination, source, size) }
133}
134
135#[unsafe(no_mangle)]
140pub unsafe extern "C" fn lv_strcmp(str1: *const c_char, str2: *const c_char) -> c_int {
141 unsafe { string_compare(str1, str2) }
142}
143
144#[unsafe(no_mangle)]
149pub unsafe extern "C" fn lv_strlen(string: *const c_char) -> usize {
150 unsafe { string_get_length(string) }
151}
152
153#[unsafe(no_mangle)]
158pub unsafe extern "C" fn lv_snprintf(
159 _: *mut c_char,
160 _: usize,
161 _: *const c_char,
162 _: *mut c_void,
163) -> c_int {
164 0
167}
168
169#[unsafe(no_mangle)]
174pub unsafe extern "C" fn lv_vsnprintf(
175 _: *mut c_char,
176 _: usize,
177 _: *const c_char,
178 _: *mut c_void,
179) -> c_int {
180 0
183}
184
185#[unsafe(no_mangle)]
190pub unsafe extern "C" fn lv_strcpy(destination: *mut c_char, source: *const c_char) -> *mut c_char {
191 unsafe { string_copy(destination, source) }
192}
193
194#[unsafe(no_mangle)]
199pub unsafe extern "C" fn lv_strncpy(
200 destination: *mut c_char,
201 source: *const c_char,
202 size: usize,
203) -> *mut c_char {
204 unsafe { string_copy_bounded(destination, source, size) }
205}
206
207#[cfg(lvgl_system_heap)]
214#[unsafe(no_mangle)]
215pub unsafe extern "C" fn lv_strdup(string: *const c_char) -> *mut c_char {
216 unsafe { string_duplicate(string) }
217}
218
219#[cfg(not(lvgl_system_heap))]
227#[unsafe(no_mangle)]
228pub unsafe extern "C" fn lv_strdup(string: *const c_char) -> *mut c_char {
229 unsafe { duplicate_into_pool(string, string_get_length(string)) }
230}
231
232#[cfg(not(lvgl_system_heap))]
237unsafe fn duplicate_into_pool(string: *const c_char, length: usize) -> *mut c_char {
238 let copy = unsafe { crate::lvgl::lv_malloc(length + 1) } as *mut c_char;
239 if !copy.is_null() {
240 unsafe {
241 memory_copy(copy.cast(), string.cast(), length);
242 *copy.add(length) = 0;
243 }
244 }
245 copy
246}
247
248#[unsafe(no_mangle)]
253pub unsafe extern "C" fn lv_memmove(
254 destination: *mut c_void,
255 source: *const c_void,
256 size: usize,
257) -> *mut c_void {
258 unsafe { memory_move(destination, source, size) }
259}
260
261#[cfg(lvgl_system_heap)]
268#[unsafe(no_mangle)]
269pub unsafe extern "C" fn lv_strndup(string: *const c_char, size: usize) -> *mut c_char {
270 unsafe { string_duplicate_bounded(string, size) }
271}
272
273#[cfg(not(lvgl_system_heap))]
281#[unsafe(no_mangle)]
282pub unsafe extern "C" fn lv_strndup(string: *const c_char, size: usize) -> *mut c_char {
283 let mut length = 0;
284 while length < size && unsafe { *string.add(length) } != 0 {
285 length += 1;
286 }
287 unsafe { duplicate_into_pool(string, length) }
288}
289
290#[unsafe(no_mangle)]
295pub unsafe extern "C" fn lv_memcmp(ptr1: *const c_void, ptr2: *const c_void, size: usize) -> c_int {
296 unsafe { memory_compare(ptr1, ptr2, size) }
297}
298
299#[unsafe(no_mangle)]
304pub unsafe extern "C" fn lv_strcat(destination: *mut c_char, source: *const c_char) -> *mut c_char {
305 unsafe { string_concatenate(destination, source) }
306}
307
308#[unsafe(no_mangle)]
314pub unsafe extern "C" fn lv_strncmp(
315 str1: *const c_char,
316 str2: *const c_char,
317 size: usize,
318) -> c_int {
319 unsafe { string_compare_bounded(str1, str2, size) }
320}