1230557Sjimharris/*-
2230557Sjimharris * This file is provided under a dual BSD/GPLv2 license.  When using or
3230557Sjimharris * redistributing this file, you may do so under either license.
4230557Sjimharris *
5230557Sjimharris * GPL LICENSE SUMMARY
6230557Sjimharris *
7230557Sjimharris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8230557Sjimharris *
9230557Sjimharris * This program is free software; you can redistribute it and/or modify
10230557Sjimharris * it under the terms of version 2 of the GNU General Public License as
11230557Sjimharris * published by the Free Software Foundation.
12230557Sjimharris *
13230557Sjimharris * This program is distributed in the hope that it will be useful, but
14230557Sjimharris * WITHOUT ANY WARRANTY; without even the implied warranty of
15230557Sjimharris * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16230557Sjimharris * General Public License for more details.
17230557Sjimharris *
18230557Sjimharris * You should have received a copy of the GNU General Public License
19230557Sjimharris * along with this program; if not, write to the Free Software
20230557Sjimharris * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21230557Sjimharris * The full GNU General Public License is included in this distribution
22230557Sjimharris * in the file called LICENSE.GPL.
23230557Sjimharris *
24230557Sjimharris * BSD LICENSE
25230557Sjimharris *
26230557Sjimharris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27230557Sjimharris * All rights reserved.
28230557Sjimharris *
29230557Sjimharris * Redistribution and use in source and binary forms, with or without
30230557Sjimharris * modification, are permitted provided that the following conditions
31230557Sjimharris * are met:
32230557Sjimharris *
33230557Sjimharris *   * Redistributions of source code must retain the above copyright
34230557Sjimharris *     notice, this list of conditions and the following disclaimer.
35230557Sjimharris *   * Redistributions in binary form must reproduce the above copyright
36230557Sjimharris *     notice, this list of conditions and the following disclaimer in
37230557Sjimharris *     the documentation and/or other materials provided with the
38230557Sjimharris *     distribution.
39230557Sjimharris *
40230557Sjimharris * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41230557Sjimharris * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42230557Sjimharris * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43230557Sjimharris * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44230557Sjimharris * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45230557Sjimharris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46230557Sjimharris * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47230557Sjimharris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48230557Sjimharris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49230557Sjimharris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50230557Sjimharris * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51230557Sjimharris *
52230557Sjimharris * $FreeBSD$
53230557Sjimharris */
54230557Sjimharris/**
55230557Sjimharris * @file
56230557Sjimharris *
57230557Sjimharris * @brief This file contains the interface to the pool class.
58230557Sjimharris *        This class allows two different two different priority tasks to
59230557Sjimharris *        insert and remove items from the free pool. The user of the pool
60230557Sjimharris *        is expected to evaluate the pool condition empty before a get
61230557Sjimharris *        operation and pool condition full before a put operation.
62230557Sjimharris *        Methods Provided:
63230557Sjimharris *        - sci_pool_create()
64230557Sjimharris *        - sci_pool_initialize()
65230557Sjimharris *        - sci_pool_empty()
66230557Sjimharris *        - sci_pool_full()
67230557Sjimharris *        - sci_pool_get()
68230557Sjimharris *        - sci_pool_put()
69230557Sjimharris */
70230557Sjimharris
71230557Sjimharris#ifndef _SCI_POOL_H_
72230557Sjimharris#define _SCI_POOL_H_
73230557Sjimharris
74230557Sjimharris#include <dev/isci/types.h>
75230557Sjimharris
76230557Sjimharris/**
77230557Sjimharris * Private operation for the pool
78230557Sjimharris */
79230557Sjimharris#define SCI_POOL_INCREMENT(this_pool, index) \
80230557Sjimharris  (((index) + 1) == (this_pool).size ? 0 : (index) + 1)
81230557Sjimharris
82230557Sjimharris/**
83230557Sjimharris * This creates a pool structure of pool_name. The members in the pool are
84230557Sjimharris * of type with number of elements equal to size.
85230557Sjimharris */
86230557Sjimharris#define SCI_POOL_CREATE(pool_name, type, pool_size) \
87230557Sjimharrisstruct \
88230557Sjimharris{ \
89230557Sjimharris   U32 size; \
90230557Sjimharris   U32 get; \
91230557Sjimharris   U32 put; \
92230557Sjimharris   type array[(pool_size) + 1]; \
93230557Sjimharris} pool_name
94230557Sjimharris
95230557Sjimharris
96230557Sjimharris/**
97230557Sjimharris * This macro evaluates the pool and returns TRUE if the pool is empty.
98230557Sjimharris * If the pool is empty the user should not perform any get operation on
99230557Sjimharris * the pool.
100230557Sjimharris */
101230557Sjimharris#define sci_pool_empty(this_pool) \
102230557Sjimharris   ((this_pool).get == (this_pool).put)
103230557Sjimharris
104230557Sjimharris/**
105230557Sjimharris * This macro evaluates the pool and returns TRUE if the pool is full.  If
106230557Sjimharris * the pool is full the user should not perform any put operation.
107230557Sjimharris */
108230557Sjimharris#define sci_pool_full(this_pool) \
109230557Sjimharris   (SCI_POOL_INCREMENT(this_pool, (this_pool).put) == (this_pool).get)
110230557Sjimharris
111230557Sjimharris/**
112230557Sjimharris * This macro returns the size of the pool created.  The internal size
113230557Sjimharris * of the pool is actually 1 larger then necessary in order to ensure
114230557Sjimharris * get and put pointers can be written simultaneously by different
115230557Sjimharris * users.  As a result, this macro subtracts 1 from the internal size
116230557Sjimharris */
117230557Sjimharris#define sci_pool_size(this_pool) \
118230557Sjimharris   ((this_pool).size - 1)
119230557Sjimharris
120230557Sjimharris/**
121230557Sjimharris * This macro indicates the number of elements currently contained in the
122230557Sjimharris * pool.
123230557Sjimharris */
124230557Sjimharris#define sci_pool_count(this_pool) \
125230557Sjimharris   ( \
126230557Sjimharris      sci_pool_empty((this_pool)) \
127230557Sjimharris      ? 0 \
128230557Sjimharris      : ( \
129230557Sjimharris           sci_pool_full((this_pool)) \
130230557Sjimharris           ? sci_pool_size((this_pool)) \
131230557Sjimharris           : ( \
132230557Sjimharris                (this_pool).get > (this_pool).put \
133230557Sjimharris                ? ((this_pool).size - (this_pool).get + (this_pool).put) \
134230557Sjimharris                : ((this_pool).put - (this_pool).get) \
135230557Sjimharris             ) \
136230557Sjimharris        ) \
137230557Sjimharris   )
138230557Sjimharris
139230557Sjimharris/**
140230557Sjimharris * This macro initializes the pool to an empty condition.
141230557Sjimharris */
142230557Sjimharris#define sci_pool_initialize(this_pool) \
143230557Sjimharris{ \
144230557Sjimharris   (this_pool).size = (sizeof((this_pool).array) / sizeof((this_pool).array[0])); \
145230557Sjimharris   (this_pool).get = 0; \
146230557Sjimharris   (this_pool).put = 0; \
147230557Sjimharris}
148230557Sjimharris
149230557Sjimharris/**
150230557Sjimharris * This macro will get the next free element from the pool.
151230557Sjimharris * This should only be called if the pool is not empty.
152230557Sjimharris */
153230557Sjimharris#define sci_pool_get(this_pool, my_value) \
154230557Sjimharris{ \
155230557Sjimharris   (my_value) = (this_pool).array[(this_pool).get]; \
156230557Sjimharris   (this_pool).get = SCI_POOL_INCREMENT((this_pool), (this_pool).get); \
157230557Sjimharris}
158230557Sjimharris
159230557Sjimharris/**
160230557Sjimharris * This macro will put the value into the pool.
161230557Sjimharris * This should only be called if the pool is not full.
162230557Sjimharris */
163230557Sjimharris#define sci_pool_put(this_pool, the_value) \
164230557Sjimharris{ \
165230557Sjimharris   (this_pool).array[(this_pool).put] = (the_value); \
166230557Sjimharris   (this_pool).put = SCI_POOL_INCREMENT((this_pool), (this_pool).put); \
167230557Sjimharris}
168230557Sjimharris
169230557Sjimharris/**
170230557Sjimharris * This macro will search the pool and remove any elements in the pool
171230557Sjimharris * matching the supplied value.
172230557Sjimharris * @note This method can only be utilized on pools
173230557Sjimharris */
174230557Sjimharris#define sci_pool_erase(this_pool, type, the_value) \
175230557Sjimharris{ \
176230557Sjimharris   type tmp_value; \
177230557Sjimharris   U32 index; \
178230557Sjimharris   U32 element_count = sci_pool_count((this_pool)); \
179230557Sjimharris \
180230557Sjimharris   for (index = 0; index < element_count; index++) \
181230557Sjimharris   { \
182230557Sjimharris      sci_pool_get((this_pool), tmp_value); \
183230557Sjimharris      if (tmp_value != (the_value)) \
184230557Sjimharris         sci_pool_put((this_pool), tmp_value); \
185230557Sjimharris   } \
186230557Sjimharris}
187230557Sjimharris
188230557Sjimharris#endif // _SCI_POOL_H_
189