open_cl_low_level/cl_retain_release.rs
1use crate::ffi::*;
2use crate::{ClObject};
3
4pub trait RetainRelease: ClObject {
5
6 /// Retains (increments the reference count of) the ClObject.
7 ///
8 /// # Safety
9 /// Balancing the release and retain reference count of a ClObject must be
10 /// done with care. Improper usage of release and retain can lead to
11 /// undefined behavior.
12 ///
13 /// Usage of an invalid ClObject is undefined behavior.
14 unsafe fn retain(&self);
15
16 /// Releases (decrements reference count of) the ClObject
17 ///
18 /// # Safety
19 /// Balancing the release and retain reference count of a ClObject
20 /// must be done with care. Improper usage of release and
21 /// retain can lead to undefined behavior.
22 ///
23 /// Usage of an invalid ClObject is undefined behavior.
24 unsafe fn release(&mut self);
25}
26
27macro_rules! impl_retain_release {
28 ($snake:ident, $pascal:ident) => {
29 paste::item! {
30 impl RetainRelease for [<cl_ $snake>] {
31 /// This function is used to increase the atomic reference count of the associated
32 /// OpenCL ARC object. This function should only be used when the OpenCL interface
33 /// returns a ARC object that is not reference counted by OpenCL (yes, OpenCL let's you do that...)
34 ///
35 /// # Safety
36 /// This function atomically decrements the OpenCL reference count. Mismanagement
37 /// of an object's OpenCL ARC can lead to undefined behavior.
38 unsafe fn retain(&self) {
39 $crate::build_output((), [<clRetain $pascal>](*self))
40 .unwrap_or_else(|e| {
41 panic!("Failed to retain cl_{} {:?} due to {:?}", stringify!($snake), self, e);
42 })
43 }
44
45 /// This function is used to decrease the OpenCL atomic reference count of the
46 /// associated OpenCL ARC object.
47 ///
48 /// # Safety
49 /// This function atomically decrements the OpenCL reference count. Mismanagement
50 /// of an object's OpenCL ARC can lead to undefined behavior.
51 unsafe fn release(&mut self) {
52 $crate::build_output((), [<clRelease $pascal>](*self))
53 .unwrap_or_else(|e| {
54 panic!("Failed to release cl_{} {:?} due to {:?}", stringify!($snake), self, e);
55 })
56 }
57 }
58 }
59 }
60}
61
62impl_retain_release!(command_queue, CommandQueue);
63impl_retain_release!(context, Context);
64impl_retain_release!(device_id, Device);
65impl_retain_release!(event, Event);
66impl_retain_release!(kernel, Kernel);
67impl_retain_release!(mem, MemObject);
68impl_retain_release!(program, Program);
69
70impl RetainRelease for cl_platform_id {
71 unsafe fn release(&mut self) { () }
72 unsafe fn retain(&self) { () }
73}
74