chap4
This commit is contained in:
20
bac2/os/chap4/RingBuffer.c
Normal file
20
bac2/os/chap4/RingBuffer.c
Normal file
@ -0,0 +1,20 @@
|
||||
#include "RingBuffer.h"
|
||||
|
||||
void rb_push(rb_t* rb, int el){
|
||||
if(rb->filled)
|
||||
return;
|
||||
rb->data[rb->w_pos] = el;
|
||||
rb->w_pos = (rb->w_pos + 1) % rb->size;
|
||||
if(rb->w_pos == rb->r_pos)
|
||||
rb->filled = true;
|
||||
}
|
||||
int rb_pop(rb_t* rb){
|
||||
if(rb->w_pos == rb->r_pos && !rb->filled){
|
||||
rb->error = true;
|
||||
return 0;
|
||||
}
|
||||
int ret = rb->data[rb->r_pos];
|
||||
rb->r_pos = (rb->r_pos + 1) % rb->size;
|
||||
rb->filled = false;
|
||||
return ret;
|
||||
}
|
Reference in New Issue
Block a user