vdr-plugin-softhddevice-drm-gles 1.6.4-d0291bb
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
32 : m_size(size)
33{
34 if (!(m_pBuffer = (char *)malloc(size))) // allocate buffer
35 LOGFATAL("ringbuffer: %s: can't allocate memory for ringbuffer", __FUNCTION__);
36
37 m_pBufferEnd = m_pBuffer + size;
41}
42
50
60
69{
70 size_t n;
71
73 if (cnt > n) { // not enough space
74 cnt = n;
75 }
76 //
77 // Hitting end of buffer?
78 //
80 if (n > cnt) { // don't cross the end
82 } else { // reached or cross the end
84 if (n < cnt) {
85 n = cnt - n;
87 }
88 }
89
90 //
91 // Only atomic modification!
92 //
94 return cnt;
95}
96
105size_t cSoftHdRingbuffer::Write(const void *buf, size_t cnt)
106{
107 size_t n;
108
110 if (cnt > n) { // not enough space
111 cnt = n;
112 }
113 //
114 // Hitting end of buffer?
115 //
117 if (n > cnt) { // don't cross the end
120 } else { // reached or cross the end
123 if (n < cnt) {
124 buf = (uint8_t *)buf + n;
125 n = cnt - n;
128 }
129 }
130
131 //
132 // Only atomic modification!
133 //
135 return cnt;
136}
137
147{
148 size_t n;
149 size_t cnt;
150
151 // Total free bytes available in ring buffer
153
155
156 //
157 // Hitting end of buffer?
158 //
160 if (n <= cnt) { // reached or cross the end
161 return n;
162 }
163 return cnt;
164}
165
174{
175 size_t n;
176
178 if (cnt > n) { // not enough filled
179 cnt = n;
180 }
181 //
182 // Hitting end of buffer?
183 //
185 if (n > cnt) { // don't cross the end
187 } else { // reached or cross the end
189 if (n < cnt) {
190 n = cnt - n;
191 m_pReadPointer += n;
192 }
193 }
194
195 //
196 // Only atomic modification!
197 //
199 return cnt;
200}
201
210size_t cSoftHdRingbuffer::Read(void *buf, size_t cnt)
211{
212 size_t n;
213
215 if (cnt > n) { // not enough filled
216 cnt = n;
217 }
218 //
219 // Hitting end of buffer?
220 //
222 if (n > cnt) { // don't cross the end
225 } else { // reached or cross the end
228 if (n < cnt) {
229 buf = (uint8_t *)buf + n;
230 n = cnt - n;
232 m_pReadPointer += n;
233 }
234 }
235
236 //
237 // Only atomic modification!
238 //
240 return cnt;
241}
242
252{
253 size_t n;
254 size_t cnt;
255
256 // Total used bytes in ring buffer
258
260
261 //
262 // Hitting end of buffer?
263 //
265 if (n <= cnt) { // reached or cross the end
266 return n;
267 }
268 return cnt;
269}
270
277{
278 return m_size - atomic_read(&m_filled);
279}
280
287{
288 return atomic_read(&m_filled);
289}
size_t GetWritePointer(void **)
Get write pointer and free bytes at this position of ring buffer.
size_t m_size
bytes in buffer (for faster calc)
Definition ringbuffer.h:59
~cSoftHdRingbuffer(void)
cSoftHdRingbuffer destructor
char * m_pWritePointer
only used by writer
Definition ringbuffer.h:61
size_t Read(void *, size_t)
Read from a ring buffer.
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
atomic_t m_filled
how many of the buffer is used
Definition ringbuffer.h:64
size_t ReadAdvance(size_t)
Advance read pointer in ring buffer.
const char * m_pReadPointer
only used by reader
Definition ringbuffer.h:60
size_t GetReadPointer(const void **)
Get read pointer and used bytes at this position of ring buffer.
const char * m_pBufferEnd
end of buffer
Definition ringbuffer.h:58
size_t Write(const void *, size_t)
Write to a ring buffer.
char * m_pBuffer
ring buffer data
Definition ringbuffer.h:57
void Reset(void)
Reset ring buffer pointers.
#define atomic_add(val, ptr)
Definition ringbuffer.h:32
#define atomic_set(ptr, val)
Definition ringbuffer.h:28
#define atomic_read(ptr)
Definition ringbuffer.h:29
#define atomic_sub(val, ptr)
Definition ringbuffer.h:33
#define LOGFATAL
log to LOG_ERR and abort
Definition logger.h:37
Logger Header File.
Audio Ringbuffer Header File.