summaryrefslogtreecommitdiffstats
path: root/include/media/RingBuffer.h
blob: df7c00eeb0437183eb73ed482aab876797bb0441 (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
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
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#ifndef ANDROID_SERVICE_UTILS_RING_BUFFER_H
#define ANDROID_SERVICE_UTILS_RING_BUFFER_H

#include <utils/Log.h>
#include <cutils/compiler.h>

#include <iterator>
#include <utility>
#include <vector>

namespace android {

/**
 * A RingBuffer class that maintains an array of objects that can grow up to a certain size.
 * Elements added to the RingBuffer are inserted in the logical front of the buffer, and
 * invalidate all current iterators for that RingBuffer object.
 */
template <class T>
class RingBuffer final {
public:

    /**
     * Construct a RingBuffer that can grow up to the given length.
     */
    RingBuffer(size_t length);

    /**
     * Forward iterator to this class.  Implements an std:forward_iterator.
     */
    class iterator : public std::iterator<std::forward_iterator_tag, T> {
    public:
        iterator(T* ptr, size_t size, size_t pos, size_t ctr);

        iterator& operator++();

        iterator operator++(int);

        bool operator==(const iterator& rhs);

        bool operator!=(const iterator& rhs);

        T& operator*();

        T* operator->();

    private:
        T* mPtr;
        size_t mSize;
        size_t mPos;
        size_t mCtr;
    };

    /**
     * Constant forward iterator to this class.  Implements an std:forward_iterator.
     */
    class const_iterator : public std::iterator<std::forward_iterator_tag, T> {
    public:
        const_iterator(const T* ptr, size_t size, size_t pos, size_t ctr);

        const_iterator& operator++();

        const_iterator operator++(int);

        bool operator==(const const_iterator& rhs);

        bool operator!=(const const_iterator& rhs);

        const T& operator*();

        const T* operator->();

    private:
        const T* mPtr;
        size_t mSize;
        size_t mPos;
        size_t mCtr;
    };

    /**
     * Adds item to the front of this RingBuffer.  If the RingBuffer is at its maximum length,
     * this will result in the last element being replaced (this is done using the element's
     * assignment operator).
     *
     * All current iterators are invalidated.
     */
    void add(const T& item);

    /**
     * Moves item to the front of this RingBuffer.  Following a call to this, item should no
     * longer be used.  If the RingBuffer is at its maximum length, this will result in the
     * last element being replaced (this is done using the element's assignment operator).
     *
     * All current iterators are invalidated.
     */
    void add(T&& item);

    /**
     * Construct item in-place in the front of this RingBuffer using the given arguments.  If
     * the RingBuffer is at its maximum length, this will result in the last element being
     * replaced (this is done using the element's assignment operator).
     *
     * All current iterators are invalidated.
     */
    template <class... Args>
    void emplace(Args&&... args);

    /**
     * Get an iterator to the front of this RingBuffer.
     */
    iterator begin();

    /**
     * Get an iterator to the end of this RingBuffer.
     */
    iterator end();

    /**
     * Get a const_iterator to the front of this RingBuffer.
     */
    const_iterator begin() const;

    /**
     * Get a const_iterator to the end of this RingBuffer.
     */
    const_iterator end() const;

    /**
     * Return a reference to the element at a given index.  If the index is out of range for
     * this ringbuffer, [0, size), the behavior for this is undefined.
     */
    T& operator[](size_t index);

    /**
     * Return a const reference to the element at a given index.  If the index is out of range
     * for this ringbuffer, [0, size), the behavior for this is undefined.
     */
    const T& operator[](size_t index) const;

    /**
     * Return the current size of this RingBuffer.
     */
    size_t size() const;

    /**
     * Remove all elements from this RingBuffer and set the size to 0.
     */
    void clear();

private:
    size_t mFrontIdx;
    size_t mMaxBufferSize;
    std::vector<T> mBuffer;
}; // class RingBuffer


template <class T>
RingBuffer<T>::RingBuffer(size_t length) : mFrontIdx{0}, mMaxBufferSize{length} {}

template <class T>
RingBuffer<T>::iterator::iterator(T* ptr, size_t size, size_t pos, size_t ctr) :
        mPtr{ptr}, mSize{size}, mPos{pos}, mCtr{ctr} {}

template <class T>
typename RingBuffer<T>::iterator& RingBuffer<T>::iterator::operator++() {
    ++mCtr;

    if (CC_UNLIKELY(mCtr == mSize)) {
        mPos = mSize;
        return *this;
    }

    mPos = ((CC_UNLIKELY(mPos == 0)) ? mSize - 1 : mPos - 1);
    return *this;
}

template <class T>
typename RingBuffer<T>::iterator RingBuffer<T>::iterator::operator++(int) {
    iterator tmp{mPtr, mSize, mPos, mCtr};
    ++(*this);
    return tmp;
}

template <class T>
bool RingBuffer<T>::iterator::operator==(const iterator& rhs) {
    return (mPtr + mPos) == (rhs.mPtr + rhs.mPos);
}

template <class T>
bool RingBuffer<T>::iterator::operator!=(const iterator& rhs) {
    return (mPtr + mPos) != (rhs.mPtr + rhs.mPos);
}

template <class T>
T& RingBuffer<T>::iterator::operator*() {
    return *(mPtr + mPos);
}

template <class T>
T* RingBuffer<T>::iterator::operator->() {
    return mPtr + mPos;
}

template <class T>
RingBuffer<T>::const_iterator::const_iterator(const T* ptr, size_t size, size_t pos, size_t ctr) :
        mPtr{ptr}, mSize{size}, mPos{pos}, mCtr{ctr} {}

template <class T>
typename RingBuffer<T>::const_iterator& RingBuffer<T>::const_iterator::operator++() {
    ++mCtr;

    if (CC_UNLIKELY(mCtr == mSize)) {
        mPos = mSize;
        return *this;
    }

    mPos = ((CC_UNLIKELY(mPos == 0)) ? mSize - 1 : mPos - 1);
    return *this;
}

template <class T>
typename RingBuffer<T>::const_iterator RingBuffer<T>::const_iterator::operator++(int) {
    const_iterator tmp{mPtr, mSize, mPos, mCtr};
    ++(*this);
    return tmp;
}

template <class T>
bool RingBuffer<T>::const_iterator::operator==(const const_iterator& rhs) {
    return (mPtr + mPos) == (rhs.mPtr + rhs.mPos);
}

template <class T>
bool RingBuffer<T>::const_iterator::operator!=(const const_iterator& rhs) {
    return (mPtr + mPos) != (rhs.mPtr + rhs.mPos);
}

template <class T>
const T& RingBuffer<T>::const_iterator::operator*() {
    return *(mPtr + mPos);
}

template <class T>
const T* RingBuffer<T>::const_iterator::operator->() {
    return mPtr + mPos;
}

template <class T>
void RingBuffer<T>::add(const T& item) {
    if (mBuffer.size() < mMaxBufferSize) {
        mBuffer.push_back(item);
        mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize);
        return;
    }

    mBuffer[mFrontIdx] = item;
    mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize);
}

template <class T>
void RingBuffer<T>::add(T&& item) {
    if (mBuffer.size() != mMaxBufferSize) {
        mBuffer.push_back(std::forward<T>(item));
        mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize);
        return;
    }

    // Only works for types with move assignment operator
    mBuffer[mFrontIdx] = std::forward<T>(item);
    mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize);
}

template <class T>
template <class... Args>
void RingBuffer<T>::emplace(Args&&... args) {
    if (mBuffer.size() != mMaxBufferSize) {
        mBuffer.emplace_back(std::forward<Args>(args)...);
        mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize);
        return;
    }

    // Only works for types with move assignment operator
    mBuffer[mFrontIdx] = T(std::forward<Args>(args)...);
    mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize);
}

template <class T>
typename RingBuffer<T>::iterator RingBuffer<T>::begin() {
    size_t tmp = (mBuffer.size() == 0) ? 0 : mBuffer.size() - 1;
    return iterator(mBuffer.data(), mBuffer.size(), (mFrontIdx == 0) ? tmp : mFrontIdx - 1, 0);
}

template <class T>
typename RingBuffer<T>::iterator RingBuffer<T>::end() {
    size_t s = mBuffer.size();
    return iterator(mBuffer.data(), s, s, s);
}

template <class T>
typename RingBuffer<T>::const_iterator RingBuffer<T>::begin() const {
    size_t tmp = (mBuffer.size() == 0) ? 0 : mBuffer.size() - 1;
    return const_iterator(mBuffer.data(), mBuffer.size(),
            (mFrontIdx == 0) ? tmp : mFrontIdx - 1, 0);
}

template <class T>
typename RingBuffer<T>::const_iterator RingBuffer<T>::end() const {
    size_t s = mBuffer.size();
    return const_iterator(mBuffer.data(), s, s, s);
}

template <class T>
T& RingBuffer<T>::operator[](size_t index) {
    LOG_ALWAYS_FATAL_IF(index >= mBuffer.size(), "Index %zu out of bounds, size is %zu.",
            index, mBuffer.size());
    size_t pos = (index >= mFrontIdx) ?
            mBuffer.size() - 1 - (index - mFrontIdx) : mFrontIdx - 1 - index;
    return mBuffer[pos];
}

template <class T>
const T& RingBuffer<T>::operator[](size_t index) const {
    LOG_ALWAYS_FATAL_IF(index >= mBuffer.size(), "Index %zu out of bounds, size is %zu.",
            index, mBuffer.size());
    size_t pos = (index >= mFrontIdx) ?
            mBuffer.size() - 1 - (index - mFrontIdx) : mFrontIdx - 1 - index;
    return mBuffer[pos];
}

template <class T>
size_t RingBuffer<T>::size() const {
    return mBuffer.size();
}

template <class T>
void RingBuffer<T>::clear() {
    mBuffer.clear();
    mFrontIdx = 0;
}

}; // namespace android

#endif // ANDROID_SERVICE_UTILS_RING_BUFFER_H