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