Skip to content

Commit

Permalink
Convert mem::zeroed() / 0 to MaybeUninit::uninit()
Browse files Browse the repository at this point in the history
As noted in #792 changes like this might help us more strictly identify
and validate that the argument in question is only relevant as an output
argument (i.e. structs with `sType` are read by the function call, even
if the caller strictly expects return values there, so that it can fill
in multiple structures in a `pNext` chain, and must hence be `Default`-
initialized).
  • Loading branch information
MarijnS95 committed Oct 11, 2023
1 parent 49de034 commit 36b6a5f
Show file tree
Hide file tree
Showing 30 changed files with 148 additions and 128 deletions.
55 changes: 34 additions & 21 deletions ash/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,16 +511,16 @@ impl Device {
&self,
memory_requirements: &vk::DeviceImageMemoryRequirements,
) -> usize {
let mut count = 0;
let mut count = mem::MaybeUninit::uninit();
(self
.device_fn_1_3
.get_device_image_sparse_memory_requirements)(
self.handle,
memory_requirements,
&mut count,
count.as_mut_ptr(),
std::ptr::null_mut(),
);
count as usize
count.assume_init() as usize
}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetDeviceImageSparseMemoryRequirements.html>
Expand Down Expand Up @@ -670,9 +670,13 @@ impl Device {
/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetSemaphoreCounterValue.html>
#[inline]
pub unsafe fn get_semaphore_counter_value(&self, semaphore: vk::Semaphore) -> VkResult<u64> {
let mut value = 0;
(self.device_fn_1_2.get_semaphore_counter_value)(self.handle(), semaphore, &mut value)
.result_with_success(value)
let mut value = mem::MaybeUninit::uninit();
(self.device_fn_1_2.get_semaphore_counter_value)(
self.handle(),
semaphore,
value.as_mut_ptr(),
)
.assume_init_on_success(value)
}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkWaitSemaphores.html>
Expand Down Expand Up @@ -828,14 +832,15 @@ impl Device {
&self,
info: &vk::ImageSparseMemoryRequirementsInfo2,
) -> usize {
let mut count = 0;
// TODO: when pSparseMemoryRequirements is NULL, is it okay/safe for count to be uninit?
let mut count = mem::MaybeUninit::uninit();
(self.device_fn_1_1.get_image_sparse_memory_requirements2)(
self.handle(),
info,
&mut count,
count.as_mut_ptr(),
ptr::null_mut(),
);
count as usize
count.assume_init() as usize
}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetImageSparseMemoryRequirements2.html>
Expand Down Expand Up @@ -2595,9 +2600,13 @@ impl Device {
/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetImageMemoryRequirements.html>
#[inline]
pub unsafe fn get_image_memory_requirements(&self, image: vk::Image) -> vk::MemoryRequirements {
let mut mem_req = mem::zeroed();
(self.device_fn_1_0.get_image_memory_requirements)(self.handle(), image, &mut mem_req);
mem_req
let mut mem_req = mem::MaybeUninit::uninit();
(self.device_fn_1_0.get_image_memory_requirements)(
self.handle(),
image,
mem_req.as_mut_ptr(),
);
mem_req.assume_init()
}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetBufferMemoryRequirements.html>
Expand All @@ -2606,9 +2615,13 @@ impl Device {
&self,
buffer: vk::Buffer,
) -> vk::MemoryRequirements {
let mut mem_req = mem::zeroed();
(self.device_fn_1_0.get_buffer_memory_requirements)(self.handle(), buffer, &mut mem_req);
mem_req
let mut mem_req = mem::MaybeUninit::uninit();
(self.device_fn_1_0.get_buffer_memory_requirements)(
self.handle(),
buffer,
mem_req.as_mut_ptr(),
);
mem_req.assume_init()
}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkAllocateMemory.html>
Expand Down Expand Up @@ -2688,25 +2701,25 @@ impl Device {
/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetRenderAreaGranularity.html>
#[inline]
pub unsafe fn get_render_area_granularity(&self, render_pass: vk::RenderPass) -> vk::Extent2D {
let mut granularity = mem::zeroed();
let mut granularity = mem::MaybeUninit::uninit();
(self.device_fn_1_0.get_render_area_granularity)(
self.handle(),
render_pass,
&mut granularity,
granularity.as_mut_ptr(),
);
granularity
granularity.assume_init()
}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetDeviceMemoryCommitment.html>
#[inline]
pub unsafe fn get_device_memory_commitment(&self, memory: vk::DeviceMemory) -> vk::DeviceSize {
let mut committed_memory_in_bytes = 0;
let mut committed_memory_in_bytes = mem::MaybeUninit::uninit();
(self.device_fn_1_0.get_device_memory_commitment)(
self.handle(),
memory,
&mut committed_memory_in_bytes,
committed_memory_in_bytes.as_mut_ptr(),
);
committed_memory_in_bytes
committed_memory_in_bytes.assume_init()
}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetImageSparseMemoryRequirements.html>
Expand Down
6 changes: 3 additions & 3 deletions ash/src/extensions/ext/debug_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ impl DebugReport {
create_info: &vk::DebugReportCallbackCreateInfoEXT,
allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> VkResult<vk::DebugReportCallbackEXT> {
let mut debug_cb = mem::zeroed();
let mut debug_cb = mem::MaybeUninit::uninit();
(self.fp.create_debug_report_callback_ext)(
self.handle,
create_info,
allocation_callbacks.as_raw_ptr(),
&mut debug_cb,
debug_cb.as_mut_ptr(),
)
.result_with_success(debug_cb)
.assume_init_on_success(debug_cb)
}

pub const NAME: &'static CStr = vk::ExtDebugReportFn::NAME;
Expand Down
6 changes: 3 additions & 3 deletions ash/src/extensions/ext/debug_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ impl DebugUtils {
create_info: &vk::DebugUtilsMessengerCreateInfoEXT,
allocator: Option<&vk::AllocationCallbacks>,
) -> VkResult<vk::DebugUtilsMessengerEXT> {
let mut messenger = mem::zeroed();
let mut messenger = mem::MaybeUninit::uninit();
(self.fp.create_debug_utils_messenger_ext)(
self.handle,
create_info,
allocator.as_raw_ptr(),
&mut messenger,
messenger.as_mut_ptr(),
)
.result_with_success(messenger)
.assume_init_on_success(messenger)
}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkDestroyDebugUtilsMessengerEXT.html>
Expand Down
6 changes: 3 additions & 3 deletions ash/src/extensions/ext/full_screen_exclusive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ impl FullScreenExclusive {
&self,
surface_info: &vk::PhysicalDeviceSurfaceInfo2KHR,
) -> VkResult<vk::DeviceGroupPresentModeFlagsKHR> {
let mut present_modes = mem::zeroed();
let mut present_modes = mem::MaybeUninit::uninit();
(self.fp.get_device_group_surface_present_modes2_ext)(
self.handle,
surface_info,
&mut present_modes,
present_modes.as_mut_ptr(),
)
.result_with_success(present_modes)
.assume_init_on_success(present_modes)
}

pub const NAME: &'static CStr = vk::ExtFullScreenExclusiveFn::NAME;
Expand Down
6 changes: 3 additions & 3 deletions ash/src/extensions/ext/headless_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ impl HeadlessSurface {
create_info: &vk::HeadlessSurfaceCreateInfoEXT,
allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> VkResult<vk::SurfaceKHR> {
let mut surface = mem::zeroed();
let mut surface = mem::MaybeUninit::uninit();
(self.fp.create_headless_surface_ext)(
self.handle,
create_info,
allocation_callbacks.as_raw_ptr(),
&mut surface,
surface.as_mut_ptr(),
)
.result_with_success(surface)
.assume_init_on_success(surface)
}

pub const NAME: &'static CStr = vk::ExtHeadlessSurfaceFn::NAME;
Expand Down
6 changes: 3 additions & 3 deletions ash/src/extensions/ext/metal_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ impl MetalSurface {
create_info: &vk::MetalSurfaceCreateInfoEXT,
allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> VkResult<vk::SurfaceKHR> {
let mut surface = mem::zeroed();
let mut surface = mem::MaybeUninit::uninit();
(self.fp.create_metal_surface_ext)(
self.handle,
create_info,
allocation_callbacks.as_raw_ptr(),
&mut surface,
surface.as_mut_ptr(),
)
.result_with_success(surface)
.assume_init_on_success(surface)
}

pub const NAME: &'static CStr = vk::ExtMetalSurfaceFn::NAME;
Expand Down
12 changes: 6 additions & 6 deletions ash/src/extensions/ext/private_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ impl PrivateData {
create_info: &vk::PrivateDataSlotCreateInfoEXT,
allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> VkResult<vk::PrivateDataSlotEXT> {
let mut private_data_slot = mem::zeroed();
let mut private_data_slot = mem::MaybeUninit::uninit();
(self.fp.create_private_data_slot_ext)(
self.handle,
create_info,
allocation_callbacks.as_raw_ptr(),
&mut private_data_slot,
private_data_slot.as_mut_ptr(),
)
.result_with_success(private_data_slot)
.assume_init_on_success(private_data_slot)
}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkDestroyPrivateDataSlotEXT.html>
Expand Down Expand Up @@ -77,15 +77,15 @@ impl PrivateData {
object: T,
private_data_slot: vk::PrivateDataSlotEXT,
) -> u64 {
let mut data = mem::zeroed();
let mut data = mem::MaybeUninit::uninit();
(self.fp.get_private_data_ext)(
self.handle,
T::TYPE,
object.as_raw(),
private_data_slot,
&mut data,
data.as_mut_ptr(),
);
data
data.assume_init()
}

pub const NAME: &'static CStr = vk::ExtPrivateDataFn::NAME;
Expand Down
6 changes: 3 additions & 3 deletions ash/src/extensions/google/display_timing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ impl DisplayTiming {
&self,
swapchain: vk::SwapchainKHR,
) -> VkResult<vk::RefreshCycleDurationGOOGLE> {
let mut properties = mem::zeroed();
(self.fp.get_refresh_cycle_duration_google)(self.handle, swapchain, &mut properties)
.result_with_success(properties)
let mut properties = mem::MaybeUninit::uninit();
(self.fp.get_refresh_cycle_duration_google)(self.handle, swapchain, properties.as_mut_ptr())
.assume_init_on_success(properties)
}

pub const NAME: &'static CStr = vk::GoogleDisplayTimingFn::NAME;
Expand Down
14 changes: 6 additions & 8 deletions ash/src/extensions/khr/acceleration_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ impl AccelerationStructure {
create_info: &vk::AccelerationStructureCreateInfoKHR,
allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> VkResult<vk::AccelerationStructureKHR> {
let mut accel_struct = mem::zeroed();
let mut accel_struct = mem::MaybeUninit::uninit();
(self.fp.create_acceleration_structure_khr)(
self.handle,
create_info,
allocation_callbacks.as_raw_ptr(),
&mut accel_struct,
accel_struct.as_mut_ptr(),
)
.result_with_success(accel_struct)
.assume_init_on_success(accel_struct)
}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkDestroyAccelerationStructureKHR.html>
Expand Down Expand Up @@ -259,15 +259,13 @@ impl AccelerationStructure {
&self,
version: &vk::AccelerationStructureVersionInfoKHR,
) -> vk::AccelerationStructureCompatibilityKHR {
let mut compatibility = mem::zeroed();

let mut compatibility = mem::MaybeUninit::uninit();
(self.fp.get_device_acceleration_structure_compatibility_khr)(
self.handle,
version,
&mut compatibility,
compatibility.as_mut_ptr(),
);

compatibility
compatibility.assume_init()
}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetAccelerationStructureBuildSizesKHR.html>
Expand Down
6 changes: 3 additions & 3 deletions ash/src/extensions/khr/android_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ impl AndroidSurface {
create_info: &vk::AndroidSurfaceCreateInfoKHR,
allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> VkResult<vk::SurfaceKHR> {
let mut surface = mem::zeroed();
let mut surface = mem::MaybeUninit::uninit();
(self.fp.create_android_surface_khr)(
self.handle,
create_info,
allocation_callbacks.as_raw_ptr(),
&mut surface,
surface.as_mut_ptr(),
)
.result_with_success(surface)
.assume_init_on_success(surface)
}

pub const NAME: &'static CStr = vk::KhrAndroidSurfaceFn::NAME;
Expand Down
6 changes: 3 additions & 3 deletions ash/src/extensions/khr/create_render_pass2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ impl CreateRenderPass2 {
create_info: &vk::RenderPassCreateInfo2,
allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> VkResult<vk::RenderPass> {
let mut renderpass = mem::zeroed();
let mut renderpass = mem::MaybeUninit::uninit();
(self.fp.create_render_pass2_khr)(
self.handle,
create_info,
allocation_callbacks.as_raw_ptr(),
&mut renderpass,
renderpass.as_mut_ptr(),
)
.result_with_success(renderpass)
.assume_init_on_success(renderpass)
}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkCmdBeginRenderPass2.html>
Expand Down
6 changes: 3 additions & 3 deletions ash/src/extensions/khr/deferred_host_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ impl DeferredHostOperations {
&self,
allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> VkResult<vk::DeferredOperationKHR> {
let mut operation = mem::zeroed();
let mut operation = mem::MaybeUninit::uninit();
(self.fp.create_deferred_operation_khr)(
self.handle,
allocation_callbacks.as_raw_ptr(),
&mut operation,
operation.as_mut_ptr(),
)
.result_with_success(operation)
.assume_init_on_success(operation)
}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkDeferredOperationJoinKHR.html>
Expand Down
16 changes: 10 additions & 6 deletions ash/src/extensions/khr/device_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ impl DeviceGroup {
local_device_index: u32,
remote_device_index: u32,
) -> vk::PeerMemoryFeatureFlags {
let mut peer_memory_features = mem::zeroed();
let mut peer_memory_features = mem::MaybeUninit::uninit();
(self.fp.get_device_group_peer_memory_features_khr)(
self.handle,
heap_index,
local_device_index,
remote_device_index,
&mut peer_memory_features,
peer_memory_features.as_mut_ptr(),
);
peer_memory_features
peer_memory_features.assume_init()
}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkCmdSetDeviceMaskKHR.html>
Expand Down Expand Up @@ -99,9 +99,13 @@ impl DeviceGroup {
&self,
surface: vk::SurfaceKHR,
) -> VkResult<vk::DeviceGroupPresentModeFlagsKHR> {
let mut modes = mem::zeroed();
(self.fp.get_device_group_surface_present_modes_khr)(self.handle, surface, &mut modes)
.result_with_success(modes)
let mut modes = mem::MaybeUninit::uninit();
(self.fp.get_device_group_surface_present_modes_khr)(
self.handle,
surface,
modes.as_mut_ptr(),
)
.assume_init_on_success(modes)
}

/// Requires [`VK_KHR_surface`] to be enabled.
Expand Down
Loading

0 comments on commit 36b6a5f

Please sign in to comment.