Rev 4509 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4509 | Rev 4581 | ||
---|---|---|---|
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 libc |
29 | /** @addtogroup libc |
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 LIBC_FIFO_H_ |
45 | #ifndef LIBC_FIFO_H_ |
46 | #define LIBC_FIFO_H_ |
46 | #define LIBC_FIFO_H_ |
47 | 47 | ||
48 | #include <malloc.h> |
48 | #include <malloc.h> |
49 | 49 | ||
50 | typedef unsigned long fifo_count_t; |
50 | typedef unsigned long fifo_count_t; |
51 | typedef unsigned long fifo_index_t; |
51 | typedef unsigned long fifo_index_t; |
52 | 52 | ||
53 | #define FIFO_CREATE_STATIC(name, t, itms) \ |
53 | #define FIFO_CREATE_STATIC(name, t, itms) \ |
54 | struct { \ |
54 | struct { \ |
55 | t fifo[(itms)]; \ |
55 | t fifo[(itms)]; \ |
56 | fifo_count_t items; \ |
56 | fifo_count_t items; \ |
57 | fifo_index_t head; \ |
57 | fifo_index_t head; \ |
58 | fifo_index_t tail; \ |
58 | fifo_index_t tail; \ |
59 | } name |
59 | } name |
60 | 60 | ||
61 | /** Create and initialize static FIFO. |
61 | /** Create and initialize static FIFO. |
62 | * |
62 | * |
63 | * FIFO is allocated statically. |
63 | * FIFO is allocated statically. |
64 | * This macro is suitable for creating smaller FIFOs. |
64 | * This macro is suitable for creating smaller FIFOs. |
65 | * |
65 | * |
66 | * @param name Name of FIFO. |
66 | * @param name Name of FIFO. |
67 | * @param t Type of values stored in FIFO. |
67 | * @param t Type of values stored in FIFO. |
68 | * @param itms Number of items that can be stored in FIFO. |
68 | * @param itms Number of items that can be stored in FIFO. |
69 | */ |
69 | */ |
70 | #define FIFO_INITIALIZE_STATIC(name, t, itms) \ |
70 | #define FIFO_INITIALIZE_STATIC(name, t, itms) \ |
71 | FIFO_CREATE_STATIC(name, t, itms) = { \ |
71 | FIFO_CREATE_STATIC(name, t, itms) = { \ |
72 | .items = (itms), \ |
72 | .items = (itms), \ |
73 | .head = 0, \ |
73 | .head = 0, \ |
74 | .tail = 0 \ |
74 | .tail = 0 \ |
75 | } |
75 | } |
76 | 76 | ||
77 | /** Create and prepare dynamic FIFO. |
77 | /** Create and prepare dynamic FIFO. |
78 | * |
78 | * |
79 | * FIFO is allocated dynamically. |
79 | * FIFO is allocated dynamically. |
80 | * This macro is suitable for creating larger FIFOs. |
80 | * This macro is suitable for creating larger FIFOs. |
81 | * |
81 | * |
82 | * @param name Name of FIFO. |
82 | * @param name Name of FIFO. |
83 | * @param t Type of values stored in FIFO. |
83 | * @param t Type of values stored in FIFO. |
84 | * @param itms Number of items that can be stored in FIFO. |
84 | * @param itms Number of items that can be stored in FIFO. |
85 | */ |
85 | */ |
86 | #define FIFO_INITIALIZE_DYNAMIC(name, t, itms) \ |
86 | #define FIFO_INITIALIZE_DYNAMIC(name, t, itms) \ |
87 | struct { \ |
87 | struct { \ |
88 | t *fifo; \ |
88 | t *fifo; \ |
89 | fifo_count_t items; \ |
89 | fifo_count_t items; \ |
90 | fifo_index_t head; \ |
90 | fifo_index_t head; \ |
91 | fifo_index_t tail; \ |
91 | fifo_index_t tail; \ |
92 | } name = { \ |
92 | } name = { \ |
93 | .fifo = NULL, \ |
93 | .fifo = NULL, \ |
94 | .items = (itms), \ |
94 | .items = (itms), \ |
95 | .head = 0, \ |
95 | .head = 0, \ |
96 | .tail = 0 \ |
96 | .tail = 0 \ |
97 | } |
97 | } |
98 | 98 | ||
99 | /** Pop value from head of FIFO. |
99 | /** Pop value from head of FIFO. |
100 | * |
100 | * |
101 | * @param name FIFO name. |
101 | * @param name FIFO name. |
102 | * |
102 | * |
103 | * @return Leading value in FIFO. |
103 | * @return Leading value in FIFO. |
104 | */ |
104 | */ |
105 | #define fifo_pop(name) \ |
105 | #define fifo_pop(name) \ |
106 | name.fifo[name.head = (name.head + 1) < name.items ? (name.head + 1) : 0] |
106 | name.fifo[name.head = (name.head + 1) < name.items ? (name.head + 1) : 0] |
107 | 107 | ||
108 | /** Push value to tail of FIFO. |
108 | /** Push value to tail of FIFO. |
109 | * |
109 | * |
110 | * @param name FIFO name. |
110 | * @param name FIFO name. |
111 | * @param value Value to be appended to FIFO. |
111 | * @param value Value to be appended to FIFO. |
112 | * |
112 | * |
113 | */ |
113 | */ |
114 | #define fifo_push(name, value) \ |
114 | #define fifo_push(name, value) \ |
115 | name.fifo[name.tail = (name.tail + 1) < name.items ? (name.tail + 1) : 0] = (value) |
115 | name.fifo[name.tail = (name.tail + 1) < name.items ? (name.tail + 1) : 0] = (value) |
116 | 116 | ||
117 | /** Allocate memory for dynamic FIFO. |
117 | /** Allocate memory for dynamic FIFO. |
118 | * |
118 | * |
119 | * @param name FIFO name. |
119 | * @param name FIFO name. |
120 | */ |
120 | */ |
121 | #define fifo_create(name) \ |
121 | #define fifo_create(name) \ |
122 | name.fifo = malloc(sizeof(*name.fifo) * name.items) |
122 | name.fifo = malloc(sizeof(*name.fifo) * name.items) |
123 | 123 | ||
124 | #endif |
124 | #endif |
125 | 125 | ||
126 | /** @} |
126 | /** @} |
127 | */ |
127 | */ |
128 | 128 |