mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
xous: use the alloc API for allocating memory
Don't rely on the trick where we use a `Vec<u8>`. Call the `alloc` API directly for all packet types. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
c956cb7da5
commit
7d8ff07caf
2 changed files with 23 additions and 58 deletions
|
|
@ -1,4 +1,8 @@
|
||||||
use crate::{Error, MemoryAddress, MemoryFlags, MemoryRange};
|
use crate::{Error, MemoryAddress, MemoryFlags, MemoryRange};
|
||||||
|
const PAGE_SIZE: usize = 4096;
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
|
use alloc::alloc::{alloc, dealloc, Layout};
|
||||||
|
|
||||||
pub fn map_memory_pre(
|
pub fn map_memory_pre(
|
||||||
_phys: &Option<MemoryAddress>,
|
_phys: &Option<MemoryAddress>,
|
||||||
|
|
@ -9,21 +13,21 @@ pub fn map_memory_pre(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused_mut)]
|
|
||||||
pub fn map_memory_post(
|
pub fn map_memory_post(
|
||||||
_phys: Option<MemoryAddress>,
|
_phys: Option<MemoryAddress>,
|
||||||
_virt: Option<MemoryAddress>,
|
_virt: Option<MemoryAddress>,
|
||||||
_size: usize,
|
size: usize,
|
||||||
_flags: MemoryFlags,
|
_flags: MemoryFlags,
|
||||||
mut range: MemoryRange,
|
_range: MemoryRange,
|
||||||
) -> core::result::Result<MemoryRange, Error> {
|
) -> core::result::Result<MemoryRange, Error> {
|
||||||
let data = vec![0u8; range.len()];
|
// let rounded_size = (size + PAGE_SIZE - 1) & !(PAGE_SIZE - 1);
|
||||||
let mut data = std::mem::ManuallyDrop::new(data);
|
let layout = Layout::from_size_align(size, PAGE_SIZE)
|
||||||
data.shrink_to_fit();
|
.unwrap()
|
||||||
assert_eq!(data.len(), data.capacity());
|
.pad_to_align();
|
||||||
let len = data.len();
|
let mem = unsafe { alloc(layout) } as usize;
|
||||||
let addr = data.as_mut_ptr();
|
|
||||||
Ok(unsafe { crate::MemoryRange::new(addr as _, len).unwrap() })
|
// println!("Allocated {} bytes (requested {}) @ {:016x}", rounded_size, size, mem);
|
||||||
|
unsafe { MemoryRange::new(mem, size) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unmap_memory_pre(_range: &MemoryRange) -> core::result::Result<(), Error> {
|
pub fn unmap_memory_pre(_range: &MemoryRange) -> core::result::Result<(), Error> {
|
||||||
|
|
@ -31,48 +35,12 @@ pub fn unmap_memory_pre(_range: &MemoryRange) -> core::result::Result<(), Error>
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unmap_memory_post(range: MemoryRange) -> core::result::Result<(), Error> {
|
pub fn unmap_memory_post(range: MemoryRange) -> core::result::Result<(), Error> {
|
||||||
let _v = unsafe { std::vec::Vec::from_raw_parts(range.as_mut_ptr(), range.len(), range.len()) };
|
// println!("Request to free {} bytes @ {:016x}", range.len(), range.as_ptr() as usize);
|
||||||
|
// let rounded_size = (range.len() + 4095) / 4096;
|
||||||
|
let layout = Layout::from_size_align(range.len(), PAGE_SIZE)
|
||||||
|
.unwrap()
|
||||||
|
.pad_to_align();
|
||||||
|
let ptr = range.as_mut_ptr();
|
||||||
|
unsafe { dealloc(ptr, layout) };
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ideally we'd use the `alloc` API directly, however this doesn't work for unknown reasons.
|
|
||||||
// The code is left here as an exercise to someone who wants to figure out why.
|
|
||||||
|
|
||||||
// use crate::{Error, MemoryAddress, MemoryFlags, MemoryRange};
|
|
||||||
|
|
||||||
// extern crate alloc;
|
|
||||||
// use alloc::alloc::{alloc, dealloc, Layout};
|
|
||||||
|
|
||||||
// pub fn map_memory_pre(
|
|
||||||
// _phys: &Option<MemoryAddress>,
|
|
||||||
// _virt: &Option<MemoryAddress>,
|
|
||||||
// _size: usize,
|
|
||||||
// _flags: MemoryFlags,
|
|
||||||
// ) -> core::result::Result<(), Error> {
|
|
||||||
// Ok(())
|
|
||||||
// }
|
|
||||||
|
|
||||||
// pub fn map_memory_post(
|
|
||||||
// _phys: Option<MemoryAddress>,
|
|
||||||
// _virt: Option<MemoryAddress>,
|
|
||||||
// size: usize,
|
|
||||||
// _flags: MemoryFlags,
|
|
||||||
// _range: MemoryRange,
|
|
||||||
// ) -> core::result::Result<MemoryRange, Error> {
|
|
||||||
// let rounded_size = (size + 4095) / 4096;
|
|
||||||
|
|
||||||
// let layout = Layout::from_size_align(rounded_size, 4096).unwrap();
|
|
||||||
// unsafe { MemoryRange::new(alloc(layout) as usize, size) }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// pub fn unmap_memory_pre(_range: &MemoryRange) -> core::result::Result<(), Error> {
|
|
||||||
// Ok(())
|
|
||||||
// }
|
|
||||||
|
|
||||||
// pub fn unmap_memory_post(range: MemoryRange) -> core::result::Result<(), Error> {
|
|
||||||
// // let rounded_size = (range.len() + 4095) / 4096;
|
|
||||||
// // let layout = Layout::from_size_align(rounded_size, 4096).unwrap();
|
|
||||||
// // let ptr = range.as_mut_ptr();
|
|
||||||
// // unsafe { dealloc(ptr, layout) };
|
|
||||||
// Ok(())
|
|
||||||
// }
|
|
||||||
|
|
|
||||||
|
|
@ -425,12 +425,9 @@ fn read_next_syscall_result(
|
||||||
mem::unmap_memory_post(mem).unwrap();
|
mem::unmap_memory_post(mem).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we're returning memory to the Server, then reconstitute the buffer we just passed,
|
// If we're returning memory to the Server, then free it here
|
||||||
// and Drop it so it can be freed.
|
|
||||||
if kind == CallMemoryKind::ReturnMemory {
|
if kind == CallMemoryKind::ReturnMemory {
|
||||||
let rebuilt =
|
mem::unmap_memory_post(mem).unwrap();
|
||||||
unsafe { Vec::from_raw_parts(mem.as_mut_ptr(), mem.len(), mem.len()) };
|
|
||||||
drop(rebuilt);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (msg_thread_id, response);
|
return (msg_thread_id, response);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue