Skip to content

Commit c11aa19

Browse files
avoid modulo
1 parent 43af4a2 commit c11aa19

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

platform/include/platform/CircularBuffer.h

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ class CircularBuffer {
114114
*/
115115
void push(const T *src, CounterType len)
116116
{
117+
MBED_ASSERT(len > 0);
118+
117119
core_util_critical_section_enter();
118120

119121
/* if we try to write more bytes than the buffer can hold we only bother writing the last bytes */
@@ -129,23 +131,21 @@ class CircularBuffer {
129131
CounterType written = len;
130132

131133
/* on first pass we write as much as we can to the right of head */
132-
if ((_head + len) > BufferSize) {
134+
if ((_head + written) > BufferSize) {
133135
written = BufferSize - _head;
134136
}
135137

136138
std::copy(src, src + written, _buffer + _head);
139+
_head = incrementCounter(_head, written);
137140

138141
CounterType left_to_write = len - written;
139142

140143
/* we might need to continue to write from the start of the buffer */
141144
if (left_to_write) {
142145
std::copy(src + written, src + written + left_to_write, _buffer);
143-
written += left_to_write;
146+
_head = left_to_write;
144147
}
145148

146-
_head += written;
147-
_head %= BufferSize;
148-
149149
if (adjust_tail) {
150150
_tail = _head;
151151
_full = true;
@@ -198,6 +198,8 @@ class CircularBuffer {
198198
*/
199199
CounterType pop(T *dest, CounterType len)
200200
{
201+
MBED_ASSERT(len > 0);
202+
201203
if (len == 0) {
202204
return 0;
203205
}
@@ -219,18 +221,18 @@ class CircularBuffer {
219221
}
220222

221223
std::copy(_buffer + _tail, _buffer + _tail + data_popped, dest);
224+
_tail = incrementCounter(_tail, data_popped);
222225

223226
/* if we looped over the end we may need to pop again */
224227
CounterType left_to_pop = len - data_popped;
225228

226229
if (left_to_pop) {
227230
std::copy(_buffer, _buffer + left_to_pop, dest + data_popped);
231+
_tail = left_to_pop;
228232

229233
data_popped += left_to_pop;
230234
}
231235

232-
_tail += data_popped;
233-
_tail %= BufferSize;
234236
_full = false;
235237
}
236238

@@ -335,9 +337,23 @@ class CircularBuffer {
335337
return elements;
336338
}
337339

338-
CounterType incrementCounter(CounterType val)
340+
/** Used to increment _tail or _head by a given value.
341+
*
342+
* @param val The value of the counter to be incremented.
343+
* @param increment The amount to be added, the value after this incremented must not exceed BufferSize.
344+
* @return The new value of the counter.
345+
*/
346+
CounterType incrementCounter(CounterType val, CounterType increment = 1)
339347
{
340-
return (++val) % BufferSize;
348+
val += increment;
349+
350+
MBED_ASSERT(val <= BufferSize);
351+
352+
if (val == BufferSize) {
353+
val = 0;
354+
}
355+
356+
return val;
341357
}
342358

343359
private:

0 commit comments

Comments
 (0)