1use crate::error::Result as OclCoreResult;
42use crate::ffi::{
43    c_void, cl_command_queue, cl_context, cl_device_id, cl_event, cl_kernel, cl_mem,
44    cl_platform_id, cl_program, cl_sampler,
45};
46use crate::functions::{self, ApiFunction, VersionKind};
47use crate::{
48    CommandExecutionStatus, CommandQueueInfo, CommandQueueInfoResult, ContextInfo,
49    ContextInfoResult, DeviceInfo, DeviceInfoResult, DeviceType, EventCallbackFn, EventInfo,
50    EventInfoResult, KernelInfo, KernelInfoResult, OclPrm, OpenclVersion, PlatformInfo,
51    ProgramInfo, ProgramInfoResult, Status,
52};
53use std::cell::Ref;
54use std::fmt::Debug;
55use std::mem;
56use std::ptr;
57use std::slice;
58
59pub trait AsMem<T>
71where
72    T: OclPrm,
73{
74    fn as_mem(&self) -> &Mem;
75}
76
77impl<'a, T, M> AsMem<T> for &'a M
78where
79    T: OclPrm,
80    M: AsMem<T>,
81{
82    fn as_mem(&self) -> &Mem {
83        (**self).as_mem()
84    }
85}
86
87impl<'a, T, M> AsMem<T> for &'a mut M
88where
89    T: OclPrm,
90    M: AsMem<T>,
91{
92    fn as_mem(&self) -> &Mem {
93        (**self).as_mem()
94    }
95}
96
97pub unsafe trait MemCmdRw {}
126
127pub unsafe trait MemCmdAll {}
135
136pub trait ClVersions {
138    fn device_versions(&self) -> OclCoreResult<Vec<OpenclVersion>>;
139    fn platform_version(&self) -> OclCoreResult<OpenclVersion>;
140
141    fn verify_device_versions(&self, required_version: [u16; 2]) -> OclCoreResult<()> {
142        functions::verify_versions(
143            &self.device_versions()?,
144            required_version,
145            ApiFunction::None,
146            VersionKind::Device,
147        )
148    }
149
150    fn verify_platform_version(&self, required_version: [u16; 2]) -> OclCoreResult<()> {
151        let ver = [self.platform_version()?];
152        functions::verify_versions(
153            &ver,
154            required_version,
155            ApiFunction::None,
156            VersionKind::Platform,
157        )
158    }
159}
160
161impl ClVersions for cl_context {
162    fn device_versions(&self) -> OclCoreResult<Vec<OpenclVersion>> {
163        let devices = match functions::get_context_info(self, ContextInfo::Devices) {
164            Ok(ContextInfoResult::Devices(ds)) => Ok(ds),
165            Err(err) => Err(err),
166            _ => unreachable!(),
167        };
168
169        let devices = match devices {
170            Ok(d) => d,
171            Err(e) => return Err(e),
172        };
173
174        functions::device_versions(&devices)
175    }
176
177    fn platform_version(&self) -> OclCoreResult<OpenclVersion> {
178        let devices = match functions::get_context_info(self, ContextInfo::Devices) {
179            Ok(ContextInfoResult::Devices(ds)) => Ok(ds),
180            Err(err) => Err(err),
181            _ => unreachable!(),
182        };
183
184        let devices = match devices {
185            Ok(d) => d,
186            Err(e) => return Err(e),
187        };
188
189        devices[0].platform_version()
190    }
191}
192
193pub unsafe trait ClEventPtrRef<'e> {
195    unsafe fn as_ptr_ref(&'e self) -> &'e cl_event;
196}
197
198unsafe impl<'e> ClEventPtrRef<'e> for &'e cl_event {
199    unsafe fn as_ptr_ref(&'e self) -> &'e cl_event {
200        self
201    }
202}
203
204unsafe impl<'e, L> ClEventPtrRef<'e> for &'e L
205where
206    L: ClEventPtrRef<'e>,
207{
208    unsafe fn as_ptr_ref(&'e self) -> &'e cl_event {
209        (*self).as_ptr_ref()
210    }
211}
212
213pub unsafe trait ClNullEventPtr: Debug {
216    fn alloc_new(&mut self) -> *mut cl_event;
217    unsafe fn clone_from<E: AsRef<Event>>(&mut self, ev: E);
218}
219
220unsafe impl ClNullEventPtr for () {
221    fn alloc_new(&mut self) -> *mut cl_event {
222        panic!("Void events may not be used.");
223    }
224
225    unsafe fn clone_from<E: AsRef<Event>>(&mut self, _: E) {
226        panic!("Void events may not be used.");
227    }
228}
229
230pub unsafe trait ClWaitListPtr: Debug {
236    unsafe fn as_ptr_ptr(&self) -> *const cl_event;
238    fn count(&self) -> u32;
240}
241
242unsafe impl<'a, W> ClWaitListPtr for Ref<'a, W>
243where
244    W: ClWaitListPtr,
245{
246    unsafe fn as_ptr_ptr(&self) -> *const cl_event {
247        (*(*self)).as_ptr_ptr()
248    }
249
250    fn count(&self) -> u32 {
251        0 as u32
252    }
253}
254
255unsafe impl<'a> ClWaitListPtr for &'a [cl_event] {
256    unsafe fn as_ptr_ptr(&self) -> *const cl_event {
257        self.as_ptr()
258    }
259
260    fn count(&self) -> u32 {
261        self.len() as u32
262    }
263}
264
265unsafe impl<'a> ClWaitListPtr for &'a [Event] {
266    unsafe fn as_ptr_ptr(&self) -> *const cl_event {
267        self.as_ptr() as *const _ as *const cl_event
268    }
269
270    fn count(&self) -> u32 {
271        self.len() as u32
272    }
273}
274
275unsafe impl<'a> ClWaitListPtr for () {
276    unsafe fn as_ptr_ptr(&self) -> *const cl_event {
277        ptr::null() as *const _ as *const cl_event
278    }
279
280    fn count(&self) -> u32 {
281        0 as u32
282    }
283}
284
285pub unsafe trait ClPlatformIdPtr: Debug + Copy {
288    fn as_ptr(&self) -> cl_platform_id;
289}
290
291unsafe impl<'a, P> ClPlatformIdPtr for &'a P
292where
293    P: ClPlatformIdPtr,
294{
295    fn as_ptr(&self) -> cl_platform_id {
296        (*self).as_ptr()
297    }
298}
299
300unsafe impl ClPlatformIdPtr for () {
301    fn as_ptr(&self) -> cl_platform_id {
302        ptr::null_mut() as *mut _ as cl_platform_id
303    }
304}
305
306pub unsafe trait ClDeviceIdPtr: Debug + Copy {
309    fn as_ptr(&self) -> cl_device_id;
310}
311
312unsafe impl ClDeviceIdPtr for () {
313    fn as_ptr(&self) -> cl_device_id {
314        ptr::null_mut() as *mut _ as cl_device_id
315    }
316}
317
318pub unsafe trait ClContextPtr: Debug + Copy {
320    fn as_ptr(&self) -> cl_context;
321}
322
323unsafe impl ClContextPtr for cl_context {
324    fn as_ptr(&self) -> cl_context {
325        *self
326    }
327}
328
329unsafe impl<'a> ClContextPtr for &'a cl_context {
330    fn as_ptr(&self) -> cl_context {
331        **self
332    }
333}
334
335#[repr(C)]
342pub struct EventRefWrapper(cl_event);
343
344impl EventRefWrapper {
345    pub unsafe fn new(ptr: cl_event) -> EventRefWrapper {
346        EventRefWrapper(ptr)
347    }
348}
349
350unsafe impl<'e> ClEventPtrRef<'e> for EventRefWrapper {
351    unsafe fn as_ptr_ref(&'e self) -> &'e cl_event {
352        &self.0
353    }
354}
355
356#[repr(C)]
358#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
359pub struct PlatformId(cl_platform_id);
360
361impl PlatformId {
362    pub unsafe fn from_raw(ptr: cl_platform_id) -> PlatformId {
364        PlatformId(ptr)
366    }
367
368    pub unsafe fn null() -> PlatformId {
371        PlatformId(ptr::null_mut())
372    }
373
374    pub fn as_ptr(&self) -> cl_platform_id {
376        self.0
377    }
378
379    pub fn version(&self) -> OclCoreResult<OpenclVersion> {
381        if !self.0.is_null() {
382            functions::get_platform_info(self, PlatformInfo::Version)?.as_opencl_version()
383        } else {
384            Err("PlatformId::version(): This platform_id is invalid.".into())
385        }
386    }
387}
388
389unsafe impl ClPlatformIdPtr for PlatformId {
390    fn as_ptr(&self) -> cl_platform_id {
391        self.0
392    }
393}
394
395unsafe impl Sync for PlatformId {}
396unsafe impl Send for PlatformId {}
397
398impl ClVersions for PlatformId {
399    fn device_versions(&self) -> OclCoreResult<Vec<OpenclVersion>> {
400        let devices = functions::get_device_ids(self, Some(DeviceType::ALL), None)?;
401        functions::device_versions(&devices)
402    }
403
404    fn platform_version(&self) -> OclCoreResult<OpenclVersion> {
406        self.version()
407    }
408}
409
410#[repr(C)]
412#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
413pub struct DeviceId(cl_device_id);
414
415impl DeviceId {
416    pub unsafe fn from_raw(ptr: cl_device_id) -> DeviceId {
418        assert!(!ptr.is_null(), "Null pointer passed.");
419        DeviceId(ptr)
420    }
421
422    pub unsafe fn null() -> DeviceId {
425        DeviceId(ptr::null_mut())
426    }
427
428    pub fn as_raw(&self) -> cl_device_id {
430        self.0
431    }
432
433    pub fn version(&self) -> OclCoreResult<OpenclVersion> {
435        if !self.0.is_null() {
436            functions::get_device_info(self, DeviceInfo::Version)?.as_opencl_version()
437        } else {
438            Err("DeviceId::device_versions(): This device_id is invalid.".into())
439        }
440    }
441}
442
443unsafe impl ClDeviceIdPtr for DeviceId {
444    fn as_ptr(&self) -> cl_device_id {
445        self.0
446    }
447}
448
449unsafe impl<'a> ClDeviceIdPtr for &'a DeviceId {
450    fn as_ptr(&self) -> cl_device_id {
451        self.0
452    }
453}
454
455unsafe impl Sync for DeviceId {}
456unsafe impl Send for DeviceId {}
457
458impl ClVersions for DeviceId {
459    fn device_versions(&self) -> OclCoreResult<Vec<OpenclVersion>> {
460        self.version().map(|dv| vec![dv])
461    }
462
463    fn platform_version(&self) -> OclCoreResult<OpenclVersion> {
464        let platform = match functions::get_device_info(self, DeviceInfo::Platform) {
465            Ok(DeviceInfoResult::Platform(p)) => p,
466            Err(err) => return Err(err),
468            _ => unreachable!(),
469        };
470
471        functions::get_platform_info(&platform, PlatformInfo::Version)?.as_opencl_version()
472    }
473}
474
475#[repr(C)]
477#[derive(Debug)]
478pub struct Context(cl_context);
479
480impl Context {
481    pub unsafe fn from_raw_create_ptr(ptr: cl_context) -> Context {
484        assert!(!ptr.is_null(), "Null pointer passed.");
485        Context(ptr)
486    }
487
488    pub unsafe fn from_raw_copied_ptr(ptr: cl_context) -> Context {
491        assert!(!ptr.is_null(), "Null pointer passed.");
492        let copy = Context(ptr);
493        functions::retain_context(©).unwrap();
494        copy
495    }
496
497    pub fn as_ptr(&self) -> cl_context {
499        self.0
500    }
501
502    pub fn devices(&self) -> OclCoreResult<Vec<DeviceId>> {
504        match functions::get_context_info(self, ContextInfo::Devices) {
505            Ok(ContextInfoResult::Devices(ds)) => Ok(ds),
506            Err(err) => Err(err),
507            _ => unreachable!(),
508        }
509    }
510
511    pub fn platform(&self) -> OclCoreResult<Option<PlatformId>> {
517        functions::get_context_platform(self)
518    }
519}
520
521unsafe impl Sync for Context {}
522unsafe impl Send for Context {}
523
524impl Clone for Context {
525    fn clone(&self) -> Context {
526        unsafe {
527            functions::retain_context(self).unwrap();
528        }
529        Context(self.0)
530    }
531}
532
533impl Drop for Context {
534    fn drop(&mut self) {
541        unsafe {
542            if let Err(e) = functions::release_context(self as &Context) {
543                if let Some(Status::CL_INVALID_CONTEXT) = e.api_status() {
544                    return;
545                }
546                panic!("{:?}", e);
547            }
548        }
549    }
550}
551
552impl PartialEq<Context> for Context {
553    fn eq(&self, other: &Context) -> bool {
554        self.0 == other.0
555    }
556}
557
558unsafe impl<'a> ClContextPtr for &'a Context {
559    fn as_ptr(&self) -> cl_context {
560        self.0
561    }
562}
563
564impl ClVersions for Context {
565    fn device_versions(&self) -> OclCoreResult<Vec<OpenclVersion>> {
566        let devices = self.devices()?;
567        functions::device_versions(&devices)
568    }
569
570    fn platform_version(&self) -> OclCoreResult<OpenclVersion> {
571        let devices = self.devices()?;
572        devices[0].platform_version()
573    }
574}
575
576impl<'a> ClVersions for &'a Context {
577    fn device_versions(&self) -> OclCoreResult<Vec<OpenclVersion>> {
578        (*self).device_versions()
579    }
580
581    fn platform_version(&self) -> OclCoreResult<OpenclVersion> {
582        (*self).platform_version()
583    }
584}
585
586#[repr(C)]
588#[derive(Debug)]
589pub struct CommandQueue(cl_command_queue);
590
591impl CommandQueue {
592    pub unsafe fn from_raw_create_ptr(ptr: cl_command_queue) -> CommandQueue {
595        assert!(!ptr.is_null(), "Null pointer passed.");
596        CommandQueue(ptr)
597    }
598
599    pub unsafe fn from_raw_copied_ptr(ptr: cl_command_queue) -> CommandQueue {
602        assert!(!ptr.is_null(), "Null pointer passed.");
603        let copy = CommandQueue(ptr);
604        functions::retain_command_queue(©).unwrap();
605        copy
606    }
607
608    pub fn as_ptr(&self) -> cl_command_queue {
610        self.0
611    }
612
613    pub fn device(&self) -> OclCoreResult<DeviceId> {
615        match functions::get_command_queue_info(self, CommandQueueInfo::Device) {
616            Ok(CommandQueueInfoResult::Device(d)) => Ok(d),
617            Err(err) => Err(err),
618            _ => unreachable!(),
619        }
620    }
621
622    pub fn context(&self) -> OclCoreResult<Context> {
624        self.context_ptr()
625            .map(|ptr| unsafe { Context::from_raw_copied_ptr(ptr) })
626    }
627
628    pub fn context_ptr(&self) -> OclCoreResult<cl_context> {
630        functions::get_command_queue_context_ptr(self)
631    }
632}
633
634impl Clone for CommandQueue {
635    fn clone(&self) -> CommandQueue {
636        unsafe {
637            functions::retain_command_queue(self).unwrap();
638        }
639        CommandQueue(self.0)
640    }
641}
642
643impl Drop for CommandQueue {
644    fn drop(&mut self) {
645        unsafe {
646            functions::release_command_queue(self).unwrap();
647        }
648    }
649}
650
651impl AsRef<CommandQueue> for CommandQueue {
652    fn as_ref(&self) -> &CommandQueue {
653        self
654    }
655}
656
657unsafe impl<'a> ClContextPtr for &'a CommandQueue {
658    fn as_ptr(&self) -> cl_context {
659        self.context_ptr().expect(
660            "<&CommandQueue as ClContextPtr>::as_ptr: \
661            Unable to obtain a context pointer.",
662        )
663    }
664}
665
666unsafe impl Sync for CommandQueue {}
667unsafe impl Send for CommandQueue {}
668
669impl ClVersions for CommandQueue {
670    fn device_versions(&self) -> OclCoreResult<Vec<OpenclVersion>> {
671        let device = self.device()?;
672        device.version().map(|dv| vec![dv])
673    }
674
675    fn platform_version(&self) -> OclCoreResult<OpenclVersion> {
676        self.device()?.platform_version()
677    }
678}
679
680#[repr(C)]
682#[derive(Debug)]
683pub struct Mem(cl_mem);
684
685impl Mem {
686    pub unsafe fn from_raw_create_ptr(ptr: cl_mem) -> Mem {
689        assert!(!ptr.is_null(), "Null pointer passed.");
690        Mem(ptr)
691    }
692
693    pub unsafe fn from_raw_copied_ptr(ptr: cl_mem) -> Mem {
696        assert!(!ptr.is_null(), "Null pointer passed.");
697        let copy = Mem(ptr);
698        functions::retain_mem_object(©).unwrap();
699        copy
700    }
701
702    #[inline(always)]
704    pub fn as_ptr(&self) -> cl_mem {
705        self.0
706    }
707}
708
709impl Clone for Mem {
710    fn clone(&self) -> Mem {
711        unsafe {
712            functions::retain_mem_object(self).unwrap();
713        }
714        Mem(self.0)
715    }
716}
717
718impl Drop for Mem {
719    fn drop(&mut self) {
720        unsafe {
721            functions::release_mem_object(self).unwrap();
722        }
723    }
724}
725
726impl<T: OclPrm> AsMem<T> for Mem {
727    #[inline(always)]
728    fn as_mem(&self) -> &Mem {
729        self
730    }
731}
732
733unsafe impl<'a> MemCmdRw for Mem {}
734unsafe impl<'a> MemCmdRw for &'a Mem {}
735unsafe impl<'a> MemCmdRw for &'a mut Mem {}
736unsafe impl<'a> MemCmdRw for &'a &'a Mem {}
737unsafe impl<'a> MemCmdRw for &'a &'a mut Mem {}
738unsafe impl<'a> MemCmdAll for Mem {}
739unsafe impl<'a> MemCmdAll for &'a Mem {}
740unsafe impl<'a> MemCmdAll for &'a mut Mem {}
741unsafe impl<'a> MemCmdAll for &'a &'a Mem {}
742unsafe impl<'a> MemCmdAll for &'a &'a mut Mem {}
743unsafe impl Sync for Mem {}
744unsafe impl Send for Mem {}
745
746#[repr(C)]
752#[derive(Debug)]
753pub struct MemMap<T>(*mut T);
754
755impl<T: OclPrm> MemMap<T> {
756    #[inline(always)]
757    pub unsafe fn from_raw(ptr: *mut T) -> MemMap<T> {
760        assert!(!ptr.is_null(), "MemMap::from_raw: Null pointer passed.");
761        MemMap(ptr)
762    }
763
764    #[inline(always)]
765    pub fn as_ptr(&self) -> *const T {
766        self.0
767    }
768
769    #[inline(always)]
770    pub fn as_mut_ptr(&mut self) -> *mut T {
771        self.0
772    }
773
774    #[inline(always)]
775    pub fn as_void_ptr(&self) -> *mut c_void {
776        self.0 as *mut _ as *mut c_void
777    }
778
779    #[inline(always)]
780    pub unsafe fn as_slice<'a>(&self, len: usize) -> &'a [T] {
781        slice::from_raw_parts(self.0, len)
782    }
783
784    #[inline(always)]
785    pub unsafe fn as_slice_mut<'a>(&mut self, len: usize) -> &'a mut [T] {
786        slice::from_raw_parts_mut(self.0, len)
787    }
788}
789
790impl<T> AsMem<T> for MemMap<T>
791where
792    T: OclPrm,
793{
794    #[inline(always)]
795    fn as_mem(&self) -> &Mem {
796        unsafe { &*(self as *const _ as *const Mem) }
797    }
798}
799
800unsafe impl<T: OclPrm> MemCmdRw for MemMap<T> {}
801unsafe impl<'a, T: OclPrm> MemCmdRw for &'a MemMap<T> {}
802unsafe impl<'a, T: OclPrm> MemCmdRw for &'a mut MemMap<T> {}
803unsafe impl<T: OclPrm> Send for MemMap<T> {}
804#[repr(C)]
808#[derive(Debug)]
809pub struct Program(cl_program);
810
811impl Program {
812    pub unsafe fn from_raw_create_ptr(ptr: cl_program) -> Program {
815        assert!(!ptr.is_null(), "Null pointer passed.");
816        Program(ptr)
817    }
818
819    pub unsafe fn from_raw_copied_ptr(ptr: cl_program) -> Program {
822        assert!(!ptr.is_null(), "Null pointer passed.");
823        let copy = Program(ptr);
824        functions::retain_program(©).unwrap();
825        copy
826    }
827
828    #[inline(always)]
830    pub fn as_ptr(&self) -> cl_program {
831        self.0
832    }
833
834    pub fn devices(&self) -> OclCoreResult<Vec<DeviceId>> {
836        match functions::get_program_info(self, ProgramInfo::Devices) {
837            Ok(ProgramInfoResult::Devices(d)) => Ok(d),
838            Err(err) => Err(err),
839            _ => unreachable!(),
840        }
841    }
842}
843
844impl Clone for Program {
845    fn clone(&self) -> Program {
846        unsafe {
847            functions::retain_program(self).unwrap();
848        }
849        Program(self.0)
850    }
851}
852
853impl Drop for Program {
854    fn drop(&mut self) {
855        unsafe {
856            functions::release_program(self).unwrap();
857        }
858    }
859}
860
861unsafe impl Sync for Program {}
862unsafe impl Send for Program {}
863
864impl ClVersions for Program {
865    fn device_versions(&self) -> OclCoreResult<Vec<OpenclVersion>> {
866        let devices = self.devices()?;
867        functions::device_versions(&devices)
868    }
869
870    fn platform_version(&self) -> OclCoreResult<OpenclVersion> {
871        let devices = self.devices()?;
872        devices[0].platform_version()
873    }
874}
875
876#[repr(C)]
893#[derive(Debug)]
894pub struct Kernel(cl_kernel);
895
896impl Kernel {
897    pub unsafe fn from_raw_create_ptr(ptr: cl_kernel) -> Kernel {
900        assert!(!ptr.is_null(), "Null pointer passed.");
901        Kernel(ptr)
902    }
903
904    pub unsafe fn from_raw_copied_ptr(ptr: cl_kernel) -> Kernel {
909        assert!(!ptr.is_null(), "Null pointer passed.");
910        let copy = Kernel(ptr);
911        functions::retain_kernel(©).unwrap();
912        copy
913    }
914
915    #[inline(always)]
917    pub fn as_ptr(&self) -> cl_kernel {
918        self.0
919    }
920
921    pub fn program(&self) -> OclCoreResult<Program> {
923        match functions::get_kernel_info(self, KernelInfo::Program) {
924            Ok(KernelInfoResult::Program(d)) => Ok(d),
925            Err(err) => Err(err),
926            _ => unreachable!(),
927        }
928    }
929
930    pub fn devices(&self) -> OclCoreResult<Vec<DeviceId>> {
931        self.program().and_then(|p| p.devices())
932    }
933}
934
935impl Clone for Kernel {
936    fn clone(&self) -> Kernel {
937        unsafe {
938            functions::retain_kernel(self).unwrap();
939        }
940        Kernel(self.0)
941    }
942}
943
944impl Drop for Kernel {
945    fn drop(&mut self) {
946        unsafe {
947            functions::release_kernel(self).unwrap();
948        }
949    }
950}
951
952impl ClVersions for Kernel {
953    fn device_versions(&self) -> OclCoreResult<Vec<OpenclVersion>> {
954        let devices = self.program()?.devices()?;
955        functions::device_versions(&devices)
956    }
957
958    fn platform_version(&self) -> OclCoreResult<OpenclVersion> {
959        let devices = self.program()?.devices()?;
960        devices[0].platform_version()
961    }
962}
963
964unsafe impl Send for Kernel {}
965
966#[repr(C)]
968#[derive(Debug, Hash, PartialEq, Eq)]
969pub struct Event(cl_event);
970
971impl Event {
972    #[inline]
974    pub fn null() -> Event {
975        Event(0 as cl_event)
976    }
977
978    #[inline]
984    pub fn user<C: ClContextPtr>(context: C) -> OclCoreResult<Event> {
985        functions::create_user_event(context)
986    }
987
988    #[inline]
991    pub unsafe fn from_raw_create_ptr(ptr: cl_event) -> Event {
992        assert!(
993            !ptr.is_null(),
994            "ocl_core::Event::from_raw_create_ptr: Null pointer passed."
995        );
996        Event(ptr)
997    }
998
999    #[inline]
1002    pub unsafe fn from_raw_copied_ptr(ptr: cl_event) -> OclCoreResult<Event> {
1003        assert!(
1004            !ptr.is_null(),
1005            "ocl_core::Event::from_raw_copied_ptr: Null pointer passed."
1006        );
1007        let copy = Event(ptr);
1008        functions::retain_event(©)?;
1009        Ok(copy)
1010    }
1011
1012    #[inline]
1018    pub fn set_complete(&self) -> OclCoreResult<()> {
1019        functions::set_user_event_status(self, CommandExecutionStatus::Complete)
1020    }
1021
1022    #[inline]
1028    pub fn is_complete(&self) -> OclCoreResult<bool> {
1029        functions::event_is_complete(self)
1030    }
1031
1032    #[inline]
1034    pub fn wait_for(&self) -> OclCoreResult<()> {
1035        crate::wait_for_event(self)
1036    }
1037
1038    #[inline]
1041    pub fn is_null(&self) -> bool {
1042        self.0.is_null()
1043    }
1044
1045    #[inline]
1052    pub fn is_valid(&self) -> bool {
1053        !self.0.is_null()
1054    }
1055
1056    pub unsafe fn set_callback(
1074        &self,
1075        callback_receiver: EventCallbackFn,
1076        user_data_ptr: *mut c_void,
1077    ) -> OclCoreResult<()> {
1078        if self.is_valid() {
1079            crate::set_event_callback(
1080                self,
1081                CommandExecutionStatus::Complete,
1082                Some(callback_receiver),
1083                user_data_ptr as *mut _ as *mut c_void,
1084            )
1085        } else {
1086            Err(
1087                "ocl_core::Event::set_callback: This event is null. Cannot set callback until \
1088                internal event pointer is actually created by a `clCreate...` function."
1089                    .into(),
1090            )
1091        }
1092    }
1093
1094    pub fn context(&self) -> OclCoreResult<Context> {
1096        match functions::get_event_info(self, EventInfo::Context) {
1097            Ok(EventInfoResult::Context(c)) => Ok(c),
1098            Err(err) => Err(err),
1099            _ => unreachable!(),
1100        }
1101    }
1102
1103    #[inline]
1117    pub unsafe fn as_ptr_ref(&self) -> &cl_event {
1118        &self.0
1119    }
1120
1121    #[inline]
1135    pub unsafe fn as_ptr_mut(&mut self) -> &mut cl_event {
1136        &mut self.0
1137    }
1138
1139    pub fn into_raw(self) -> cl_event {
1147        let ptr = self.0;
1148        mem::forget(self);
1149        ptr
1150    }
1151
1152    #[inline]
1159    pub unsafe fn from_raw(ptr: cl_event) -> Event {
1160        assert!(!ptr.is_null(), "Null pointer passed.");
1161        Event(ptr)
1162    }
1163
1164    fn _alloc_new(&mut self) -> *mut cl_event {
1166        assert!(
1167            self.0.is_null(),
1168            "ocl_core::Event::alloc_new: An 'Event' cannot be \
1169            used as target for event creation (as a new event) more than once."
1170        );
1171        &mut self.0
1172    }
1173
1174    unsafe fn _as_ptr_ptr(&self) -> *const cl_event {
1176        if self.0.is_null() {
1177            ptr::null()
1178        } else {
1179            &self.0 as *const cl_event
1180        }
1181    }
1182
1183    fn _count(&self) -> u32 {
1185        if self.0.is_null() {
1186            0
1187        } else {
1188            1
1189        }
1190    }
1191}
1192
1193unsafe impl<'a> ClNullEventPtr for &'a mut Event {
1194    #[inline(always)]
1195    fn alloc_new(&mut self) -> *mut cl_event {
1196        self._alloc_new()
1197    }
1198
1199    #[inline(always)]
1200    unsafe fn clone_from<E: AsRef<Event>>(&mut self, ev: E) {
1201        let ptr = ev.as_ref().clone().into_raw();
1202        assert!(!ptr.is_null());
1203        self.0 = ptr;
1204    }
1205}
1206
1207unsafe impl ClWaitListPtr for Event {
1208    #[inline(always)]
1209    unsafe fn as_ptr_ptr(&self) -> *const cl_event {
1210        self._as_ptr_ptr()
1211    }
1212    #[inline(always)]
1213    fn count(&self) -> u32 {
1214        self._count()
1215    }
1216}
1217
1218unsafe impl<'a> ClWaitListPtr for &'a Event {
1219    #[inline(always)]
1220    unsafe fn as_ptr_ptr(&self) -> *const cl_event {
1221        self._as_ptr_ptr()
1222    }
1223    #[inline(always)]
1224    fn count(&self) -> u32 {
1225        self._count()
1226    }
1227}
1228
1229unsafe impl<'e> ClEventPtrRef<'e> for Event {
1230    #[inline(always)]
1231    unsafe fn as_ptr_ref(&'e self) -> &'e cl_event {
1232        &self.0
1233    }
1234}
1235
1236impl AsRef<Event> for Event {
1237    fn as_ref(&self) -> &Event {
1238        self
1239    }
1240}
1241
1242impl Clone for Event {
1243    fn clone(&self) -> Event {
1244        assert!(
1245            !self.0.is_null(),
1246            "ocl_core::Event::clone: \
1247            Cannot clone a null (empty) event."
1248        );
1249        unsafe {
1250            functions::retain_event(self).expect("core::Event::clone");
1251        }
1252        Event(self.0)
1253    }
1254}
1255
1256impl Drop for Event {
1257    fn drop(&mut self) {
1258        if !self.0.is_null() {
1259            unsafe {
1260                functions::release_event(self).unwrap();
1261            }
1262        }
1263    }
1264}
1265
1266unsafe impl Sync for Event {}
1268unsafe impl Send for Event {}
1269
1270#[repr(C)]
1272#[derive(Debug)]
1273pub struct Sampler(cl_sampler);
1274
1275impl Sampler {
1276    pub unsafe fn from_raw_create_ptr(ptr: cl_sampler) -> Sampler {
1279        assert!(!ptr.is_null(), "Null pointer passed.");
1280        Sampler(ptr)
1281    }
1282
1283    pub unsafe fn as_ptr(&self) -> cl_sampler {
1285        self.0
1286    }
1287}
1288
1289impl Clone for Sampler {
1290    fn clone(&self) -> Sampler {
1291        unsafe {
1292            functions::retain_sampler(self).unwrap();
1293        }
1294        Sampler(self.0)
1295    }
1296}
1297
1298impl Drop for Sampler {
1299    fn drop(&mut self) {
1300        unsafe {
1301            functions::release_sampler(self).unwrap();
1302        }
1303    }
1304}
1305
1306unsafe impl Sync for Sampler {}
1307unsafe impl Send for Sampler {}