MythTV  master
programs/mythtranscode/external/replex/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 <stdio.h>
31 #include <stdint.h>
32 #include <unistd.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif /* __cplusplus */
37 
38 #define FULL_BUFFER -1000
39 #define EMPTY_BUFFER -1000
40  typedef struct ringbuffer {
41  int read_pos;
42  int write_pos;
43  uint32_t size;
44  uint8_t *buffer;
45  } ringbuffer;
46 
47 
48 #define DBUF_INDEX 10000
49 
50  typedef struct dummy_buffer_s {
51  uint32_t size;
52  uint32_t fill;
55  } dummy_buffer;
56 
57 
58  int ring_init (ringbuffer *rbuf, int size);
59  int ring_reinit (ringbuffer *rbuf, int size);
60  void ring_clear(ringbuffer *rbuf);
61  void ring_destroy(ringbuffer *rbuf);
62  int ring_write(ringbuffer *rbuf, uint8_t *data, int count);
63  int ring_read(ringbuffer *rbuf, uint8_t *data, int count);
64  int ring_write_file(ringbuffer *rbuf, int fd, int count);
65  int ring_read_file(ringbuffer *rbuf, int fd, int count);
66  int ring_peek(ringbuffer *rbuf, uint8_t *data, unsigned int count,
67  uint32_t off);
68  int ring_poke(ringbuffer *rbuf, uint8_t *data, unsigned int count,
69  uint32_t off);
70  int ring_skip(ringbuffer *rbuf, int count);
71 
72  static inline int ring_wpos(ringbuffer *rbuf)
73  {
74  return rbuf->write_pos;
75  }
76 
77  static inline int ring_rpos(ringbuffer *rbuf)
78  {
79  return rbuf->read_pos;
80  }
81 
82  static inline int ring_posdiff(ringbuffer *rbuf, int pos1, int pos2){
83  int diff;
84 
85  diff = (pos2%rbuf->size) - (pos1%rbuf->size);
86  if (diff < 0) diff += rbuf->size;
87  return diff;
88  }
89 
90  static inline int ring_wdiff(ringbuffer *rbuf, int pos){
91  return ring_posdiff(rbuf, rbuf->write_pos,pos);
92  }
93 
94  static inline int ring_rdiff(ringbuffer *rbuf, int pos){
95  return ring_posdiff(rbuf, rbuf->read_pos,pos);
96  }
97 
98  static inline unsigned int ring_free(ringbuffer *rbuf){
99  int free;
100  free = rbuf->read_pos - rbuf->write_pos;
101  if (free <= 0) free += rbuf->size;
102  //Note: free is gauranteed to be >=1 from the above
103  return free - 1;
104  }
105 
106  static inline unsigned int ring_avail(ringbuffer *rbuf){
107  int avail;
108  avail = rbuf->write_pos - rbuf->read_pos;
109  if (avail < 0) avail += rbuf->size;
110 
111  return avail;
112  }
113 
114 
115 
116  static inline uint32_t dummy_space(dummy_buffer *dbuf)
117  {
118  return (dbuf->size - dbuf->fill);
119  }
120  int dummy_delete(dummy_buffer *dbuf, uint64_t time);
121  int dummy_add(dummy_buffer *dbuf, uint64_t time, uint32_t size);
122  void dummy_clear(dummy_buffer *dbuf);
123  int dummy_init(dummy_buffer *dbuf, int s);
124  void dummy_destroy(dummy_buffer *dbuf);
125  void ring_show(ringbuffer *rbuf, unsigned int count, uint32_t off);
126 
127 #ifdef __cplusplus
128 }
129 #endif /* __cplusplus */
130 #endif /* _RINGBUFFER_H_ */
int ring_init(ringbuffer *rbuf, int size)
Definition: ringbuffer.c:37
int ring_peek(ringbuffer *rbuf, uint8_t *data, unsigned int count, uint32_t off)
Definition: ringbuffer.c:123
static int ring_rpos(ringbuffer *rbuf)
void ring_destroy(ringbuffer *rbuf)
Definition: ringbuffer.c:83
void ring_show(ringbuffer *rbuf, unsigned int count, uint32_t off)
Definition: ringbuffer.c:376
struct dummy_buffer_s dummy_buffer
void dummy_clear(dummy_buffer *dbuf)
Definition: ringbuffer.c:426
void dummy_destroy(dummy_buffer *dbuf)
Definition: ringbuffer.c:420
static int ring_posdiff(ringbuffer *rbuf, int pos1, int pos2)
int dummy_add(dummy_buffer *dbuf, uint64_t time, uint32_t size)
Definition: ringbuffer.c:433
static unsigned int ring_avail(ringbuffer *rbuf)
int ring_read_file(ringbuffer *rbuf, int fd, int count)
Definition: ringbuffer.c:295
int ring_read(ringbuffer *rbuf, uint8_t *data, int count)
Definition: ringbuffer.c:189
int ring_write(ringbuffer *rbuf, uint8_t *data, int count)
Definition: ringbuffer.c:89
int dummy_delete(dummy_buffer *dbuf, uint64_t time)
Definition: ringbuffer.c:450
struct ringbuffer ringbuffer
static int ring_wpos(ringbuffer *rbuf)
static int ring_wdiff(ringbuffer *rbuf, int pos)
int ring_write_file(ringbuffer *rbuf, int fd, int count)
Definition: ringbuffer.c:257
static uint32_t dummy_space(dummy_buffer *dbuf)
int ring_skip(ringbuffer *rbuf, int count)
Definition: ringbuffer.c:225
static int ring_rdiff(ringbuffer *rbuf, int pos)
void ring_clear(ringbuffer *rbuf)
Definition: ringbuffer.c:75
int ring_reinit(ringbuffer *rbuf, int size)
Definition: ringbuffer.c:55
int ring_poke(ringbuffer *rbuf, uint8_t *data, unsigned int count, uint32_t off)
Definition: ringbuffer.c:156
int dummy_init(dummy_buffer *dbuf, int s)
Definition: ringbuffer.c:408
static unsigned int ring_free(ringbuffer *rbuf)