Pierre-Clément Tosi | fc53115 | 2022-10-20 12:22:23 +0100 | [diff] [blame] | 1 | // Copyright 2022, 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 | //! Heap implementation. |
| 16 | |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 17 | use alloc::alloc::alloc; |
| 18 | use alloc::alloc::Layout; |
| 19 | use alloc::boxed::Box; |
| 20 | |
Pierre-Clément Tosi | 54e71d0 | 2022-12-08 13:57:43 +0000 | [diff] [blame] | 21 | use core::alloc::GlobalAlloc as _; |
Pierre-Clément Tosi | 54e71d0 | 2022-12-08 13:57:43 +0000 | [diff] [blame] | 22 | use core::ffi::c_void; |
| 23 | use core::mem; |
| 24 | use core::num::NonZeroUsize; |
| 25 | use core::ptr; |
| 26 | use core::ptr::NonNull; |
| 27 | |
Pierre-Clément Tosi | fc53115 | 2022-10-20 12:22:23 +0100 | [diff] [blame] | 28 | use buddy_system_allocator::LockedHeap; |
| 29 | |
| 30 | #[global_allocator] |
| 31 | static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::new(); |
| 32 | |
Andrew Walbran | 739c9e6 | 2023-01-16 11:59:33 +0000 | [diff] [blame] | 33 | /// 128 KiB |
Alice Wang | 815461f | 2023-01-31 12:59:00 +0000 | [diff] [blame] | 34 | const HEAP_SIZE: usize = 0x20000; |
Andrew Walbran | 739c9e6 | 2023-01-16 11:59:33 +0000 | [diff] [blame] | 35 | static mut HEAP: [u8; HEAP_SIZE] = [0; HEAP_SIZE]; |
Pierre-Clément Tosi | fc53115 | 2022-10-20 12:22:23 +0100 | [diff] [blame] | 36 | |
| 37 | pub unsafe fn init() { |
| 38 | HEAP_ALLOCATOR.lock().init(HEAP.as_mut_ptr() as usize, HEAP.len()); |
| 39 | } |
Pierre-Clément Tosi | 54e71d0 | 2022-12-08 13:57:43 +0000 | [diff] [blame] | 40 | |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 41 | /// Allocate an aligned but uninitialized slice of heap. |
| 42 | pub fn aligned_boxed_slice(size: usize, align: usize) -> Option<Box<[u8]>> { |
| 43 | let size = NonZeroUsize::new(size)?.get(); |
| 44 | let layout = Layout::from_size_align(size, align).ok()?; |
| 45 | // SAFETY - We verify that `size` and the returned `ptr` are non-null. |
| 46 | let ptr = unsafe { alloc(layout) }; |
| 47 | let ptr = NonNull::new(ptr)?.as_ptr(); |
| 48 | let slice_ptr = ptr::slice_from_raw_parts_mut(ptr, size); |
| 49 | |
| 50 | // SAFETY - The memory was allocated using the proper layout by our global_allocator. |
| 51 | Some(unsafe { Box::from_raw(slice_ptr) }) |
| 52 | } |
| 53 | |
Pierre-Clément Tosi | 54e71d0 | 2022-12-08 13:57:43 +0000 | [diff] [blame] | 54 | #[no_mangle] |
| 55 | unsafe extern "C" fn malloc(size: usize) -> *mut c_void { |
| 56 | malloc_(size).map_or(ptr::null_mut(), |p| p.cast::<c_void>().as_ptr()) |
| 57 | } |
| 58 | |
| 59 | #[no_mangle] |
| 60 | unsafe extern "C" fn free(ptr: *mut c_void) { |
| 61 | if let Some(ptr) = NonNull::new(ptr).map(|p| p.cast::<usize>().as_ptr().offset(-1)) { |
| 62 | if let Some(size) = NonZeroUsize::new(*ptr) { |
| 63 | if let Some(layout) = malloc_layout(size) { |
| 64 | HEAP_ALLOCATOR.dealloc(ptr as *mut u8, layout); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | unsafe fn malloc_(size: usize) -> Option<NonNull<usize>> { |
| 71 | let size = NonZeroUsize::new(size)?.checked_add(mem::size_of::<usize>())?; |
| 72 | let ptr = HEAP_ALLOCATOR.alloc(malloc_layout(size)?); |
| 73 | let ptr = NonNull::new(ptr)?.cast::<usize>().as_ptr(); |
| 74 | *ptr = size.get(); |
| 75 | NonNull::new(ptr.offset(1)) |
| 76 | } |
| 77 | |
| 78 | fn malloc_layout(size: NonZeroUsize) -> Option<Layout> { |
| 79 | const ALIGN: usize = mem::size_of::<u64>(); |
| 80 | Layout::from_size_align(size.get(), ALIGN).ok() |
| 81 | } |