open_cl_low_level/
cl_object.rs1use std::fmt::Debug;
2use crate::ffi::{cl_command_queue, cl_context, cl_device_id, cl_event, cl_kernel, cl_mem, cl_program, cl_platform_id};
3use crate::{Output, Error, DeviceError};
4
5pub trait CheckValidClObject {
6 fn check_valid_cl_object(&self) -> Output<()>;
7}
8
9macro_rules! check_is_null {
10 ($t:ident) => {
11 impl CheckValidClObject for $t {
12 fn check_valid_cl_object(&self) -> Output<()> {
13 if self.is_null() {
14 return Err(Error::ClObjectCannotBeNull)
15 }
16 Ok(())
17 }
18 }
19 }
20}
21
22pub const UNUSABLE_DEVICE_ID: cl_device_id = 0xFFFF_FFFF as *mut usize as cl_device_id;
23
24impl CheckValidClObject for cl_device_id {
25 fn check_valid_cl_object(&self) -> Output<()> {
26 if self.is_null() {
27 return Err(Error::ClObjectCannotBeNull);
28 }
29 if *self == UNUSABLE_DEVICE_ID {
30 return Err(DeviceError::UnusableDevice.into());
31 }
32 Ok(())
33 }
34}
35
36check_is_null!(cl_command_queue);
37check_is_null!(cl_context);
38check_is_null!(cl_event);
39check_is_null!(cl_kernel);
40check_is_null!(cl_mem);
41check_is_null!(cl_program);
42check_is_null!(cl_platform_id);
43
44
45pub unsafe trait ClObject: Sized + Clone + Copy + Debug + CheckValidClObject + PartialEq {
46 fn type_name(&self) -> &'static str;
47 fn address(&self) -> String {
48 format!("{:?}", *self)
49 }
50}
51
52unsafe impl ClObject for cl_command_queue {
53 fn type_name(&self) -> &'static str {
54 "cl_command_queue"
55 }
56}
57unsafe impl ClObject for cl_context {
58 fn type_name(&self) -> &'static str {
59 "cl_context"
60 }
61}
62unsafe impl ClObject for cl_device_id {
63 fn type_name(&self) -> &'static str {
64 "cl_device_id"
65 }
66}
67unsafe impl ClObject for cl_event {
68 fn type_name(&self) -> &'static str {
69 "cl_event"
70 }
71}
72unsafe impl ClObject for cl_kernel {
73 fn type_name(&self) -> &'static str {
74 "cl_kernel"
75 }
76}
77unsafe impl ClObject for cl_mem {
78 fn type_name(&self) -> &'static str {
79 "cl_mem"
80 }
81}
82unsafe impl ClObject for cl_program {
83 fn type_name(&self) -> &'static str {
84 "cl_program"
85 }
86}
87unsafe impl ClObject for cl_platform_id {
88 fn type_name(&self) -> &'static str {
89 "cl_platform_id"
90 }
91}