source: roaraudio/libroar/buffer.c @ 1215:5cf113ac883c

Last change on this file since 1215:5cf113ac883c was 1215:5cf113ac883c, checked in by phi, 15 years ago

added support to buffer system for non alleced buffers

File size: 6.8 KB
Line 
1//buffer.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar 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 General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
37int roar_buffer_new      (struct roar_buffer ** buf, size_t len) {
38 void * data;
39
40 if ((data = malloc(len)) == NULL) {
41  return -1;
42 }
43
44 if ( roar_buffer_new_no_ma(buf, len, data) == -1 ) {
45  free(data);
46  return -1;
47 }
48
49 if ( roar_buffer_set_flag(*buf, ROAR_BUFFER_FLAG_NOFREE, ROAR_BUFFER_RESET) == -1 ) {
50  roar_buffer_free(*buf);
51  free(data);
52  return -1;
53 }
54
55 return 0;
56}
57
58int roar_buffer_new_no_ma(struct roar_buffer ** buf, size_t len, void * data) { // no internal malloc
59 struct roar_buffer * new;
60
61 ROAR_DBG("buffer_new(buf=%p, len=%i) = ?", buf, len);
62
63 if ( buf == NULL || data == NULL )
64  return -1;
65
66 if ((new = malloc(sizeof(struct roar_buffer))) == NULL) {
67  *buf = NULL;
68  return -1;
69 }
70
71 new->data      = data;
72
73 new->flags     = ROAR_BUFFER_FLAG_NONE|ROAR_BUFFER_FLAG_NOFREE;
74
75 new->user_data = new->data;
76
77 new->next      = NULL;
78
79 new->len       = len;
80 new->user_len  = len;
81 *buf           = new;
82
83 ROAR_DBG("buffer_new(buf=%p, len=%i): New buffer at %p", buf, len, new);
84
85 return 0;
86}
87
88int roar_buffer_free     (struct roar_buffer * buf) {
89 struct roar_buffer * next;
90
91 if ( buf == NULL )
92  return -1;
93
94 while ((next = buf->next)) {
95  free(buf->data);
96  free(buf);
97  buf = next;
98 }
99
100 if ( roar_buffer_get_flag(buf, ROAR_BUFFER_FLAG_NOFREE) != 1 )
101  free(buf->data);
102
103 free(buf);
104
105 return 0;
106}
107
108int roar_buffer_delete   (struct roar_buffer * buf, struct roar_buffer ** next) {
109 if ( buf == NULL ) {
110  if ( next != NULL )
111   *next = NULL;
112  return -1;
113 }
114
115 ROAR_DBG("buffer_delete(buf=%p, next=%p) = ?", buf, next);
116
117 if ( next != NULL )
118  *next = buf->next;
119
120 free(buf->data);
121 free(buf);
122
123 ROAR_DBG("buffer_delete(buf=%p, next=%p) = 0", buf, next);
124 return 0;
125}
126
127int roar_buffer_add      (struct roar_buffer * buf, struct roar_buffer *  next) {
128 if ( buf == NULL )
129  return -1;
130
131 ROAR_DBG("buffer_add(buf=%p, next=%p) = ?", buf, next);
132
133 while ( buf->next != NULL ) {
134  ROAR_DBG("buffer_add(*): buf=%p, next=%p (len=%i)", buf, buf->next, buf->user_len);
135//  ROAR_DBG("buffer_add(): buf=%p, buf->next=%p", buf, buf->next);
136  buf = buf->next;
137 }
138
139 buf->next = next;
140
141 return 0;
142}
143
144int roar_buffer_get_next (struct roar_buffer *  buf, struct roar_buffer ** next) {
145 if ( buf == NULL )
146  return -1;
147
148 *next = buf->next;
149
150 return 0;
151}
152
153int roar_buffer_get_data (struct roar_buffer *  buf, void   ** data) {
154 if ( buf == NULL )
155  return -1;
156
157 *data = buf->user_data;
158
159 return 0;
160}
161
162int roar_buffer_set_offset (struct roar_buffer *  buf, size_t off) {
163 if ( buf == NULL )
164  return -1;
165
166 buf->user_len  -= off;
167 buf->user_data += off;
168
169 return 0;
170}
171
172int roar_buffer_shift_out (struct roar_buffer ** buf, void * data, size_t * len) {
173 size_t todo, cl;
174 struct roar_buffer * cur;
175 void * cd;
176
177 if ( len == NULL || buf == NULL || data == NULL ) {
178  ROAR_DBG("roar_buffer_shift_out(buf=%p, data=%p, len={%lu}) = -1 // Invalid input", buf, data, (unsigned long)len);
179  return -1;
180 }
181
182 if ( *buf == NULL ) {
183  ROAR_DBG("roar_buffer_shift_out(buf=%p, data=%p, len={%lu}) = -1 // Invalid pointer to buffer ring", buf, data, (unsigned long)len);
184  return -1;
185 }
186
187 todo = *len;
188 cur  = *buf;
189
190 *len = 0;
191
192 while (todo) {
193  ROAR_DBG("roar_buffer_shift_out(*): todo=%u, cur=%p", (unsigned int) todo, cur);
194
195  if ( roar_buffer_get_len(cur, &cl) == -1 )
196   return -1;
197
198  if ( cl > todo ) {
199   if ( roar_buffer_get_data(cur, &cd) == -1 )
200    return -1;
201
202   cl = todo;
203
204   memcpy(data, cd, cl);
205   todo -= cl;
206   data += cl;
207   *len += cl;
208
209   if ( roar_buffer_set_offset(cur, cl) == -1 )
210    return -1;
211  } else {
212   if ( roar_buffer_get_data(cur, &cd) == -1 )
213    return -1;
214
215   memcpy(data, cd, cl);
216   todo -= cl;
217   data += cl;
218   *len += cl;
219
220   if ( roar_buffer_next(&cur) == -1 )
221    return -1;
222  }
223
224/*
225  if ( cur == NULL )
226   break;
227*/
228 }
229
230 *buf = cur;
231
232 return 0;
233}
234
235int roar_buffer_set_meta (struct roar_buffer * buf, void *  meta) {
236 if ( buf == NULL )
237  return -1;
238
239 buf->meta = meta;
240
241 return 0;
242}
243
244int roar_buffer_get_meta (struct roar_buffer * buf, void ** meta) {
245 if ( buf == NULL )
246  return -1;
247
248 *meta = buf->meta;
249
250 return 0;
251}
252
253int roar_buffer_set_len  (struct roar_buffer *  buf, size_t    len) {
254 if ( buf == NULL )
255  return -1;
256
257 buf->user_len = len;
258
259 return 0;
260}
261
262int roar_buffer_get_len  (struct roar_buffer *  buf, size_t *  len) {
263 if ( buf == NULL )
264  return -1;
265
266 *len = buf->user_len;
267
268 return 0;
269}
270
271int roar_buffer_set_flag (struct roar_buffer *  buf, int flag, int reset) {
272 if ( buf == NULL )
273  return -1;
274
275 buf->flags |= flag;
276
277 if ( reset )
278  buf->flags -= flag;
279
280 return 0;
281}
282
283int roar_buffer_get_flag (struct roar_buffer *  buf, int flag) {
284 if ( buf == NULL )
285  return -1;
286
287 return buf->flags & flag;
288}
289
290int roar_buffer_duplicate (struct roar_buffer *  buf, struct roar_buffer ** copy) {
291 struct roar_buffer *  cur = buf;
292 struct roar_buffer *  new;
293 void * od, * nd;
294
295 *copy = NULL;
296
297 while (cur) {
298  if ( roar_buffer_new(&new, cur->user_len) == -1 ) {
299   roar_buffer_free(*copy);
300   return -1;
301  }
302
303  if ( *copy == NULL )
304   *copy = new;
305
306  roar_buffer_get_data(cur, &od);
307  roar_buffer_get_data(new, &nd);
308  memcpy(nd, od, cur->user_len);
309
310  roar_buffer_add(*copy, new);
311
312  cur = cur->next;
313 }
314 return 0;
315}
316
317int roar_buffer_ring_stats (struct roar_buffer *  buf, struct roar_buffer_stats * stats) {
318 if ( buf == NULL )
319  return -1;
320
321 stats->parts        = 0;
322 stats->bytes        = 0;
323 stats->memory_usage = 0;
324
325 while (buf) {
326  stats->parts++;
327  stats->bytes        += buf->user_len;
328  stats->memory_usage += buf->len + sizeof(struct roar_buffer);
329  buf = buf->next;
330 }
331
332 return 0;
333}
334
335//ll
Note: See TracBrowser for help on using the repository browser.