From 54e4b9281fcc425d6b84a14fdd99fd09b895f9ea Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 31 Oct 2019 19:20:49 +0100 Subject: [PATCH] main: rewrap linked_list_allocator --- Cargo.lock | 9 --------- Cargo.toml | 2 +- src/main.rs | 58 ++++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 996bb70..865175b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -24,9 +24,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "linked_list_allocator" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", -] [[package]] name = "managed" @@ -48,11 +45,6 @@ dependencies = [ "managed 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "spin" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "vcell" version = "0.1.0" @@ -88,6 +80,5 @@ dependencies = [ "checksum managed 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fdcec5e97041c7f0f1c5b7d93f12e57293c831c646f4cc7a5db59460c7ea8de6" "checksum r0 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e2a38df5b15c8d5c7e8654189744d8e396bddc18ad48041a500ce52d6948941f" "checksum smoltcp 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fef582369edb298c6c41319a544ca9c4e83622f226055ccfcb35974fbb55ed34" -"checksum spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" "checksum vcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "45c297f0afb6928cd08ab1ff9d95e99392595ea25ae1b5ecf822ff8764e57a0d" "checksum volatile-register 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0d67cb4616d99b940db1d6bd28844ff97108b498a6ca850e5b6191a532063286" diff --git a/Cargo.toml b/Cargo.toml index 7255169..e75b502 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,4 +24,4 @@ volatile-register = "0.2" bit_field = "0.10" compiler_builtins = { version = "~0.1", default-features = false, features = ["mem", "no-lang-items"]} smoltcp = { version = "0.5", default-features = false, features = ["proto-ipv4", "socket-tcp"] } -linked_list_allocator = "0.6" +linked_list_allocator = { version = "0.6", default-features = false } diff --git a/src/main.rs b/src/main.rs index 0ca695f..1df0a28 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,11 @@ // TODO: disallow unused/dead_code when code moves into a lib crate #![allow(dead_code)] +extern crate alloc; +use alloc::{vec, vec::Vec, alloc::Layout}; +use core::alloc::GlobalAlloc; +use core::ptr::NonNull; +use core::cell::RefCell; use core::mem::{uninitialized, transmute}; use r0::zero_bss; use compiler_builtins as _; @@ -16,7 +21,7 @@ use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr}; use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder, EthernetInterface}; use smoltcp::time::Instant; use smoltcp::socket::SocketSet; -use linked_list_allocator::LockedHeap; +use linked_list_allocator::Heap; mod regs; mod cortex_a9; @@ -84,9 +89,6 @@ fn l1_cache_init() { dciall(); } -#[global_allocator] -static ALLOCATOR: LockedHeap = LockedHeap::empty(); - const HWADDR: [u8; 6] = [0, 0x23, 0xde, 0xea, 0xbe, 0xef]; fn main() { @@ -97,33 +99,38 @@ fn main() { ddr.memtest(); unsafe { - ALLOCATOR.lock().init(ddr.ptr::() as usize, ddr.size()); + ALLOCATOR.0.borrow_mut() + .init(ddr.ptr::() as usize, ddr.size()); } let eth = zynq::eth::Eth::default(HWADDR.clone()); println!("Eth on"); - const RX_LEN: usize = 2; - let mut rx_descs: [zynq::eth::rx::DescEntry; RX_LEN] = unsafe { uninitialized() }; - let mut rx_buffers = [[0u8; zynq::eth::MTU]; RX_LEN]; + const RX_LEN: usize = 8; + let mut rx_descs = (0..RX_LEN) + .map(|_| zynq::eth::rx::DescEntry::zeroed()) + .collect::>(); + let mut rx_buffers = vec![[0u8; zynq::eth::MTU]; RX_LEN]; // Number of transmission buffers (minimum is two because with // one, duplicate packet transmission occurs) - const TX_LEN: usize = 2; - let mut tx_descs: [zynq::eth::tx::DescEntry; TX_LEN] = unsafe { uninitialized() }; - let mut tx_buffers = [[0u8; zynq::eth::MTU]; TX_LEN]; + const TX_LEN: usize = 8; + let mut tx_descs = (0..TX_LEN) + .map(|_| zynq::eth::tx::DescEntry::zeroed()) + .collect::>(); + let mut tx_buffers = vec![[0u8; zynq::eth::MTU]; TX_LEN]; let eth = eth.start_rx(&mut rx_descs, &mut rx_buffers); //let mut eth = eth.start_tx(&mut tx_descs, &mut tx_buffers); let mut eth = eth.start_tx( // HACK - unsafe { transmute(tx_descs.as_mut()) }, - unsafe { transmute(tx_buffers.as_mut()) }, + unsafe { transmute(tx_descs.as_mut_slice()) }, + unsafe { transmute(tx_buffers.as_mut_slice()) }, ); let ethernet_addr = EthernetAddress(HWADDR); // IP stack let local_addr = IpAddress::v4(10, 0, 0, 1); let mut ip_addrs = [IpCidr::new(local_addr, 24)]; - let mut neighbor_storage = [None; 16]; + let mut neighbor_storage = vec![None; 256]; let neighbor_cache = NeighborCache::new(&mut neighbor_storage[..]); let mut iface = EthernetInterfaceBuilder::new(&mut eth) .ethernet_addr(ethernet_addr) @@ -184,6 +191,29 @@ fn panic(info: &core::panic::PanicInfo) -> ! { loop {} } +#[global_allocator] +static ALLOCATOR: HeapAlloc = HeapAlloc(RefCell::new(Heap::empty())); + +/// LockedHeap doesn't locking properly +struct HeapAlloc(RefCell); + +/// FIXME: unsound; lock properly +unsafe impl Sync for HeapAlloc {} + +unsafe impl GlobalAlloc for HeapAlloc { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + self.0.borrow_mut() + .allocate_first_fit(layout) + .ok() + .map_or(0 as *mut u8, |allocation| allocation.as_ptr()) + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + self.0.borrow_mut() + .deallocate(NonNull::new_unchecked(ptr), layout) + } +} + #[alloc_error_handler] fn alloc_error(_: core::alloc::Layout) -> ! { panic!("alloc_error")