runtime: get rid of config_dummy.rs. NFC.

Use the same strategy as elsewhere.
This commit is contained in:
whitequark 2017-10-02 03:20:50 +00:00
parent 73c76ebb9a
commit fd07c3f4d7
3 changed files with 189 additions and 183 deletions

View File

@ -1,12 +1,14 @@
use core::str;
use std::btree_map::BTreeMap;
use byteorder::{ByteOrder, BigEndian};
use board::{mem, csr, cache, spiflash};
#[cfg(has_spiflash)]
mod imp {
use core::str;
use std::btree_map::BTreeMap;
use byteorder::{ByteOrder, BigEndian};
use board::{mem, csr, cache, spiflash};
const ADDR: usize = mem::FLASH_BOOT_ADDRESS + 0x80000 /* max runtime size */;
const SIZE: usize = csr::CONFIG_SPIFLASH_SECTOR_SIZE as usize;
const ADDR: usize = mem::FLASH_BOOT_ADDRESS + 0x80000 /* max runtime size */;
const SIZE: usize = csr::CONFIG_SPIFLASH_SECTOR_SIZE as usize;
mod lock {
mod lock {
use core::slice;
use core::sync::atomic::{AtomicUsize, Ordering};
@ -33,22 +35,22 @@ mod lock {
LOCKED.store(0, Ordering::SeqCst)
}
}
}
}
pub use self::lock::Lock;
use self::lock::Lock;
struct Iter<'a> {
struct Iter<'a> {
data: &'a [u8],
offset: usize
}
}
impl<'a> Iter<'a> {
impl<'a> Iter<'a> {
fn new(data: &'a [u8]) -> Iter<'a> {
Iter { data: data, offset: 0 }
}
}
}
impl<'a> Iterator for Iter<'a> {
impl<'a> Iterator for Iter<'a> {
type Item = Result<(&'a [u8], &'a [u8]), ()>;
fn next(&mut self) -> Option<Self::Item> {
@ -82,9 +84,9 @@ impl<'a> Iterator for Iter<'a> {
}
}
}
}
}
pub fn read<F: FnOnce(Result<&[u8], ()>) -> R, R>(key: &str, f: F) -> R {
pub fn read<F: FnOnce(Result<&[u8], ()>) -> R, R>(key: &str, f: F) -> R {
f(Lock::take().and_then(|lock| {
let mut iter = Iter::new(lock.data());
let mut value = &[][..];
@ -97,15 +99,15 @@ pub fn read<F: FnOnce(Result<&[u8], ()>) -> R, R>(key: &str, f: F) -> R {
}
Ok(value)
}))
}
}
pub fn read_str<F: FnOnce(Result<&str, ()>) -> R, R>(key: &str, f: F) -> R {
pub fn read_str<F: FnOnce(Result<&str, ()>) -> R, R>(key: &str, f: F) -> R {
read(key, |result| {
f(result.and_then(|value| str::from_utf8(value).map_err(|_| ())))
})
}
}
fn append_at(mut offset: usize, key: &[u8], value: &[u8]) -> Result<usize, ()> {
fn append_at(mut offset: usize, key: &[u8], value: &[u8]) -> Result<usize, ()> {
let record_size = 4 + key.len() + 1 + value.len();
if offset + record_size > SIZE {
return Err(())
@ -128,9 +130,9 @@ fn append_at(mut offset: usize, key: &[u8], value: &[u8]) -> Result<usize, ()> {
cache::flush_l2_cache();
Ok(offset)
}
}
fn compact() -> Result<(), ()> {
fn compact() -> Result<(), ()> {
let lock = Lock::take()?;
let mut items = BTreeMap::new();
@ -150,9 +152,9 @@ fn compact() -> Result<(), ()> {
offset = append_at(offset, key, value)?;
}
Ok(())
}
}
fn append(key: &str, value: &[u8]) -> Result<(), ()> {
fn append(key: &str, value: &[u8]) -> Result<(), ()> {
let lock = Lock::take()?;
let free_offset = {
@ -165,9 +167,9 @@ fn append(key: &str, value: &[u8]) -> Result<(), ()> {
append_at(free_offset, key.as_bytes(), value)?;
Ok(())
}
}
pub fn write(key: &str, value: &[u8]) -> Result<(), ()> {
pub fn write(key: &str, value: &[u8]) -> Result<(), ()> {
match append(key, value) {
Ok(()) => (),
Err(()) => {
@ -176,17 +178,43 @@ pub fn write(key: &str, value: &[u8]) -> Result<(), ()> {
}
}
Ok(())
}
}
pub fn remove(key: &str) -> Result<(), ()> {
pub fn remove(key: &str) -> Result<(), ()> {
write(key, &[])
}
}
pub fn erase() -> Result<(), ()> {
pub fn erase() -> Result<(), ()> {
let _lock = Lock::take()?;
spiflash::erase_sector(ADDR);
cache::flush_l2_cache();
Ok(())
}
}
#[cfg(not(has_spiflash))]
mod imp {
pub fn read<F: FnOnce(Result<&[u8], ()>) -> R, R>(_key: &str, f: F) -> R {
f(Err(()))
}
pub fn read_str<F: FnOnce(Result<&str, ()>) -> R, R>(_key: &str, f: F) -> R {
f(Err(()))
}
pub fn write(_key: &str, _value: &[u8]) -> Result<(), ()> {
Err(())
}
pub fn remove(_key: &str) -> Result<(), ()> {
Err(())
}
pub fn erase() -> Result<(), ()> {
Err(())
}
}
pub use self::imp::*;

View File

@ -1,19 +0,0 @@
pub fn read<F: FnOnce(Result<&[u8], ()>) -> R, R>(_key: &str, f: F) -> R {
f(Err(()))
}
pub fn read_str<F: FnOnce(Result<&str, ()>) -> R, R>(_key: &str, f: F) -> R {
f(Err(()))
}
pub fn write(_key: &str, _value: &[u8]) -> Result<(), ()> {
Err(())
}
pub fn remove(_key: &str) -> Result<(), ()> {
Err(())
}
pub fn erase() -> Result<(), ()> {
Err(())
}

View File

@ -35,10 +35,7 @@ macro_rules! borrow_mut {
})
}
#[cfg(has_spiflash)]
mod config;
#[cfg(not(has_spiflash))]
#[path="config_dummy.rs"] mod config;
mod ethmac;
mod rtio_mgt;