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