Subversion Repositories HelenOS-historic

Rev

Rev 788 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 788 Rev 830
Line 25... Line 25...
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/*
29
/*
30
 * This implementation of FIFO stores values in a statically
30
 * This implementation of FIFO stores values in an array
31
 * allocated array created on each FIFO's initialization.
-
 
32
 * As such, these FIFOs have upper bound on number of values
31
 * (static or dynamic). As such, these FIFOs have upper bound
33
 * they can store. Push and pop operations are done via accessing
32
 * on number of values they can store. Push and pop operations
34
 * the array through head and tail indices. Because of better
33
 * are done via accessing the array through head and tail indices.
35
 * operation ordering in fifo_pop(), the access policy for these
34
 * Because of better operation ordering in fifo_pop(), the access
36
 * two indices is to 'increment (mod size of FIFO) and use'.
35
 * policy for these two indices is to 'increment (mod size of FIFO)
-
 
36
 * and use'.
37
 */
37
 */
38
 
38
 
39
#ifndef __FIFO_H__
39
#ifndef __FIFO_H__
40
#define __FIFO_H__
40
#define __FIFO_H__
41
 
41
 
42
#include <typedefs.h>
42
#include <typedefs.h>
-
 
43
#include <mm/slab.h>
43
 
44
 
44
/** Create and initialize FIFO.
45
/** Create and initialize static FIFO.
-
 
46
 *
-
 
47
 * FIFO is allocated statically.
-
 
48
 * This macro is suitable for creating smaller FIFOs.
45
 *
49
 *
46
 * @param name Name of FIFO.
50
 * @param name Name of FIFO.
47
 * @param t Type of values stored in FIFO.
51
 * @param t Type of values stored in FIFO.
48
 * @param itms Number of items that can be stored in FIFO.
52
 * @param itms Number of items that can be stored in FIFO.
49
 */
53
 */
50
#define FIFO_INITIALIZE(name, t, itms)          \
54
#define FIFO_INITIALIZE_STATIC(name, t, itms)       \
51
    struct {                    \
55
    struct {                    \
52
        t fifo[(itms)];             \
56
        t fifo[(itms)];             \
53
        count_t items;              \
57
        count_t items;              \
54
        index_t head;               \
58
        index_t head;               \
55
        index_t tail;               \
59
        index_t tail;               \
Line 57... Line 61...
57
        .items = (itms),            \
61
        .items = (itms),            \
58
        .head = 0,              \
62
        .head = 0,              \
59
        .tail = 0               \
63
        .tail = 0               \
60
    }
64
    }
61
 
65
 
-
 
66
/** Create and prepare dynamic FIFO.
-
 
67
 *
-
 
68
 * FIFO is allocated dynamically.
-
 
69
 * This macro is suitable for creating larger FIFOs.
-
 
70
 *
-
 
71
 * @param name Name of FIFO.
-
 
72
 * @param t Type of values stored in FIFO.
-
 
73
 * @param itms Number of items that can be stored in FIFO.
-
 
74
 */
-
 
75
#define FIFO_INITIALIZE_DYNAMIC(name, t, itms)      \
-
 
76
    struct {                    \
-
 
77
        t *fifo;                \
-
 
78
        count_t items;              \
-
 
79
        index_t head;               \
-
 
80
        index_t tail;               \
-
 
81
    } name = {                  \
-
 
82
        .fifo = NULL,               \
-
 
83
        .items = (itms),            \
-
 
84
        .head = 0,              \
-
 
85
        .tail = 0               \
-
 
86
    }
-
 
87
 
62
/** Pop value from head of FIFO.
88
/** Pop value from head of FIFO.
63
 *
89
 *
64
 * @param name FIFO name.
90
 * @param name FIFO name.
65
 *
91
 *
66
 * @return Leading value in FIFO.
92
 * @return Leading value in FIFO.
Line 75... Line 101...
75
 *
101
 *
76
 */
102
 */
77
#define fifo_push(name, value) \
103
#define fifo_push(name, value) \
78
    name.fifo[name.tail = (name.tail + 1) < name.items ? (name.tail + 1) : 0] = (value) 
104
    name.fifo[name.tail = (name.tail + 1) < name.items ? (name.tail + 1) : 0] = (value) 
79
 
105
 
-
 
106
/** Allocate memory for dynamic FIFO.
-
 
107
 *
-
 
108
 * @param name FIFO name.
-
 
109
 */
-
 
110
#define fifo_create(name) \
-
 
111
    name.fifo = malloc(sizeof(*name.fifo) * name.items, 0)
-
 
112
 
80
#endif
113
#endif