mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-26 23:11:19 +01:00
Merge pull request #1651 from Kijewski/ringbuffer-use-memcpy
sys:ringbuffer: memcpy and static inline
This commit is contained in:
commit
fe2284454d
@ -45,7 +45,13 @@ typedef struct ringbuffer {
|
||||
* @param[in] buffer Buffer to use by rb.
|
||||
* @param[in] bufsize `sizeof (buffer)`
|
||||
*/
|
||||
void ringbuffer_init(ringbuffer_t *restrict rb, char *buffer, unsigned bufsize);
|
||||
static inline void ringbuffer_init(ringbuffer_t *restrict rb, char *buffer, unsigned bufsize)
|
||||
{
|
||||
rb->buf = buffer;
|
||||
rb->size = bufsize;
|
||||
rb->start = 0;
|
||||
rb->avail = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Add an element to the ringbuffer.
|
||||
|
||||
@ -18,13 +18,7 @@
|
||||
|
||||
#include "ringbuffer.h"
|
||||
|
||||
void ringbuffer_init(ringbuffer_t *restrict rb, char *buffer, unsigned bufsize)
|
||||
{
|
||||
rb->buf = buffer;
|
||||
rb->size = bufsize;
|
||||
rb->start = 0;
|
||||
rb->avail = 0;
|
||||
}
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* @brief Add an element to the end of the ringbuffer.
|
||||
@ -95,9 +89,23 @@ unsigned ringbuffer_get(ringbuffer_t *restrict rb, char *buf, unsigned n)
|
||||
if (n > rb->avail) {
|
||||
n = rb->avail;
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < n; ++i) {
|
||||
buf[i] = get_head(rb);
|
||||
if (n > 0) {
|
||||
unsigned bytes_till_end = rb->size - rb->start;
|
||||
if (bytes_till_end >= n) {
|
||||
memcpy(buf, rb->buf + rb->start, n);
|
||||
if (bytes_till_end == n) {
|
||||
rb->start = 0;
|
||||
}
|
||||
else {
|
||||
rb->start += n;
|
||||
}
|
||||
}
|
||||
else {
|
||||
memcpy(buf, rb->buf + rb->start, bytes_till_end);
|
||||
rb->start = n - bytes_till_end;
|
||||
memcpy(buf + bytes_till_end, rb->buf, rb->start);
|
||||
}
|
||||
rb->avail -= n;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user