CPPMyth
Library to interoperate with MythTV server
Loading...
Searching...
No Matches
ringbuffer.h
1/*
2 * Copyright (C) 2022 Jean-Luc Barriere
3 *
4 * This Program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 * This Program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 */
21
22#ifndef RINGBUFFER_H
23#define RINGBUFFER_H
24
25#include "local_config.h"
26
27#include <cstring>
28#include <cassert>
29#include <vector>
30#include <list>
31
32namespace NSROOT
33{
34
35namespace OS
36{
37class Mutex;
38}
39
40class RingBufferPacket
41{
42public:
43 RingBufferPacket(int _capacity);
44 ~RingBufferPacket();
45 unsigned id;
46 int size;
47 char * const data;
48 const int capacity;
49private:
50 // prevent copy
51 RingBufferPacket(const RingBufferPacket& other);
52 RingBufferPacket& operator=(const RingBufferPacket& other);
53};
54
55class RingBuffer
56{
57public:
58 RingBuffer(int capacity);
59 virtual ~RingBuffer();
60
67 int capacity() const;
68
73 int bytesAvailable();
74
79 unsigned bytesUnread() const;
80
86 bool full() const;
87
91 void clear();
92
99 int write(const char * data, int len);
100
110 RingBufferPacket * newPacket(int len);
111
117 void writePacket(RingBufferPacket * packet);
118
125
131
132private:
133 // Prevent copy
134 RingBuffer(const RingBuffer& other);
135 RingBuffer& operator=(const RingBuffer& other);
136
137private:
138 mutable OS::Mutex * m_ringlock;
139 mutable OS::Mutex * m_poollock;
140 const int m_capacity;
141 unsigned m_count;
142 unsigned m_unread;
143
144 struct Chunk
145 {
146 Chunk() : packet(nullptr), next(nullptr) { }
147 ~Chunk() { if (packet) delete packet; }
148 RingBufferPacket * packet;
149 Chunk * next;
150 };
151
152 std::vector<Chunk*> m_buffer;
155
156 void init();
157
158 std::list<RingBufferPacket*> m_pool;
159 RingBufferPacket * needPacket(int size);
160};
161
162}
163
164#endif /* RINGBUFFER_H */
165
bool full() const
int capacity() const
RingBufferPacket * read()
unsigned m_count
buffer size
Definition ringbuffer.h:141
Chunk * m_write
chunk to read
Definition ringbuffer.h:154
unsigned m_unread
total count of processed chunk
Definition ringbuffer.h:142
void freePacket(RingBufferPacket *p)
unsigned bytesUnread() const
RingBufferPacket * newPacket(int len)
void init()
chunk to write
int write(const char *data, int len)
Chunk * m_read
buffer of chunk
Definition ringbuffer.h:153
void writePacket(RingBufferPacket *packet)
total size of unread data in the buffer
Definition ringbuffer.h:145