Debian Codegraph @

src/libout123/sfifo.h

raw
1 /*
2 SFIFO 1.3 Simple portable lock-free FIFO
3
4 (c) 2000-2002, David Olofson - free software under the terms of the LGPL 2.1
5 */
6
7
8 /*
9 * Platform support:
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
23 *
24 * 1.2: Max buffer size halved, to avoid problems with
25 * the sign bit...
26 *
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*)
33 */
34
35 #ifndef _SFIFO_H_
36 #define _SFIFO_H_
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 #include <errno.h>
43
44 /* Defining SFIFO_STATIC and then including the sfifo.c will result in local code. */
45 #ifdef SFIFO_STATIC
46 #define SFIFO_SCOPE static
47 #else
48 #define SFIFO_SCOPE
49 #endif
50
51 /*------------------------------------------------
52 "Private" stuff
53 ------------------------------------------------*/
54 /*
55 * Porting note:
56 * Reads and writes of a variable of this type in memory
57 * must be *atomic*! 'int' is *not* atomic on all platforms.
58 * A safe type should be used, and sfifo should limit the
59 * maximum buffer size accordingly.
60 */
61 typedef int sfifo_atomic_t;
62 #ifdef __TURBOC__
63 # define SFIFO_MAX_BUFFER_SIZE 0x7fff
64 #else /* Kludge: Assume 32 bit platform */
65 # define SFIFO_MAX_BUFFER_SIZE 0x7fffffff
66 #endif
67
68 typedef struct sfifo_t
69 {
70 char *buffer;
71 int size; /* Number of bytes */
72 sfifo_atomic_t readpos; /* Read position */
73 sfifo_atomic_t writepos; /* Write position */
74 } sfifo_t;
75
76 #define SFIFO_SIZEMASK(x) ((x)->size - 1)
77
78
79 /*------------------------------------------------
80 API
81 ------------------------------------------------*/
82 SFIFO_SCOPE int sfifo_init(sfifo_t *f, int size);
83 SFIFO_SCOPE void sfifo_close(sfifo_t *f);
84 SFIFO_SCOPE void sfifo_flush(sfifo_t *f);
85 SFIFO_SCOPE int sfifo_write(sfifo_t *f, const void *buf, int len);
86 SFIFO_SCOPE int sfifo_read(sfifo_t *f, void *buf, int len);
87 #define sfifo_used(x) (((x)->writepos - (x)->readpos) & SFIFO_SIZEMASK(x))
88 #define sfifo_space(x) ((x)->size - 1 - sfifo_used(x))
89 #define sfifo_size(x) ((x)->size - 1)
90
91
92 #ifdef __cplusplus
93 };
94 #endif
95 #endif