Rev 1702 | Rev 1888 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1702 | Rev 1787 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2006 Jakub Jermar |
2 | * Copyright (C) 2006 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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 | /** @addtogroup genericadt |
29 | /** @addtogroup genericadt |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** @file |
32 | /** @file |
33 | */ |
33 | */ |
34 | 34 | ||
35 | /* |
35 | /* |
36 | * This implementation of FIFO stores values in an array |
36 | * This implementation of FIFO stores values in an array |
37 | * (static or dynamic). As such, these FIFOs have upper bound |
37 | * (static or dynamic). As such, these FIFOs have upper bound |
38 | * on number of values they can store. Push and pop operations |
38 | * on number of values they can store. Push and pop operations |
39 | * are done via accessing the array through head and tail indices. |
39 | * are done via accessing the array through head and tail indices. |
40 | * Because of better operation ordering in fifo_pop(), the access |
40 | * Because of better operation ordering in fifo_pop(), the access |
41 | * policy for these two indices is to 'increment (mod size of FIFO) |
41 | * policy for these two indices is to 'increment (mod size of FIFO) |
42 | * and use'. |
42 | * and use'. |
43 | */ |
43 | */ |
44 | 44 | ||
45 | #ifndef __FIFO_H__ |
45 | #ifndef __FIFO_H__ |
46 | #define __FIFO_H__ |
46 | #define __FIFO_H__ |
47 | 47 | ||
48 | #include <typedefs.h> |
48 | #include <typedefs.h> |
49 | #include <mm/slab.h> |
49 | #include <mm/slab.h> |
50 | 50 | ||
51 | /** Create and initialize static FIFO. |
51 | /** Create and initialize static FIFO. |
52 | * |
52 | * |
53 | * FIFO is allocated statically. |
53 | * FIFO is allocated statically. |
54 | * This macro is suitable for creating smaller FIFOs. |
54 | * This macro is suitable for creating smaller FIFOs. |
55 | * |
55 | * |
56 | * @param name Name of FIFO. |
56 | * @param name Name of FIFO. |
57 | * @param t Type of values stored in FIFO. |
57 | * @param t Type of values stored in FIFO. |
58 | * @param itms Number of items that can be stored in FIFO. |
58 | * @param itms Number of items that can be stored in FIFO. |
59 | */ |
59 | */ |
60 | #define FIFO_INITIALIZE_STATIC(name, t, itms) \ |
60 | #define FIFO_INITIALIZE_STATIC(name, t, itms) \ |
61 | struct { \ |
61 | struct { \ |
62 | t fifo[(itms)]; \ |
62 | t fifo[(itms)]; \ |
63 | count_t items; \ |
63 | count_t items; \ |
64 | index_t head; \ |
64 | index_t head; \ |
65 | index_t tail; \ |
65 | index_t tail; \ |
66 | } name = { \ |
66 | } name = { \ |
67 | .items = (itms), \ |
67 | .items = (itms), \ |
68 | .head = 0, \ |
68 | .head = 0, \ |
69 | .tail = 0 \ |
69 | .tail = 0 \ |
70 | } |
70 | } |
71 | 71 | ||
72 | /** Create and prepare dynamic FIFO. |
72 | /** Create and prepare dynamic FIFO. |
73 | * |
73 | * |
74 | * FIFO is allocated dynamically. |
74 | * FIFO is allocated dynamically. |
75 | * This macro is suitable for creating larger FIFOs. |
75 | * This macro is suitable for creating larger FIFOs. |
76 | * |
76 | * |
77 | * @param name Name of FIFO. |
77 | * @param name Name of FIFO. |
78 | * @param t Type of values stored in FIFO. |
78 | * @param t Type of values stored in FIFO. |
79 | * @param itms Number of items that can be stored in FIFO. |
79 | * @param itms Number of items that can be stored in FIFO. |
80 | */ |
80 | */ |
81 | #define FIFO_INITIALIZE_DYNAMIC(name, t, itms) \ |
81 | #define FIFO_INITIALIZE_DYNAMIC(name, t, itms) \ |
82 | struct { \ |
82 | struct { \ |
83 | t *fifo; \ |
83 | t *fifo; \ |
84 | count_t items; \ |
84 | count_t items; \ |
85 | index_t head; \ |
85 | index_t head; \ |
86 | index_t tail; \ |
86 | index_t tail; \ |
87 | } name = { \ |
87 | } name = { \ |
88 | .fifo = NULL, \ |
88 | .fifo = NULL, \ |
89 | .items = (itms), \ |
89 | .items = (itms), \ |
90 | .head = 0, \ |
90 | .head = 0, \ |
91 | .tail = 0 \ |
91 | .tail = 0 \ |
92 | } |
92 | } |
93 | 93 | ||
94 | /** Pop value from head of FIFO. |
94 | /** Pop value from head of FIFO. |
95 | * |
95 | * |
96 | * @param name FIFO name. |
96 | * @param name FIFO name. |
97 | * |
97 | * |
98 | * @return Leading value in FIFO. |
98 | * @return Leading value in FIFO. |
99 | */ |
99 | */ |
100 | #define fifo_pop(name) \ |
100 | #define fifo_pop(name) \ |
101 | name.fifo[name.head = (name.head + 1) < name.items ? (name.head + 1) : 0] |
101 | name.fifo[name.head = (name.head + 1) < name.items ? (name.head + 1) : 0] |
102 | 102 | ||
103 | /** Push value to tail of FIFO. |
103 | /** Push value to tail of FIFO. |
104 | * |
104 | * |
105 | * @param name FIFO name. |
105 | * @param name FIFO name. |
106 | * @param value Value to be appended to FIFO. |
106 | * @param value Value to be appended to FIFO. |
107 | * |
107 | * |
108 | */ |
108 | */ |
109 | #define fifo_push(name, value) \ |
109 | #define fifo_push(name, value) \ |
110 | name.fifo[name.tail = (name.tail + 1) < name.items ? (name.tail + 1) : 0] = (value) |
110 | name.fifo[name.tail = (name.tail + 1) < name.items ? (name.tail + 1) : 0] = (value) |
111 | 111 | ||
112 | /** Allocate memory for dynamic FIFO. |
112 | /** Allocate memory for dynamic FIFO. |
113 | * |
113 | * |
114 | * @param name FIFO name. |
114 | * @param name FIFO name. |
115 | */ |
115 | */ |
116 | #define fifo_create(name) \ |
116 | #define fifo_create(name) \ |
117 | name.fifo = malloc(sizeof(*name.fifo) * name.items, 0) |
117 | name.fifo = malloc(sizeof(*name.fifo) * name.items, 0) |
118 | 118 | ||
119 | #endif |
119 | #endif |
120 | 120 | ||
121 | /** @} |
121 | /** @} |
122 | */ |
122 | */ |
123 | 123 | ||
124 | 124 |