~ubuntu-branches/ubuntu/feisty/jamin/feisty

« back to all changes in this revision

Viewing changes to src/ringbuffer.h

  • Committer: Bazaar Package Importer
  • Author(s): Robert Jordens
  • Date: 2005-03-24 17:08:30 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 hoary)
  • Revision ID: james.westby@ubuntu.com-20050324170830-3efg06mxys3gl55s
Tags: 0.9.0+0.95.0rc2-1
new upstream prerelease

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  Copyright (C) 2003 Paul Davis
3
 
 *
4
 
 *  This program is free software; you can redistribute it and/or modify
5
 
 *  it under the terms of the GNU General Public License as published by
6
 
 *  the Free Software Foundation; either version 2 of the License, or
7
 
 *  (at your option) any later version.
8
 
 *
9
 
 *  This program is distributed in the hope that it will be useful,
10
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 *  GNU General Public License for more details.
13
 
 *
14
 
 *  $Id: ringbuffer.h,v 1.3 2003/11/19 15:28:17 theno23 Exp $
15
 
 */
16
 
 
17
 
#ifndef _RINGBUFFER_H
18
 
#define _RINGBUFFER_H
 
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
    $Id: ringbuffer.h,v 1.4 2004/01/15 06:26:08 joq Exp $
 
20
*/
 
21
 
 
22
#ifndef JAMIN_RINGBUFFER_H
 
23
#define JAMIN_RINGBUFFER_H
 
24
 
 
25
/* use the built-in JACK ringbuffer implementation, if available */
 
26
#ifdef HAVE_JACK_RINGBUFFER
 
27
#include <jack/ringbuffer.h>
 
28
#else /* !HAVE_JACK_RINGBUFFER */
 
29
 
 
30
#ifdef __cplusplus
 
31
extern "C" {
 
32
#endif
19
33
 
20
34
#include <sys/types.h>
21
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
 
22
50
typedef struct  
23
51
{
24
 
  char *buf;
 
52
  char  *buf;
25
53
  size_t len;
26
54
27
 
ringbuffer_data_t ;
 
55
jack_ringbuffer_data_t ;
28
56
 
29
57
typedef struct
30
58
{
31
 
  char *buf;
 
59
  char           *buf;
32
60
  volatile size_t write_ptr;
33
61
  volatile size_t read_ptr;
34
 
  size_t size;
35
 
  size_t size_mask;
36
 
  int mlocked;
 
62
  size_t          size;
 
63
  size_t          size_mask;
 
64
  int             mlocked;
37
65
38
 
ringbuffer_t ;
39
 
 
40
 
ringbuffer_t *ringbuffer_create(int sz);
41
 
void ringbuffer_free(ringbuffer_t *rb);
42
 
 
43
 
int ringbuffer_mlock(ringbuffer_t *rb);
44
 
void ringbuffer_reset(ringbuffer_t *rb);
45
 
 
46
 
void ringbuffer_write_advance(ringbuffer_t *rb, size_t cnt);
47
 
void ringbuffer_read_advance(ringbuffer_t *rb, size_t cnt);
48
 
 
49
 
size_t ringbuffer_write_space(ringbuffer_t *rb);
50
 
size_t ringbuffer_read_space(ringbuffer_t *rb);
51
 
 
52
 
size_t ringbuffer_read(ringbuffer_t *rb, char *dest, size_t cnt);
53
 
size_t ringbuffer_write(ringbuffer_t *rb, char *src, size_t cnt);
54
 
 
55
 
void ringbuffer_get_read_vector(ringbuffer_t *rb, ringbuffer_data_t *vec);
56
 
void ringbuffer_get_write_vector(ringbuffer_t *rb, ringbuffer_data_t *vec);
57
 
 
 
66
jack_ringbuffer_t ;
 
67
 
 
68
/**
 
69
 * Allocates a ringbuffer data structure of a specified size. The
 
70
 * caller must arrange for a call to jack_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 jack_ringbuffer_t, if successful; NULL
 
76
 * otherwise.
 
77
 */
 
78
jack_ringbuffer_t *jack_ringbuffer_create(size_t sz);
 
79
 
 
80
/**
 
81
 * Frees the ringbuffer data structure allocated by an earlier call to
 
82
 * jack_ringbuffer_create().
 
83
 *
 
84
 * @param rb a pointer to the ringbuffer structure.
 
85
 */
 
86
void jack_ringbuffer_free(jack_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 jack_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 jack_ringbuffer_data_t.
 
106
 *
 
107
 */
 
108
void jack_ringbuffer_get_read_vector(const jack_ringbuffer_t *rb,
 
109
                                     jack_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 jack_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 jack_ringbuffer_data_t.
 
129
 */
 
130
void jack_ringbuffer_get_write_vector(const jack_ringbuffer_t *rb,
 
131
                                      jack_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 jack_ringbuffer_read(jack_ringbuffer_t *rb, char *dest, size_t cnt);
 
144
 
 
145
/**
 
146
 * Advance the read pointer.
 
147
 *
 
148
 * After data have been read from the ringbuffer using the pointers
 
149
 * returned by jack_ringbuffer_get_read_vector(), use this function to
 
150
 * advance the buffer pointers, making that space available for future
 
151
 * write operations.
 
152
 *
 
153
 * @param rb a pointer to the ringbuffer structure.
 
154
 * @param cnt the number of bytes read.
 
155
 */
 
156
void jack_ringbuffer_read_advance(jack_ringbuffer_t *rb, size_t cnt);
 
157
 
 
158
/**
 
159
 * Return the number of bytes available for reading.
 
160
 *
 
161
 * @param rb a pointer to the ringbuffer structure.
 
162
 *
 
163
 * @return the number of bytes available to read.
 
164
 */
 
165
size_t jack_ringbuffer_read_space(const jack_ringbuffer_t *rb);
 
166
 
 
167
/**
 
168
 * Lock a ringbuffer data block into memory.
 
169
 *
 
170
 * Uses the mlock() system call.  This is not a realtime operation.
 
171
 *
 
172
 * @param rb a pointer to the ringbuffer structure.
 
173
 */
 
174
int jack_ringbuffer_mlock(jack_ringbuffer_t *rb);
 
175
 
 
176
/**
 
177
 * Reset the read and write pointers, making an empty buffer.
 
178
 *
 
179
 * This is not thread safe.
 
180
 *
 
181
 * @param rb a pointer to the ringbuffer structure.
 
182
 */
 
183
void jack_ringbuffer_reset(jack_ringbuffer_t *rb);
 
184
 
 
185
/**
 
186
 * Write data into the ringbuffer.
 
187
 *
 
188
 * @param rb a pointer to the ringbuffer structure.
 
189
 * @param src a pointer to the data to be written to the ringbuffer.
 
190
 * @param cnt the number of bytes to write.
 
191
 *
 
192
 * @return the number of bytes write, which may range from 0 to cnt
 
193
 */
 
194
size_t jack_ringbuffer_write(jack_ringbuffer_t *rb, const char *src,
 
195
                             size_t cnt);
 
196
 
 
197
/**
 
198
 * Advance the write pointer.
 
199
 *
 
200
 * After data have been written the ringbuffer using the pointers
 
201
 * returned by jack_ringbuffer_get_write_vector(), use this function
 
202
 * to advance the buffer pointer, making the data available for future
 
203
 * read operations.
 
204
 *
 
205
 * @param rb a pointer to the ringbuffer structure.
 
206
 * @param cnt the number of bytes written.
 
207
 */
 
208
void jack_ringbuffer_write_advance(jack_ringbuffer_t *rb, size_t cnt);
 
209
 
 
210
/**
 
211
 * Return the number of bytes available for writing.
 
212
 *
 
213
 * @param rb a pointer to the ringbuffer structure.
 
214
 *
 
215
 * @return the amount of free space (in bytes) available for writing.
 
216
 */
 
217
size_t jack_ringbuffer_write_space(const jack_ringbuffer_t *rb);
 
218
 
 
219
 
 
220
#ifdef __cplusplus
 
221
}
58
222
#endif
 
223
 
 
224
#endif /* HAVE_JACK_RINGBUFFER */
 
225
 
 
226
#endif /* JAMIN_RINGBUFFER_H */