-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathringbuffer.c
More file actions
280 lines (264 loc) · 7.96 KB
/
Copy pathringbuffer.c
File metadata and controls
280 lines (264 loc) · 7.96 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* This file is part of the Snippets project.
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include "usefull_macros.h"
/**
* @brief sl_RB_new - create ringbuffer with `size` bytes
* @param size - RB size
* @return RB
*/
sl_ringbuffer_t *sl_RB_new(size_t size){
sl_ringbuffer_t *b = MALLOC(sl_ringbuffer_t, 1);
b->data = MALLOC(uint8_t, size);
pthread_mutex_init(&b->busy, NULL);
b->head = b->tail = 0;
b->length = size;
return b;
}
/**
* @brief sl_RB_delete - free ringbuffer
* @param b - buffer to free
*/
void sl_RB_delete(sl_ringbuffer_t **b){
if(!b || !*b) return;
sl_ringbuffer_t *bptr = *b;
FREE(bptr->data);
*b = 0;
FREE(bptr);
}
/**
* @brief datalen - amount of bytes in buffer
* @param b - buffer
* @return N
*/
static size_t datalen(sl_ringbuffer_t *b){
if(b->tail >= b->head) return (b->tail - b->head);
else return (b->length - b->head + b->tail);
}
// datalen but with blocking of RB
size_t sl_RB_datalen(sl_ringbuffer_t *b){
pthread_mutex_lock(&b->busy);
size_t l = datalen(b);
pthread_mutex_unlock(&b->busy);
return l;
}
// size of free space in buffer
size_t sl_RB_freesize(sl_ringbuffer_t *b){
pthread_mutex_lock(&b->busy);
size_t l = b->length - datalen(b) - 1;
pthread_mutex_unlock(&b->busy);
return l;
}
/**
* @brief hasbyte - check if RB have given byte
* @param b - rb
* @param byte - what to find
* @return index of byte, -2 if not found or -1 if no data in buffer
*/
static ssize_t hasbyte(sl_ringbuffer_t *b, uint8_t byte){
if(b->head == b->tail) return -1; // no data in buffer
size_t startidx = b->head;
/*DBG("head: %zd, tail: %zd (%c %c %c %c), search %02x",
b->head, b->tail, b->data[startidx], b->data[startidx+1],
b->data[startidx+2], b->data[startidx+3], byte);*/
if(b->head > b->tail){
for(size_t found = b->head; found < b->length; ++found)
if(b->data[found] == byte) return found;
startidx = 0;
}
for(size_t found = startidx; found < b->tail; ++found)
if(b->data[found] == byte) return found;
return -2;
}
// hasbyte with block
ssize_t sl_RB_hasbyte(sl_ringbuffer_t *b, uint8_t byte){
pthread_mutex_lock(&b->busy);
size_t idx = hasbyte(b, byte);
pthread_mutex_unlock(&b->busy);
return idx;
}
// increment head or tail
inline void incr(sl_ringbuffer_t *b, volatile size_t *what, size_t n){
*what += n;
if(*what >= b->length) *what -= b->length;
}
static size_t rbread(sl_ringbuffer_t *b, uint8_t *s, size_t len){
size_t l = datalen(b);
if(!l) return 0;
if(l > len) l = len;
size_t _1st = b->length - b->head;
if(_1st > l) _1st = l;
if(_1st > len) _1st = len;
memcpy(s, b->data + b->head, _1st);
if(_1st < len && l > _1st){
memcpy(s+_1st, b->data, l - _1st);
incr(b, &b->head, l);
return l;
}
incr(b, &b->head, _1st);
return _1st;
}
/**
* @brief sl_RB_read - read data from rb
* @param b - rb
* @param s - buffer for data
* @param len - length of `s`
* @return amount of bytes read
*/
size_t sl_RB_read(sl_ringbuffer_t *b, uint8_t *s, size_t len){
pthread_mutex_lock(&b->busy);
size_t got = rbread(b, s, len);
pthread_mutex_unlock(&b->busy);
return got;
}
/**
* @brief sl_RB_readto - read until meet byte `byte`
* @param b - rb
* @param byte - byte to find
* @param s - receiver
* @param len - length of `s`
* @return amount of bytes read or -1 if `s` have insufficient size
*/
ssize_t sl_RB_readto(sl_ringbuffer_t *b, uint8_t byte, uint8_t *s, size_t len){
ssize_t got = 0;
pthread_mutex_lock(&b->busy);
ssize_t idx = hasbyte(b, byte);
if(idx < 0) goto ret;
size_t partlen = idx + 1 - b->head;
// now calculate length of new data portion
if((size_t)idx < b->head) partlen += b->length;
if(partlen > len) got = -1;
else got = rbread(b, s, partlen);
ret:
pthread_mutex_unlock(&b->busy);
return got;
}
/**
* @brief sl_RB_readline - read string of text ends with '\n'
* @param b - rb
* @param s - string buffer
* @param len - length of `s`
* @return amount of characters read or -2 if buffer overflow
* !!! this function changes '\n' to 0 in `s`; `len` should include trailing '\0' too
*/
ssize_t sl_RB_readline(sl_ringbuffer_t *b, char *s, size_t len){
ssize_t got = 0;
pthread_mutex_lock(&b->busy);
ssize_t idx = hasbyte(b, '\n');
if(idx < 0){
if(idx == -1) idx = 0; // buffer is empty - return 0; else return error
goto ret;
}
DBG("Got newline -> read");
size_t partlen = idx + 1 - b->head;
// now calculate length of new data portion
if((size_t)idx < b->head) partlen += b->length;
if(partlen > len) got = -1;
else{
got = rbread(b, (uint8_t*)s, partlen);
s[partlen - 1] = 0; // substitute '\n' with trailing zero
}
DBG("read: '%s'", s);
ret:
pthread_mutex_unlock(&b->busy);
return got;
}
/**
* @brief sl_RB_putbyte - put one byte into rb
* @param b - rb
* @param byte - data byte
* @return FALSE if there's no place for `byte` in `b`
*/
int sl_RB_putbyte(sl_ringbuffer_t *b, uint8_t byte){
int rtn = FALSE;
pthread_mutex_lock(&b->busy);
size_t s = datalen(b);
if(b->length == s + 1) goto ret;
b->data[b->tail] = byte;
incr(b, &b->tail, 1);
rtn = TRUE;
ret:
pthread_mutex_unlock(&b->busy);
return rtn;
}
/**
* @brief sl_RB_write - write data to buffer
* @param b - buffer
* @param str - data
* @param len - data length
* @return amount of bytes wrote (can be less than `len`)
*/
size_t sl_RB_write(sl_ringbuffer_t *b, const uint8_t *str, size_t len){
pthread_mutex_lock(&b->busy);
size_t r = b->length - 1 - datalen(b); // rest length
DBG("rest: %zd, need: %zd", r, len);
if(len > r) len = r;
if(!len) goto ret;
/*green("buf:\n");
for(size_t i = 0; i < len; ++i){
char c = (char)str[i];
if(c > 31) printf("%c", c);
else printf("\\x%02x", c);
}
green("(end)\n");*/
size_t _1st = b->length - b->tail;
if(_1st > len) _1st = len;
memcpy(b->data + b->tail, str, _1st);
if(_1st < len){ // add another piece from start
memcpy(b->data, str+_1st, len-_1st);
}
incr(b, &b->tail, len);
ret:
pthread_mutex_unlock(&b->busy);
return len;
}
/**
* @brief sl_RB_clearbuf - reset buffer
* @param b - rb
*/
void sl_RB_clearbuf(sl_ringbuffer_t *b){
pthread_mutex_lock(&b->busy);
b->head = 0;
b->tail = 0;
pthread_mutex_unlock(&b->busy);
}
/**
* @brief sl_RB_writestr - write FULL string `s` to buffer (without trailing zero!)
* @param b - rb
* @param s - string !!! can be modified if have no '\n' on end !!!
* @return amount of bytes written (strlen of s) or 0
*/
size_t sl_RB_writestr(sl_ringbuffer_t *b, char *s){
size_t len = strlen(s);
pthread_mutex_lock(&b->busy);
size_t r = b->length - 1 - datalen(b); // rest length
if(s[len-1] != '\n') s[len++] = '\n';
if(len > r){ len = 0; goto ret; } // insufficient space - don't even try to write a part
size_t _1st = b->length - b->tail;
if(_1st > len) _1st = len;
memcpy(b->data + b->tail, s, _1st);
if(_1st < len){ // add another piece from start
memcpy(b->data, s+_1st, len-_1st);
}
incr(b, &b->tail, len);
ret:
pthread_mutex_unlock(&b->busy);
return len;
}