1 | /*
|
---|
2 | * Copyright (C) 2006 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
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.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /*
|
---|
30 | * This implementation of FIFO stores values in a statically
|
---|
31 | * allocated array created on each FIFO's initialization.
|
---|
32 | * As such, these FIFOs have upper bound on number of values
|
---|
33 | * they can store. Push and pop operations are done via accessing
|
---|
34 | * the array through head and tail indices. Because of better
|
---|
35 | * operation ordering in fifo_pop(), the access policy for these
|
---|
36 | * two indices is to 'increment (mod size of FIFO) and use'.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #ifndef __FIFO_H__
|
---|
40 | #define __FIFO_H__
|
---|
41 |
|
---|
42 | #include <typedefs.h>
|
---|
43 |
|
---|
44 | /** Create and initialize FIFO.
|
---|
45 | *
|
---|
46 | * @param name Name of FIFO.
|
---|
47 | * @param t Type of values stored in FIFO.
|
---|
48 | * @param itms Number of items that can be stored in FIFO.
|
---|
49 | */
|
---|
50 | #define FIFO_INITIALIZE(name, t, itms) \
|
---|
51 | struct { \
|
---|
52 | t fifo[(itms)]; \
|
---|
53 | count_t items; \
|
---|
54 | index_t head; \
|
---|
55 | index_t tail; \
|
---|
56 | } name = { \
|
---|
57 | .items = (itms), \
|
---|
58 | .head = 0, \
|
---|
59 | .tail = 0 \
|
---|
60 | }
|
---|
61 |
|
---|
62 | /** Pop value from head of FIFO.
|
---|
63 | *
|
---|
64 | * @param name FIFO name.
|
---|
65 | *
|
---|
66 | * @return Leading value in FIFO.
|
---|
67 | */
|
---|
68 | #define fifo_pop(name) \
|
---|
69 | name.fifo[name.head = (name.head + 1) < name.items ? (name.head + 1) : 0]
|
---|
70 |
|
---|
71 | /** Push value to tail of FIFO.
|
---|
72 | *
|
---|
73 | * @param name FIFO name.
|
---|
74 | * @param value Value to be appended to FIFO.
|
---|
75 | *
|
---|
76 | */
|
---|
77 | #define fifo_push(name, value) \
|
---|
78 | name.fifo[name.tail = (name.tail + 1) < name.items ? (name.tail + 1) : 0] = (value)
|
---|
79 |
|
---|
80 | #endif
|
---|