Use MaybeUninit for uninitialized

toolchain_update
Jiajie Chen 6 years ago
parent e63f11d199
commit c67f00d7ca

@ -109,8 +109,8 @@ impl<T: PageTable> CowExt<T> {
self.rc_map.write_decrease(&frame); self.rc_map.write_decrease(&frame);
return true; return true;
} }
use core::mem::uninitialized; use core::mem::MaybeUninit;
let mut temp_data: [u8; PAGE_SIZE] = unsafe { uninitialized() }; let mut temp_data: [u8; PAGE_SIZE] = unsafe { MaybeUninit::uninitialized().into_initialized() };
temp_data[..].copy_from_slice(self.get_page_slice_mut(addr)); temp_data[..].copy_from_slice(self.get_page_slice_mut(addr));
self.unmap_shared(addr); self.unmap_shared(addr);

@ -1,6 +1,7 @@
#![cfg_attr(not(test), no_std)] #![cfg_attr(not(test), no_std)]
#![feature(alloc)] #![feature(alloc)]
#![feature(nll)] #![feature(nll)]
#![feature(maybe_uninit)]
// import macros from log // import macros from log
use log::*; use log::*;

@ -144,10 +144,10 @@ impl MockPageTable {
** @retval MockPageTable the mock page table created ** @retval MockPageTable the mock page table created
*/ */
pub fn new() -> Self { pub fn new() -> Self {
use core::mem::uninitialized; use core::mem::MaybeUninit;
MockPageTable { MockPageTable {
entries: [MockEntry::default(); PAGE_COUNT], entries: [MockEntry::default(); PAGE_COUNT],
data: unsafe { uninitialized() }, data: unsafe { MaybeUninit::uninitialized().into_initialized() },
page_fault_handler: None, page_fault_handler: None,
} }
} }

@ -5,7 +5,7 @@
use super::Swapper; use super::Swapper;
use alloc::collections::BTreeMap; use alloc::collections::BTreeMap;
use core::mem::uninitialized; use core::mem::MaybeUninit;
const PAGE_SIZE: usize = 4096; const PAGE_SIZE: usize = 4096;
@ -17,7 +17,7 @@ pub struct MockSwapper {
impl Swapper for MockSwapper { impl Swapper for MockSwapper {
fn swap_out(&mut self, data: &[u8]) -> Result<usize, ()> { fn swap_out(&mut self, data: &[u8]) -> Result<usize, ()> {
let id = self.alloc_id(); let id = self.alloc_id();
let mut slice: [u8; PAGE_SIZE] = unsafe { uninitialized() }; let mut slice: [u8; PAGE_SIZE] = unsafe { MaybeUninit::uninitialized().into_initialized() };
slice.copy_from_slice(data); slice.copy_from_slice(data);
self.map.insert(id, slice); self.map.insert(id, slice);
Ok(id) Ok(id)
@ -27,7 +27,7 @@ impl Swapper for MockSwapper {
if !self.map.contains_key(&token) { if !self.map.contains_key(&token) {
return Err(()); return Err(());
} }
let mut slice: [u8; PAGE_SIZE] = unsafe { uninitialized() }; let mut slice: [u8; PAGE_SIZE] = unsafe { MaybeUninit::uninitialized().into_initialized() };
slice.copy_from_slice(data); slice.copy_from_slice(data);
self.map.insert(token, slice); self.map.insert(token, slice);
Ok(()) Ok(())
@ -64,8 +64,8 @@ mod test {
#[test] #[test]
fn swap_out_in() { fn swap_out_in() {
let mut swapper = MockSwapper::default(); let mut swapper = MockSwapper::default();
let mut data: [u8; 4096] = unsafe { uninitialized() }; let mut data: [u8; 4096] = unsafe { MaybeUninit::uninitialized().into_initialized() };
let data1: [u8; 4096] = unsafe { uninitialized() }; let data1: [u8; 4096] = unsafe { MaybeUninit::uninitialized().into_initialized() };
let token = swapper.swap_out(&data1).unwrap(); let token = swapper.swap_out(&data1).unwrap();
swapper.swap_in(token, &mut data).unwrap(); swapper.swap_in(token, &mut data).unwrap();
assert_data_eq(&data, &data1); assert_data_eq(&data, &data1);
@ -74,9 +74,9 @@ mod test {
#[test] #[test]
fn swap_update() { fn swap_update() {
let mut swapper = MockSwapper::default(); let mut swapper = MockSwapper::default();
let mut data: [u8; 4096] = unsafe { uninitialized() }; let mut data: [u8; 4096] = unsafe { MaybeUninit::uninitialized().into_initialized() };
let data1: [u8; 4096] = unsafe { uninitialized() }; let data1: [u8; 4096] = unsafe { MaybeUninit::uninitialized().into_initialized() };
let data2: [u8; 4096] = unsafe { uninitialized() }; let data2: [u8; 4096] = unsafe { MaybeUninit::uninitialized().into_initialized() };
let token = swapper.swap_out(&data1).unwrap(); let token = swapper.swap_out(&data1).unwrap();
swapper.swap_update(token, &data2).unwrap(); swapper.swap_update(token, &data2).unwrap();
swapper.swap_in(token, &mut data).unwrap(); swapper.swap_in(token, &mut data).unwrap();
@ -86,7 +86,7 @@ mod test {
#[test] #[test]
fn invalid_token() { fn invalid_token() {
let mut swapper = MockSwapper::default(); let mut swapper = MockSwapper::default();
let mut data: [u8; 4096] = unsafe { uninitialized() }; let mut data: [u8; 4096] = unsafe { MaybeUninit::uninitialized().into_initialized() };
assert_eq!(swapper.swap_in(0, &mut data), Err(())); assert_eq!(swapper.swap_in(0, &mut data), Err(()));
} }
} }

@ -212,7 +212,7 @@ impl PageTableImpl {
PageTableImpl { PageTableImpl {
page_table: MappedPageTable::new(table, frame_to_page_table), page_table: MappedPageTable::new(table, frame_to_page_table),
root_frame: frame, root_frame: frame,
entry: core::mem::uninitialized(), entry: core::mem::MaybeUninit::uninitialized().into_initialized(),
} }
} }
} }
@ -227,7 +227,7 @@ impl PageTableExt for PageTableImpl {
PageTableImpl { PageTableImpl {
page_table: MappedPageTable::new(table, frame_to_page_table), page_table: MappedPageTable::new(table, frame_to_page_table),
root_frame: frame, root_frame: frame,
entry: core::mem::uninitialized(), entry: core::mem::MaybeUninit::uninitialized().into_initialized(),
} }
} }
} }

@ -155,7 +155,7 @@ impl PageTableImpl {
PageTableImpl { PageTableImpl {
page_table: TwoLevelPageTable::new(table), page_table: TwoLevelPageTable::new(table),
root_frame: frame, root_frame: frame,
entry: unsafe { core::mem::uninitialized() }, entry: unsafe { core::mem::MaybeUninit::uninitialized().into_initialized() },
} }
} }
} }
@ -171,7 +171,7 @@ impl PageTableExt for PageTableImpl {
PageTableImpl { PageTableImpl {
page_table: TwoLevelPageTable::new(table), page_table: TwoLevelPageTable::new(table),
root_frame: frame, root_frame: frame,
entry: unsafe { core::mem::uninitialized() }, entry: unsafe { core::mem::MaybeUninit::uninitialized().into_initialized() },
} }
} }

@ -202,7 +202,7 @@ impl PageTableImpl {
let table = unsafe { &mut *frame_to_page_table(frame) }; let table = unsafe { &mut *frame_to_page_table(frame) };
PageTableImpl( PageTableImpl(
MappedPageTable::new(table, frame_to_page_table), MappedPageTable::new(table, frame_to_page_table),
core::mem::uninitialized(), core::mem::MaybeUninit::uninitialized().into_initialized()(),
frame, frame,
) )
} }
@ -217,7 +217,7 @@ impl PageTableExt for PageTableImpl {
unsafe { unsafe {
PageTableImpl( PageTableImpl(
MappedPageTable::new(table, frame_to_page_table), MappedPageTable::new(table, frame_to_page_table),
core::mem::uninitialized(), core::mem::MaybeUninit::uninitialized().into_initialized()(),
frame, frame,
) )
} }

@ -7,6 +7,7 @@
#![feature(panic_info_message)] #![feature(panic_info_message)]
#![feature(global_asm)] #![feature(global_asm)]
#![feature(fnbox)] #![feature(fnbox)]
#![feature(maybe_uninit)]
#![deny(unused_must_use)] #![deny(unused_must_use)]
#![no_std] #![no_std]

@ -21,7 +21,7 @@ use crate::sync::{Condvar, SpinNoIrqLock as Mutex};
use super::abi::{self, ProcInitInfo}; use super::abi::{self, ProcInitInfo};
use crate::processor; use crate::processor;
use core::mem::uninitialized; use core::mem::MaybeUninit;
use rcore_fs::vfs::INode; use rcore_fs::vfs::INode;
pub struct Thread { pub struct Thread {
@ -103,7 +103,7 @@ impl Thread {
Box::new(Thread { Box::new(Thread {
context: Context::null(), context: Context::null(),
// safety: other fields will never be used // safety: other fields will never be used
..core::mem::uninitialized() ..core::mem::MaybeUninit::uninitialized().into_initialized()
}) })
} }
@ -146,7 +146,7 @@ impl Thread {
) -> Result<(MemorySet, usize, usize), &'static str> { ) -> Result<(MemorySet, usize, usize), &'static str> {
// Read ELF header // Read ELF header
// 0x3c0: magic number from ld-musl.so // 0x3c0: magic number from ld-musl.so
let mut data: [u8; 0x3c0] = unsafe { uninitialized() }; let mut data: [u8; 0x3c0] = unsafe { MaybeUninit::uninitialized().into_initialized() };
inode inode
.read_at(0, &mut data) .read_at(0, &mut data)
.map_err(|_| "failed to read from INode")?; .map_err(|_| "failed to read from INode")?;

Loading…
Cancel
Save