~ubuntu-branches/ubuntu/lucid/mpg123/lucid

« back to all changes in this revision

Viewing changes to src/sfifo.h

Tags: upstream-0.60
ImportĀ upstreamĀ versionĀ 0.60

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
/*------------------------------------------------
 
45
        "Private" stuff
 
46
------------------------------------------------*/
 
47
/*
 
48
 * Porting note:
 
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.
 
53
 */
 
54
typedef int sfifo_atomic_t;
 
55
#ifdef __TURBOC__
 
56
#       define  SFIFO_MAX_BUFFER_SIZE   0x7fff
 
57
#else /* Kludge: Assume 32 bit platform */
 
58
#       define  SFIFO_MAX_BUFFER_SIZE   0x7fffffff
 
59
#endif
 
60
 
 
61
typedef struct sfifo_t
 
62
{
 
63
        char *buffer;
 
64
        int size;                       /* Number of bytes */
 
65
        sfifo_atomic_t readpos;         /* Read position */
 
66
        sfifo_atomic_t writepos;        /* Write position */
 
67
} sfifo_t;
 
68
 
 
69
#define SFIFO_SIZEMASK(x)       ((x)->size - 1)
 
70
 
 
71
 
 
72
/*------------------------------------------------
 
73
        API
 
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)
 
83
 
 
84
 
 
85
#ifdef __cplusplus
 
86
};
 
87
#endif
 
88
#endif