2
SFIFO 1.3 Simple portable lock-free FIFO
4
(c) 2000-2002, David Olofson - free software under the terms of the LGPL 2.1
10
* gcc / Linux / x86: Works
11
* gcc / Linux / x86 kernel: Works
12
* gcc / FreeBSD / x86: Works
13
* gcc / NetBSD / x86: Works
14
* gcc / Mac OS X / PPC: Works
15
* gcc / Win32 / x86: Works
16
* Borland C++ / DOS / x86RM: Works
17
* Borland C++ / Win32 / x86PM16: Untested
18
* ? / Various Un*ces / ?: Untested
19
* ? / Mac OS / PPC: Untested
20
* gcc / BeOS / x86: Untested
21
* gcc / BeOS / PPC: Untested
22
* ? / ? / Alpha: Untested
24
* 1.2: Max buffer size halved, to avoid problems with
27
* 1.3: Critical buffer allocation bug fixed! For certain
28
* requested buffer sizes, older version would
29
* allocate a buffer of insufficient size, which
30
* would result in memory thrashing. (Amazing that
31
* I've manage to use this to the extent I have
32
* without running into this... *heh*)
44
/*------------------------------------------------
46
------------------------------------------------*/
49
* Reads and writes of a variable of this type in memory
50
* must be *atomic*! 'int' is *not* atomic on all platforms.
51
* A safe type should be used, and sfifo should limit the
52
* maximum buffer size accordingly.
54
typedef int sfifo_atomic_t;
56
# define SFIFO_MAX_BUFFER_SIZE 0x7fff
57
#else /* Kludge: Assume 32 bit platform */
58
# define SFIFO_MAX_BUFFER_SIZE 0x7fffffff
61
typedef struct sfifo_t
64
int size; /* Number of bytes */
65
sfifo_atomic_t readpos; /* Read position */
66
sfifo_atomic_t writepos; /* Write position */
69
#define SFIFO_SIZEMASK(x) ((x)->size - 1)
72
/*------------------------------------------------
74
------------------------------------------------*/
75
int sfifo_init(sfifo_t *f, int size);
76
void sfifo_close(sfifo_t *f);
77
void sfifo_flush(sfifo_t *f);
78
int sfifo_write(sfifo_t *f, const void *buf, int len);
79
int sfifo_read(sfifo_t *f, void *buf, int len);
80
#define sfifo_used(x) (((x)->writepos - (x)->readpos) & SFIFO_SIZEMASK(x))
81
#define sfifo_space(x) ((x)->size - 1 - sfifo_used(x))
82
#define sfifo_size(x) ((x)->size - 1)