-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcirc_buffer.c
124 lines (109 loc) · 2.97 KB
/
circ_buffer.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
/*
This file is part of fm-multimix, A multiple channel fm downmixer.
Copyright (C) 2013 Hendrik van Wyk
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/>.
*/
/*
* This is a simple circular buffer used for buffering input data.
*/
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "circ_buffer.h"
void circInit(struct circ_buf* buffer, int size)
{
buffer->begin =0;
buffer-> size = size;
buffer-> count = 0;
buffer->buffer = (uint8_t*) malloc(size);
}
void circFree(struct circ_buf* buffer, int size)
{
free (buffer->buffer);
}
int circSpaceLeft(struct circ_buf* buffer)
{
return buffer->size - buffer->count;
}
int circCount(struct circ_buf* buffer)
{
return buffer->count;
}
void circWrite(struct circ_buf* buffer, uint8_t* elements, int len)
{
int rem;
int end = (buffer->begin + buffer->count) % buffer->size;
if(buffer-> size - end < len) //wrap around
{
rem = len - (buffer->size - end);
memcpy(buffer->buffer+end, elements, (buffer->size - end));
memcpy(buffer->buffer, elements+(buffer->size - end), rem);
}
else
{
memcpy(buffer->buffer+end , elements, len);
}
if(buffer->count + len > buffer->size)
{
buffer->begin= (buffer->begin + len) % buffer->size;
buffer->count = buffer->size;
}
else
{
buffer->count += len;
}
}
int circRead(struct circ_buf* buffer, uint8_t* out, int len)
{
int rem;
if(len > buffer->count)
{
len = buffer->count;
}
if(buffer->size - buffer->begin < len) //wrap around
{
rem = len - (buffer->size - buffer->begin);
memcpy(out, buffer->buffer + buffer->begin, (buffer->size - buffer->begin));
memcpy(out + (buffer->size - buffer->begin), buffer->buffer, rem);
}
else
{
memcpy(out, buffer->buffer+buffer->begin, len);
}
buffer->begin= (buffer->begin + len) % buffer->size;
buffer->count -= len;
return len;
}
int circPeek(struct circ_buf* buffer, uint8_t* out, int len)
{
int rem;
if(len > buffer->count)
{
len = buffer->count;
}
if(buffer->size - buffer->begin < len) //wrap around
{
rem = len - (buffer->size - buffer->begin);
memcpy(out, buffer->buffer + buffer->begin, (buffer->size - buffer->begin));
memcpy(out + (buffer->size - buffer->begin), buffer->buffer, rem);
}
else
{
memcpy(out, buffer->buffer+buffer->begin, len);
}
return len;
}
void circReadAdvance(struct circ_buf* buffer, int len)
{
buffer->begin= (buffer->begin + len) % buffer->size;
buffer->count -= len;
}