1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

ringbuffer: don't overwrite in ringbuffer_add

This commit is contained in:
René Kijewski 2014-05-03 18:55:38 +02:00
parent 47950e8bab
commit a346276d30
2 changed files with 14 additions and 7 deletions

View File

@ -27,8 +27,8 @@ typedef struct ringbuffer {
} ringbuffer_t; } ringbuffer_t;
void ringbuffer_init(ringbuffer_t *restrict rb, char *buffer, unsigned bufsize); void ringbuffer_init(ringbuffer_t *restrict rb, char *buffer, unsigned bufsize);
void ringbuffer_add_one(ringbuffer_t *restrict rb, char c); int ringbuffer_add_one(ringbuffer_t *restrict rb, char c);
void ringbuffer_add(ringbuffer_t *restrict rb, const char *buf, unsigned n); unsigned ringbuffer_add(ringbuffer_t *restrict rb, const char *buf, unsigned n);
int ringbuffer_get_one(ringbuffer_t *restrict rb); int ringbuffer_get_one(ringbuffer_t *restrict rb);
unsigned ringbuffer_get(ringbuffer_t *restrict rb, char *buf, unsigned n); unsigned ringbuffer_get(ringbuffer_t *restrict rb, char *buf, unsigned n);

View File

@ -44,19 +44,26 @@ static char get_head(ringbuffer_t *restrict rb)
return result; return result;
} }
void ringbuffer_add(ringbuffer_t *restrict rb, const char *buf, unsigned n) unsigned ringbuffer_add(ringbuffer_t *restrict rb, const char *buf, unsigned n)
{ {
for (unsigned i = 0; i < n; i++) { unsigned i;
ringbuffer_add_one(rb, buf[i]); for (i = 0; i < n; i++) {
if (ringbuffer_full(rb)) {
break;
}
add_tail(rb, buf[i]);
} }
return i;
} }
void ringbuffer_add_one(ringbuffer_t *restrict rb, char c) int ringbuffer_add_one(ringbuffer_t *restrict rb, char c)
{ {
int result = -1;
if (ringbuffer_full(rb)) { if (ringbuffer_full(rb)) {
get_head(rb); result = (unsigned char) get_head(rb);
} }
add_tail(rb, c); add_tail(rb, c);
return result;
} }
int ringbuffer_get_one(ringbuffer_t *restrict rb) int ringbuffer_get_one(ringbuffer_t *restrict rb)