sci_pool.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * This file contains the interface to the pool class. This class allows two
  57. * different two different priority tasks to insert and remove items from
  58. * the free pool. The user of the pool is expected to evaluate the pool
  59. * condition empty before a get operation and pool condition full before a
  60. * put operation. Methods Provided: - sci_pool_create() -
  61. * sci_pool_initialize() - sci_pool_empty() - sci_pool_full() -
  62. * sci_pool_get() - sci_pool_put()
  63. *
  64. *
  65. */
  66. #ifndef _SCI_POOL_H_
  67. #define _SCI_POOL_H_
  68. /**
  69. * SCI_POOL_INCREMENT() -
  70. *
  71. * Private operation for the pool
  72. */
  73. #define SCI_POOL_INCREMENT(pool, index) \
  74. (((index) + 1) == (pool).size ? 0 : (index) + 1)
  75. /**
  76. * SCI_POOL_CREATE() -
  77. *
  78. * This creates a pool structure of pool_name. The members in the pool are of
  79. * type with number of elements equal to size.
  80. */
  81. #define SCI_POOL_CREATE(pool_name, type, pool_size) \
  82. struct \
  83. { \
  84. u32 size; \
  85. u32 get; \
  86. u32 put; \
  87. type array[(pool_size) + 1]; \
  88. } pool_name
  89. /**
  90. * sci_pool_empty() -
  91. *
  92. * This macro evaluates the pool and returns true if the pool is empty. If the
  93. * pool is empty the user should not perform any get operation on the pool.
  94. */
  95. #define sci_pool_empty(pool) \
  96. ((pool).get == (pool).put)
  97. /**
  98. * sci_pool_full() -
  99. *
  100. * This macro evaluates the pool and returns true if the pool is full. If the
  101. * pool is full the user should not perform any put operation.
  102. */
  103. #define sci_pool_full(pool) \
  104. (SCI_POOL_INCREMENT(pool, (pool).put) == (pool).get)
  105. /**
  106. * sci_pool_size() -
  107. *
  108. * This macro returns the size of the pool created. The internal size of the
  109. * pool is actually 1 larger then necessary in order to ensure get and put
  110. * pointers can be written simultaneously by different users. As a result,
  111. * this macro subtracts 1 from the internal size
  112. */
  113. #define sci_pool_size(pool) \
  114. ((pool).size - 1)
  115. /**
  116. * sci_pool_count() -
  117. *
  118. * This macro indicates the number of elements currently contained in the pool.
  119. */
  120. #define sci_pool_count(pool) \
  121. (\
  122. sci_pool_empty((pool)) \
  123. ? 0 \
  124. : (\
  125. sci_pool_full((pool)) \
  126. ? sci_pool_size((pool)) \
  127. : (\
  128. (pool).get > (pool).put \
  129. ? ((pool).size - (pool).get + (pool).put) \
  130. : ((pool).put - (pool).get) \
  131. ) \
  132. ) \
  133. )
  134. /**
  135. * sci_pool_initialize() -
  136. *
  137. * This macro initializes the pool to an empty condition.
  138. */
  139. #define sci_pool_initialize(pool) \
  140. { \
  141. (pool).size = (sizeof((pool).array) / sizeof((pool).array[0])); \
  142. (pool).get = 0; \
  143. (pool).put = 0; \
  144. }
  145. /**
  146. * sci_pool_get() -
  147. *
  148. * This macro will get the next free element from the pool. This should only be
  149. * called if the pool is not empty.
  150. */
  151. #define sci_pool_get(pool, my_value) \
  152. { \
  153. (my_value) = (pool).array[(pool).get]; \
  154. (pool).get = SCI_POOL_INCREMENT((pool), (pool).get); \
  155. }
  156. /**
  157. * sci_pool_put() -
  158. *
  159. * This macro will put the value into the pool. This should only be called if
  160. * the pool is not full.
  161. */
  162. #define sci_pool_put(pool, value) \
  163. { \
  164. (pool).array[(pool).put] = (value); \
  165. (pool).put = SCI_POOL_INCREMENT((pool), (pool).put); \
  166. }
  167. /**
  168. * sci_pool_erase() -
  169. *
  170. * This macro will search the pool and remove any elements in the pool matching
  171. * the supplied value. This method can only be utilized on pools
  172. */
  173. #define sci_pool_erase(pool, type, value) \
  174. { \
  175. type tmp_value; \
  176. u32 index; \
  177. u32 element_count = sci_pool_count((pool)); \
  178. \
  179. for (index = 0; index < element_count; index++) { \
  180. sci_pool_get((pool), tmp_value); \
  181. if (tmp_value != (value)) \
  182. sci_pool_put((pool), tmp_value); \
  183. } \
  184. }
  185. #endif /* _SCI_POOL_H_ */