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 | |
---|
37 | int roar_buffer_new (struct roar_buffer ** buf, size_t len) { |
---|
38 | struct roar_buffer * new; |
---|
39 | |
---|
40 | ROAR_DBG("buffer_new(buf=%p, len=%i) = ?", buf, len); |
---|
41 | |
---|
42 | if ((new = malloc(sizeof(struct roar_buffer))) == NULL) { |
---|
43 | *buf = NULL; |
---|
44 | return -1; |
---|
45 | } |
---|
46 | |
---|
47 | if ((new->data = malloc(len)) == NULL) { |
---|
48 | free(new); |
---|
49 | *buf = NULL; |
---|
50 | return -1; |
---|
51 | } |
---|
52 | |
---|
53 | new->user_data = new->data; |
---|
54 | |
---|
55 | new->next = NULL; |
---|
56 | |
---|
57 | new->len = len; |
---|
58 | new->user_len = len; |
---|
59 | *buf = new; |
---|
60 | |
---|
61 | ROAR_DBG("buffer_new(buf=%p, len=%i): New buffer at %p", buf, len, new); |
---|
62 | |
---|
63 | return 0; |
---|
64 | } |
---|
65 | |
---|
66 | int roar_buffer_free (struct roar_buffer * buf) { |
---|
67 | struct roar_buffer * next; |
---|
68 | |
---|
69 | if ( buf == NULL ) |
---|
70 | return -1; |
---|
71 | |
---|
72 | while ((next = buf->next)) { |
---|
73 | free(buf->data); |
---|
74 | free(buf); |
---|
75 | buf = next; |
---|
76 | } |
---|
77 | |
---|
78 | free(buf->data); |
---|
79 | free(buf); |
---|
80 | |
---|
81 | return 0; |
---|
82 | } |
---|
83 | |
---|
84 | int roar_buffer_delete (struct roar_buffer * buf, struct roar_buffer ** next) { |
---|
85 | if ( buf == NULL ) { |
---|
86 | if ( next != NULL ) |
---|
87 | *next = NULL; |
---|
88 | return -1; |
---|
89 | } |
---|
90 | |
---|
91 | ROAR_DBG("buffer_delete(buf=%p, next=%p) = ?", buf, next); |
---|
92 | |
---|
93 | if ( next != NULL ) |
---|
94 | *next = buf->next; |
---|
95 | |
---|
96 | free(buf->data); |
---|
97 | free(buf); |
---|
98 | |
---|
99 | ROAR_DBG("buffer_delete(buf=%p, next=%p) = 0", buf, next); |
---|
100 | return 0; |
---|
101 | } |
---|
102 | |
---|
103 | int roar_buffer_add (struct roar_buffer * buf, struct roar_buffer * next) { |
---|
104 | if ( buf == NULL ) |
---|
105 | return -1; |
---|
106 | |
---|
107 | ROAR_DBG("buffer_add(buf=%p, next=%p) = ?", buf, next); |
---|
108 | |
---|
109 | while ( buf->next != NULL ) { |
---|
110 | ROAR_DBG("buffer_add(*): buf=%p, next=%p (len=%i)", buf, buf->next, buf->user_len); |
---|
111 | // ROAR_DBG("buffer_add(): buf=%p, buf->next=%p", buf, buf->next); |
---|
112 | buf = buf->next; |
---|
113 | } |
---|
114 | |
---|
115 | buf->next = next; |
---|
116 | |
---|
117 | return 0; |
---|
118 | } |
---|
119 | |
---|
120 | int roar_buffer_get_next (struct roar_buffer * buf, struct roar_buffer ** next) { |
---|
121 | if ( buf == NULL ) |
---|
122 | return -1; |
---|
123 | |
---|
124 | *next = buf->next; |
---|
125 | |
---|
126 | return 0; |
---|
127 | } |
---|
128 | |
---|
129 | int roar_buffer_get_data (struct roar_buffer * buf, void ** data) { |
---|
130 | if ( buf == NULL ) |
---|
131 | return -1; |
---|
132 | |
---|
133 | *data = buf->user_data; |
---|
134 | |
---|
135 | return 0; |
---|
136 | } |
---|
137 | |
---|
138 | int roar_buffer_set_offset (struct roar_buffer * buf, size_t off) { |
---|
139 | if ( buf == NULL ) |
---|
140 | return -1; |
---|
141 | |
---|
142 | buf->user_len -= off; |
---|
143 | buf->user_data += off; |
---|
144 | |
---|
145 | return 0; |
---|
146 | } |
---|
147 | |
---|
148 | int roar_buffer_set_meta (struct roar_buffer * buf, void * meta) { |
---|
149 | if ( buf == NULL ) |
---|
150 | return -1; |
---|
151 | |
---|
152 | buf->meta = meta; |
---|
153 | |
---|
154 | return 0; |
---|
155 | } |
---|
156 | |
---|
157 | int roar_buffer_get_meta (struct roar_buffer * buf, void ** meta) { |
---|
158 | if ( buf == NULL ) |
---|
159 | return -1; |
---|
160 | |
---|
161 | *meta = buf->meta; |
---|
162 | |
---|
163 | return 0; |
---|
164 | } |
---|
165 | |
---|
166 | int roar_buffer_set_len (struct roar_buffer * buf, size_t len) { |
---|
167 | if ( buf == NULL ) |
---|
168 | return -1; |
---|
169 | |
---|
170 | buf->user_len = len; |
---|
171 | |
---|
172 | return 0; |
---|
173 | } |
---|
174 | |
---|
175 | int roar_buffer_get_len (struct roar_buffer * buf, size_t * len) { |
---|
176 | if ( buf == NULL ) |
---|
177 | return -1; |
---|
178 | |
---|
179 | *len = buf->user_len; |
---|
180 | |
---|
181 | return 0; |
---|
182 | } |
---|
183 | |
---|
184 | int roar_buffer_duplicate (struct roar_buffer * buf, struct roar_buffer ** copy) { |
---|
185 | struct roar_buffer * cur = buf; |
---|
186 | struct roar_buffer * new; |
---|
187 | void * od, * nd; |
---|
188 | |
---|
189 | *copy = NULL; |
---|
190 | |
---|
191 | while (cur) { |
---|
192 | if ( roar_buffer_new(&new, cur->user_len) == -1 ) { |
---|
193 | roar_buffer_free(*copy); |
---|
194 | return -1; |
---|
195 | } |
---|
196 | |
---|
197 | if ( *copy == NULL ) |
---|
198 | *copy = new; |
---|
199 | |
---|
200 | roar_buffer_get_data(cur, &od); |
---|
201 | roar_buffer_get_data(new, &nd); |
---|
202 | memcpy(nd, od, cur->user_len); |
---|
203 | |
---|
204 | roar_buffer_add(*copy, new); |
---|
205 | |
---|
206 | cur = cur->next; |
---|
207 | } |
---|
208 | return 0; |
---|
209 | } |
---|
210 | |
---|
211 | int roar_buffer_ring_stats (struct roar_buffer * buf, struct roar_buffer_stats * stats) { |
---|
212 | if ( buf == NULL ) |
---|
213 | return -1; |
---|
214 | |
---|
215 | stats->parts = 0; |
---|
216 | stats->bytes = 0; |
---|
217 | stats->memory_usage = 0; |
---|
218 | |
---|
219 | while (buf) { |
---|
220 | stats->parts++; |
---|
221 | stats->bytes += buf->user_len; |
---|
222 | stats->memory_usage += buf->len + sizeof(struct roar_buffer); |
---|
223 | buf = buf->next; |
---|
224 | } |
---|
225 | |
---|
226 | return 0; |
---|
227 | } |
---|
228 | |
---|
229 | //ll |
---|