summaryrefslogtreecommitdiffstats
path: root/src/com/jogamp/opencl/CLEventList.java
blob: da88aa05ed5bee6f41de343f70bb435c1f995b31 (plain)
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
package com.jogamp.opencl;

import com.jogamp.common.nio.PointerBuffer;
import java.util.Iterator;

/**
 * Fixed size list for storing CLEvents.
 * @author Michael Bien
 */
public final class CLEventList implements CLResource, AutoCloseable, Iterable<CLEvent> {

    private final CLEvent[] events;

    final PointerBuffer IDs;
    int size;

    public CLEventList(int capacity) {
        this.events = new CLEvent[capacity];
        this.IDs = PointerBuffer.allocateDirect(capacity);
    }

    public CLEventList(CLEvent... events) {
        this.events = events;
        this.IDs = PointerBuffer.allocateDirect(events.length);
        for (CLEvent event : events) {
            IDs.put(event.ID);
        }
        IDs.rewind();
        size = events.length;
    }

    void createEvent(CLContext context) {

        if(events[size] != null)
            events[size].release();

        events[size] = new CLEvent(context, IDs.get());
        size++;
    }

    /**
     * Releases all CLEvents in this list.
     */
    public void release() {
        for (int i = 0; i < size; i++) {
            events[i].release();
            events[i] = null;
        }
        size = 0;
        IDs.rewind();
    }

    public final void close() throws Exception {
        release();
    }

    public CLEvent getEvent(int index) {
        if(index >= size)
            throw new IndexOutOfBoundsException("list contains "+size+" events, can not return event with index "+index);
        return events[index];
    }

    /**
     * Returns the current size of this list.
     */
    public int size() {
        return size;
    }

    /**
     * Returns the maximum size of this list.
     */
    public int capacity() {
        return events.length;
    }

    public Iterator<CLEvent> iterator() {
        return new EventIterator(events, size);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getName()).append('[');
        for (int i = 0; i < size; i++) {
            sb.append(events[i].toString());
            if(i+1 != size) {
                sb.append(", ");
            }
        }
        return sb.append(']').toString();
    }

    private static class EventIterator implements Iterator<CLEvent> {

        private final CLEvent[] events;
        private final int size;
        private int index;

        private EventIterator(CLEvent[] events, int size) {
            this.events = events;
            this.size = size;
        }

        public boolean hasNext() {
            return index < size;
        }

        public CLEvent next() {
            if(hasNext())
                return events[index++];
            else
                return null;
        }

        public void remove() {
            throw new UnsupportedOperationException("remove() not supported.");
        }

    }

}