nil/copy: Wrap all unsafe code in unsafe blocks

Starting with Rust 2024, the bodies of unsafe functions will no longer
be implicitly unsafe contexts.

Reviewed-by: Karol Herbst <kherbst@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36849>
This commit is contained in:
Faith Ekstrand 2025-08-19 10:10:34 -04:00 committed by Marge Bot
parent 4dd7ed18b9
commit fb04c68573

View file

@ -45,12 +45,14 @@ trait CopyGOB {
// No bounding box for this one
unsafe fn copy_whole_gob(tiled: usize, linear: LinearPointer) {
Self::copy_gob(
tiled,
linear,
Offset4D::new(0, 0, 0, 0),
Offset4D::new(0, 0, 0, 0) + Self::GOB_EXTENT_B,
);
unsafe {
Self::copy_gob(
tiled,
linear,
Offset4D::new(0, 0, 0, 0),
Offset4D::new(0, 0, 0, 0) + Self::GOB_EXTENT_B,
);
}
}
}
@ -84,16 +86,20 @@ impl<C: CopyGOBLines> CopyGOB for C {
let tiled = tiled + (offset as usize);
let linear = linear.at(Offset4D::new(x, y, z, 0));
if x >= start.x && x + C::LINE_WIDTH_B <= end.x {
C::copy_whole_line(tiled as *mut _, linear as *mut _);
unsafe {
C::copy_whole_line(tiled as *mut _, linear as *mut _);
}
} else if x + C::LINE_WIDTH_B >= start.x && x < end.x {
let start = (std::cmp::max(x, start.x) - x) as usize;
let end =
std::cmp::min(end.x - x, C::LINE_WIDTH_B) as usize;
C::copy(
(tiled + start) as *mut _,
(linear + start) as *mut _,
end - start,
);
unsafe {
C::copy(
(tiled + start) as *mut _,
(linear + start) as *mut _,
end - start,
);
}
}
}
});
@ -103,7 +109,9 @@ impl<C: CopyGOBLines> CopyGOB for C {
Self::for_each_gob_line(|offset, x, y, z| {
let tiled = tiled + (offset as usize);
let linear = linear.at(Offset4D::new(x, y, z, 0));
C::copy_whole_line(tiled as *mut _, linear as *mut _);
unsafe {
C::copy_whole_line(tiled as *mut _, linear as *mut _);
}
});
}
}
@ -114,10 +122,14 @@ trait CopyBytes {
unsafe fn copy(tiled: *mut u8, linear: *mut u8, bytes: usize);
unsafe fn copy_16b(tiled: *mut [u8; 16], linear: *mut [u8; 16]) {
Self::copy(tiled as *mut _, linear as *mut _, 16);
unsafe {
Self::copy(tiled as *mut _, linear as *mut _, 16);
}
}
unsafe fn copy_8b(tiled: *mut [u8; 8], linear: *mut [u8; 8]) {
Self::copy(tiled as *mut _, linear as *mut _, 8);
unsafe {
Self::copy(tiled as *mut _, linear as *mut _, 8);
}
}
}
@ -132,11 +144,15 @@ impl<C: CopyBytes> CopyGOBLines for CopyGOBFermi<C> {
const X_DIVISOR: u32 = C::X_DIVISOR;
unsafe fn copy(tiled: *mut u8, linear: *mut u8, bytes: usize) {
C::copy(tiled, linear, bytes);
unsafe {
C::copy(tiled, linear, bytes);
}
}
unsafe fn copy_whole_line(tiled: *mut u8, linear: *mut u8) {
C::copy_16b(tiled as *mut _, linear as *mut _);
unsafe {
C::copy_16b(tiled as *mut _, linear as *mut _);
}
}
#[inline(always)]
@ -176,11 +192,15 @@ impl<C: CopyBytes> CopyGOBLines for CopyGOBTuring2D<C> {
const X_DIVISOR: u32 = C::X_DIVISOR;
unsafe fn copy(tiled: *mut u8, linear: *mut u8, bytes: usize) {
C::copy(tiled, linear, bytes);
unsafe {
C::copy(tiled, linear, bytes);
}
}
unsafe fn copy_whole_line(tiled: *mut u8, linear: *mut u8) {
C::copy_16b(tiled as *mut _, linear as *mut _);
unsafe {
C::copy_16b(tiled as *mut _, linear as *mut _);
}
}
#[inline(always)]
@ -220,11 +240,15 @@ impl<C: CopyBytes> CopyGOBLines for CopyGOBBlackwell2D2BPP<C> {
const X_DIVISOR: u32 = C::X_DIVISOR;
unsafe fn copy(tiled: *mut u8, linear: *mut u8, bytes: usize) {
C::copy(tiled, linear, bytes);
unsafe {
C::copy(tiled, linear, bytes);
}
}
unsafe fn copy_whole_line(tiled: *mut u8, linear: *mut u8) {
C::copy_16b(tiled as *mut _, linear as *mut _);
unsafe {
C::copy_16b(tiled as *mut _, linear as *mut _);
}
}
#[inline(always)]
@ -264,11 +288,15 @@ impl<C: CopyBytes> CopyGOBLines for CopyGOBBlackwell2D1BPP<C> {
const X_DIVISOR: u32 = C::X_DIVISOR;
unsafe fn copy(tiled: *mut u8, linear: *mut u8, bytes: usize) {
C::copy(tiled, linear, bytes);
unsafe {
C::copy(tiled, linear, bytes);
}
}
unsafe fn copy_whole_line(tiled: *mut u8, linear: *mut u8) {
C::copy_8b(tiled as *mut _, linear as *mut _);
unsafe {
C::copy_8b(tiled as *mut _, linear as *mut _);
}
}
#[inline(always)]
@ -522,7 +550,7 @@ unsafe fn copy_tile<CG: CopyGOB>(
if start.is_aligned_to(CG::GOB_EXTENT_B)
&& end.is_aligned_to(CG::GOB_EXTENT_B)
{
for_each_extent4d_aligned(start, end, CG::GOB_EXTENT_B, |gob| {
for_each_extent4d_aligned(start, end, CG::GOB_EXTENT_B, |gob| unsafe {
CG::copy_whole_gob(tile_ptr.at(gob), linear.offset(gob));
});
} else {
@ -532,9 +560,13 @@ unsafe fn copy_tile<CG: CopyGOB>(
if start == Offset4D::new(0, 0, 0, 0)
&& end == Offset4D::new(0, 0, 0, 0) + CG::GOB_EXTENT_B
{
CG::copy_whole_gob(tiled, linear);
unsafe {
CG::copy_whole_gob(tiled, linear);
}
} else {
CG::copy_gob(tiled, linear, start, end);
unsafe {
CG::copy_gob(tiled, linear, start, end);
}
}
});
}
@ -562,7 +594,9 @@ unsafe fn copy_tiled<CG: CopyGOB>(
for_each_extent4d(start, end, tile_extent_B, |tile, start, end| {
let tile_ptr = level_tiled_ptr.at(tile);
let linear = linear.offset(tile);
copy_tile::<CG>(tiling, tile_ptr, linear, start, end);
unsafe {
copy_tile::<CG>(tiling, tile_ptr, linear, start, end);
}
});
}
@ -573,7 +607,9 @@ impl CopyBytes for RawCopyToTiled {
unsafe fn copy(tiled: *mut u8, linear: *mut u8, bytes: usize) {
// This is backwards from memcpy
std::ptr::copy_nonoverlapping(linear, tiled, bytes);
unsafe {
std::ptr::copy_nonoverlapping(linear, tiled, bytes);
}
}
}
@ -584,7 +620,9 @@ impl CopyBytes for RawCopyToLinear {
unsafe fn copy(tiled: *mut u8, linear: *mut u8, bytes: usize) {
// This is backwards from memcpy
std::ptr::copy_nonoverlapping(tiled, linear, bytes);
unsafe {
std::ptr::copy_nonoverlapping(tiled, linear, bytes);
}
}
}
@ -610,48 +648,50 @@ pub unsafe extern "C" fn nil_copy_linear_to_tiled(
linear_plane_stride_B,
);
match tiling.gob_type {
GOBType::Blackwell16Bit => {
copy_tiled::<CopyGOBBlackwell2D2BPP<RawCopyToTiled>>(
*tiling,
level_extent_B,
tiled_dst,
linear_pointer,
offset_B,
end_B,
);
unsafe {
match tiling.gob_type {
GOBType::Blackwell16Bit => {
copy_tiled::<CopyGOBBlackwell2D2BPP<RawCopyToTiled>>(
*tiling,
level_extent_B,
tiled_dst,
linear_pointer,
offset_B,
end_B,
);
}
GOBType::Blackwell8Bit => {
copy_tiled::<CopyGOBBlackwell2D1BPP<RawCopyToTiled>>(
*tiling,
level_extent_B,
tiled_dst,
linear_pointer,
offset_B,
end_B,
);
}
GOBType::TuringColor2D => {
copy_tiled::<CopyGOBTuring2D<RawCopyToTiled>>(
*tiling,
level_extent_B,
tiled_dst,
linear_pointer,
offset_B,
end_B,
);
}
GOBType::FermiColor => {
copy_tiled::<CopyGOBFermi<RawCopyToTiled>>(
*tiling,
level_extent_B,
tiled_dst,
linear_pointer,
offset_B,
end_B,
);
}
_ => panic!("Unsupported GOB type"),
}
GOBType::Blackwell8Bit => {
copy_tiled::<CopyGOBBlackwell2D1BPP<RawCopyToTiled>>(
*tiling,
level_extent_B,
tiled_dst,
linear_pointer,
offset_B,
end_B,
);
}
GOBType::TuringColor2D => {
copy_tiled::<CopyGOBTuring2D<RawCopyToTiled>>(
*tiling,
level_extent_B,
tiled_dst,
linear_pointer,
offset_B,
end_B,
);
}
GOBType::FermiColor => {
copy_tiled::<CopyGOBFermi<RawCopyToTiled>>(
*tiling,
level_extent_B,
tiled_dst,
linear_pointer,
offset_B,
end_B,
);
}
_ => panic!("Unsupported GOB type"),
}
}
@ -677,47 +717,49 @@ pub unsafe extern "C" fn nil_copy_tiled_to_linear(
linear_plane_stride_B,
);
match tiling.gob_type {
GOBType::Blackwell16Bit => {
copy_tiled::<CopyGOBBlackwell2D2BPP<RawCopyToLinear>>(
*tiling,
level_extent_B,
tiled_src,
linear_pointer,
offset_B,
end_B,
);
unsafe {
match tiling.gob_type {
GOBType::Blackwell16Bit => {
copy_tiled::<CopyGOBBlackwell2D2BPP<RawCopyToLinear>>(
*tiling,
level_extent_B,
tiled_src,
linear_pointer,
offset_B,
end_B,
);
}
GOBType::Blackwell8Bit => {
copy_tiled::<CopyGOBBlackwell2D1BPP<RawCopyToLinear>>(
*tiling,
level_extent_B,
tiled_src,
linear_pointer,
offset_B,
end_B,
);
}
GOBType::TuringColor2D => {
copy_tiled::<CopyGOBTuring2D<RawCopyToLinear>>(
*tiling,
level_extent_B,
tiled_src,
linear_pointer,
offset_B,
end_B,
);
}
GOBType::FermiColor => {
copy_tiled::<CopyGOBFermi<RawCopyToLinear>>(
*tiling,
level_extent_B,
tiled_src,
linear_pointer,
offset_B,
end_B,
);
}
_ => panic!("Unsupported GOB type"),
}
GOBType::Blackwell8Bit => {
copy_tiled::<CopyGOBBlackwell2D1BPP<RawCopyToLinear>>(
*tiling,
level_extent_B,
tiled_src,
linear_pointer,
offset_B,
end_B,
);
}
GOBType::TuringColor2D => {
copy_tiled::<CopyGOBTuring2D<RawCopyToLinear>>(
*tiling,
level_extent_B,
tiled_src,
linear_pointer,
offset_B,
end_B,
);
}
GOBType::FermiColor => {
copy_tiled::<CopyGOBFermi<RawCopyToLinear>>(
*tiling,
level_extent_B,
tiled_src,
linear_pointer,
offset_B,
end_B,
);
}
_ => panic!("Unsupported GOB type"),
}
}