forked from cmus/cmus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.c
209 lines (184 loc) · 4.26 KB
/
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
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
/*
* Copyright 2008-2013 Various Authors
* Copyright 2004 Timo Hirvonen
*
* 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 2 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 "buffer.h"
#include "xmalloc.h"
#include "locking.h"
#include "debug.h"
/*
* chunk can be accessed by either consumer OR producer, not both at same time
* -> no need to lock
*/
struct chunk {
char data[CHUNK_SIZE];
/* index to data, first filled byte */
unsigned int l;
/* index to data, last filled byte + 1
*
* there are h - l bytes available (filled)
*/
unsigned int h : 31;
/* if chunk is marked filled it can only be accessed by consumer
* otherwise only producer is allowed to access the chunk
*/
unsigned int filled : 1;
};
unsigned int buffer_nr_chunks;
static pthread_mutex_t buffer_mutex = CMUS_MUTEX_INITIALIZER;
static struct chunk *buffer_chunks = NULL;
static unsigned int buffer_ridx;
static unsigned int buffer_widx;
void buffer_init(void)
{
free(buffer_chunks);
buffer_chunks = xnew(struct chunk, buffer_nr_chunks);
buffer_reset();
}
void buffer_free(void)
{
free(buffer_chunks);
}
/*
* @pos: returned pointer to available data
*
* Returns number of bytes available at @pos
*
* After reading bytes mark them consumed calling buffer_consume().
*/
int buffer_get_rpos(char **pos)
{
struct chunk *c;
int size = 0;
cmus_mutex_lock(&buffer_mutex);
c = &buffer_chunks[buffer_ridx];
if (c->filled) {
size = c->h - c->l;
*pos = c->data + c->l;
}
cmus_mutex_unlock(&buffer_mutex);
return size;
}
/*
* @pos: pointer to buffer position where data can be written
*
* Returns number of bytes can be written to @pos. If the return value is
* non-zero it is guaranteed to be >= 1024.
*
* After writing bytes mark them filled calling buffer_fill().
*/
int buffer_get_wpos(char **pos)
{
struct chunk *c;
int size = 0;
cmus_mutex_lock(&buffer_mutex);
c = &buffer_chunks[buffer_widx];
if (!c->filled) {
size = CHUNK_SIZE - c->h;
*pos = c->data + c->h;
}
cmus_mutex_unlock(&buffer_mutex);
return size;
}
void buffer_consume(int count)
{
struct chunk *c;
BUG_ON(count < 0);
cmus_mutex_lock(&buffer_mutex);
c = &buffer_chunks[buffer_ridx];
BUG_ON(!c->filled);
c->l += count;
if (c->l == c->h) {
c->l = 0;
c->h = 0;
c->filled = 0;
buffer_ridx++;
buffer_ridx %= buffer_nr_chunks;
}
cmus_mutex_unlock(&buffer_mutex);
}
/* chunk is marked filled if free bytes < 1024 or count == 0 */
int buffer_fill(int count)
{
struct chunk *c;
int filled = 0;
cmus_mutex_lock(&buffer_mutex);
c = &buffer_chunks[buffer_widx];
BUG_ON(c->filled);
c->h += count;
if (CHUNK_SIZE - c->h < 1024 || (count == 0 && c->h > 0)) {
c->filled = 1;
buffer_widx++;
buffer_widx %= buffer_nr_chunks;
filled = 1;
}
cmus_mutex_unlock(&buffer_mutex);
return filled;
}
void buffer_reset(void)
{
int i;
cmus_mutex_lock(&buffer_mutex);
buffer_ridx = 0;
buffer_widx = 0;
for (i = 0; i < buffer_nr_chunks; i++) {
buffer_chunks[i].l = 0;
buffer_chunks[i].h = 0;
buffer_chunks[i].filled = 0;
}
cmus_mutex_unlock(&buffer_mutex);
}
int buffer_get_filled_chunks(void)
{
int c;
cmus_mutex_lock(&buffer_mutex);
if (buffer_ridx < buffer_widx) {
/*
* |__##########____|
* r w
*
* |############____|
* r w
*/
c = buffer_widx - buffer_ridx;
} else if (buffer_ridx > buffer_widx) {
/*
* |#######______###|
* w r
*
* |_____________###|
* w r
*/
c = buffer_nr_chunks - buffer_ridx + buffer_widx;
} else {
/*
* |################|
* r
* w
*
* |________________|
* r
* w
*/
if (buffer_chunks[buffer_ridx].filled) {
c = buffer_nr_chunks;
} else {
c = 0;
}
}
cmus_mutex_unlock(&buffer_mutex);
return c;
}