Debian Codegraph @

sound/sfifo.c

raw symbols SCIP
1 /*
2 ------------------------------------------------------------
3 SFIFO 1.4
4 ------------------------------------------------------------
5 * Simple portable lock-free FIFO
6 * (c) 2000-2002, David Olofson
7 -----------------------------------------------------------
8 TODO:
9 * Is there a way to avoid losing one byte of buffer
10 space to avoid extra variables or locking?
11
12 * Test more compilers and environments.
13 -----------------------------------------------------------
14 */
15
16 #ifdef __KERNEL__
17 # include <linux/string.h>
18 # include <asm/uaccess.h>
19 # include <linux/malloc.h>
20 # define _sf_malloc(x) kmalloc(x, GFP_KERNEL)
21 # define _sf_free(x, y) kfree_s(x, y)
22 #else
23 # include <string.h>
24 # include <stdlib.h>
25 # define _sf_malloccxx . . $ `sound/sfifo.c:25:10`!(x) malloc(x)
26 # define _sf_freecxx . . $ `sound/sfifo.c:26:10`!(x, y) free(x)
27 #endif
28
29 #include "sfifo.h"cxx . . $ `<file>/sound/sfifo.h`/
30
31 #ifdef _SFIFO_TEST_
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <pthread.h>
35 #define DBG(x) /*(x)*/
36 #define TEST_BUFSIZE 10
37 #else
38 #define DBGcxx . . $ `sound/sfifo.c:38:9`!(x)
39 #endif
40
41
42 /*
43 * Alloc buffer, init FIFO etc...
44 */
45 int sfifo_initcxx . . $ sfifo_init(27926066c38fdf0f).(sfifo_tcxx . . $ sfifo_t# *f, unsigned size)
46 {
47 memsetcxx . . $ memset(d1de1e4fb215f1c6).(f, 0, sizeof(sfifo_tcxx . . $ sfifo_t#));
48
49 if(size > SFIFO_MAX_BUFFER_SIZEcxx . . $ `sound/sfifo.h:70:10`!)
50 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
51
52 /*
53 * Set sufficient power-of-2 size.
54 *
55 * No, there's no bug. If you need
56 * room for N bytes, the buffer must
57 * be at least N+1 bytes. (The fifo
58 * can't tell 'empty' from 'full'
59 * without unsafe index manipulations
60 * otherwise.)
61 */
62 f->sizecxx . . $ sfifo_t#size. = 1;
63 for(; f->sizecxx . . $ sfifo_t#size. <= size; f->sizecxx . . $ sfifo_t#size. <<= 1)
64 ;
65
66 /* Get buffer */
67 if( 0 == (f->buffercxx . . $ sfifo_t#buffer. = (void *)_sf_malloccxx . . $ `sound/sfifo.c:25:10`!(f->size)) )
68 return -ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
69
70 return 0;
71 }
72
73 /*
74 * Dealloc buffer etc...
75 */
76 void sfifo_closecxx . . $ sfifo_close(ea65734933742a6b).(sfifo_tcxx . . $ sfifo_t# *f)
77 {
78 if(f->buffercxx . . $ sfifo_t#buffer.)
79 {
80 _sf_freecxx . . $ `sound/sfifo.c:26:10`!(f->buffer, f->size);
81 memsetcxx . . $ memset(d1de1e4fb215f1c6).(f, 0, sizeof(sfifo_tcxx . . $ sfifo_t#));
82 }
83 }
84
85 /*
86 * Empty FIFO buffer
87 */
88 void sfifo_flushcxx . . $ sfifo_flush(ea65734933742a6b).(sfifo_tcxx . . $ sfifo_t# *f)
89 {
90 /* Reset positions */
91 f->readposcxx . . $ sfifo_t#readpos. = 0;
92 f->writeposcxx . . $ sfifo_t#writepos. = 0;
93 }
94
95 /*
96 * Write bytes to a FIFO
97 * Return number of bytes written, or an error code
98 */
99 int sfifo_writecxx . . $ sfifo_write(b495d09471afdf18).(sfifo_tcxx . . $ sfifo_t# *f, const void *_buf, unsigned len)
100 {
101 unsigned total;
102 int i;
103 const char *buf = (const char *)_buf;
104
105 if(!f->buffercxx . . $ sfifo_t#buffer.)
106 return -ENODEVcxx . . $ `<external>/8987895939074593727/errno-base.h:23:9`!; /* No buffer! */
107
108 /* total = len = min(space, len) */
109 total = sfifo_spacecxx . . $ `sound/sfifo.h:93:9`!(f);
110 DBGcxx . . $ `sound/sfifo.c:38:9`!(printf("sfifo_space() = %d\n",total));
111 if(len > total)
112 len = total;
113 else
114 total = len;
115
116 i = f->writeposcxx . . $ sfifo_t#writepos.;
117 if(i + len > f->sizecxx . . $ sfifo_t#size.)
118 {
119 memcpycxx . . $ memcpy(cc817cb175f282d).(f->buffercxx . . $ sfifo_t#buffer. + i, buf, f->sizecxx . . $ sfifo_t#size. - i);
120 buf += f->sizecxx . . $ sfifo_t#size. - i;
121 len -= f->sizecxx . . $ sfifo_t#size. - i;
122 i = 0;
123 }
124 memcpycxx . . $ memcpy(cc817cb175f282d).(f->buffercxx . . $ sfifo_t#buffer. + i, buf, len);
125 f->writeposcxx . . $ sfifo_t#writepos. = i + len;
126
127 return (int)total;
128 }
129
130 #ifdef __KERNEL__
131 /*
132 * Write bytes to a FIFO from a user space buffer
133 * Return number of bytes written, or an error code
134 */
135 int sfifo_write_user(sfifo_t *f, const void *buf, unsigned len)
136 {
137 int total;
138 int i;
139
140 if(!f->buffer)
141 return -ENODEV; /* No buffer! */
142
143 /* total = len = min(space, len) */
144 total = sfifo_space(f);
145 if(len > total)
146 len = total;
147 else
148 total = len;
149
150 i = f->writepos;
151 if(i + len > f->size)
152 {
153 if(f->size - i)
154 {
155 if(copy_from_user(f->buffer + i, buf, f->size - i))
156 return -EFAULT;
157 buf += f->size - i;
158 len -= f->size - i;
159 }
160 i = 0;
161 }
162 if(copy_from_user(f->buffer + i, buf, len))
163 return -EFAULT;
164 f->writepos = i + len;
165
166 return total;
167 }
168 #endif
169
170 /*
171 * Read bytes from a FIFO
172 * Return number of bytes read, or an error code
173 */
174 int sfifo_readcxx . . $ sfifo_read(2549111c1a0b0c59).(sfifo_tcxx . . $ sfifo_t# *f, void *_buf, unsigned len)
175 {
176 unsigned total;
177 int i;
178 char *buf = (char *)_buf;
179
180 if(!f->buffercxx . . $ sfifo_t#buffer.)
181 return -ENODEVcxx . . $ `<external>/8987895939074593727/errno-base.h:23:9`!; /* No buffer! */
182
183 /* total = len = min(used, len) */
184 total = sfifo_usedcxx . . $ `sound/sfifo.h:92:9`!(f);
185 DBGcxx . . $ `sound/sfifo.c:38:9`!(printf("sfifo_used() = %d\n",total));
186 if(len > total)
187 len = total;
188 else
189 total = len;
190
191 i = f->readposcxx . . $ sfifo_t#readpos.;
192 if(i + len > f->sizecxx . . $ sfifo_t#size.)
193 {
194 memcpycxx . . $ memcpy(cc817cb175f282d).(buf, f->buffercxx . . $ sfifo_t#buffer. + i, f->sizecxx . . $ sfifo_t#size. - i);
195 buf += f->sizecxx . . $ sfifo_t#size. - i;
196 len -= f->sizecxx . . $ sfifo_t#size. - i;
197 i = 0;
198 }
199 memcpycxx . . $ memcpy(cc817cb175f282d).(buf, f->buffercxx . . $ sfifo_t#buffer. + i, len);
200 f->readposcxx . . $ sfifo_t#readpos. = i + len;
201
202 return (int)total;
203 }
204
205 #ifdef __KERNEL__
206 /*
207 * Read bytes from a FIFO into a user space buffer
208 * Return number of bytes read, or an error code
209 */
210 int sfifo_read_user(sfifo_t *f, void *buf, unsigned len)
211 {
212 int total;
213 int i;
214
215 if(!f->buffer)
216 return -ENODEV; /* No buffer! */
217
218 /* total = len = min(used, len) */
219 total = sfifo_used(f);
220 if(len > total)
221 len = total;
222 else
223 total = len;
224
225 i = f->readpos;
226 if(i + len > f->size)
227 {
228 if(f->size - i)
229 {
230 if(copy_to_user(buf, f->buffer + i, f->size - i))
231 return -EFAULT;
232 buf += f->size - i;
233 len -= f->size - i;
234 }
235 i = 0;
236 }
237 if(copy_to_user(buf, f->buffer + i, len))
238 return -EFAULT;
239 f->readpos = i + len;
240
241 return total;
242 }
243 #endif
244
245 #ifdef _SFIFO_TEST_
246 void *sender(void *arg)
247 {
248 char buf[TEST_BUFSIZE*2];
249 int i,j;
250 int cnt = 0;
251 int res;
252 sfifo_t *sf = (sfifo_t *)arg;
253 while(1)
254 {
255 j = sfifo_space(sf);
256 for(i = 0; i < j; ++i)
257 {
258 ++cnt;
259 buf[i] = cnt;
260 }
261 res = sfifo_write(sf, &buf, j);
262 if(res != j)
263 {
264 printf("Write failed!\n");
265 sleep(1);
266 } else if(res)
267 printf("Wrote %d\n", res);
268 }
269 }
270 int main()
271 {
272 sfifo_t sf;
273 char last = 0;
274 pthread_t thread;
275 char buf[100] = "---------------------------------------";
276
277 printf("sfifo_init(&sf, %d) = %d\n",
278 TEST_BUFSIZE, sfifo_init(&sf, TEST_BUFSIZE)
279 );
280
281 #if 0
282 printf("sfifo_write(&sf,\"0123456789\",7) = %d\n",
283 sfifo_write(&sf,"0123456789",7) );
284
285 printf("sfifo_write(&sf,\"abcdefghij\",7) = %d\n",
286 sfifo_write(&sf,"abcdefghij",7) );
287
288 printf("sfifo_read(&sf,buf,8) = %d\n",
289 sfifo_read(&sf,buf,8) );
290
291 buf[20] = 0;
292 printf("buf =\"%s\"\n",buf);
293
294 printf("sfifo_write(&sf,\"0123456789\",7) = %d\n",
295 sfifo_write(&sf,"0123456789",7) );
296
297 printf("sfifo_read(&sf,buf,10) = %d\n",
298 sfifo_read(&sf,buf,10) );
299
300 buf[20] = 0;
301 printf("buf =\"%s\"\n",buf);
302 #else
303 pthread_create(&thread, NULL, sender, &sf);
304
305 while(1)
306 {
307 static int c = 0;
308 ++last;
309 while( sfifo_read(&sf,buf,1) != 1 )
310 /*sleep(1)*/;
311 if(last != buf[0])
312 {
313 printf("Error %d!\n", buf[0]-last);
314 last = buf[0];
315 }
316 else
317 printf("Ok. (%d)\n",++c);
318 }
319 #endif
320
321 sfifo_close(&sf);
322 printf("sfifo_close(&sf)\n");
323
324 return 0;
325 }
326
327 #endif