rusticl: add a safe abstraction to execute a MemCB

Reviewed-by: Karol Herbst <kherbst@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25669>
This commit is contained in:
LingMan
2023-10-12 20:15:47 +02:00
committed by Marge Bot
parent 241d16c9e8
commit 8b1d73ff23
2 changed files with 15 additions and 7 deletions
@@ -2,6 +2,7 @@ use crate::api::icd::CLResult;
use crate::api::icd::ReferenceCountedAPIPointer;
use crate::core::context::Context;
use crate::core::event::Event;
use crate::core::memory::Mem;
use rusticl_opencl_gen::*;
@@ -146,6 +147,15 @@ cl_callback!(
}
);
impl MemCB {
pub fn call(self, mem: &Mem) {
let cl = cl_mem::from_ptr(mem);
// SAFETY: `cl` must have pointed to an OpenCL context, which is where we just got it from.
// All other requirements are covered by this callback's type invariants.
unsafe { (self.func)(cl, self.data) };
}
}
cl_callback!(
ProgramCB(FuncProgramCB) {
program: cl_program,
+5 -7
View File
@@ -21,6 +21,7 @@ use std::cmp;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::convert::TryInto;
use std::mem;
use std::mem::size_of;
use std::ops::AddAssign;
use std::os::raw::c_void;
@@ -1234,13 +1235,10 @@ impl Mem {
impl Drop for Mem {
fn drop(&mut self) {
let cl = cl_mem::from_ptr(self);
self.cbs
.get_mut()
.unwrap()
.iter()
.rev()
.for_each(|cb| unsafe { (cb.func)(cl, cb.data) });
let cbs = mem::take(self.cbs.get_mut().unwrap());
for cb in cbs.into_iter().rev() {
cb.call(self);
}
for (d, tx) in self.maps.get_mut().unwrap().tx.drain() {
d.helper_ctx().unmap(tx.tx);