From 5f0e4a7605c00d2c5d6d3798209672b5f151ffe5 Mon Sep 17 00:00:00 2001 From: Mel Henning Date: Mon, 23 Dec 2024 16:08:16 -0500 Subject: [PATCH] nak,nir: Stop using std::mem::zeroed() We can replace all of these with safe alternatives if we ask bindgen for implementations of Default. Reviewed-by: Faith Ekstrand Part-of: --- src/compiler/rust/memstream.rs | 2 +- src/compiler/rust/meson.build | 1 + src/nouveau/compiler/meson.build | 1 + src/nouveau/compiler/nak/api.rs | 6 +++--- src/nouveau/compiler/nak/hw_runner.rs | 4 ++-- 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/compiler/rust/memstream.rs b/src/compiler/rust/memstream.rs index 00adaa71517..622f55b4b47 100644 --- a/src/compiler/rust/memstream.rs +++ b/src/compiler/rust/memstream.rs @@ -23,7 +23,7 @@ pub struct MemStream(Pin>); impl MemStream { pub fn new() -> io::Result { let mut stream_impl = Box::pin(MemStreamImpl { - stream: unsafe { std::mem::zeroed() }, + stream: Default::default(), buffer: std::ptr::null_mut(), buffer_size: 0, _pin: PhantomPinned, diff --git a/src/compiler/rust/meson.build b/src/compiler/rust/meson.build index 2f4a4728d62..98f1a93291a 100644 --- a/src/compiler/rust/meson.build +++ b/src/compiler/rust/meson.build @@ -47,6 +47,7 @@ _compiler_bindgen_args = [ '--allowlist-function', 'u_memstream.*', '--allowlist-type', 'u_memstream', '--no-prepend-enum-name', + '--with-derive-default', ] foreach type : _compiler_binding_types diff --git a/src/nouveau/compiler/meson.build b/src/nouveau/compiler/meson.build index 7a4c60b18b2..76b15edc073 100644 --- a/src/nouveau/compiler/meson.build +++ b/src/nouveau/compiler/meson.build @@ -66,6 +66,7 @@ _nak_bindings_rs = rust.bindgen( '--allowlist-function', 'nak_.*', '--allowlist-function', 'nouveau_ws_.*', '--no-prepend-enum-name', + '--with-derive-default', ], dependencies : [ dep_libdrm, diff --git a/src/nouveau/compiler/nak/api.rs b/src/nouveau/compiler/nak/api.rs index 3d41a9c59c9..9b381ab0eb6 100644 --- a/src/nouveau/compiler/nak/api.rs +++ b/src/nouveau/compiler/nak/api.rs @@ -108,7 +108,7 @@ pub extern "C" fn nak_should_print_nir() -> bool { } fn nir_options(dev: &nv_device_info) -> nir_shader_compiler_options { - let mut op: nir_shader_compiler_options = unsafe { std::mem::zeroed() }; + let mut op: nir_shader_compiler_options = Default::default(); op.lower_fdiv = true; op.fuse_ffma16 = true; @@ -319,11 +319,11 @@ impl ShaderBin { xfb: if let Some(xfb) = &io.xfb { **xfb } else { - unsafe { std::mem::zeroed() } + Default::default() }, _pad: Default::default(), }, - _ => unsafe { std::mem::zeroed() }, + _ => Default::default(), }, hdr: sph::encode_header(sm, info, fs_key), }; diff --git a/src/nouveau/compiler/nak/hw_runner.rs b/src/nouveau/compiler/nak/hw_runner.rs index 06f9686ce20..1ad725e676f 100644 --- a/src/nouveau/compiler/nak/hw_runner.rs +++ b/src/nouveau/compiler/nak/hw_runner.rs @@ -46,7 +46,7 @@ struct DrmDevices { impl DrmDevices { fn get() -> io::Result { unsafe { - let mut devices: [drmDevicePtr; 16] = std::mem::zeroed(); + let mut devices: [drmDevicePtr; 16] = [std::ptr::null_mut(); 16]; let num_devices = drmGetDevices( devices.as_mut_ptr(), devices.len().try_into().unwrap(), @@ -465,7 +465,7 @@ impl<'a> Runner { ); // Populate and upload the QMD - let mut qmd_cbufs: [nak_qmd_cbuf; 8] = unsafe { std::mem::zeroed() }; + let mut qmd_cbufs: [nak_qmd_cbuf; 8] = Default::default(); qmd_cbufs[0] = nak_qmd_cbuf { index: 0, size: std::mem::size_of::()