Skip to content
Snippets Groups Projects

Fix misaligned pointer dereference in the buddy example file main.rs

2 unresolved threads
@@ -9,7 +9,24 @@ use core::alloc::Layout;
/// The size must be a power of two.
const HEAP_SIZE: usize = 1024 * 1024;
static mut HEAP: [u8; HEAP_SIZE] = [0; HEAP_SIZE];
#[repr(C, align(8))]
struct Heap([u8; HEAP_SIZE]);
impl Heap {
pub const fn new() -> Self {
Self([0; HEAP_SIZE])
}
pub fn as_ptr(&self) -> *const u8 {
self.0.as_ptr()
}
pub fn as_mut_ptr(&mut self) -> *mut u8 {
self.0.as_mut_ptr()
}
}
static mut HEAP: Heap = Heap::new();
fn main() {
println!("HEAP starts at 0x{:x}", unsafe { HEAP.as_ptr() as usize });
Loading