/
overwritingringbuffer.h
62 lines (44 loc) · 1.82 KB
/
overwritingringbuffer.h
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
/*
* Part of permashift, a plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
*/
#ifndef OVERWRITINGRINGBUFFER_H_
#define OVERWRITINGRINGBUFFER_H_
#include <vdr/tools.h>
/// ring buffer overwriting oldest data when full
class cOverwritingRingBuffer
{
private:
uchar* m_buffer; ///< data container
uint64_t m_bufferLength; ///< size of buffer
uint64_t m_dataStart; ///< offset of data start
uint64_t m_dataLength; ///< used bytes in buffer
uint64_t m_dataWritten; ///< total bytes written to buffer (lifetime)
public:
/// create buffer object and allocate data buffer
cOverwritingRingBuffer(uint64_t bufferSize);
/// destroy buffer object and deallocate data buffer
virtual ~cOverwritingRingBuffer();
/// (re)allocates buffer - only needed if size 0 has been given to constructor
/// returns false and deallocates whole buffer if out of memory
bool Allocate(uint64_t bufferSize);
/// writes data to the buffer, dropping old data if necessary
void WriteData(uchar* Data, uint64_t Length);
/// fetches and removes up to maxLength bytes from the buffer
/// the pointer provided is not to be deleted by the caller
uint64_t ReadData(uchar** Data, uint64_t MaxLength);
/// fetches and removes up to maxLength bytes from the end of the buffer
/// the pointer provided is not to be deleted by the caller
uint64_t ReadDataFromEnd(uchar** Data, uint64_t MaxLength);
/// drops oldest bytes from buffer
void DropData(uint64_t bytesToDrop);
/// bytes available
uint64_t BytesAvailable() { return m_dataLength; }
/// total bytes written in buffer lifetime
uint64_t BytesWritten() { return m_dataWritten; }
/// total bytes dropped in buffer lifetime
uint64_t BytesDropped() { return BytesWritten() - BytesAvailable(); }
};
#endif /* OVERWRITINGRINGBUFFER_H_ */