forked from ralph-irving/squeezelite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.c
115 lines (100 loc) · 3.05 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
/*
* Squeezelite - lightweight headless squeezebox emulator
*
* (c) Adrian Smith 2012-2015, [email protected]
* Ralph Irving 2015-2017, [email protected]
*
* 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/>.
*
*/
// fifo bufffers
#define _GNU_SOURCE
#include "squeezelite.h"
// _* called with muxtex locked
inline unsigned _buf_used(struct buffer *buf) {
return buf->writep >= buf->readp ? buf->writep - buf->readp : buf->size - (buf->readp - buf->writep);
}
unsigned _buf_space(struct buffer *buf) {
return buf->size - _buf_used(buf) - 1; // reduce by one as full same as empty otherwise
}
unsigned _buf_cont_read(struct buffer *buf) {
return buf->writep >= buf->readp ? buf->writep - buf->readp : buf->wrap - buf->readp;
}
unsigned _buf_cont_write(struct buffer *buf) {
return buf->writep >= buf->readp ? buf->wrap - buf->writep : buf->readp - buf->writep;
}
void _buf_inc_readp(struct buffer *buf, unsigned by) {
buf->readp += by;
if (buf->readp >= buf->wrap) {
buf->readp -= buf->size;
}
}
void _buf_inc_writep(struct buffer *buf, unsigned by) {
buf->writep += by;
if (buf->writep >= buf->wrap) {
buf->writep -= buf->size;
}
}
void buf_flush(struct buffer *buf) {
mutex_lock(buf->mutex);
buf->readp = buf->buf;
buf->writep = buf->buf;
mutex_unlock(buf->mutex);
}
// adjust buffer to multiple of mod bytes so reading in multiple always wraps on frame boundary
void buf_adjust(struct buffer *buf, size_t mod) {
size_t size;
mutex_lock(buf->mutex);
size = ((unsigned)(buf->base_size / mod)) * mod;
buf->readp = buf->buf;
buf->writep = buf->buf;
buf->wrap = buf->buf + size;
buf->size = size;
mutex_unlock(buf->mutex);
}
// called with mutex locked to resize, does not retain contents, reverts to original size if fails
void _buf_resize(struct buffer *buf, size_t size) {
free(buf->buf);
buf->buf = malloc(size);
if (!buf->buf) {
size = buf->size;
buf->buf= malloc(size);
if (!buf->buf) {
size = 0;
}
}
buf->readp = buf->buf;
buf->writep = buf->buf;
buf->wrap = buf->buf + size;
buf->size = size;
buf->base_size = size;
}
void buf_init(struct buffer *buf, size_t size) {
buf->buf = malloc(size);
buf->readp = buf->buf;
buf->writep = buf->buf;
buf->wrap = buf->buf + size;
buf->size = size;
buf->base_size = size;
mutex_create_p(buf->mutex);
}
void buf_destroy(struct buffer *buf) {
if (buf->buf) {
free(buf->buf);
buf->buf = NULL;
buf->size = 0;
buf->base_size = 0;
mutex_destroy(buf->mutex);
}
}