blob: 8b998f77c3767931c4e03c71b7064d3223707f5d (
plain)
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
|
/*
* 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) };
}
}
|