2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
9 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23 * The full GNU General Public License is included in this distribution
24 * in the file called LICENSE.GPL.
28 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
29 * All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
35 * * Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * * Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in
39 * the documentation and/or other materials provided with the
42 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 * @brief This file contains the interface to the pool class.
60 * This class allows two different two different priority tasks to
61 * insert and remove items from the free pool. The user of the pool
62 * is expected to evaluate the pool condition empty before a get
63 * operation and pool condition full before a put operation.
66 * - sci_pool_initialize()
76 #include <dev/isci/types.h>
79 * Private operation for the pool
81 #define SCI_POOL_INCREMENT(this_pool, index) \
82 (((index) + 1) == (this_pool).size ? 0 : (index) + 1)
85 * This creates a pool structure of pool_name. The members in the pool are
86 * of type with number of elements equal to size.
88 #define SCI_POOL_CREATE(pool_name, type, pool_size) \
94 type array[(pool_size) + 1]; \
99 * This macro evaluates the pool and returns TRUE if the pool is empty.
100 * If the pool is empty the user should not perform any get operation on
103 #define sci_pool_empty(this_pool) \
104 ((this_pool).get == (this_pool).put)
107 * This macro evaluates the pool and returns TRUE if the pool is full. If
108 * the pool is full the user should not perform any put operation.
110 #define sci_pool_full(this_pool) \
111 (SCI_POOL_INCREMENT(this_pool, (this_pool).put) == (this_pool).get)
114 * This macro returns the size of the pool created. The internal size
115 * of the pool is actually 1 larger then necessary in order to ensure
116 * get and put pointers can be written simultaneously by different
117 * users. As a result, this macro subtracts 1 from the internal size
119 #define sci_pool_size(this_pool) \
120 ((this_pool).size - 1)
123 * This macro indicates the number of elements currently contained in the
126 #define sci_pool_count(this_pool) \
128 sci_pool_empty((this_pool)) \
131 sci_pool_full((this_pool)) \
132 ? sci_pool_size((this_pool)) \
134 (this_pool).get > (this_pool).put \
135 ? ((this_pool).size - (this_pool).get + (this_pool).put) \
136 : ((this_pool).put - (this_pool).get) \
142 * This macro initializes the pool to an empty condition.
144 #define sci_pool_initialize(this_pool) \
146 (this_pool).size = (sizeof((this_pool).array) / sizeof((this_pool).array[0])); \
147 (this_pool).get = 0; \
148 (this_pool).put = 0; \
152 * This macro will get the next free element from the pool.
153 * This should only be called if the pool is not empty.
155 #define sci_pool_get(this_pool, my_value) \
157 (my_value) = (this_pool).array[(this_pool).get]; \
158 (this_pool).get = SCI_POOL_INCREMENT((this_pool), (this_pool).get); \
162 * This macro will put the value into the pool.
163 * This should only be called if the pool is not full.
165 #define sci_pool_put(this_pool, the_value) \
167 (this_pool).array[(this_pool).put] = (the_value); \
168 (this_pool).put = SCI_POOL_INCREMENT((this_pool), (this_pool).put); \
172 * This macro will search the pool and remove any elements in the pool
173 * matching the supplied value.
174 * @note This method can only be utilized on pools
176 #define sci_pool_erase(this_pool, type, the_value) \
180 U32 element_count = sci_pool_count((this_pool)); \
182 for (index = 0; index < element_count; index++) \
184 sci_pool_get((this_pool), tmp_value); \
185 if (tmp_value != (the_value)) \
186 sci_pool_put((this_pool), tmp_value); \
190 #endif // _SCI_POOL_H_