Commit | Line | Data |
---|---|---|
6f231dda DW |
1 | /* |
2 | * This file is provided under a dual BSD/GPLv2 license. When using or | |
3 | * redistributing this file, you may do so under either license. | |
4 | * | |
5 | * GPL LICENSE SUMMARY | |
6 | * | |
7 | * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved. | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of version 2 of the GNU General Public License as | |
11 | * published by the Free Software Foundation. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | * The full GNU General Public License is included in this distribution | |
22 | * in the file called LICENSE.GPL. | |
23 | * | |
24 | * BSD LICENSE | |
25 | * | |
26 | * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved. | |
27 | * All rights reserved. | |
28 | * | |
29 | * Redistribution and use in source and binary forms, with or without | |
30 | * modification, are permitted provided that the following conditions | |
31 | * are met: | |
32 | * | |
33 | * * Redistributions of source code must retain the above copyright | |
34 | * notice, this list of conditions and the following disclaimer. | |
35 | * * Redistributions in binary form must reproduce the above copyright | |
36 | * notice, this list of conditions and the following disclaimer in | |
37 | * the documentation and/or other materials provided with the | |
38 | * distribution. | |
39 | * * Neither the name of Intel Corporation nor the names of its | |
40 | * contributors may be used to endorse or promote products derived | |
41 | * from this software without specific prior written permission. | |
42 | * | |
43 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
44 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
45 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
46 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
47 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
48 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
49 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
50 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
51 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
52 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
53 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
54 | */ | |
55 | ||
56 | /** | |
57 | * This file contains the interface to the pool class. This class allows two | |
58 | * different two different priority tasks to insert and remove items from | |
59 | * the free pool. The user of the pool is expected to evaluate the pool | |
60 | * condition empty before a get operation and pool condition full before a | |
61 | * put operation. Methods Provided: - sci_pool_create() - | |
62 | * sci_pool_initialize() - sci_pool_empty() - sci_pool_full() - | |
63 | * sci_pool_get() - sci_pool_put() | |
64 | * | |
65 | * | |
66 | */ | |
67 | ||
68 | #ifndef _SCI_POOL_H_ | |
69 | #define _SCI_POOL_H_ | |
70 | ||
71 | /** | |
72 | * SCI_POOL_INCREMENT() - | |
73 | * | |
74 | * Private operation for the pool | |
75 | */ | |
e2023b87 DJ |
76 | #define SCI_POOL_INCREMENT(pool, index) \ |
77 | (((index) + 1) == (pool).size ? 0 : (index) + 1) | |
6f231dda DW |
78 | |
79 | /** | |
80 | * SCI_POOL_CREATE() - | |
81 | * | |
82 | * This creates a pool structure of pool_name. The members in the pool are of | |
83 | * type with number of elements equal to size. | |
84 | */ | |
85 | #define SCI_POOL_CREATE(pool_name, type, pool_size) \ | |
86 | struct \ | |
87 | { \ | |
88 | u32 size; \ | |
89 | u32 get; \ | |
90 | u32 put; \ | |
91 | type array[(pool_size) + 1]; \ | |
92 | } pool_name | |
93 | ||
94 | ||
95 | /** | |
96 | * sci_pool_empty() - | |
97 | * | |
98 | * This macro evaluates the pool and returns true if the pool is empty. If the | |
99 | * pool is empty the user should not perform any get operation on the pool. | |
100 | */ | |
e2023b87 DJ |
101 | #define sci_pool_empty(pool) \ |
102 | ((pool).get == (pool).put) | |
6f231dda DW |
103 | |
104 | /** | |
105 | * sci_pool_full() - | |
106 | * | |
107 | * This macro evaluates the pool and returns true if the pool is full. If the | |
108 | * pool is full the user should not perform any put operation. | |
109 | */ | |
e2023b87 DJ |
110 | #define sci_pool_full(pool) \ |
111 | (SCI_POOL_INCREMENT(pool, (pool).put) == (pool).get) | |
6f231dda DW |
112 | |
113 | /** | |
114 | * sci_pool_size() - | |
115 | * | |
116 | * This macro returns the size of the pool created. The internal size of the | |
117 | * pool is actually 1 larger then necessary in order to ensure get and put | |
118 | * pointers can be written simultaneously by different users. As a result, | |
119 | * this macro subtracts 1 from the internal size | |
120 | */ | |
e2023b87 DJ |
121 | #define sci_pool_size(pool) \ |
122 | ((pool).size - 1) | |
6f231dda DW |
123 | |
124 | /** | |
125 | * sci_pool_count() - | |
126 | * | |
127 | * This macro indicates the number of elements currently contained in the pool. | |
128 | */ | |
e2023b87 | 129 | #define sci_pool_count(pool) \ |
6f231dda | 130 | (\ |
e2023b87 | 131 | sci_pool_empty((pool)) \ |
6f231dda DW |
132 | ? 0 \ |
133 | : (\ | |
e2023b87 DJ |
134 | sci_pool_full((pool)) \ |
135 | ? sci_pool_size((pool)) \ | |
6f231dda | 136 | : (\ |
e2023b87 DJ |
137 | (pool).get > (pool).put \ |
138 | ? ((pool).size - (pool).get + (pool).put) \ | |
139 | : ((pool).put - (pool).get) \ | |
6f231dda DW |
140 | ) \ |
141 | ) \ | |
142 | ) | |
143 | ||
144 | /** | |
145 | * sci_pool_initialize() - | |
146 | * | |
147 | * This macro initializes the pool to an empty condition. | |
148 | */ | |
e2023b87 | 149 | #define sci_pool_initialize(pool) \ |
6f231dda | 150 | { \ |
e2023b87 DJ |
151 | (pool).size = (sizeof((pool).array) / sizeof((pool).array[0])); \ |
152 | (pool).get = 0; \ | |
153 | (pool).put = 0; \ | |
6f231dda DW |
154 | } |
155 | ||
156 | /** | |
157 | * sci_pool_get() - | |
158 | * | |
159 | * This macro will get the next free element from the pool. This should only be | |
160 | * called if the pool is not empty. | |
161 | */ | |
e2023b87 | 162 | #define sci_pool_get(pool, my_value) \ |
6f231dda | 163 | { \ |
e2023b87 DJ |
164 | (my_value) = (pool).array[(pool).get]; \ |
165 | (pool).get = SCI_POOL_INCREMENT((pool), (pool).get); \ | |
6f231dda DW |
166 | } |
167 | ||
168 | /** | |
169 | * sci_pool_put() - | |
170 | * | |
171 | * This macro will put the value into the pool. This should only be called if | |
172 | * the pool is not full. | |
173 | */ | |
e2023b87 | 174 | #define sci_pool_put(pool, value) \ |
6f231dda | 175 | { \ |
e2023b87 DJ |
176 | (pool).array[(pool).put] = (value); \ |
177 | (pool).put = SCI_POOL_INCREMENT((pool), (pool).put); \ | |
6f231dda DW |
178 | } |
179 | ||
180 | /** | |
181 | * sci_pool_erase() - | |
182 | * | |
183 | * This macro will search the pool and remove any elements in the pool matching | |
184 | * the supplied value. This method can only be utilized on pools | |
185 | */ | |
e2023b87 | 186 | #define sci_pool_erase(pool, type, value) \ |
6f231dda DW |
187 | { \ |
188 | type tmp_value; \ | |
189 | u32 index; \ | |
e2023b87 | 190 | u32 element_count = sci_pool_count((pool)); \ |
6f231dda DW |
191 | \ |
192 | for (index = 0; index < element_count; index++) { \ | |
e2023b87 DJ |
193 | sci_pool_get((pool), tmp_value); \ |
194 | if (tmp_value != (value)) \ | |
195 | sci_pool_put((pool), tmp_value); \ | |
6f231dda DW |
196 | } \ |
197 | } | |
198 | ||
199 | #endif /* _SCI_POOL_H_ */ |