forked from irungentoo/toxcore
-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ring buffer out of toxcore/util into toxav.
Toxcore itself doesn't use this data structure. Only toxav does, so now toxav owns the code for it.
- Loading branch information
Showing
10 changed files
with
135 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#include "ring_buffer.h" | ||
|
||
#include <stdlib.h> | ||
|
||
struct RingBuffer { | ||
uint16_t size; /* Max size */ | ||
uint16_t start; | ||
uint16_t end; | ||
void **data; | ||
}; | ||
|
||
bool rb_full(const RingBuffer *b) | ||
{ | ||
return (b->end + 1) % b->size == b->start; | ||
} | ||
bool rb_empty(const RingBuffer *b) | ||
{ | ||
return b->end == b->start; | ||
} | ||
void *rb_write(RingBuffer *b, void *p) | ||
{ | ||
void *rc = NULL; | ||
|
||
if ((b->end + 1) % b->size == b->start) { /* full */ | ||
rc = b->data[b->start]; | ||
} | ||
|
||
b->data[b->end] = p; | ||
b->end = (b->end + 1) % b->size; | ||
|
||
if (b->end == b->start) { | ||
b->start = (b->start + 1) % b->size; | ||
} | ||
|
||
return rc; | ||
} | ||
bool rb_read(RingBuffer *b, void **p) | ||
{ | ||
if (b->end == b->start) { /* Empty */ | ||
*p = NULL; | ||
return false; | ||
} | ||
|
||
*p = b->data[b->start]; | ||
b->start = (b->start + 1) % b->size; | ||
return true; | ||
} | ||
RingBuffer *rb_new(int size) | ||
{ | ||
RingBuffer *buf = calloc(sizeof(RingBuffer), 1); | ||
|
||
if (!buf) { | ||
return NULL; | ||
} | ||
|
||
buf->size = size + 1; /* include empty elem */ | ||
|
||
if (!(buf->data = calloc(buf->size, sizeof(void *)))) { | ||
free(buf); | ||
return NULL; | ||
} | ||
|
||
return buf; | ||
} | ||
void rb_kill(RingBuffer *b) | ||
{ | ||
if (b) { | ||
free(b->data); | ||
free(b); | ||
} | ||
} | ||
uint16_t rb_size(const RingBuffer *b) | ||
{ | ||
if (rb_empty(b)) { | ||
return 0; | ||
} | ||
|
||
return | ||
b->end > b->start ? | ||
b->end - b->start : | ||
(b->size - b->start) + b->end; | ||
} | ||
uint16_t rb_data(const RingBuffer *b, void **dest) | ||
{ | ||
uint16_t i = 0; | ||
|
||
for (; i < rb_size(b); i++) { | ||
dest[i] = b->data[(b->start + i) % b->size]; | ||
} | ||
|
||
return i; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef RING_BUFFER_H | ||
#define RING_BUFFER_H | ||
|
||
#include <stdbool.h> | ||
#include <stdint.h> | ||
|
||
/* Ring buffer */ | ||
typedef struct RingBuffer RingBuffer; | ||
bool rb_full(const RingBuffer *b); | ||
bool rb_empty(const RingBuffer *b); | ||
void *rb_write(RingBuffer *b, void *p); | ||
bool rb_read(RingBuffer *b, void **p); | ||
RingBuffer *rb_new(int size); | ||
void rb_kill(RingBuffer *b); | ||
uint16_t rb_size(const RingBuffer *b); | ||
uint16_t rb_data(const RingBuffer *b, void **dest); | ||
|
||
#endif /* RING_BUFFER_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters