Skip to content

Commit a85a320

Browse files
committed
Default allocator fix: heap base alignment
1 parent 4bee898 commit a85a320

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

library/std/src/sys/custom/alloc.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::alloc::{GlobalAlloc, Layout, System};
44
use crate::os::custom::alloc::IMPL;
55
use crate::sync::Mutex;
6+
use core::ops::{Deref, DerefMut};
67

78
// Simple implementation of a sequential fit allocator
89
//
@@ -39,11 +40,29 @@ use crate::sync::Mutex;
3940
// note: copy the value of the global "first free slot" variable into the next pointer pair
4041
// c. update the global "first free slot" variable
4142

42-
static mut HEAP: [u8; SIZE_BYTES] = init_heap();
43-
4443
// maximum: 0xffff
4544
// more than 0xffff => will cause infinite loops
4645
const SIZE_BYTES: usize = 4096 * 4;
46+
type HeapArray = [u8; SIZE_BYTES];
47+
48+
// align the heap to a page
49+
#[repr(align(4096))]
50+
struct Heap(HeapArray);
51+
52+
impl Deref for Heap {
53+
type Target = HeapArray;
54+
fn deref(&self) -> &HeapArray {
55+
&self.0
56+
}
57+
}
58+
59+
impl DerefMut for Heap {
60+
fn deref_mut(&mut self) -> &mut HeapArray {
61+
&mut self.0
62+
}
63+
}
64+
65+
static mut HEAP: Heap = Heap(init_heap());
4766
static FIRST_SLOT: Mutex<usize> = Mutex::new(0);
4867

4968
const fn init_heap() -> [u8; SIZE_BYTES] {

0 commit comments

Comments
 (0)