mirror of
https://gitlab.freedesktop.org/mesa/mesa.git
synced 2026-05-14 14:28:08 +02:00
rusticl/mem: use cl_slice::from_raw_parts in place of std::slice
The cl_slice version of this function checks several invariants for us, providing a slight safety net. v2: reordered commits to allow cherry-picking bugfixes Reviewed-by: Karol Herbst <kherbst@redhat.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/33989>
This commit is contained in:
parent
5e365f1674
commit
aa9109f0b5
1 changed files with 12 additions and 5 deletions
|
|
@ -26,7 +26,6 @@ use std::cmp::Ordering;
|
||||||
use std::mem::{self, MaybeUninit};
|
use std::mem::{self, MaybeUninit};
|
||||||
use std::os::raw::c_void;
|
use std::os::raw::c_void;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::slice;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
fn validate_mem_flags(flags: cl_mem_flags, images: bool) -> CLResult<()> {
|
fn validate_mem_flags(flags: cl_mem_flags, images: bool) -> CLResult<()> {
|
||||||
|
|
@ -1612,8 +1611,12 @@ fn enqueue_fill_buffer(
|
||||||
return Err(CL_INVALID_CONTEXT);
|
return Err(CL_INVALID_CONTEXT);
|
||||||
}
|
}
|
||||||
|
|
||||||
// we have to copy memory
|
// The caller may free `pattern` once the `clEnqueueFillBuffer()` call
|
||||||
let pattern = unsafe { slice::from_raw_parts(pattern.cast(), pattern_size).to_vec() };
|
// returns, so we need to duplicate its contents to hold onto.
|
||||||
|
// SAFETY: `cl_slice::from_raw_parts()` verifies the testable invariants of
|
||||||
|
// `slice::from_raw_parts()`. The caller is responsible for providing a
|
||||||
|
// pointer to appropriately-sized, initialized memory.
|
||||||
|
let pattern = unsafe { cl_slice::from_raw_parts(pattern.cast(), pattern_size)? }.to_vec();
|
||||||
create_and_queue(
|
create_and_queue(
|
||||||
q,
|
q,
|
||||||
CL_COMMAND_FILL_BUFFER,
|
CL_COMMAND_FILL_BUFFER,
|
||||||
|
|
@ -2421,8 +2424,12 @@ fn enqueue_svm_free_impl(
|
||||||
// The application is allowed to reuse or free the memory referenced by `svm_pointers` after this
|
// The application is allowed to reuse or free the memory referenced by `svm_pointers` after this
|
||||||
// function returns, so we have to make a copy.
|
// function returns, so we have to make a copy.
|
||||||
let mut svm_pointers = if !svm_pointers.is_null() {
|
let mut svm_pointers = if !svm_pointers.is_null() {
|
||||||
// SAFETY: num_svm_pointers specifies the amount of elements in svm_pointers
|
// SAFETY: `cl_slice::from_raw_parts()` verifies that testable
|
||||||
unsafe { slice::from_raw_parts(svm_pointers.cast(), num_svm_pointers as usize) }.to_vec()
|
// invariants of `slice::from_raw_parts()` are satisfied. Callers are
|
||||||
|
// responsible for providing pointers to appropriately-sized,
|
||||||
|
// initialized memory.
|
||||||
|
unsafe { cl_slice::from_raw_parts(svm_pointers.cast(), num_svm_pointers as usize)? }
|
||||||
|
.to_vec()
|
||||||
} else {
|
} else {
|
||||||
// A slice must not be created from a raw null pointer, so simply create
|
// A slice must not be created from a raw null pointer, so simply create
|
||||||
// an empty vec instead.
|
// an empty vec instead.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue