mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-20 09:03:28 +00:00
ensure all allocs meet the 16-byte alignment criteria
This commit is contained in:
parent
c12d9cbe8f
commit
322de782d7
1 changed files with 15 additions and 7 deletions
|
|
@ -163,16 +163,23 @@ fn xmain() -> ! {
|
||||||
xous::terminate_process(0)
|
xous::terminate_process(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) const START_OFFSET: u32 = 0x0048 + 8; // align spinal free space to 16-byte boundary
|
||||||
|
|
||||||
pub(crate) const START_OFFSET: u32 = 0x0048;
|
|
||||||
pub(crate) const END_OFFSET: u32 = 0xFF00;
|
pub(crate) const END_OFFSET: u32 = 0xFF00;
|
||||||
|
/// USB endpoint allocator. The SpinalHDL USB controller appears as a block of
|
||||||
|
/// unstructured memory to the host. You can specify pointers into the memory with
|
||||||
|
/// an offset and length to define where various USB descriptors should be placed.
|
||||||
|
/// This allocator manages that space.
|
||||||
|
///
|
||||||
|
/// Note that all allocations must be aligned to 16-byte boundaries. This is a restriction
|
||||||
|
/// of the USB core.
|
||||||
pub(crate) fn alloc_inner(allocs: &mut BTreeMap<u32, u32>, requested: u32) -> Option<u32> {
|
pub(crate) fn alloc_inner(allocs: &mut BTreeMap<u32, u32>, requested: u32) -> Option<u32> {
|
||||||
if requested == 0 {
|
if requested == 0 {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let mut alloc_offset = START_OFFSET;
|
let mut alloc_offset = START_OFFSET;
|
||||||
for (&offset, &length) in allocs.iter() {
|
for (&offset, &length) in allocs.iter() {
|
||||||
|
// round length up to the nearest 16-byte increment
|
||||||
|
let length = if length & 0xF == 0 { length } else { (length + 16) & !0xF };
|
||||||
// println!("aoff: {}, cur: {}+{}", alloc_offset, offset, length);
|
// println!("aoff: {}, cur: {}+{}", alloc_offset, offset, length);
|
||||||
assert!(offset >= alloc_offset, "allocated regions overlap");
|
assert!(offset >= alloc_offset, "allocated regions overlap");
|
||||||
if offset > alloc_offset {
|
if offset > alloc_offset {
|
||||||
|
|
@ -251,16 +258,16 @@ mod tests {
|
||||||
for _ in 0..10240 {
|
for _ in 0..10240 {
|
||||||
if rng.next_u32() % 2 == 0 {
|
if rng.next_u32() % 2 == 0 {
|
||||||
if tracker.len() > 0 {
|
if tracker.len() > 0 {
|
||||||
println!("tracker: {:?}", tracker);
|
//println!("tracker: {:?}", tracker);
|
||||||
let index = tracker.remove((rng.next_u32() % tracker.len() as u32) as usize);
|
let index = tracker.remove((rng.next_u32() % tracker.len() as u32) as usize);
|
||||||
println!("removing: {} of {}", index, tracker.len());
|
//println!("removing: {} of {}", index, tracker.len());
|
||||||
assert_eq!(dealloc_inner(&mut allocs, index), true);
|
assert_eq!(dealloc_inner(&mut allocs, index), true);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let req = rng.next_u32() % 256;
|
let req = rng.next_u32() % 256;
|
||||||
if let Some(offset) = alloc_inner(&mut allocs, req) {
|
if let Some(offset) = alloc_inner(&mut allocs, req) {
|
||||||
println!("tracker: {:?}", tracker);
|
//println!("tracker: {:?}", tracker);
|
||||||
println!("alloc: {}+{}", offset, req);
|
//println!("alloc: {}+{}", offset, req);
|
||||||
tracker.push(offset);
|
tracker.push(offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -271,6 +278,7 @@ mod tests {
|
||||||
println!("after random test:");
|
println!("after random test:");
|
||||||
for (&offset, &len) in allocs.iter() {
|
for (&offset, &len) in allocs.iter() {
|
||||||
assert!(offset >= last_alloc, "new offset is inside last allocation!");
|
assert!(offset >= last_alloc, "new offset is inside last allocation!");
|
||||||
|
assert!(offset & 0xF == 0, "misaligned allocation detected");
|
||||||
println!("{}-{}({})", offset, offset+len, len);
|
println!("{}-{}({})", offset, offset+len, len);
|
||||||
last_alloc = offset + len;
|
last_alloc = offset + len;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue