-
Notifications
You must be signed in to change notification settings - Fork 28
/
RingBuf.c
186 lines (161 loc) · 3.83 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
RingBuf.c - Library for implementing a simple Ring Buffer on Arduino boards.
Created by D. Aaron Wisner ([email protected])
January 17, 2015.
Released into the public domain.
*/
#include "RingBuf.h"
#include <string.h>
/////// Constructor //////////
RingBuf *RingBuf_new(int size, int len)
{
RingBuf *self = (RingBuf *)malloc(sizeof(RingBuf));
if (!self) return NULL;
memset(self, 0, sizeof(RingBuf));
if (RingBuf_init(self, size, len) < 0)
{
free(self);
return NULL;
}
return self;
}
int RingBuf_init(RingBuf *self, int size, int len)
{
self->buf = (unsigned char *)malloc(size*len);
if (!self->buf) return -1;
memset(self->buf, 0, size*len);
self->size = size;
self->len = len;
self->start = 0;
self->end = 0;
self->elements = 0;
self->next_end_index = &RingBufNextEndIndex;
self->incr_end_index = &RingBufIncrEnd;
self->incr_start_index = &RingBufIncrStart;
self->isFull = &RingBufIsFull;
self->isEmpty = &RingBufIsEmpty;
self->add = &RingBufAdd;
self->numElements = &RingBufNumElements;
self->peek = &RingBufPeek;
self->pull = &RingBufPull;
return 0;
}
/////// Deconstructor //////////
int RingBuf_delete(RingBuf *self)
{
free(self->buf);
free(self);
return 0;
}
/////// PRIVATE METHODS //////////
// get next empty index
int RingBufNextEndIndex(RingBuf *self)
{
//buffer is full
if (self->isFull(self)) return -1;
//if empty dont incriment
return (self->end+(unsigned int)!self->isEmpty(self))%self->len;
}
// incriment index of RingBuf struct, only call if safe to do so
int RingBufIncrEnd(RingBuf *self)
{
self->end = (self->end+1)%self->len;
return self->end;
}
// incriment index of RingBuf struct, only call if safe to do so
int RingBufIncrStart(RingBuf *self)
{
self->start = (self->start+1)%self->len;
return self->start;
}
/////// PUBLIC METHODS //////////
// Add an object struct to RingBuf
int RingBufAdd(RingBuf *self, const void *object)
{
int index;
// Perform all atomic opertaions
RB_ATOMIC_START
{
index = self->next_end_index(self);
//if not full
if (index >= 0)
{
memcpy(self->buf + index*self->size, object, self->size);
if (!self->isEmpty(self)) self->incr_end_index(self);
self->elements++;
}
}
RB_ATOMIC_END
return index;
}
// Return pointer to num element, return null on empty or num out of bounds
void *RingBufPeek(RingBuf *self, unsigned int num)
{
void *ret = NULL;
// Perform all atomic opertaions
RB_ATOMIC_START
{
//empty or out of bounds
if (self->isEmpty(self) || num > self->elements - 1) ret = NULL;
else ret = &self->buf[((self->start + num)%self->len)*self->size];
}
RB_ATOMIC_END
return ret;
}
// Returns and removes first buffer element
void *RingBufPull(RingBuf *self, void *object)
{
void *ret = NULL;
// Perform all atomic opertaions
RB_ATOMIC_START
{
if (self->isEmpty(self)) ret = NULL;
// Else copy Object
else
{
memcpy(object, self->buf+self->start*self->size, self->size);
self->elements--;
// don't incriment start if removing last element
if (!self->isEmpty(self)) self->incr_start_index(self);
ret = object;
}
}
RB_ATOMIC_END
return ret;
}
// Returns number of elemnts in buffer
unsigned int RingBufNumElements(RingBuf *self)
{
unsigned int elements;
// Perform all atomic opertaions
RB_ATOMIC_START
{
elements = self->elements;
}
RB_ATOMIC_END
return elements;
}
// Returns true if buffer is full
bool RingBufIsFull(RingBuf *self)
{
bool ret;
// Perform all atomic opertaions
RB_ATOMIC_START
{
ret = self->elements == self->len;
}
RB_ATOMIC_END
return ret;
}
// Returns true if buffer is empty
bool RingBufIsEmpty(RingBuf *self)
{
bool ret;
// Perform all atomic opertaions
RB_ATOMIC_START
{
ret = !self->elements;
}
RB_ATOMIC_END
return ret;
}