Rev 1963 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1963 | Rev 1968 | ||
---|---|---|---|
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 | /* |
29 | /* |
30 | * This implementation of FIFO stores values in an array |
30 | * This implementation of FIFO stores values in an array |
31 | * (static or dynamic). As such, these FIFOs have upper bound |
31 | * (static or dynamic). As such, these FIFOs have upper bound |
32 | * on number of values they can store. Push and pop operations |
32 | * on number of values they can store. Push and pop operations |
33 | * are done via accessing the array through head and tail indices. |
33 | * are done via accessing the array through head and tail indices. |
34 | * Because of better operation ordering in fifo_pop(), the access |
34 | * Because of better operation ordering in fifo_pop(), the access |
35 | * policy for these two indices is to 'increment (mod size of FIFO) |
35 | * policy for these two indices is to 'increment (mod size of FIFO) |
36 | * and use'. |
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 | #include <mm/slab.h> |
44 | 44 | ||
45 | /** Create and initialize static FIFO. |
45 | /** Create and initialize static FIFO. |
46 | * |
46 | * |
47 | * FIFO is allocated statically. |
47 | * FIFO is allocated statically. |
48 | * This macro is suitable for creating smaller FIFOs. |
48 | * This macro is suitable for creating smaller FIFOs. |
49 | * |
49 | * |
50 | * @param name Name of FIFO. |
50 | * @param name Name of FIFO. |
51 | * @param t Type of values stored in FIFO. |
51 | * @param t Type of values stored in FIFO. |
52 | * @param itms Number of items that can be stored in FIFO. |
52 | * @param itms Number of items that can be stored in FIFO. |
53 | */ |
53 | */ |
54 | #define FIFO_INITIALIZE_STATIC(name, t, itms) \ |
54 | #define FIFO_INITIALIZE_STATIC(name, t, itms) \ |
55 | struct { \ |
55 | struct { \ |
56 | t fifo[(itms)]; \ |
56 | t fifo[(itms)]; \ |
57 | count_t items; \ |
57 | count_t items; \ |
58 | index_t head; \ |
58 | index_t head; \ |
59 | index_t tail; \ |
59 | index_t tail; \ |
60 | } name = { \ |
60 | } name = { \ |
61 | .items = (itms), \ |
61 | .items = (itms), \ |
62 | .head = 0, \ |
62 | .head = 0, \ |
63 | .tail = 0 \ |
63 | .tail = 0 \ |
64 | } |
64 | } |
65 | 65 | ||
66 | /** Create and prepare dynamic FIFO. |
66 | /** Create and prepare dynamic FIFO. |
67 | * |
67 | * |
68 | * FIFO is allocated dynamically. |
68 | * FIFO is allocated dynamically. |
69 | * This macro is suitable for creating larger FIFOs. |
69 | * This macro is suitable for creating larger FIFOs. |
70 | * |
70 | * |
71 | * @param name Name of FIFO. |
71 | * @param name Name of FIFO. |
72 | * @param t Type of values stored in FIFO. |
72 | * @param t Type of values stored in FIFO. |
73 | * @param itms Number of items that can be stored in FIFO. |
73 | * @param itms Number of items that can be stored in FIFO. |
74 | */ |
74 | */ |
75 | #define FIFO_INITIALIZE_DYNAMIC(name, t, itms) \ |
75 | #define FIFO_INITIALIZE_DYNAMIC(name, t, itms) \ |
76 | struct { \ |
76 | struct { \ |
77 | t *fifo; \ |
77 | t *fifo; \ |
78 | count_t items; \ |
78 | count_t items; \ |
79 | index_t head; \ |
79 | index_t head; \ |
80 | index_t tail; \ |
80 | index_t tail; \ |
81 | } name = { \ |
81 | } name = { \ |
82 | .fifo = NULL, \ |
82 | .fifo = NULL, \ |
83 | .items = (itms), \ |
83 | .items = (itms), \ |
84 | .head = 0, \ |
84 | .head = 0, \ |
85 | .tail = 0 \ |
85 | .tail = 0 \ |
86 | } |
86 | } |
87 | 87 | ||
88 | /** Pop value from head of FIFO. |
88 | /** Pop value from head of FIFO. |
89 | * |
89 | * |
90 | * @param name FIFO name. |
90 | * @param name FIFO name. |
91 | * |
91 | * |
92 | * @return Leading value in FIFO. |
92 | * @return Leading value in FIFO. |
93 | */ |
93 | */ |
94 | #define fifo_pop(name) \ |
94 | #define fifo_pop(name) \ |
95 | name.fifo[name.head = (name.head + 1) < name.items ? (name.head + 1) : 0] |
95 | name.fifo[name.head = (name.head + 1) < name.items ? (name.head + 1) : 0] |
96 | 96 | ||
97 | /** Push value to tail of FIFO. |
97 | /** Push value to tail of FIFO. |
98 | * |
98 | * |
99 | * @param name FIFO name. |
99 | * @param name FIFO name. |
100 | * @param value Value to be appended to FIFO. |
100 | * @param value Value to be appended to FIFO. |
101 | * |
101 | * |
102 | */ |
102 | */ |
103 | #define fifo_push(name, value) \ |
103 | #define fifo_push(name, value) \ |
104 | 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) |
105 | 105 | ||
106 | /** Allocate memory for dynamic FIFO. |
106 | /** Allocate memory for dynamic FIFO. |
107 | * |
107 | * |
108 | * @param name FIFO name. |
108 | * @param name FIFO name. |
109 | */ |
109 | */ |
110 | #define fifo_create(name) \ |
110 | #define fifo_create(name) \ |
111 | name.fifo = malloc(sizeof(*name.fifo) * name.items, 0) |
111 | name.fifo = malloc(sizeof(*name.fifo) * name.items, 0) |
112 | 112 | ||
113 | #endif |
113 | #endif |
114 | 114 |