diff options
| author | zhang san <[email protected]> | 2025-12-08 18:34:25 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-12-08 18:34:25 +0800 |
| commit | 69980f8b9d29b38dbd4ddf6373b4b45f3a138409 (patch) | |
| tree | 21dd9a8ce80e3d30192e9ffa56ea39a199bae309 /components/rust/core/src/api/mem.rs | |
| parent | cd1d47b87c5d47e131730e5506eb3ad77d357653 (diff) | |
Adding Rust Language Support for RT-Thread #10910
Diffstat (limited to 'components/rust/core/src/api/mem.rs')
| -rw-r--r-- | components/rust/core/src/api/mem.rs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/components/rust/core/src/api/mem.rs b/components/rust/core/src/api/mem.rs new file mode 100644 index 0000000000..8b998f77c3 --- /dev/null +++ b/components/rust/core/src/api/mem.rs @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2006-2025, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-10-10 foxglove memory management API + */ + +use crate::bindings::*; +use core::ffi::c_void; + +pub fn mem_alloc(bytes: usize) -> Option<*mut c_void> { + if bytes == 0 { + return None; + } + + let ptr = unsafe { rt_malloc(bytes as rt_size_t) }; + if ptr.is_null() { + None + } else { + Some(ptr) + } +} + +pub fn mem_calloc(count: usize, size: usize) -> Option<*mut c_void> { + if count == 0 || size == 0 { + return None; + } + + let ptr = unsafe { rt_calloc(count as rt_size_t, size as rt_size_t) }; + if ptr.is_null() { + None + } else { + Some(ptr) + } +} + +pub fn mem_alloc_aligned(bytes: usize, align: usize) -> Option<*mut c_void> { + if bytes == 0 || align == 0 || !align.is_power_of_two() { + return None; + } + + let ptr = unsafe { rt_malloc_align(bytes as rt_size_t, align as rt_size_t) }; + if ptr.is_null() { + None + } else { + Some(ptr) + } +} + +pub fn mem_realloc(ptr: *mut c_void, new_size: usize) -> Option<*mut c_void> { + if new_size == 0 { + if !ptr.is_null() { + mem_free(ptr); + } + return None; + } + + let new_ptr = unsafe { rt_realloc(ptr, new_size as rt_size_t) }; + if new_ptr.is_null() { + None + } else { + Some(new_ptr) + } +} + +pub fn mem_free(ptr: *mut c_void) { + if !ptr.is_null() { + unsafe { rt_free(ptr) }; + } +} + +pub fn mem_free_aligned(ptr: *mut c_void) { + if !ptr.is_null() { + unsafe { rt_free_align(ptr) }; + } +} |
