root/branches/libfreebob-2.0/src/libstreaming/ringbuffer.h

Revision 200, 7.7 kB (checked in by pieterpalmers, 18 years ago)

- temp commit

Line 
1 /*
2   Copied from the jackd sources
3   function names changed in order to avoid naming problems when using this in
4   a jackd backend.
5  
6   Modifications for FreeBoB by Pieter Palmers
7    
8     Copyright (C) 2000 Paul Davis
9     Copyright (C) 2003 Rohan Drape
10    
11     This program is free software; you can redistribute it and/or modify
12     it under the terms of the GNU Lesser General Public License as published by
13     the Free Software Foundation; either version 2.1 of the License, or
14     (at your option) any later version.
15    
16     This program is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     GNU Lesser General Public License for more details.
20    
21     You should have received a copy of the GNU Lesser General Public License
22     along with this program; if not, write to the Free Software
23     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25 */
26
27 #ifndef _RINGBUFFER_H
28 #define _RINGBUFFER_H
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 #include <sys/types.h>
35
36 /** @file ringbuffer.h
37  *
38  * A set of library functions to make lock-free ringbuffers available
39  * to JACK clients.  The `capture_client.c' (in the example_clients
40  * directory) is a fully functioning user of this API.
41  *
42  * The key attribute of a ringbuffer is that it can be safely accessed
43  * by two threads simultaneously -- one reading from the buffer and
44  * the other writing to it -- without using any synchronization or
45  * mutual exclusion primitives.  For this to work correctly, there can
46  * only be a single reader and a single writer thread.  Their
47  * identities cannot be interchanged.
48  */
49
50 typedef struct 
51 {
52   char  *buf;
53   size_t len;
54 }
55 freebob_ringbuffer_data_t ;
56
57 typedef struct
58 {
59   char           *buf;
60   volatile size_t write_ptr;
61   volatile size_t read_ptr;
62   size_t          size;
63   size_t          size_mask;
64   int             mlocked;
65 }
66 freebob_ringbuffer_t ;
67
68 /**
69  * Allocates a ringbuffer data structure of a specified size. The
70  * caller must arrange for a call to freebob_ringbuffer_free() to release
71  * the memory associated with the ringbuffer.
72  *
73  * @param sz the ringbuffer size in bytes.
74  *
75  * @return a pointer to a new freebob_ringbuffer_t, if successful; NULL
76  * otherwise.
77  */
78 freebob_ringbuffer_t *freebob_ringbuffer_create(size_t sz);
79
80 /**
81  * Frees the ringbuffer data structure allocated by an earlier call to
82  * freebob_ringbuffer_create().
83  *
84  * @param rb a pointer to the ringbuffer structure.
85  */
86 void freebob_ringbuffer_free(freebob_ringbuffer_t *rb);
87
88 /**
89  * Fill a data structure with a description of the current readable
90  * data held in the ringbuffer.  This description is returned in a two
91  * element array of freebob_ringbuffer_data_t.  Two elements are needed
92  * because the data to be read may be split across the end of the
93  * ringbuffer.
94  *
95  * The first element will always contain a valid @a len field, which
96  * may be zero or greater.  If the @a len field is non-zero, then data
97  * can be read in a contiguous fashion using the address given in the
98  * corresponding @a buf field.
99  *
100  * If the second element has a non-zero @a len field, then a second
101  * contiguous stretch of data can be read from the address given in
102  * its corresponding @a buf field.
103  *
104  * @param rb a pointer to the ringbuffer structure.
105  * @param vec a pointer to a 2 element array of freebob_ringbuffer_data_t.
106  *
107  */
108 void freebob_ringbuffer_get_read_vector(const freebob_ringbuffer_t *rb,
109                                      freebob_ringbuffer_data_t *vec);
110
111 /**
112  * Fill a data structure with a description of the current writable
113  * space in the ringbuffer.  The description is returned in a two
114  * element array of freebob_ringbuffer_data_t.  Two elements are needed
115  * because the space available for writing may be split across the end
116  * of the ringbuffer.
117  *
118  * The first element will always contain a valid @a len field, which
119  * may be zero or greater.  If the @a len field is non-zero, then data
120  * can be written in a contiguous fashion using the address given in
121  * the corresponding @a buf field.
122  *
123  * If the second element has a non-zero @a len field, then a second
124  * contiguous stretch of data can be written to the address given in
125  * the corresponding @a buf field.
126  *
127  * @param rb a pointer to the ringbuffer structure.
128  * @param vec a pointer to a 2 element array of freebob_ringbuffer_data_t.
129  */
130 void freebob_ringbuffer_get_write_vector(const freebob_ringbuffer_t *rb,
131                                       freebob_ringbuffer_data_t *vec);
132
133 /**
134  * Read data from the ringbuffer.
135  *
136  * @param rb a pointer to the ringbuffer structure.
137  * @param dest a pointer to a buffer where data read from the
138  * ringbuffer will go.
139  * @param cnt the number of bytes to read.
140  *
141  * @return the number of bytes read, which may range from 0 to cnt.
142  */
143 size_t freebob_ringbuffer_read(freebob_ringbuffer_t *rb, char *dest, size_t cnt);
144
145 /**
146  * Read data from the ringbuffer. Opposed to freebob_ringbuffer_read()
147  * this function does not move the read pointer. Thus it's
148  * a convenient way to inspect data in the ringbuffer in a
149  * continous fashion. The price is that the data is copied
150  * into a user provided buffer. For "raw" non-copy inspection
151  * of the data in the ringbuffer use freebob_ringbuffer_get_read_vector().
152  *
153  * @param rb a pointer to the ringbuffer structure.
154  * @param dest a pointer to a buffer where data read from the
155  * ringbuffer will go.
156  * @param cnt the number of bytes to read.
157  *
158  * @return the number of bytes read, which may range from 0 to cnt.
159  */
160 size_t freebob_ringbuffer_peek(freebob_ringbuffer_t *rb, char *dest, size_t cnt);
161
162 /**
163  * Advance the read pointer.
164  *
165  * After data have been read from the ringbuffer using the pointers
166  * returned by freebob_ringbuffer_get_read_vector(), use this function to
167  * advance the buffer pointers, making that space available for future
168  * write operations.
169  *
170  * @param rb a pointer to the ringbuffer structure.
171  * @param cnt the number of bytes read.
172  */
173 void freebob_ringbuffer_read_advance(freebob_ringbuffer_t *rb, size_t cnt);
174
175 /**
176  * Return the number of bytes available for reading.
177  *
178  * @param rb a pointer to the ringbuffer structure.
179  *
180  * @return the number of bytes available to read.
181  */
182 size_t freebob_ringbuffer_read_space(const freebob_ringbuffer_t *rb);
183
184 /**
185  * Lock a ringbuffer data block into memory.
186  *
187  * Uses the mlock() system call.  This is not a realtime operation.
188  *
189  * @param rb a pointer to the ringbuffer structure.
190  */
191 int freebob_ringbuffer_mlock(freebob_ringbuffer_t *rb);
192
193 /**
194  * Reset the read and write pointers, making an empty buffer.
195  *
196  * This is not thread safe.
197  *
198  * @param rb a pointer to the ringbuffer structure.
199  */
200 void freebob_ringbuffer_reset(freebob_ringbuffer_t *rb);
201
202 /**
203  * Write data into the ringbuffer.
204  *
205  * @param rb a pointer to the ringbuffer structure.
206  * @param src a pointer to the data to be written to the ringbuffer.
207  * @param cnt the number of bytes to write.
208  *
209  * @return the number of bytes write, which may range from 0 to cnt
210  */
211 size_t freebob_ringbuffer_write(freebob_ringbuffer_t *rb, const char *src,
212                              size_t cnt);
213
214 /**
215  * Advance the write pointer.
216  *
217  * After data have been written the ringbuffer using the pointers
218  * returned by freebob_ringbuffer_get_write_vector(), use this function
219  * to advance the buffer pointer, making the data available for future
220  * read operations.
221  *
222  * @param rb a pointer to the ringbuffer structure.
223  * @param cnt the number of bytes written.
224  */
225 void freebob_ringbuffer_write_advance(freebob_ringbuffer_t *rb, size_t cnt);
226
227 /**
228  * Return the number of bytes available for writing.
229  *
230  * @param rb a pointer to the ringbuffer structure.
231  *
232  * @return the amount of free space (in bytes) available for writing.
233  */
234 size_t freebob_ringbuffer_write_space(const freebob_ringbuffer_t *rb);
235
236
237 #ifdef __cplusplus
238 }
239 #endif
240
241 #endif
Note: See TracBrowser for help on using the browser.