MythTV  master
ringbuffer.h
Go to the documentation of this file.
1 /*
2  * ringbuffer.h
3  *
4  *
5  * Copyright (C) 2003 Marcus Metzler <mocm@metzlerbros.de>
6  * Metzler Brothers Systementwicklung GbR
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * General Public License for more details.
18  *
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
24  *
25  */
26 
27 #ifndef RINGBUFFER_H
28 #define RINGBUFFER_H
29 
30 #include <cstdio>
31 #include <cstdint>
32 #include <unistd.h>
33 #include <vector>
34 
35 using peek_poke_vec = std::vector<uint8_t>;
36 
37 #define FULL_BUFFER (-1000)
38 #define EMPTY_BUFFER (-1000)
39  struct ringbuffer {
40  int read_pos;
41  int write_pos;
42  uint32_t size;
43  uint8_t *buffer;
44  };
45 
46 
47 #define DBUF_INDEX 10000
48 
49  struct dummy_buffer {
50  uint32_t size;
51  uint32_t fill;
54  };
55 
56 
57  int ring_init (ringbuffer *rbuf, int size);
58  int ring_reinit (ringbuffer *rbuf, int size);
59  void ring_clear(ringbuffer *rbuf);
60  void ring_destroy(ringbuffer *rbuf);
61  int ring_write(ringbuffer *rbuf, uint8_t *data, int count);
62  int ring_read(ringbuffer *rbuf, uint8_t *data, int count);
63  int ring_write_file(ringbuffer *rbuf, int fd, int count);
64  int ring_read_file(ringbuffer *rbuf, int fd, int count);
65  int ring_peek(ringbuffer *rbuf, uint8_t *data, unsigned int count,
66  uint32_t off);
67  int ring_peek(ringbuffer *rbuf, peek_poke_vec data, unsigned int count,
68  uint32_t off);
69  int ring_peek(ringbuffer *rbuf, peek_poke_vec data, uint32_t off);
70  int ring_poke(ringbuffer *rbuf, uint8_t *data, unsigned int count,
71  uint32_t off);
72  int ring_poke(ringbuffer *rbuf, peek_poke_vec data, unsigned int count,
73  uint32_t off);
74  int ring_poke(ringbuffer *rbuf, peek_poke_vec data, uint32_t off);
75  int ring_skip(ringbuffer *rbuf, int count);
76 
77  static inline int ring_wpos(ringbuffer *rbuf)
78  {
79  return rbuf->write_pos;
80  }
81 
82  static inline int ring_rpos(ringbuffer *rbuf)
83  {
84  return rbuf->read_pos;
85  }
86 
87  static inline int ring_posdiff(ringbuffer *rbuf, int pos1, int pos2){
88  int diff = (pos2%rbuf->size) - (pos1%rbuf->size);
89  if (diff < 0) diff += rbuf->size;
90  return diff;
91  }
92 
93  static inline int ring_wdiff(ringbuffer *rbuf, int pos){
94  return ring_posdiff(rbuf, rbuf->write_pos,pos);
95  }
96 
97  static inline int ring_rdiff(ringbuffer *rbuf, int pos){
98  return ring_posdiff(rbuf, rbuf->read_pos,pos);
99  }
100 
101  static inline unsigned int ring_free(ringbuffer *rbuf){
102  int free = rbuf->read_pos - rbuf->write_pos;
103  if (free <= 0) free += rbuf->size;
104  //Note: free is gauranteed to be >=1 from the above
105  return free - 1;
106  }
107 
108  static inline unsigned int ring_avail(ringbuffer *rbuf){
109  int avail = rbuf->write_pos - rbuf->read_pos;
110  if (avail < 0) avail += rbuf->size;
111 
112  return avail;
113  }
114 
115 
116 
117  static inline uint32_t dummy_space(dummy_buffer *dbuf)
118  {
119  return (dbuf->size - dbuf->fill);
120  }
121  int dummy_delete(dummy_buffer *dbuf, uint64_t time);
122  int dummy_add(dummy_buffer *dbuf, uint64_t time, uint32_t size);
123  void dummy_clear(dummy_buffer *dbuf);
124  int dummy_init(dummy_buffer *dbuf, int s);
125  void dummy_destroy(dummy_buffer *dbuf);
126  void ring_show(ringbuffer *rbuf, unsigned int count, uint32_t off);
127 
128 #endif /* RINGBUFFER_H */
ring_free
static unsigned int ring_free(ringbuffer *rbuf)
Definition: ringbuffer.h:101
ring_posdiff
static int ring_posdiff(ringbuffer *rbuf, int pos1, int pos2)
Definition: ringbuffer.h:87
ringbuffer::write_pos
int write_pos
Definition: ringbuffer.h:41
ring_init
int ring_init(ringbuffer *rbuf, int size)
Definition: ringbuffer.cpp:38
ring_reinit
int ring_reinit(ringbuffer *rbuf, int size)
Definition: ringbuffer.cpp:56
dummy_buffer::size
uint32_t size
Definition: ringbuffer.h:50
ringbuffer
Definition: ringbuffer.h:39
ring_write
int ring_write(ringbuffer *rbuf, uint8_t *data, int count)
Definition: ringbuffer.cpp:90
ring_show
void ring_show(ringbuffer *rbuf, unsigned int count, uint32_t off)
Definition: ringbuffer.cpp:371
dummy_buffer::data_index
ringbuffer data_index
Definition: ringbuffer.h:53
dummy_buffer::time_index
ringbuffer time_index
Definition: ringbuffer.h:52
ring_peek
int ring_peek(ringbuffer *rbuf, uint8_t *data, unsigned int count, uint32_t off)
Definition: ringbuffer.cpp:122
dummy_delete
int dummy_delete(dummy_buffer *dbuf, uint64_t time)
Definition: ringbuffer.cpp:442
ringbuffer::buffer
uint8_t * buffer
Definition: ringbuffer.h:43
ring_wdiff
static int ring_wdiff(ringbuffer *rbuf, int pos)
Definition: ringbuffer.h:93
ringbuffer::size
uint32_t size
Definition: ringbuffer.h:42
ring_rpos
static int ring_rpos(ringbuffer *rbuf)
Definition: ringbuffer.h:82
ring_wpos
static int ring_wpos(ringbuffer *rbuf)
Definition: ringbuffer.h:77
ring_skip
int ring_skip(ringbuffer *rbuf, int count)
Definition: ringbuffer.cpp:233
ring_read_file
int ring_read_file(ringbuffer *rbuf, int fd, int count)
Definition: ringbuffer.cpp:300
dummy_buffer
Definition: ringbuffer.h:49
dummy_clear
void dummy_clear(dummy_buffer *dbuf)
Definition: ringbuffer.cpp:417
ring_avail
static unsigned int ring_avail(ringbuffer *rbuf)
Definition: ringbuffer.h:108
dummy_destroy
void dummy_destroy(dummy_buffer *dbuf)
Definition: ringbuffer.cpp:411
ring_poke
int ring_poke(ringbuffer *rbuf, uint8_t *data, unsigned int count, uint32_t off)
Definition: ringbuffer.cpp:161
dummy_space
static uint32_t dummy_space(dummy_buffer *dbuf)
Definition: ringbuffer.h:117
ring_write_file
int ring_write_file(ringbuffer *rbuf, int fd, int count)
Definition: ringbuffer.cpp:262
ringbuffer::read_pos
int read_pos
Definition: ringbuffer.h:40
ring_clear
void ring_clear(ringbuffer *rbuf)
Definition: ringbuffer.cpp:76
ring_read
int ring_read(ringbuffer *rbuf, uint8_t *data, int count)
Definition: ringbuffer.cpp:200
dummy_add
int dummy_add(dummy_buffer *dbuf, uint64_t time, uint32_t size)
Definition: ringbuffer.cpp:424
ring_destroy
void ring_destroy(ringbuffer *rbuf)
Definition: ringbuffer.cpp:84
dummy_buffer::fill
uint32_t fill
Definition: ringbuffer.h:51
peek_poke_vec
std::vector< uint8_t > peek_poke_vec
Definition: ringbuffer.h:35
dummy_init
int dummy_init(dummy_buffer *dbuf, int s)
Definition: ringbuffer.cpp:399
ring_rdiff
static int ring_rdiff(ringbuffer *rbuf, int pos)
Definition: ringbuffer.h:97