Skip to content

Commit

Permalink
Use zeroed_box() when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
kelpsyberry committed Mar 31, 2024
1 parent 618a418 commit ff8d5c7
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 54 deletions.
4 changes: 2 additions & 2 deletions core/src/ds_slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub mod spi;
use crate::{
cpu::{arm7, arm9, Engine, Schedule as _},
emu::{Emu, Timestamp},
utils::{mem_prelude::*, schedule::RawTimestamp, Savestate},
utils::{mem_prelude::*, schedule::RawTimestamp, zeroed_box, Savestate},
};

proc_bitfield::bitfield! {
Expand Down Expand Up @@ -127,7 +127,7 @@ impl DsSlot {
rom_cmd: Bytes::new([0; 8]),
arm7_access: false,
arm9_access: true,
rom_output_buffer: unsafe { Box::new_zeroed().assume_init() },
rom_output_buffer: zeroed_box(),
rom_output_len: RomOutputLen::new(0),
rom_output_pos: RomOutputPos::new(0),
rom_data_out: 0,
Expand Down
4 changes: 2 additions & 2 deletions core/src/flash.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
utils::{mem_prelude::*, Savestate},
utils::{mem_prelude::*, zeroed_box, Savestate},
SaveContents,
};

Expand Down Expand Up @@ -72,7 +72,7 @@ impl Flash {
status: Status(0),
powered_down: false,

write_buffer: unsafe { Box::new_zeroed().assume_init() },
write_buffer: zeroed_box(),
write_buffer_end: 0,
write_buffer_len: 0,

Expand Down
4 changes: 2 additions & 2 deletions core/src/spi/tsc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::Power;
use crate::{
emu::{input, Timestamp},
utils::Savestate,
utils::{zeroed_box, Savestate},
};

proc_bitfield::bitfield! {
Expand Down Expand Up @@ -33,7 +33,7 @@ impl MicData {
MicData {
backend,
read_in_current_frame: false,
samples: unsafe { Box::new_zeroed().assume_init() },
samples: zeroed_box(),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/wifi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod io;

use crate::utils::{Bytes, Savestate};
use crate::utils::{zeroed_box, Bytes, Savestate};

#[derive(Savestate)]
pub struct WiFi {
Expand All @@ -11,7 +11,7 @@ pub struct WiFi {

impl WiFi {
pub(crate) fn new() -> Self {
let mut mmio = unsafe { Box::<Bytes<0x1000>>::new_zeroed().assume_init() };
let mut mmio = zeroed_box::<Bytes<0x1000>>();
mmio[0x3D] = 0x02;

let mut bb_regs = [0; 0x100];
Expand All @@ -22,7 +22,7 @@ impl WiFi {

WiFi {
mmio,
ram: unsafe { Box::new_zeroed().assume_init() },
ram: zeroed_box(),
bb_regs,
}
}
Expand Down
10 changes: 3 additions & 7 deletions frontend/desktop/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use dust_core::{
audio::ChannelInterpMethod as AudioChannelInterpMethod,
cpu::{arm7, arm9},
spi::firmware,
utils::{BoxedByteSlice, Bytes},
utils::{zeroed_box, BoxedByteSlice, Bytes},
Model,
};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -747,9 +747,7 @@ impl Launch {
open_file!(&config.sys_paths.get().arm7_bios, Arm7Bios, |file| {
let len = file.metadata()?.len();
if len == arm7::BIOS_SIZE as u64 {
let mut buf = unsafe {
Box::<Bytes<{ arm7::BIOS_SIZE }>>::new_zeroed().assume_init()
};
let mut buf = zeroed_box::<Bytes<{ arm7::BIOS_SIZE }>>();
file.read_exact(&mut **buf)?;
Some(buf)
} else {
Expand All @@ -768,9 +766,7 @@ impl Launch {
open_file!(&config.sys_paths.get().arm9_bios, Arm9Bios, |file| {
let len = file.metadata()?.len();
if len == arm9::BIOS_SIZE as u64 {
let mut buf = unsafe {
Box::<Bytes<{ arm9::BIOS_SIZE }>>::new_zeroed().assume_init()
};
let mut buf = zeroed_box::<Bytes<{ arm9::BIOS_SIZE }>>();
file.read_exact(&mut **buf)?;
Some(buf)
} else {
Expand Down
54 changes: 25 additions & 29 deletions frontend/desktop/src/debug_views/bg_maps_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use dust_core::{
engine_2d::{self, BgIndex, Role},
vram::Vram,
},
utils::mem_prelude::*,
utils::{mem_prelude::*, zeroed_box},
};
use imgui::{Image, MouseButton, SliderFlags, StyleColor, TextureId, WindowHoveredFlags};
use std::slice;
Expand Down Expand Up @@ -81,19 +81,17 @@ impl BgMapData {

impl Default for BgMapData {
fn default() -> Self {
unsafe {
BgMapData {
bgs: Self::default_bgs(),
selection: None,
cur_bg: BgData {
display_mode: BgDisplayMode::Text16,
uses_ext_palettes: false,
size: [128; 2],
},
tiles: Box::new_zeroed().assume_init(),
tile_bitmap_data: Box::new_zeroed().assume_init(),
palette: Box::new_zeroed().assume_init(),
}
BgMapData {
bgs: Self::default_bgs(),
selection: None,
cur_bg: BgData {
display_mode: BgDisplayMode::Text16,
uses_ext_palettes: false,
size: [128; 2],
},
tiles: zeroed_box(),
tile_bitmap_data: zeroed_box(),
palette: zeroed_box(),
}
}
}
Expand Down Expand Up @@ -411,21 +409,19 @@ impl View for BgMaps2d {
..Default::default()
},
);
unsafe {
BgMaps2d {
cur_selection: Selection {
engine: Engine2d::A,
bg_index: BgIndex::new(0),
use_ext_palettes: None,
display_mode: None,
},
tex_id,
show_transparency_checkerboard: true,
show_grid_lines: true,
palette_buffer: Box::new_zeroed().assume_init(),
pixel_buffer: Box::new_zeroed().assume_init(),
data: BgMapData::default(),
}
BgMaps2d {
cur_selection: Selection {
engine: Engine2d::A,
bg_index: BgIndex::new(0),
use_ext_palettes: None,
display_mode: None,
},
tex_id,
show_transparency_checkerboard: true,
show_grid_lines: true,
palette_buffer: zeroed_box(),
pixel_buffer: zeroed_box(),
data: BgMapData::default(),
}
}

Expand Down
8 changes: 6 additions & 2 deletions frontend/desktop/src/debug_views/palettes_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use super::{
BaseView, FrameDataSlot, InstanceableEmuState, InstanceableView, Messages, View,
};
use crate::ui::{utils::combo_value, window::Window};
use dust_core::{cpu, emu::Emu, utils::mem_prelude::*};
use dust_core::{
cpu,
emu::Emu,
utils::{mem_prelude::*, zeroed_box},
};
use imgui::{StyleVar, TableFlags, Ui};

#[derive(Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -49,7 +53,7 @@ impl Default for PaletteData {
fn default() -> Self {
PaletteData {
selection: None,
data: unsafe { Box::new_zeroed().assume_init() },
data: zeroed_box(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/desktop/src/emu/ds_slot_rom.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dust_core::{
ds_slot::rom::{self, Contents},
utils::mem_prelude::*,
utils::{mem_prelude::*, zeroed_box},
Model,
};
use std::{
Expand Down Expand Up @@ -33,7 +33,7 @@ impl Contents for File {
fn secure_area_mut(&mut self) -> Option<&mut [u8]> {
self.secure_area
.get_or_insert_with(|| {
let mut buf = unsafe { Box::<Bytes<0x800>>::new_zeroed().assume_init() };
let mut buf = zeroed_box::<Bytes<0x800>>();
self.file
.seek(SeekFrom::Start(self.secure_area_start as u64))
.and_then(|_| self.file.read_exact(&mut **buf))
Expand Down
4 changes: 2 additions & 2 deletions frontend/desktop/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::{
use dust_core::{
ds_slot::rom::Contents,
gpu::{engine_2d, engine_3d, Framebuffer, SCREEN_HEIGHT, SCREEN_WIDTH},
utils::zeroed_box,
};
use emu_utils::triple_buffer;
#[cfg(feature = "logging")]
Expand Down Expand Up @@ -806,8 +807,7 @@ impl FbTexture {
}

fn clear(&self, window: &window::Window) {
let mut data =
unsafe { Box::<[u8; SCREEN_WIDTH * SCREEN_HEIGHT * 8]>::new_zeroed().assume_init() };
let mut data = zeroed_box::<[u8; SCREEN_WIDTH * SCREEN_HEIGHT * 8]>();
for i in (3..data.len()).step_by(4) {
data[i] = 0xFF;
}
Expand Down
6 changes: 3 additions & 3 deletions frontend/web/crate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use dust_core::{
gpu::{SCREEN_HEIGHT, SCREEN_WIDTH},
rtc,
spi::firmware,
utils::{BoxedByteSlice, Bytes},
utils::{zeroed_box, BoxedByteSlice, Bytes},
Model, SaveContents,
};
use js_sys::{Function, Uint32Array, Uint8Array};
Expand Down Expand Up @@ -206,13 +206,13 @@ pub fn create_emu_state(
let logger = slog::Logger::root(console_log::Console::new(), slog::o!());

let arm7_bios = arm7_bios_arr.map(|arr| {
let mut buf = unsafe { Box::<Bytes<{ arm7::BIOS_SIZE }>>::new_zeroed().assume_init() };
let mut buf = zeroed_box::<Bytes<{ arm7::BIOS_SIZE }>>();
arr.copy_to(&mut **buf);
buf
});

let arm9_bios = arm9_bios_arr.map(|arr| {
let mut buf = unsafe { Box::<Bytes<{ arm9::BIOS_SIZE }>>::new_zeroed().assume_init() };
let mut buf = zeroed_box::<Bytes<{ arm9::BIOS_SIZE }>>();
arr.copy_to(&mut **buf);
buf
});
Expand Down

0 comments on commit ff8d5c7

Please sign in to comment.