root/branches/start/freebobstreaming/ringbuffer.h

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

Initial revision

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