xref: /freebsd/sys/dev/isci/scil/sci_pool.h (revision a0409676120c1e558d0ade943019934e0f15118d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
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.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
10  *
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.
14  *
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.
19  *
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.
25  *
26  * BSD LICENSE
27  *
28  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  *
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
40  *     distribution.
41  *
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.
53  *
54  * $FreeBSD$
55  */
56 /**
57  * @file
58  *
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.
64  *        Methods Provided:
65  *        - sci_pool_create()
66  *        - sci_pool_initialize()
67  *        - sci_pool_empty()
68  *        - sci_pool_full()
69  *        - sci_pool_get()
70  *        - sci_pool_put()
71  */
72 
73 #ifndef _SCI_POOL_H_
74 #define _SCI_POOL_H_
75 
76 #include <dev/isci/types.h>
77 
78 /**
79  * Private operation for the pool
80  */
81 #define SCI_POOL_INCREMENT(this_pool, index) \
82   (((index) + 1) == (this_pool).size ? 0 : (index) + 1)
83 
84 /**
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.
87  */
88 #define SCI_POOL_CREATE(pool_name, type, pool_size) \
89 struct \
90 { \
91    U32 size; \
92    U32 get; \
93    U32 put; \
94    type array[(pool_size) + 1]; \
95 } pool_name
96 
97 
98 /**
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
101  * the pool.
102  */
103 #define sci_pool_empty(this_pool) \
104    ((this_pool).get == (this_pool).put)
105 
106 /**
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.
109  */
110 #define sci_pool_full(this_pool) \
111    (SCI_POOL_INCREMENT(this_pool, (this_pool).put) == (this_pool).get)
112 
113 /**
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
118  */
119 #define sci_pool_size(this_pool) \
120    ((this_pool).size - 1)
121 
122 /**
123  * This macro indicates the number of elements currently contained in the
124  * pool.
125  */
126 #define sci_pool_count(this_pool) \
127    ( \
128       sci_pool_empty((this_pool)) \
129       ? 0 \
130       : ( \
131            sci_pool_full((this_pool)) \
132            ? sci_pool_size((this_pool)) \
133            : ( \
134                 (this_pool).get > (this_pool).put \
135                 ? ((this_pool).size - (this_pool).get + (this_pool).put) \
136                 : ((this_pool).put - (this_pool).get) \
137              ) \
138         ) \
139    )
140 
141 /**
142  * This macro initializes the pool to an empty condition.
143  */
144 #define sci_pool_initialize(this_pool) \
145 { \
146    (this_pool).size = (sizeof((this_pool).array) / sizeof((this_pool).array[0])); \
147    (this_pool).get = 0; \
148    (this_pool).put = 0; \
149 }
150 
151 /**
152  * This macro will get the next free element from the pool.
153  * This should only be called if the pool is not empty.
154  */
155 #define sci_pool_get(this_pool, my_value) \
156 { \
157    (my_value) = (this_pool).array[(this_pool).get]; \
158    (this_pool).get = SCI_POOL_INCREMENT((this_pool), (this_pool).get); \
159 }
160 
161 /**
162  * This macro will put the value into the pool.
163  * This should only be called if the pool is not full.
164  */
165 #define sci_pool_put(this_pool, the_value) \
166 { \
167    (this_pool).array[(this_pool).put] = (the_value); \
168    (this_pool).put = SCI_POOL_INCREMENT((this_pool), (this_pool).put); \
169 }
170 
171 /**
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
175  */
176 #define sci_pool_erase(this_pool, type, the_value) \
177 { \
178    type tmp_value; \
179    U32 index; \
180    U32 element_count = sci_pool_count((this_pool)); \
181  \
182    for (index = 0; index < element_count; index++) \
183    { \
184       sci_pool_get((this_pool), tmp_value); \
185       if (tmp_value != (the_value)) \
186          sci_pool_put((this_pool), tmp_value); \
187    } \
188 }
189 
190 #endif // _SCI_POOL_H_
191