Skip to content
Snippets Groups Projects
Commit a346276d authored by René Kijewski's avatar René Kijewski
Browse files

ringbuffer: don't overwrite in `ringbuffer_add`

parent 47950e8b
No related branches found
No related tags found
No related merge requests found
...@@ -27,8 +27,8 @@ typedef struct ringbuffer { ...@@ -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);
......
...@@ -44,19 +44,26 @@ static char get_head(ringbuffer_t *restrict rb) ...@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment