Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f2483df

Browse files
committedOct 30, 2014
Merge pull request RIOT-OS#1649 from kaspar030/ringbuffer_remove
sys: lib: introduce ringbuffer_remove()
2 parents 54c3cb2 + e425728 commit f2483df

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
 

‎sys/include/ringbuffer.h

+8
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ int ringbuffer_get_one(ringbuffer_t *restrict rb);
9292
*/
9393
unsigned ringbuffer_get(ringbuffer_t *restrict rb, char *buf, unsigned n);
9494

95+
/**
96+
* @brief Remove a number of elements from the ringbuffer.
97+
* @param[in,out] rb Ringbuffer to operate on.
98+
* @param[in] n Read at most n elements.
99+
* @returns Number of elements actually removed.
100+
*/
101+
unsigned ringbuffer_remove(ringbuffer_t *restrict rb, unsigned n);
102+
95103
/**
96104
* @brief Test if the ringbuffer is empty.
97105
* @param[in,out] rb Ringbuffer to operate on.

‎sys/lib/ringbuffer.c

+19
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ unsigned ringbuffer_get(ringbuffer_t *restrict rb, char *buf, unsigned n)
110110
return n;
111111
}
112112

113+
unsigned ringbuffer_remove(ringbuffer_t *restrict rb, unsigned n)
114+
{
115+
if (n > rb->avail) {
116+
n = rb->avail;
117+
rb->start = rb->avail = 0;
118+
}
119+
else {
120+
rb->start -= n;
121+
rb->avail -= n;
122+
123+
/* compensate underflow */
124+
if (rb->start > rb->size) {
125+
rb->start += rb->size;
126+
}
127+
}
128+
129+
return n;
130+
}
131+
113132
int ringbuffer_peek_one(const ringbuffer_t *restrict rb_)
114133
{
115134
ringbuffer_t rb = *rb_;

0 commit comments

Comments
 (0)
Please sign in to comment.