Mercurial > hg > audiostuff
comparison spandsp-0.0.3/spandsp-0.0.3/src/queue.c @ 5:f762bf195c4b
import spandsp-0.0.3
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Fri, 25 Jun 2010 16:00:21 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4:26cd8f1ef0b1 | 5:f762bf195c4b |
---|---|
1 /* | |
2 * SpanDSP - a series of DSP components for telephony | |
3 * | |
4 * queue.c - simple in process message queuing | |
5 * | |
6 * Written by Steve Underwood <steveu@coppice.org> | |
7 * | |
8 * Copyright (C) 2004 Steve Underwood | |
9 * | |
10 * All rights reserved. | |
11 * | |
12 * This program is free software; you can redistribute it and/or modify | |
13 * it under the terms of the GNU General Public License version 2, as | |
14 * published by the Free Software Foundation. | |
15 * | |
16 * This program is distributed in the hope that it will be useful, | |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 * GNU General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU General Public License | |
22 * along with this program; if not, write to the Free Software | |
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 * | |
25 * $Id: queue.c,v 1.10 2006/11/19 14:07:25 steveu Exp $ | |
26 */ | |
27 | |
28 /*! \file */ | |
29 | |
30 #ifdef HAVE_CONFIG_H | |
31 #include <config.h> | |
32 #endif | |
33 | |
34 #include <stdio.h> | |
35 #include <string.h> | |
36 #include <time.h> | |
37 #include <ctype.h> | |
38 #include <stdlib.h> | |
39 #include <inttypes.h> | |
40 #include <sys/types.h> | |
41 | |
42 #include <tiffio.h> | |
43 | |
44 #include "spandsp/queue.h" | |
45 | |
46 int queue_empty(queue_t *p) | |
47 { | |
48 return (p->iptr == p->optr); | |
49 } | |
50 /*- End of function --------------------------------------------------------*/ | |
51 | |
52 int queue_free_space(queue_t *p) | |
53 { | |
54 if (p->iptr < p->optr) | |
55 return ((p->optr - p->iptr) - 1); | |
56 /*endif*/ | |
57 return (p->len - (p->iptr - p->optr)); | |
58 } | |
59 /*- End of function --------------------------------------------------------*/ | |
60 | |
61 int queue_contents(queue_t *p) | |
62 { | |
63 if (p->iptr < p->optr) | |
64 return (p->len - (p->optr - p->iptr) + 1); | |
65 /*endif*/ | |
66 return (p->iptr - p->optr); | |
67 } | |
68 /*- End of function --------------------------------------------------------*/ | |
69 | |
70 void queue_flush(queue_t *p) | |
71 { | |
72 p->iptr = | |
73 p->optr = 0; | |
74 } | |
75 /*- End of function --------------------------------------------------------*/ | |
76 | |
77 int queue_view(queue_t *p, uint8_t *buf, int len) | |
78 { | |
79 int real_len; | |
80 int to_end; | |
81 | |
82 real_len = queue_contents(p); | |
83 if (real_len < len) | |
84 { | |
85 if (p->flags & QUEUE_READ_ATOMIC) | |
86 return -1; | |
87 /*endif*/ | |
88 } | |
89 else | |
90 { | |
91 real_len = len; | |
92 } | |
93 /*endif*/ | |
94 if (real_len == 0) | |
95 return 0; | |
96 /*endif*/ | |
97 to_end = p->len + 1 - p->optr; | |
98 if (p->iptr < p->optr && to_end < real_len) | |
99 { | |
100 /* A two step process */ | |
101 if (buf) | |
102 { | |
103 memcpy(buf, p->data + p->optr, to_end); | |
104 memcpy(buf + to_end, p->data, real_len - to_end); | |
105 } | |
106 /*endif*/ | |
107 } | |
108 else | |
109 { | |
110 /* A one step process */ | |
111 if (buf) | |
112 memcpy(buf, p->data + p->optr, real_len); | |
113 /*endif*/ | |
114 } | |
115 /*endif*/ | |
116 return real_len; | |
117 } | |
118 /*- End of function --------------------------------------------------------*/ | |
119 | |
120 int queue_read(queue_t *p, uint8_t *buf, int len) | |
121 { | |
122 int real_len; | |
123 int to_end; | |
124 | |
125 real_len = queue_contents(p); | |
126 if (real_len < len) | |
127 { | |
128 if (p->flags & QUEUE_READ_ATOMIC) | |
129 return -1; | |
130 /*endif*/ | |
131 } | |
132 else | |
133 { | |
134 real_len = len; | |
135 } | |
136 /*endif*/ | |
137 if (real_len == 0) | |
138 return 0; | |
139 /*endif*/ | |
140 to_end = p->len + 1 - p->optr; | |
141 if (p->iptr < p->optr && to_end < real_len) | |
142 { | |
143 /* A two step process */ | |
144 if (buf) | |
145 { | |
146 memcpy(buf, p->data + p->optr, to_end); | |
147 memcpy(buf + to_end, p->data, real_len - to_end); | |
148 } | |
149 /*endif*/ | |
150 p->optr += (real_len - p->len + 1); | |
151 } | |
152 else | |
153 { | |
154 /* A one step process */ | |
155 if (buf) | |
156 memcpy(buf, p->data + p->optr, real_len); | |
157 /*endif*/ | |
158 p->optr += real_len; | |
159 if (p->optr > (p->len + 1)) | |
160 p->optr = 0; | |
161 /*endif*/ | |
162 } | |
163 /*endif*/ | |
164 return real_len; | |
165 } | |
166 /*- End of function --------------------------------------------------------*/ | |
167 | |
168 int queue_write(queue_t *p, const uint8_t *buf, int len) | |
169 { | |
170 int real_len; | |
171 int to_end; | |
172 | |
173 real_len = queue_free_space(p); | |
174 if (real_len < len) | |
175 { | |
176 if (p->flags & QUEUE_WRITE_ATOMIC) | |
177 return -1; | |
178 /*endif*/ | |
179 } | |
180 else | |
181 { | |
182 real_len = len; | |
183 } | |
184 /*endif*/ | |
185 if (real_len == 0) | |
186 return 0; | |
187 /*endif*/ | |
188 to_end = p->len + 1 - p->iptr; | |
189 if (p->iptr < p->optr || to_end >= real_len) | |
190 { | |
191 /* A one step process */ | |
192 memcpy(p->data + p->iptr, buf, real_len); | |
193 p->iptr += real_len; | |
194 if (p->iptr > (p->len + 1)) | |
195 p->iptr = 0; | |
196 /*endif*/ | |
197 } | |
198 else | |
199 { | |
200 /* A two step process */ | |
201 memcpy(p->data + p->iptr, buf, to_end); | |
202 memcpy(p->data, buf + to_end, real_len - to_end); | |
203 p->iptr += (real_len - p->len + 1); | |
204 } | |
205 /*endif*/ | |
206 return real_len; | |
207 } | |
208 /*- End of function --------------------------------------------------------*/ | |
209 | |
210 int queue_test_msg(queue_t *p) | |
211 { | |
212 uint16_t lenx; | |
213 | |
214 if (queue_view(p, (uint8_t *) &lenx, sizeof(uint16_t)) != sizeof(uint16_t)) | |
215 return -1; | |
216 /*endif*/ | |
217 return lenx; | |
218 } | |
219 /*- End of function --------------------------------------------------------*/ | |
220 | |
221 int queue_read_msg(queue_t *p, uint8_t *buf, int len) | |
222 { | |
223 uint16_t lenx; | |
224 | |
225 if (queue_read(p, (uint8_t *) &lenx, sizeof(uint16_t)) != sizeof(uint16_t)) | |
226 return -1; | |
227 /*endif*/ | |
228 if (lenx == 0) | |
229 return 0; | |
230 /*endif*/ | |
231 if ((int) lenx > len) | |
232 { | |
233 len = queue_read(p, buf, len); | |
234 /* Discard the rest of the message */ | |
235 queue_read(p, NULL, lenx - len); | |
236 return len; | |
237 } | |
238 /*endif*/ | |
239 return queue_read(p, buf, lenx); | |
240 } | |
241 /*- End of function --------------------------------------------------------*/ | |
242 | |
243 int queue_write_msg(queue_t *p, const uint8_t *buf, int len) | |
244 { | |
245 uint16_t lenx; | |
246 | |
247 if (queue_free_space(p) < (int) (len + sizeof(uint16_t))) | |
248 return 0; | |
249 /*endif*/ | |
250 lenx = (uint16_t) len; | |
251 if (queue_write(p, (uint8_t *) &lenx, sizeof(uint16_t)) != sizeof(uint16_t)) | |
252 return -1; | |
253 /*endif*/ | |
254 if (len == 0) | |
255 return 0; | |
256 return queue_write(p, buf, len); | |
257 } | |
258 /*- End of function --------------------------------------------------------*/ | |
259 | |
260 int queue_create(queue_t *p, int len, int flags) | |
261 { | |
262 p->iptr = | |
263 p->optr = 0; | |
264 p->flags = flags; | |
265 p->len = len; | |
266 p->data = malloc(len + 1); | |
267 if (p->data == NULL) | |
268 { | |
269 p->flags = 0; | |
270 p->len = 0; | |
271 return -1; | |
272 } | |
273 /*endif*/ | |
274 return 0; | |
275 } | |
276 /*- End of function --------------------------------------------------------*/ | |
277 | |
278 int queue_delete(queue_t *p) | |
279 { | |
280 p->flags = 0; | |
281 p->iptr = | |
282 p->optr = 0; | |
283 p->len = 0; | |
284 if (p->data) | |
285 { | |
286 free(p->data); | |
287 p->data = NULL; | |
288 } | |
289 return 0; | |
290 } | |
291 /*- End of function --------------------------------------------------------*/ | |
292 /*- End of file ------------------------------------------------------------*/ |