-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRingBuffer.cpp
More file actions
60 lines (51 loc) · 1.3 KB
/
Copy pathRingBuffer.cpp
File metadata and controls
60 lines (51 loc) · 1.3 KB
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
/*
* (C) 2014, 2015, 2016 Onwrikbaar
*/
#include <string.h>
#include "RingBuffer.h"
size_t RingBuffer::write(const uint8_t *srcp, size_t len)
{
int bytesFree = ri - wi - 1;
size_t nb = 0;
if (bytesFree >= 0) {
if (len > bytesFree) return (size_t)0;
} else {
bytesFree += RB_BUFSIZE;
if (len > bytesFree) return (size_t)0;
if (wi + len >= RB_BUFSIZE) {
memcpy(bbuf + wi, srcp, (nb = RB_BUFSIZE - wi));
wi = 0;
len -= nb;
}
}
if (len != 0) {
memcpy(bbuf + wi, srcp + nb, len);
if ((wi += len) == RB_BUFSIZE) wi = 0; // Wrap around.
}
return nb + len;
}
int RingBuffer::getNextByte()
{
if (ri == wi) {
/* Possible optimisation.
wi = 0;
ri = 0;
*/
return -1;
}
const uint8_t b = *(bbuf + ri++);
if (ri == RB_BUFSIZE) ri = 0;
return (int)b;
}
// C interface to the above write() method.
size_t RingBuffer_write(void *untyped_self, const uint8_t *srcp, size_t len)
{
RingBuffer *rb = static_cast<RingBuffer*>(untyped_self);
return rb->write(srcp, len);
}
// C interface to the above getNextByte() method.
int RingBuffer_getNextByte(void *untyped_self)
{
RingBuffer *rb = static_cast<RingBuffer*>(untyped_self);
return rb->getNextByte();
}