vdr-plugin-softhddevice-drm-gles 1.6.2
ringbuffer.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-or-later
2
16#include <cstdio>
17#include <cstdint>
18#include <cstdlib>
19#include <cstring>
20
21#include "logger.h"
22#include "ringbuffer.h"
23
37 : m_size(size)
38{
39 if (!(m_pBuffer = (char *)malloc(size))) // allocate buffer
40 LOGFATAL("ringbuffer: %s: can't allocate memory for ringbuffer", __FUNCTION__);
41
42 m_pBufferEnd = m_pBuffer + size;
46}
47
55
65
74{
75 size_t n;
76
78 if (cnt > n) { // not enough space
79 cnt = n;
80 }
81 //
82 // Hitting end of buffer?
83 //
85 if (n > cnt) { // don't cross the end
87 } else { // reached or cross the end
89 if (n < cnt) {
90 n = cnt - n;
92 }
93 }
94
95 //
96 // Only atomic modification!
97 //
99 return cnt;
100}
101
110size_t cSoftHdRingbuffer::Write(const void *buf, size_t cnt)
111{
112 size_t n;
113
115 if (cnt > n) { // not enough space
116 cnt = n;
117 }
118 //
119 // Hitting end of buffer?
120 //
122 if (n > cnt) { // don't cross the end
125 } else { // reached or cross the end
128 if (n < cnt) {
129 buf = (uint8_t *)buf + n;
130 n = cnt - n;
133 }
134 }
135
136 //
137 // Only atomic modification!
138 //
140 return cnt;
141}
142
152{
153 size_t n;
154 size_t cnt;
155
156 // Total free bytes available in ring buffer
158
160
161 //
162 // Hitting end of buffer?
163 //
165 if (n <= cnt) { // reached or cross the end
166 return n;
167 }
168 return cnt;
169}
170
179{
180 size_t n;
181
183 if (cnt > n) { // not enough filled
184 cnt = n;
185 }
186 //
187 // Hitting end of buffer?
188 //
190 if (n > cnt) { // don't cross the end
192 } else { // reached or cross the end
194 if (n < cnt) {
195 n = cnt - n;
196 m_pReadPointer += n;
197 }
198 }
199
200 //
201 // Only atomic modification!
202 //
204 return cnt;
205}
206
215size_t cSoftHdRingbuffer::Read(void *buf, size_t cnt)
216{
217 size_t n;
218
220 if (cnt > n) { // not enough filled
221 cnt = n;
222 }
223 //
224 // Hitting end of buffer?
225 //
227 if (n > cnt) { // don't cross the end
230 } else { // reached or cross the end
233 if (n < cnt) {
234 buf = (uint8_t *)buf + n;
235 n = cnt - n;
237 m_pReadPointer += n;
238 }
239 }
240
241 //
242 // Only atomic modification!
243 //
245 return cnt;
246}
247
257{
258 size_t n;
259 size_t cnt;
260
261 // Total used bytes in ring buffer
263
265
266 //
267 // Hitting end of buffer?
268 //
270 if (n <= cnt) { // reached or cross the end
271 return n;
272 }
273 return cnt;
274}
275
282{
283 return m_size - atomic_read(&m_filled);
284}
285
292{
293 return atomic_read(&m_filled);
294}
295
size_t m_size
bytes in buffer (for faster calc)
Definition ringbuffer.h:55
char * m_pWritePointer
only used by writer
Definition ringbuffer.h:57
atomic_t m_filled
how many of the buffer is used
Definition ringbuffer.h:60
const char * m_pReadPointer
only used by reader
Definition ringbuffer.h:56
const char * m_pBufferEnd
end of buffer
Definition ringbuffer.h:54
char * m_pBuffer
ring buffer data
Definition ringbuffer.h:53
#define atomic_add(val, ptr)
Definition ringbuffer.h:32
size_t GetWritePointer(void **)
Get write pointer and free bytes at this position of ring buffer.
~cSoftHdRingbuffer(void)
cSoftHdRingbuffer destructor
size_t Read(void *, size_t)
Read from a ring buffer.
#define atomic_set(ptr, val)
Definition ringbuffer.h:28
size_t UsedBytes(void)
Get used bytes in ring buffer.
size_t WriteAdvance(size_t)
Advance write pointer in ring buffer.
size_t FreeBytes(void)
Get free bytes in ring buffer.
cSoftHdRingbuffer(size_t)
cSoftHdRingbuffer constructor
#define atomic_read(ptr)
Definition ringbuffer.h:29
size_t ReadAdvance(size_t)
Advance read pointer in ring buffer.
size_t GetReadPointer(const void **)
Get read pointer and used bytes at this position of ring buffer.
size_t Write(const void *, size_t)
Write to a ring buffer.
#define atomic_sub(val, ptr)
Definition ringbuffer.h:33
void Reset(void)
Reset ring buffer pointers.
#define LOGFATAL
log to LOG_ERR and abort
Definition logger.h:32
Logger Header File.
Audio Ringbuffer Header File.