Skip to content

Commit 93b323b

Browse files
committed
Use addr_of_mut! instead of as_ptr() to avoid shared reference to mutable static
1 parent 0e9baba commit 93b323b

File tree

5 files changed

+10
-5
lines changed

5 files changed

+10
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Starting with Rust 1.68, this crate can be used as a global allocator on stable
2222

2323
extern crate alloc;
2424

25+
use core::ptr::addr_of_mut;
2526
use cortex_m_rt::entry;
2627
use embedded_alloc::LlffHeap as Heap;
2728

@@ -35,7 +36,7 @@ fn main() -> ! {
3536
use core::mem::MaybeUninit;
3637
const HEAP_SIZE: usize = 1024;
3738
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
38-
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
39+
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }
3940
}
4041

4142
// now the allocator is ready types like Box, Vec can be used.

examples/allocator_api.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ extern crate alloc;
77
use alloc::vec::Vec;
88
use core::mem::MaybeUninit;
99
use core::panic::PanicInfo;
10+
use core::ptr::addr_of_mut;
1011
use cortex_m_rt::entry;
1112
use embedded_alloc::LlffHeap as Heap;
1213

@@ -20,7 +21,7 @@ fn main() -> ! {
2021
const HEAP_SIZE: usize = 16;
2122
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
2223
let heap: Heap = Heap::empty();
23-
unsafe { heap.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
24+
unsafe { heap.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }
2425

2526
let mut xs = Vec::new_in(heap);
2627
xs.push(1);

examples/global_alloc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate alloc;
55

66
use alloc::vec::Vec;
77
use core::panic::PanicInfo;
8+
use core::ptr::addr_of_mut;
89
use cortex_m_rt::entry;
910
// Linked-List First Fit Heap allocator (feature = "llff")
1011
use embedded_alloc::LlffHeap as Heap;
@@ -21,7 +22,7 @@ fn main() -> ! {
2122
use core::mem::MaybeUninit;
2223
const HEAP_SIZE: usize = 1024;
2324
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
24-
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
25+
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }
2526
}
2627

2728
let mut xs = Vec::new();

examples/llff_integration_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern crate panic_semihosting;
2121

2222
use alloc::vec::Vec;
2323
use core::mem::{size_of, MaybeUninit};
24+
use core::ptr::addr_of_mut;
2425
use cortex_m_rt::entry;
2526
use cortex_m_semihosting::{debug, hprintln};
2627
use embedded_alloc::LlffHeap as Heap;
@@ -66,7 +67,7 @@ fn main() -> ! {
6667
{
6768
const HEAP_SIZE: usize = 1024;
6869
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
69-
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
70+
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }
7071
}
7172

7273
#[allow(clippy::type_complexity)]

examples/tlsf_integration_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern crate panic_semihosting;
2121

2222
use alloc::collections::LinkedList;
2323
use core::mem::MaybeUninit;
24+
use core::ptr::addr_of_mut;
2425
use cortex_m_rt::entry;
2526
use cortex_m_semihosting::{debug, hprintln};
2627
use embedded_alloc::TlsfHeap as Heap;
@@ -83,7 +84,7 @@ fn test_allocator_api() {
8384
fn main() -> ! {
8485
{
8586
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
86-
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
87+
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }
8788
}
8889

8990
#[allow(clippy::type_complexity)]

0 commit comments

Comments
 (0)