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 = (pos2%rbuf->size) - (pos1%rbuf->size);
84  if (diff < 0) diff += rbuf->size;
85  return diff;
86  }
87 
88  static inline int ring_wdiff(ringbuffer *rbuf, int pos){
89  return ring_posdiff(rbuf, rbuf->write_pos,pos);
90  }
91 
92  static inline int ring_rdiff(ringbuffer *rbuf, int pos){
93  return ring_posdiff(rbuf, rbuf->read_pos,pos);
94  }
95 
96  static inline unsigned int ring_free(ringbuffer *rbuf){
97  int free = rbuf->read_pos - rbuf->write_pos;
98  if (free <= 0) free += rbuf->size;
99  //Note: free is gauranteed to be >=1 from the above
100  return free - 1;
101  }
102 
103  static inline unsigned int ring_avail(ringbuffer *rbuf){
104  int avail = rbuf->write_pos - rbuf->read_pos;
105  if (avail < 0) avail += rbuf->size;
106 
107  return avail;
108  }
109 
110 
111 
112  static inline uint32_t dummy_space(dummy_buffer *dbuf)
113  {
114  return (dbuf->size - dbuf->fill);
115  }
116  int dummy_delete(dummy_buffer *dbuf, uint64_t time);
117  int dummy_add(dummy_buffer *dbuf, uint64_t time, uint32_t size);
118  void dummy_clear(dummy_buffer *dbuf);
119  int dummy_init(dummy_buffer *dbuf, int s);
120  void dummy_destroy(dummy_buffer *dbuf);
121  void ring_show(ringbuffer *rbuf, unsigned int count, uint32_t off);
122 
123 #ifdef __cplusplus
124 }
125 #endif /* __cplusplus */
126 #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:121
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:359
struct dummy_buffer_s dummy_buffer
void dummy_clear(dummy_buffer *dbuf)
Definition: ringbuffer.c:405
void dummy_destroy(dummy_buffer *dbuf)
Definition: ringbuffer.c:399
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:412
static unsigned int ring_avail(ringbuffer *rbuf)
int ring_read_file(ringbuffer *rbuf, int fd, int count)
Definition: ringbuffer.c:279
int ring_read(ringbuffer *rbuf, uint8_t *data, int count)
Definition: ringbuffer.c:179
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:429
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:241
static uint32_t dummy_space(dummy_buffer *dbuf)
int ring_skip(ringbuffer *rbuf, int count)
Definition: ringbuffer.c:212
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:150
int dummy_init(dummy_buffer *dbuf, int s)
Definition: ringbuffer.c:387
static unsigned int ring_free(ringbuffer *rbuf)