1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
//! Abstract data type wrappers.
//!
//! ### Reference
//!
//! The following table describes abstract data types supported by OpenCL
//! (from [SDK]):
//!
//! * cl_platform_id: The ID for a platform.
//! * cl_device_id: The ID for a device.
//! * cl_context: A context.
//! * cl_command_queue: A command queue.
//! * cl_mem: A memory object.
//! * cl_program: A program.
//! * cl_kernel: A kernel.
//! * cl_event: An event.
//! * cl_sampler: A sampler.
//!
//! The following new derived wrappers are also included in this module:
//!
//! * cl_events: A list of events.
//!
//!
//! ### Who cares. Why bother?
//!
//! These types ensure as best they can that stored pointers to any of the
//! above objects will be valid until that pointer is dropped by the Rust
//! runtime (which obviously is not a 100% guarantee).
//!
//! What this means is that you can share, clone, store, and throw away these
//! types, and any types that contain them, among multiple threads, for as
//! long as you'd like, with an insignificant amount of overhead, without
//! having to worry about the dangers of dereferencing those types later on.
//! As good as the OpenCL library generally is about this, it fails in many
//! cases to provide complete protection against segfaults due to
//! dereferencing old pointers particularly on certain *ahem* platforms.
//!
//!
//!
//! [SDK]: https://www.khronos.org/registry/cl/sdk/1.2/docs/man/xhtml/abstractDataTypes.html

use std::mem;
use std::ptr;
use std::fmt::Debug;
use std::marker::Sized;
use libc;
use ffi::{cl_platform_id, cl_device_id,  cl_context, cl_command_queue, cl_mem, cl_program,
    cl_kernel, cl_event, cl_sampler};
use ::{CommandExecutionStatus, OpenclVersion, PlatformInfo, DeviceInfo, DeviceInfoResult,
    ContextInfo, ContextInfoResult, CommandQueueInfo, CommandQueueInfoResult, ProgramInfo,
    ProgramInfoResult, KernelInfo, KernelInfoResult, Status};
use error::{Result as OclResult, Error as OclError};
use functions;
use util;

//=============================================================================
//================================ CONSTANTS ==================================
//=============================================================================

// TODO: Evaluate optimal parameters:
const EL_INIT_CAPACITY: usize = 64;
const EL_CLEAR_MAX_LEN: usize = 48;
const EL_CLEAR_INTERVAL: i32 = 32;
const EL_CLEAR_AUTO: bool = true;

const DEBUG_PRINT: bool = false;

//=============================================================================
//================================== TRAITS ===================================
//=============================================================================

/// Types with a fixed set of associated devices and an associated platform.
pub trait ClVersions {
    fn device_versions(&self) -> OclResult<Vec<OpenclVersion>>;
    fn platform_version(&self) -> OclResult<OpenclVersion>;

    fn verify_device_versions(&self, required_version: [u16; 2]) -> OclResult<()> {
        functions::verify_versions(&try!(self.device_versions()), required_version)
    }

    fn verify_platform_version(&self, required_version: [u16; 2]) -> OclResult<()> {
        let ver = [try!(self.platform_version())];
        functions::verify_versions(&ver, required_version)
    }
}


/// Types with a mutable pointer to a new, null raw event pointer.
pub unsafe trait ClEventPtrNew: Debug {
    fn ptr_mut_ptr_new(&mut self) -> OclResult<*mut cl_event>;
}

/// Types with a reference to a raw event pointer.
///
/// Using references just to be extra paranoid about copying raw pointers.
pub trait ClEventRef<'e> {
    unsafe fn as_ptr_ref(&'e self) -> &'e cl_event;
}

/// Types with a reference to a raw event array and an associated element
/// count.
///
/// Using references just to be extra paranoid about copying raw pointers.
pub unsafe trait ClWaitList: Debug {
    unsafe fn as_ptr_ptr(&self) -> *const cl_event;
    fn count (&self) -> u32;
}

impl<'e, L> ClEventRef<'e> for &'e L where L: ClEventRef<'e> {
    unsafe fn as_ptr_ref(&'e self) -> &'e cl_event {
        (*self).as_ptr_ref()
    }
}


/// Types with a reference to a raw platform_id pointer.
pub unsafe trait ClPlatformIdPtr: Sized {
    unsafe fn as_ptr(&self) -> cl_platform_id {
        debug_assert!(mem::size_of_val(self) == mem::size_of::<PlatformId>());
        // mem::transmute_copy()
        let core = self as *const Self as *const _ as *const PlatformId;
        (*core).as_ptr()
    }
}

/// Types with a reference to a raw device_id pointer.
pub unsafe trait ClDeviceIdPtr: Sized {
    unsafe fn as_ptr(&self) -> cl_device_id {
        debug_assert!(mem::size_of_val(self) == mem::size_of::<DeviceId>());
        // mem::transmute_copy(self)
        let core = self as *const Self as *const _ as *const DeviceId;
        (*core).as_ptr()
    }
}

//=============================================================================
//=================================== TYPES ===================================
//=============================================================================

/// Wrapper used by `EventList` to send event pointers to core functions
/// cheaply.
pub struct EventRefWrapper<'e>(&'e cl_event, u32);

impl<'e> ClEventRef<'e> for EventRefWrapper<'e> {
    unsafe fn as_ptr_ref(&'e self) -> &'e cl_event {
        self.0
    }
}




/// cl_platform_id
#[repr(C)]
#[derive(Clone, Copy, Debug, Hash)]
pub struct PlatformId(cl_platform_id);

impl PlatformId {
    /// Only call this when passing a newly created pointer directly from
    /// `clCreate...`. Do not use this to clone or copy.
    pub unsafe fn from_fresh_ptr(ptr: cl_platform_id) -> PlatformId {
        PlatformId(ptr)
    }

    pub unsafe fn null() -> PlatformId {
        PlatformId(0 as *mut libc::c_void)
    }

    /// Returns a pointer.
    pub unsafe fn as_ptr(&self) -> cl_platform_id {
        self.0
    }

    /// Returns the looked up and parsed OpenCL version for this platform.
    pub fn version(&self) -> OclResult<OpenclVersion> {
        if !self.0.is_null() {
            functions::get_platform_info(self, PlatformInfo::Version).as_opencl_version()
        } else {
            OclError::err("PlatformId::version(): This platform_id is invalid.")
        }
    }
}

unsafe impl ClPlatformIdPtr for PlatformId {}
unsafe impl<'a> ClPlatformIdPtr for &'a PlatformId {}
unsafe impl Sync for PlatformId {}
unsafe impl Send for PlatformId {}

impl PartialEq<PlatformId> for PlatformId {
    fn eq(&self, other: &PlatformId) -> bool {
        self.0 == other.0
    }
}

impl ClVersions for PlatformId {
    fn device_versions(&self) -> OclResult<Vec<OpenclVersion>> {
        let devices = try!(functions::get_device_ids(self, Some(::DEVICE_TYPE_ALL), None));
        functions::device_versions(&devices)
    }

    // [FIXME]: TEMPORARY
    fn platform_version(&self) -> OclResult<OpenclVersion> {
        self.version()
    }
}



/// cl_device_id
#[repr(C)]
#[derive(Clone, Copy, Debug, Hash)]
pub struct DeviceId(cl_device_id);

impl DeviceId {
    /// Only call this when passing a newly created pointer directly from
    /// `clCreate...`. Do not use this to clone or copy.
    pub unsafe fn from_fresh_ptr(ptr: cl_device_id) -> DeviceId {
        DeviceId(ptr)
    }

    /// Only call this when passing a copied pointer such as from an
    /// `clGet*****Info` function.
    pub unsafe fn from_copied_ptr(ptr: cl_device_id) -> DeviceId {
        DeviceId(ptr)
    }

    pub unsafe fn null() -> DeviceId {
        DeviceId(0 as *mut libc::c_void)
    }

    /// Returns a pointer.
    pub unsafe fn as_ptr(&self) -> cl_device_id {
        self.0
    }

    /// Returns the looked up and parsed OpenCL version for this device.
    pub fn version(&self) -> OclResult<OpenclVersion> {
        if !self.0.is_null() {
            functions::get_device_info(self, DeviceInfo::Version).as_opencl_version()
        } else {
            OclError::err("DeviceId::device_versions(): This device_id is invalid.")
        }
    }
}

unsafe impl ClDeviceIdPtr for DeviceId {}
unsafe impl<'a> ClDeviceIdPtr for &'a DeviceId {}
unsafe impl Sync for DeviceId {}
unsafe impl Send for DeviceId {}

impl PartialEq<DeviceId> for DeviceId {
    fn eq(&self, other: &DeviceId) -> bool {
        self.0 == other.0
    }
}

impl ClVersions for DeviceId {
    fn device_versions(&self) -> OclResult<Vec<OpenclVersion>> {
        self.version().map(|dv| vec![dv])
    }

    fn platform_version(&self) -> OclResult<OpenclVersion> {
        let platform = match functions::get_device_info(self, DeviceInfo::Platform) {
            DeviceInfoResult::Platform(p) => p,
            DeviceInfoResult::Error(e) => return Err(OclError::from(*e)),
            _ => unreachable!(),
        };

        functions::get_platform_info(&platform, PlatformInfo::Version).as_opencl_version()
    }
}



/// cl_context
#[derive(Debug)]
pub struct Context(cl_context);

impl Context {
    /// Only call this when passing a newly created pointer directly from
    /// `clCreate...`. Do not use this to clone or copy.
    pub unsafe fn from_fresh_ptr(ptr: cl_context) -> Context {
        Context(ptr)
    }

    /// Only call this when passing a copied pointer such as from an
    /// `clGet*****Info` function.
    pub unsafe fn from_copied_ptr(ptr: cl_context) -> Context {
        let copy = Context(ptr);
        functions::retain_context(&copy).unwrap();
        copy
    }

    /// Returns a pointer, do not store it.
    pub unsafe fn as_ptr(&self) -> cl_context {
        self.0
    }

    /// Returns the devices associated with this context.
    fn devices(&self) -> OclResult<Vec<DeviceId>> {
        match functions::get_context_info(self, ContextInfo::Devices) {
            ContextInfoResult::Devices(ds) => Ok(ds),
            ContextInfoResult::Error(e) => return Err(OclError::from(*e)),
            _ => unreachable!(),
        }
    }
}

unsafe impl Sync for Context {}
unsafe impl Send for Context {}

impl Clone for Context {
    fn clone(&self) -> Context {
        unsafe { functions::retain_context(self).unwrap(); }
        Context(self.0)
    }
}

impl Drop for Context {
    /// Panics in the event of an error of type `Error::Status` except when
    /// the status code is `CL_INVALID_CONTEXT` (which is ignored).
    ///
    /// This is done because certain platforms error with `CL_INVALID_CONTEXT`
    /// for unknown reasons and as far as we know can be safely ignored.
    ///
    fn drop(&mut self) {
        unsafe {
            if let Err(e) = functions::release_context(self) {
                if let OclError::Status { ref status, .. } = e {
                    if status == &Status::CL_INVALID_CONTEXT {
                        return;
                    }
                }
                panic!("{:?}", e);
            }
        }
    }
}

impl PartialEq<Context> for Context {
    fn eq(&self, other: &Context) -> bool {
        self.0 == other.0
    }
}

impl ClVersions for Context {
    fn device_versions(&self) -> OclResult<Vec<OpenclVersion>> {
        let devices = try!(self.devices());
        functions::device_versions(&devices)
    }

    fn platform_version(&self) -> OclResult<OpenclVersion> {
        let devices = try!(self.devices());
        devices[0].platform_version()
    }


}


/// cl_command_queue
#[derive(Debug)]
pub struct CommandQueue(cl_command_queue);

impl CommandQueue {
    /// Only call this when passing a newly created pointer directly from
    /// `clCreate...`. Do not use this to clone or copy.
    pub unsafe fn from_fresh_ptr(ptr: cl_command_queue) -> CommandQueue {
        CommandQueue(ptr)
    }

    /// Only call this when passing a copied pointer such as from an
    /// `clGet*****Info` function.
    pub unsafe fn from_copied_ptr(ptr: cl_command_queue) -> CommandQueue {
        let copy = CommandQueue(ptr);
        functions::retain_command_queue(&copy).unwrap();
        copy
    }

    /// Returns a pointer, do not store it.
    pub unsafe fn as_ptr(&self) -> cl_command_queue {
        self.0
    }

    /// Returns the `DeviceId` associated with this command queue.
    pub fn device(&self) -> OclResult<DeviceId> {
        match functions::get_command_queue_info(self, CommandQueueInfo::Device) {
            CommandQueueInfoResult::Device(d) => Ok(d),
            CommandQueueInfoResult::Error(e) => Err(OclError::from(*e)),
            _ => unreachable!(),
        }
    }
}

impl Clone for CommandQueue {
    fn clone(&self) -> CommandQueue {
        unsafe { functions::retain_command_queue(self).unwrap(); }
        CommandQueue(self.0)
    }
}

impl Drop for CommandQueue {
    fn drop(&mut self) {
        unsafe { functions::release_command_queue(self).unwrap(); }
    }
}

impl AsRef<CommandQueue> for CommandQueue {
    fn as_ref(&self) -> &CommandQueue {
        self
    }
}

unsafe impl Sync for CommandQueue {}
unsafe impl Send for CommandQueue {}

impl ClVersions for CommandQueue{
    fn device_versions(&self) -> OclResult<Vec<OpenclVersion>> {
        let device = try!(self.device());
        device.version().map(|dv| vec![dv])
    }

    fn platform_version(&self) -> OclResult<OpenclVersion> {
        try!(self.device()).platform_version()
    }
}



/// cl_mem
#[derive(Debug)]
pub struct Mem(cl_mem);

impl Mem {
    /// Only call this when passing a newly created pointer directly from
    /// `clCreate...`. Do not use this to clone or copy.
    pub unsafe fn from_fresh_ptr(ptr: cl_mem) -> Mem {
        Mem(ptr)
    }

	/// Only call this when passing a copied pointer such as from an
	/// `clGet*****Info` function.
	pub unsafe fn from_copied_ptr(ptr: cl_mem) -> Mem {
		let copy = Mem(ptr);
		functions::retain_mem_object(&copy).unwrap();
		copy
	}

	// pub unsafe fn null() -> Mem {
	// 	Mem(0 as *mut libc::c_void, PhantomData)
	// }

    /// Returns a pointer, do not store it.
    pub unsafe fn as_ptr(&self) -> cl_mem {
        self.0
    }
}

impl Clone for Mem {
    fn clone(&self) -> Mem {
        unsafe { functions::retain_mem_object(self).unwrap(); }
        Mem(self.0)
    }
}

impl Drop for Mem {
    fn drop(&mut self) {
        unsafe { functions::release_mem_object(self).unwrap(); }
    }
}

unsafe impl Sync for Mem {}
unsafe impl Send for Mem {}



/// cl_program
#[derive(Debug)]
pub struct Program(cl_program);

impl Program {
    /// Only call this when passing a newly created pointer directly from
    /// `clCreate...`. Do not use this to clone or copy.
    pub unsafe fn from_fresh_ptr(ptr: cl_program) -> Program {
        Program(ptr)
    }

	/// Only call this when passing a copied pointer such as from an
	/// `clGet*****Info` function.
	pub unsafe fn from_copied_ptr(ptr: cl_program) -> Program {
		let copy = Program(ptr);
		functions::retain_program(&copy).unwrap();
		copy
	}

	/// Returns a pointer, do not store it.
	pub unsafe fn as_ptr(&self) -> cl_program {
		self.0
	}

    /// Returns the devices associated with this program.
    pub fn devices(&self) -> OclResult<Vec<DeviceId>> {
        match functions::get_program_info(self, ProgramInfo::Devices) {
            ProgramInfoResult::Devices(d) => Ok(d),
            ProgramInfoResult::Error(e) => Err(OclError::from(*e)),
            _ => unreachable!(),
        }
    }
}

impl Clone for Program {
    fn clone(&self) -> Program {
        unsafe { functions::retain_program(self).unwrap(); }
        Program(self.0)
    }
}

impl Drop for Program {
    fn drop(&mut self) {
        unsafe { functions::release_program(self).unwrap(); }
    }
}

unsafe impl Sync for Program {}
unsafe impl Send for Program {}

impl ClVersions for Program {
    fn device_versions(&self) -> OclResult<Vec<OpenclVersion>> {
        let devices = try!(self.devices());
        functions::device_versions(&devices)
    }

    fn platform_version(&self) -> OclResult<OpenclVersion> {
        let devices = try!(self.devices());
        devices[0].platform_version()
    }
}


/// cl_kernel
///
/// ### Thread Safety
///
/// Not thread safe: do not implement `Send` or `Sync`.
///
/// It's possible to do with some work but it's not worth the bother, just
/// make another identical kernel in the other thread and call it good.
///
///
#[derive(Debug)]
pub struct Kernel(cl_kernel);

impl Kernel {
    /// Only call this when passing a newly created pointer directly from
    /// `clCreate...`. Do not use this to clone or copy.
    pub unsafe fn from_fresh_ptr(ptr: cl_kernel) -> Kernel {
        Kernel(ptr)
    }

    /// Returns a pointer, do not store it.
    pub unsafe fn as_ptr(&self) -> cl_kernel {
        self.0
    }

    /// Returns the program associated with this kernel.
    pub fn program(&self) -> OclResult<Program> {
        match functions::get_kernel_info(self, KernelInfo::Program) {
            KernelInfoResult::Program(d) => Ok(d),
            KernelInfoResult::Error(e) => Err(OclError::from(*e)),
            _ => unreachable!(),
        }
    }
}

impl Clone for Kernel {
    fn clone(&self) -> Kernel {
        unsafe { functions::retain_kernel(self).unwrap(); }
        Kernel(self.0)
    }
}

impl Drop for Kernel {
    fn drop(&mut self) {
        unsafe { functions::release_kernel(self).unwrap(); }
    }
}

impl ClVersions for Kernel {
    fn device_versions(&self) -> OclResult<Vec<OpenclVersion>> {
        let devices = try!(try!(self.program()).devices());
        functions::device_versions(&devices)
    }

    fn platform_version(&self) -> OclResult<OpenclVersion> {
        let devices = try!(try!(self.program()).devices());
        devices[0].platform_version()
    }
}



/// cl_event
#[derive(Debug)]
pub struct Event(cl_event);

impl Event {
    /// Only call this when passing a newly created pointer directly from
    /// `clCreate...`. Do not use this to clone or copy.
    pub unsafe fn from_fresh_ptr(ptr: cl_event) -> Event {
        Event(ptr)
    }

    /// Only use when cloning from a pre-existing and valid `cl_event`.
    pub unsafe fn from_cloned_ptr(ptr: cl_event) -> OclResult<Event> {
        let new_core = Event(ptr);

        if new_core.is_valid() {
            try!(functions::retain_event(&new_core));
            Ok(new_core)
        } else {
            OclError::err("core::Event::from_cloned_ptr: Invalid pointer `ptr`.")
        }
    }

    /// For passage directly to an 'event creation' function (such as enqueue).
    pub unsafe fn null() -> Event {
        Event(0 as cl_event)
    }

    // /// Returns a pointer, do not store it unless you will manage its
    // /// associated reference count carefully (as does `EventList`).
    // pub unsafe fn as_ptr(&self) -> cl_event {
    //  self.0
    // }

    /// Returns an immutable reference to a pointer, do not deref and store it unless
    /// you will manage its associated reference count carefully.
    pub unsafe fn as_ptr_ref(&self) -> &cl_event {
        &self.0
    }

    /// Returns a mutable reference to a pointer, do not deref then modify or store it
    /// unless you will manage its associated reference count carefully.
    pub unsafe fn as_ptr_mut(&mut self) -> &mut cl_event {
        &mut self.0
    }

    /// [FIXME]: ADD VALIDITY CHECK BY CALLING '_INFO' OR SOMETHING:
    /// NULL CHECK IS NOT ENOUGH
    pub fn is_valid(&self) -> bool {
        !self.0.is_null()
    }
}

unsafe impl ClEventPtrNew for Event {
    fn ptr_mut_ptr_new(&mut self) -> OclResult<*mut cl_event> {
        if self.0.is_null() {
            Ok(&mut self.0)
        } else {
            unsafe { try!(functions::release_event(self)); }
            Ok(&mut self.0)
        }
    }
}

impl<'e> ClEventRef<'e> for Event {
    unsafe fn as_ptr_ref(&'e self) -> &'e cl_event {
        &self.0
    }
}

unsafe impl ClWaitList for Event {
    unsafe fn as_ptr_ptr(&self) -> *const cl_event {
        if self.0.is_null() { 0 as *const cl_event } else { &self.0 as *const cl_event }
    }

    fn count(&self) -> u32 {
        if self.0.is_null() { 0 } else { 1 }
    }
}

impl Clone for Event {
    fn clone(&self) -> Event {
        unsafe { functions::retain_event(self).expect("core::Event::clone"); }
        Event(self.0)
    }
}

impl Drop for Event {
    fn drop(&mut self) {
        if self.is_valid() {
            if cfg!(release) {
                unsafe { functions::release_event(self).unwrap(); }
            } else {
                unsafe { functions::release_event(self).ok(); }
            }
        }
    }
}

// unsafe impl EventPtr for Event {}
unsafe impl Sync for Event {}
unsafe impl Send for Event {}



/// List of `cl_event`s.
#[derive(Debug)]
pub struct EventList {
    event_ptrs: Vec<cl_event>,
    clear_max_len: usize,
    clear_counter_max: i32,
    clear_auto: bool,
    clear_counter: i32,
}

impl EventList {
    /// Returns a new, empty, `EventList`.
    pub fn new() -> EventList {
        EventList {
            event_ptrs: Vec::with_capacity(EL_INIT_CAPACITY),
            clear_max_len: EL_CLEAR_MAX_LEN,
            clear_counter_max: EL_CLEAR_INTERVAL,
            clear_auto: EL_CLEAR_AUTO,
            clear_counter: 0,
        }
    }

    /// Pushes a new event onto the list.
    ///
    /// Technically, copies `event`'s contained pointer (a `cl_event`) then
    /// `mem::forget`s it. This seems preferrable to incrementing the reference
    /// count (with `functions::retain_event`) then letting `event` drop which just decrements it right back.
    pub fn push(&mut self, event: Event) {
        assert!(event.is_valid());

        unsafe {
            self.event_ptrs.push((*event.as_ptr_ref()));
            mem::forget(event);
        }
        self.decr_counter();
    }

    /// Appends a new null element to the end of the list and returns a reference to it.
    pub fn allot(&mut self) -> &mut cl_event {
        self.event_ptrs.push(0 as cl_event);
        self.event_ptrs.last_mut().unwrap()
    }

    pub fn len(&self) -> usize {
        self.event_ptrs.len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn count(&self) -> u32 {
        self.event_ptrs.len() as u32
    }

    /// Returns an immutable reference to a pointer, do not deref and store it unless
    /// you will manage its associated reference count carefully.
    pub unsafe fn as_ptr_ref(&self) -> &cl_event {
        self.event_ptrs.first().expect("ocl::core::EventList::as_ptr_ref(): \
            Attempted to take a reference to the first element of an empty list.")
    }

    /// Clones an event by index.
    pub fn get_clone(&self, index: usize) -> Option<OclResult<Event>> {
        self.event_ptrs.get(index).map(|ptr| unsafe { Event::from_cloned_ptr(*ptr) } )
    }

    /// Clones the last event.
    pub fn last_clone(&self) -> Option<OclResult<Event>> {
        self.event_ptrs.last().map(|ptr| unsafe { Event::from_cloned_ptr(*ptr) } )
    }

    /// Clears each completed event from the list.
    ///
    /// TODO: TEST THIS
    pub fn clear_completed(&mut self) -> OclResult<()> {
        if self.len() < 16 { return Ok(()) }

        let mut cmpltd_events: Vec<usize> = Vec::with_capacity(EL_CLEAR_MAX_LEN);

        for (idx, event_ptr) in self.event_ptrs.iter().enumerate() {
            let status = try!(functions::get_event_status(&EventRefWrapper(event_ptr, 1)));

            if status == CommandExecutionStatus::Complete {
                cmpltd_events.push(idx)
            }
        }

        // Release completed events:
        for &idx in &cmpltd_events {
            unsafe {
                try!(functions::release_event(&EventRefWrapper(&self.event_ptrs[idx], 1)));
            }
        }

        try!(util::vec_remove_rebuild(&mut self.event_ptrs, &cmpltd_events[..], 2));

        if EL_CLEAR_AUTO {
            self.clear_counter = EL_CLEAR_INTERVAL;
        }

        Ok(())
    }


    // /// Merges the copied contents of this list and another into a new list and returns it.
    // pub fn union(&self, other_list: &EventList) -> EventList {
    //     let new_cap = other_list.events().len() + self.events.len() + EXTRA_CAPACITY;

    //     let mut new_list = EventList {
    //         events: Vec::with_capacity(new_cap),
    //         clear_counter: 0,
    //     };

    //     new_list.events.extend(self.events().iter().cloned());
    //     new_list.events.extend(other_list.events().iter().cloned());

    //     if AUTO_CLEAR {
    //         new_list.clear_completed();
    //     }

    //     new_list
    // }

    /// Counts down the auto-list-clear counter.
    fn decr_counter(&mut self) {
        if EL_CLEAR_AUTO {
            self.clear_counter -= 1;

            if self.clear_counter <= 0 && self.event_ptrs.len() > EL_CLEAR_MAX_LEN {
                // self.clear_completed();
                unimplemented!();
            }
        }
    }
}

unsafe impl ClEventPtrNew for EventList {
    fn ptr_mut_ptr_new(&mut self) -> OclResult<*mut cl_event> {
        Ok(self.allot())
    }
}

unsafe impl ClWaitList for EventList {
    unsafe fn as_ptr_ptr(&self) -> *const cl_event {
        match self.event_ptrs.first() {
            Some(ele) => ele as *const cl_event,
            None => ptr::null(),
        }
    }

    fn count(&self) -> u32 {
        self.event_ptrs.len() as u32
    }
}

impl Clone for EventList {
    /// Clones this list in a thread safe manner.
    fn clone(&self) -> EventList {
        for event_ptr in &self.event_ptrs {
            if !(*event_ptr).is_null() {
                unsafe { functions::retain_event(&EventRefWrapper(event_ptr, 1))
                    .expect("core::EventList::clone") }
            }
        }

        EventList {
            event_ptrs: self.event_ptrs.clone(),
            clear_max_len: self.clear_max_len,
            clear_counter_max: self.clear_counter_max,
            clear_auto: self.clear_auto,
            clear_counter: self.clear_counter,
        }
    }
}

impl Drop for EventList {
    fn drop(&mut self) {
        if DEBUG_PRINT { print!("Dropping events... "); }
        for event_ptr in &self.event_ptrs {
            if cfg!(release) {
                unsafe { functions::release_event(&EventRefWrapper(event_ptr, 1)).unwrap(); }
            } else {
                unsafe { functions::release_event(&EventRefWrapper(event_ptr, 1)).ok(); }
            }
            if DEBUG_PRINT { print!("{{.}}"); }
        }
        if DEBUG_PRINT { print!("\n"); }
    }
}

impl AsRef<EventList> for EventList {
    fn as_ref(&self) -> &EventList {
        self
    }
}

unsafe impl Sync for EventList {}
unsafe impl Send for EventList {}



/// cl_sampler
#[derive(Debug)]
pub struct Sampler(cl_sampler);

impl Sampler {
    /// Only call this when passing a newly created pointer directly from
    /// `clCreate...`. Do not use this to clone or copy.
    pub unsafe fn from_fresh_ptr(ptr: cl_sampler) -> Sampler {
        Sampler(ptr)
    }

    /// Returns a pointer, do not store it.
    pub unsafe fn as_ptr(&self) -> cl_sampler {
        self.0
    }
}

impl Clone for Sampler {
    fn clone(&self) -> Sampler {
        unsafe { functions::retain_sampler(self).unwrap(); }
        Sampler(self.0)
    }
}

impl Drop for Sampler {
    fn drop(&mut self) {
        unsafe { functions::release_sampler(self).unwrap(); }
    }
}

unsafe impl Sync for Sampler {}
unsafe impl Send for Sampler {}