1 // Copyright 2024, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //! This file provides one possible implementation of the sysdeps functions for libufdt.
16 //! Global allocator is required.
17
18 #![cfg_attr(not(test), no_std)]
19
20 use core::ffi::c_void;
21 use libc::{gbl_free, gbl_malloc};
22
23 const DTO_MALLOC_ALIGNMENT: usize = 8;
24
25 /// void *malloc(size_t size)
26 ///
27 /// # Safety:
28 ///
29 /// * `return value` pointing buffer must be used within provided `size`
30 #[no_mangle]
dto_malloc(size: usize) -> *mut c_void31 pub unsafe extern "C" fn dto_malloc(size: usize) -> *mut c_void {
32 // SAFETY: libufdt calls are compatible with libc counterparts, alignment the same as
33 // dto_free
34 unsafe { gbl_malloc(size, DTO_MALLOC_ALIGNMENT) }
35 }
36
37 /// void free(void *ptr)
38 ///
39 /// # Safety:
40 ///
41 /// * `ptr` must be a pointer allocated by `dto_malloc` or null
42 #[no_mangle]
dto_free(ptr: *mut c_void)43 pub unsafe extern "C" fn dto_free(ptr: *mut c_void) {
44 // SAFETY: libufdt calls are compatible with libc counterparts, alignment the same as
45 // dto_malloc
46 unsafe { gbl_free(ptr, DTO_MALLOC_ALIGNMENT) }
47 }
48