-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcirb.c
More file actions
255 lines (206 loc) · 5.53 KB
/
Copy pathcirb.c
File metadata and controls
255 lines (206 loc) · 5.53 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// ====================================
// 2^n Circular Buffer Functions
// -----------------------------
//
// Author: Reine Gill
// Date: 2002-10-21
// ====================================
#include <stddef.h>
#include <stdlib.h>
#include <pthread.h> // Linux implementation of POSIX threads
#include <time.h> // Standard date and time
#include "cirb.h"
#include "nice.h"
void InB(buffer_struct_type *buff,unsigned char *data,int n);
void In(buffer_struct_type *buff,unsigned char data);
int InitBuffer(buffer_struct_type *buff,int size);
void FreeBuffer(buffer_struct_type *buff);
int Look(buffer_struct_type *buff,unsigned char *data,unsigned int offset);
int LookB(buffer_struct_type *buff,unsigned char *data,unsigned int n);
int Get(buffer_struct_type *buff,unsigned char *data);
int GetB(buffer_struct_type *buff,unsigned char *data,unsigned int n);
int Forward(buffer_struct_type *buff,unsigned int n);
int FullBuffer(buffer_struct_type *buff,double fratio);
unsigned int GetBufferFill(buffer_struct_type *buff);
static pthread_mutex_t protect_cbuff=PTHREAD_MUTEX_INITIALIZER;
//Add 1 byte of data to circular buffer
void In(buffer_struct_type *buff,unsigned char data)
{
pthread_mutex_lock(&protect_cbuff);
buff->data[buff->inp++]=data;
buff->inp&=buff->mask;
if(buff->fill<buff->max) buff->fill++;
pthread_mutex_unlock(&protect_cbuff);
}
//Add n bytes of data to circular buffer
void InB(buffer_struct_type *buff,unsigned char *data,int n)
{
int i;
pthread_mutex_lock(&protect_cbuff);
for(i=0;i<n;i++)
{
buff->data[buff->inp++]=data[i];
buff->inp&=buff->mask;
if(buff->fill<buff->max)
buff->fill++;
}
pthread_mutex_unlock(&protect_cbuff);
}
// Get 1 byte of data from cirb, oldest data first
int Get(buffer_struct_type *buff,unsigned char *data)
{
unsigned int outp;
int status=0;
pthread_mutex_lock(&protect_cbuff);
if(buff->fill>0)
{
outp=(buff->inp+buff->max-buff->fill) & buff->mask;
*data=buff->data[outp];
buff->fill--;
}
else
status=-1; // Empty
pthread_mutex_unlock(&protect_cbuff);
return status;
}
/*
* Get n bytes of data from circular buffer, oldest data first
*
* buff : Circular buffer
* data : Destination buffer
* n : Number of bytes to retrieve
*
* Assumes data buffer is large enough!
*/
int GetB(buffer_struct_type *buff, unsigned char *data, unsigned int n)
{
unsigned int outp;
int i;
int status=0;
pthread_mutex_lock(&protect_cbuff);
if(buff->fill>n)
{
for(i=0;i<n;i++)
{
outp=(buff->inp+buff->max-buff->fill) & buff->mask;
data[i]=buff->data[outp];
buff->fill--;
}
data[n]='\0';
}
else
status=-2;
pthread_mutex_unlock(&protect_cbuff);
return status;
}
// Look at n bytes of data from circular buffer, oldest data first
// Assumes data buffer is large enough!
// Note that the data is still considered to be in the circular buffer
int LookB(buffer_struct_type *buff,unsigned char *data,unsigned int n)
{
unsigned int outp;
unsigned int fill;
int i;
int status=0;
pthread_mutex_lock(&protect_cbuff);
fill=buff->fill;
if(buff->fill>n)
{
for(i=0;i<n;i++)
{
outp=(buff->inp+buff->max-fill) & buff->mask;
data[i]=buff->data[outp];
fill--;
}
data[n]='\0';
}
else
status= -3;
pthread_mutex_unlock(&protect_cbuff);
return status;
}
// Look in buffer from oldest data and forward offset steps
int Look(buffer_struct_type *buff,unsigned char *data,unsigned int offset)
{
unsigned int outp;
int status=0;
pthread_mutex_lock(&protect_cbuff);
if(buff->fill>offset)
{
outp=(buff->inp+buff->max-buff->fill+offset) & buff->mask;
*data=buff->data[outp];
}
else
status = -4; //Out of range!
pthread_mutex_unlock(&protect_cbuff);
return status;
}
// Move Forward to newer data in buffer
// Returns steps left to move
int Forward(buffer_struct_type *buff,unsigned int n)
{
int diff;
pthread_mutex_lock(&protect_cbuff);
do
{
diff=buff->fill-n;
if(diff<0)
{
pthread_mutex_unlock(&protect_cbuff);
sched_yield();
pthread_mutex_lock(&protect_cbuff);
}
else
{
buff->fill=diff;
pthread_mutex_unlock(&protect_cbuff);
return 0;
}
} while(diff<0);
pthread_mutex_unlock(&protect_cbuff);
return 0;
}
// Make buffer structure
int InitBuffer(buffer_struct_type *buff,int size)
{
pthread_mutex_lock(&protect_cbuff);
buff->max = size;
buff->mask = size-1;
buff->fill=0;
buff->inp=0;
if((buff->data =(unsigned char *) malloc(size))==NULL) return -5;
pthread_mutex_unlock(&protect_cbuff);
return 0; //OK
}
// Free buffer
void FreeBuffer(buffer_struct_type *buff)
{
pthread_mutex_lock(&protect_cbuff);
if(buff->data!=NULL)
free(buff->data);
buff->data=NULL;
pthread_mutex_unlock(&protect_cbuff);
}
int FullBuffer(buffer_struct_type *buff,double fratio)
{
int status;
double ratio;
pthread_mutex_lock(&protect_cbuff);
ratio=(((double)buff->fill)/((double)buff->max));
if(ratio<fratio)
status=0;
else
status=1;
pthread_mutex_unlock(&protect_cbuff);
return status;
}
// Erik P G Johansson 2015-03-31: Added function
// Read the circular buffer fill value, i.e. how much data is in the buffer.
unsigned int GetBufferFill(buffer_struct_type *buff)
{
unsigned int fill_value;
pthread_mutex_lock(&protect_cbuff); // Should not use.
fill_value = buff->fill;
pthread_mutex_unlock(&protect_cbuff);
return fill_value;
}