-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathringbuf.c
72 lines (61 loc) · 1.2 KB
/
ringbuf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <rtdm/rtdm.h>
#include <rtdm/rtdm_driver.h>
#include "ringbuf.h"
void ringBufS_init (ringBufS *_this)
{
/*****
The following clears:
-> buf
-> head
-> tail
-> count
and sets head = tail
***/
memset (_this, 0, sizeof (*_this));
_this->buf = rtdm_malloc(RBUF_SIZE);
}
void ringBufS_free (ringBufS *_this){
rtdm_free(_this->buf);
}
int ringBufS_empty (ringBufS *_this)
{
return (0==_this->count);
}
int ringBufS_full (ringBufS *_this)
{
return (_this->count >= RBUF_SIZE);
}
char ringBufS_get (ringBufS *_this)
{
char c;
if (_this->count>0)
{
c = _this->buf[_this->tail];
_this->tail = (_this->tail+1) % RBUF_SIZE;
--_this->count;
}
else
{
c = -1;
}
return (c);
}
void ringBufS_put (ringBufS *_this, const char c)
{
if (_this->count < RBUF_SIZE)
{
_this->buf[_this->head] = c;
_this->head = (_this->head + 1) % RBUF_SIZE;
++_this->count;
}
}
void ringBufS_flush (ringBufS *_this, const int clearBuffer)
{
_this->count = 0;
_this->head = 0;
_this->tail = 0;
if (clearBuffer)
{
memset (_this->buf, 0, sizeof (_this->buf));
}
}