File: sfifo.c

package info (click to toggle)
fuse-emulator 1.0.0.1a%2Bdfsg1-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 9,568 kB
  • sloc: ansic: 67,895; sh: 10,265; perl: 3,386; makefile: 787; yacc: 227; lex: 139
file content (330 lines) | stat: -rw-r--r-- 6,010 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
------------------------------------------------------------
	SFIFO 1.3
------------------------------------------------------------
 * Simple portable lock-free FIFO
 * (c) 2000-2002, David Olofson
 * Released under the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1

 * Modifications by Philip Kendall (c) 2007
 * This modified version is released under the GNU GENERAL PUBLIC LICENSE
 * version 2, or any later version
-----------------------------------------------------------
TODO:
	* Is there a way to avoid losing one byte of buffer
	  space to avoid extra variables or locking?

	* Test more compilers and environments.
-----------------------------------------------------------
 */

#include <config.h>

#ifdef __KERNEL__
#	include	<linux/string.h>
#	include	<asm/uaccess.h>
#	include	<linux/malloc.h>
#	define	malloc(x)	kmalloc(x, GFP_KERNEL)
#	define	free(x, y)	kfree_s(x, y)
#else
#	include	<string.h>
#	include	<stdlib.h>
#	define	free(x, y)	free(x)
#endif

#include "sfifo.h"

#ifdef _SFIFO_TEST_
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#define DBG(x)	/*(x)*/
#define TEST_BUFSIZE	10
#else
#define DBG(x)
#endif


/*
 * Alloc buffer, init FIFO etc...
 */
int sfifo_init(sfifo_t *f, int size)
{
	memset(f, 0, sizeof(sfifo_t));

	if(size > SFIFO_MAX_BUFFER_SIZE)
		return -EINVAL;

	/*
	 * Set sufficient power-of-2 size.
	 *
	 * No, there's no bug. If you need
	 * room for N bytes, the buffer must
	 * be at least N+1 bytes. (The fifo
	 * can't tell 'empty' from 'full'
	 * without unsafe index manipulations
	 * otherwise.)
	 */
	f->size = 1;
	for(; f->size <= size; f->size <<= 1)
		;

	/* Get buffer */
	if( 0 == (f->buffer = malloc(f->size)) )
		return -ENOMEM;

	return 0;
}

/*
 * Dealloc buffer etc...
 */
void sfifo_close(sfifo_t *f)
{
	if(f->buffer)
		free(f->buffer, f->size);
}

/*
 * Empty FIFO buffer
 */
void sfifo_flush(sfifo_t *f)
{
	/* Reset positions */
	f->readpos = 0;
	f->writepos = 0;
}

/*
 * Write bytes to a FIFO
 * Return number of bytes written, or an error code
 */
int sfifo_write(sfifo_t *f, const void *_buf, int len)
{
	int total;
	int i;
	const char *buf = (const char *)_buf;

	if(!f->buffer)
		return -ENODEV;	/* No buffer! */

	/* total = len = min(space, len) */
	total = sfifo_space(f);
	DBG(printf("sfifo_space() = %d\n",total));
	if(len > total)
		len = total;
	else
		total = len;

	i = f->writepos;
	if(i + len > f->size)
	{
		memcpy(f->buffer + i, buf, f->size - i);
		buf += f->size - i;
		len -= f->size - i;
		i = 0;
	}
	memcpy(f->buffer + i, buf, len);
	f->writepos = i + len;

	return total;
}

#ifdef __KERNEL__
/*
 * Write bytes to a FIFO from a user space buffer
 * Return number of bytes written, or an error code
 */
int sfifo_write_user(sfifo_t *f, const void *buf, int len)
{
	int total;
	int i;

	if(!f->buffer)
		return -ENODEV;	/* No buffer! */

	/* total = len = min(space, len) */
	total = sfifo_space(f);
	if(len > total)
		len = total;
	else
		total = len;

	i = f->writepos;
	if(i + len > f->size)
	{
		if(f->size - i)
		{
			if(copy_from_user(f->buffer + i, buf, f->size - i))
				return -EFAULT;
			buf += f->size - i;
			len -= f->size - i;
		}
		i = 0;
	}
	if(copy_from_user(f->buffer + i, buf, len))
		return -EFAULT;
	f->writepos = i + len;

	return total;
}
#endif

/*
 * Read bytes from a FIFO
 * Return number of bytes read, or an error code
 */
int sfifo_read(sfifo_t *f, void *_buf, int len)
{
	int total;
	int i;
	char *buf = (char *)_buf;

	if(!f->buffer)
		return -ENODEV;	/* No buffer! */

	/* total = len = min(used, len) */
	total = sfifo_used(f);
	DBG(printf("sfifo_used() = %d\n",total));
	if(len > total)
		len = total;
	else
		total = len;

	i = f->readpos;
	if(i + len > f->size)
	{
		memcpy(buf, f->buffer + i, f->size - i);
		buf += f->size - i;
		len -= f->size - i;
		i = 0;
	}
	memcpy(buf, f->buffer + i, len);
	f->readpos = i + len;

	return total;
}

#ifdef __KERNEL__
/*
 * Read bytes from a FIFO into a user space buffer
 * Return number of bytes read, or an error code
 */
int sfifo_read_user(sfifo_t *f, void *buf, int len)
{
	int total;
	int i;

	if(!f->buffer)
		return -ENODEV;	/* No buffer! */

	/* total = len = min(used, len) */
	total = sfifo_used(f);
	if(len > total)
		len = total;
	else
		total = len;

	i = f->readpos;
	if(i + len > f->size)
	{
		if(f->size - i)
		{
			if(copy_to_user(buf, f->buffer + i, f->size - i))
				return -EFAULT;
			buf += f->size - i;
			len -= f->size - i;
		}
		i = 0;
	}
	if(copy_to_user(buf, f->buffer + i, len))
		return -EFAULT;
	f->readpos = i + len;

	return total;
}
#endif

#ifdef _SFIFO_TEST_
void *sender(void *arg)
{
	char buf[TEST_BUFSIZE*2];
	int i,j;
	int cnt = 0;
	int res;
	sfifo_t *sf = (sfifo_t *)arg;
	while(1)
	{
		j = sfifo_space(sf);
		for(i = 0; i < j; ++i)
		{
			++cnt;
			buf[i] = cnt;
		}
		res = sfifo_write(sf, &buf, j);
		if(res != j)
		{
			printf("Write failed!\n");
			sleep(1);
		} else if(res)
			printf("Wrote %d\n", res);
	}
}
int main()
{
	sfifo_t sf;
	char last = 0;
	pthread_t thread;
	char buf[100] = "---------------------------------------";

	printf("sfifo_init(&sf, %d) = %d\n",
			TEST_BUFSIZE, sfifo_init(&sf, TEST_BUFSIZE)
		);

#if 0
	printf("sfifo_write(&sf,\"0123456789\",7) = %d\n",
		sfifo_write(&sf,"0123456789",7) );

	printf("sfifo_write(&sf,\"abcdefghij\",7) = %d\n",
		sfifo_write(&sf,"abcdefghij",7) );

	printf("sfifo_read(&sf,buf,8) = %d\n",
		sfifo_read(&sf,buf,8) );

	buf[20] = 0;
	printf("buf =\"%s\"\n",buf);

	printf("sfifo_write(&sf,\"0123456789\",7) = %d\n",
		sfifo_write(&sf,"0123456789",7) );

	printf("sfifo_read(&sf,buf,10) = %d\n",
		sfifo_read(&sf,buf,10) );

	buf[20] = 0;
	printf("buf =\"%s\"\n",buf);
#else
	pthread_create(&thread, NULL, sender, &sf);

	while(1)
	{
		static int c = 0;
		++last;
		while( sfifo_read(&sf,buf,1) != 1 )
			/*sleep(1)*/;
		if(last != buf[0])
		{
			printf("Error %d!\n", buf[0]-last);
			last = buf[0];
		}
		else
			printf("Ok. (%d)\n",++c);
	}
#endif

	sfifo_close(&sf);
	printf("sfifo_close(&sf)\n");

	return 0;
}

#endif