Skip to main content

graphics/
stubs.rs

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/// Set a block of memory to a specific value
13///
14/// # Safety
15/// This function is unsafe because it dereferences raw pointers.
16#[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
21// LVGL's heap. With `LV_STDLIB_CUSTOM` (hosts, `lvgl_system_heap`) every LVGL
22// allocation goes to Xila's system allocator and the hooks below implement it.
23// With `LV_STDLIB_BUILTIN` (microcontrollers) LVGL manages its own fixed pool
24// (see `include/lv_conf.h` and the README) and provides all of these itself,
25// so they must not be defined here.
26//
27// `build.rs` queries the selected C header; this guards against it
28// disagreeing with `lv_conf.h` (which would otherwise surface as duplicate or
29// undefined `lv_mem_*` symbols at link time).
30const _: () = 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/// Deinitialize the memory manager
36///
37/// # Safety
38/// This function is unsafe because it may involve low-level memory operations.
39#[cfg(lvgl_system_heap)]
40#[unsafe(no_mangle)]
41pub unsafe extern "C" fn lv_mem_deinit() {
42    log::information!("Deinitializing memory manager");
43    // This function is a no-op in this stub.
44    // In a real implementation, you would clean up memory resources here.
45}
46
47/// Initialize the memory manager
48///
49/// # Safety
50/// This function is unsafe because it may involve low-level memory operations.
51#[cfg(lvgl_system_heap)]
52#[unsafe(no_mangle)]
53pub unsafe extern "C" fn lv_mem_init() {
54    log::information!("Initializing memory manager");
55    // This function is a no-op in this stub.
56    // In a real implementation, you would set up memory resources here.
57}
58
59/// Allocate a block of memory
60///
61/// # Safety
62/// This function is unsafe because it dereferences raw pointers.
63#[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/// Free a block of memory
70///
71/// # Safety
72/// This function is unsafe because it dereferences raw pointers.
73#[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/// Reallocate a block of memory
80///
81/// # Safety
82/// This function is unsafe because it dereferences raw pointers.
83#[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/// Report the memory usage through `lv_mem_monitor()`.
90///
91/// LVGL shares Xila's system heap in this configuration, so this reports the
92/// system heap as a whole, not LVGL's own share: `max_used`, the block counts
93/// and the biggest free block are not tracked. Use the built-in pool (see the
94/// README) to measure LVGL's own peak usage.
95///
96/// # Safety
97/// `monitor` must be null or point to a writable `lv_mem_monitor_t`.
98#[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/// Check the allocator's integrity. The system allocator has nothing to check.
113///
114/// # Safety
115/// Always safe to call.
116#[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/// Copy a block of memory from source to destination
123///
124/// # Safety
125/// This function is unsafe because it dereferences raw pointers.
126#[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/// Compare two strings
136///
137/// # Safety
138/// This function is unsafe because it dereferences raw pointers.
139#[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/// Get the length of a string
145///
146/// # Safety
147/// This function is unsafe because it dereferences raw pointers.
148#[unsafe(no_mangle)]
149pub unsafe extern "C" fn lv_strlen(string: *const c_char) -> usize {
150    unsafe { string_get_length(string) }
151}
152
153/// Format a string
154///
155/// # Safety
156/// This function is unsafe because it dereferences raw pointers.
157#[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    // This function is not implemented in this stub.
165    // In a real implementation, you would handle formatted output here.
166    0
167}
168
169/// Format a string with variable arguments
170///
171/// # Safety
172/// This function is unsafe because it dereferences raw pointers.
173#[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    // This function is not implemented in this stub.
181    // In a real implementation, you would handle formatted output here.
182    0
183}
184
185/// Copy a string from source to destination
186///
187/// # Safety
188/// This function is unsafe because it dereferences raw pointers.
189#[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/// Copy a string up to a given size
195///
196/// # Safety
197/// This function is unsafe because it dereferences raw pointers.
198#[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/// Duplicate a string.
208///
209/// The copy is released with `lv_free()`, so it comes from LVGL's heap.
210///
211/// # Safety
212/// This function is unsafe because it dereferences raw pointers.
213#[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/// Duplicate a string.
220///
221/// The copy is released with `lv_free()`, so it must come from LVGL's own pool,
222/// not from the system allocator.
223///
224/// # Safety
225/// This function is unsafe because it dereferences raw pointers.
226#[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/// Copy `length` bytes of `string` plus a terminator into a new LVGL allocation.
233///
234/// # Safety
235/// `string` must be readable for `length` bytes.
236#[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/// Move a block of memory from source to destination
249///
250/// # Safety
251/// This function is unsafe because it dereferences raw pointers.
252#[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/// Duplicate a string up to a given size
262///
263/// The copy is released with `lv_free()`, so it comes from LVGL's heap.
264///
265/// # Safety
266/// This function is unsafe because it dereferences raw pointers.
267#[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/// Duplicate a string up to a given size.
274///
275/// The copy is released with `lv_free()`, so it must come from LVGL's own pool,
276/// not from the system allocator.
277///
278/// # Safety
279/// This function is unsafe because it dereferences raw pointers.
280#[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/// Compare two memory areas up to a given size
291///
292/// # Safety
293/// This function is unsafe because it dereferences raw pointers.
294#[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/// Concatenate two strings
300///
301/// # Safety
302/// This function is unsafe because it dereferences raw pointers.
303#[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/// Compare two strings up to a given size
309///
310/// # Safety
311///
312/// This function is unsafe because it dereferences raw pointers.
313#[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}