Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | Copyright (c) 2001, 2002, 2003, 2004 Eliot Dresselhaus |
| 17 | |
| 18 | Permission is hereby granted, free of charge, to any person obtaining |
| 19 | a copy of this software and associated documentation files (the |
| 20 | "Software"), to deal in the Software without restriction, including |
| 21 | without limitation the rights to use, copy, modify, merge, publish, |
| 22 | distribute, sublicense, and/or sell copies of the Software, and to |
| 23 | permit persons to whom the Software is furnished to do so, subject to |
| 24 | the following conditions: |
| 25 | |
| 26 | The above copyright notice and this permission notice shall be |
| 27 | included in all copies or substantial portions of the Software. |
| 28 | |
| 29 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 30 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 32 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 33 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 34 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 35 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 | */ |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 37 | /** @file |
| 38 | * @brief Fixed length block allocator. |
| 39 | Pools are built from clib vectors and bitmaps. Use pools when |
| 40 | repeatedly allocating and freeing fixed-size data. Pools are |
| 41 | fast, and avoid memory fragmentation. |
| 42 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | |
| 44 | #ifndef included_pool_h |
| 45 | #define included_pool_h |
| 46 | |
| 47 | #include <vppinfra/bitmap.h> |
| 48 | #include <vppinfra/error.h> |
| 49 | #include <vppinfra/mheap.h> |
| 50 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 52 | typedef struct |
| 53 | { |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 54 | /** Bitmap of indices of free objects. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 55 | uword *free_bitmap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 57 | /** Vector of free indices. One element for each set bit in bitmap. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 58 | u32 *free_indices; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | } pool_header_t; |
| 60 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 61 | /** Align pool header so that pointers are naturally aligned. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | #define pool_aligned_header_bytes \ |
| 63 | vec_aligned_header_bytes (sizeof (pool_header_t), sizeof (void *)) |
| 64 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 65 | /** Get pool header from user pool pointer */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 66 | always_inline pool_header_t * |
| 67 | pool_header (void *v) |
| 68 | { |
| 69 | return vec_aligned_header (v, sizeof (pool_header_t), sizeof (void *)); |
| 70 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 72 | /** Validate a pool */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 73 | always_inline void |
| 74 | pool_validate (void *v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 76 | pool_header_t *p = pool_header (v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | uword i, n_free_bitmap; |
| 78 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 79 | if (!v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | return; |
| 81 | |
| 82 | n_free_bitmap = clib_bitmap_count_set_bits (p->free_bitmap); |
| 83 | ASSERT (n_free_bitmap == vec_len (p->free_indices)); |
| 84 | for (i = 0; i < vec_len (p->free_indices); i++) |
| 85 | ASSERT (clib_bitmap_get (p->free_bitmap, p->free_indices[i]) == 1); |
| 86 | } |
| 87 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 88 | always_inline void |
| 89 | pool_header_validate_index (void *v, uword index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 91 | pool_header_t *p = pool_header (v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | |
| 93 | if (v) |
| 94 | vec_validate (p->free_bitmap, index / BITS (uword)); |
| 95 | } |
| 96 | |
| 97 | #define pool_validate_index(v,i) \ |
| 98 | do { \ |
| 99 | uword __pool_validate_index = (i); \ |
| 100 | vec_validate_ha ((v), __pool_validate_index, \ |
| 101 | pool_aligned_header_bytes, /* align */ 0); \ |
| 102 | pool_header_validate_index ((v), __pool_validate_index); \ |
| 103 | } while (0) |
| 104 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 105 | /** Number of active elements in a pool. |
| 106 | * @return Number of active elements in a pool |
| 107 | */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 108 | always_inline uword |
| 109 | pool_elts (void *v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | { |
| 111 | uword ret = vec_len (v); |
| 112 | if (v) |
| 113 | ret -= vec_len (pool_header (v)->free_indices); |
| 114 | return ret; |
| 115 | } |
| 116 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 117 | /** Number of elements in pool vector. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 119 | @note You probably want to call pool_elts() instead. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | */ |
| 121 | #define pool_len(p) vec_len(p) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 123 | /** Number of elements in pool vector (usable as an lvalue) |
| 124 | |
| 125 | @note You probably don't want to use this macro. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | */ |
| 127 | #define _pool_len(p) _vec_len(p) |
| 128 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 129 | /** Memory usage of pool header. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | always_inline uword |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 131 | pool_header_bytes (void *v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 133 | pool_header_t *p = pool_header (v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 135 | if (!v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | return 0; |
| 137 | |
| 138 | return vec_bytes (p->free_bitmap) + vec_bytes (p->free_indices); |
| 139 | } |
| 140 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 141 | /** Memory usage of pool. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | #define pool_bytes(P) (vec_bytes (P) + pool_header_bytes (P)) |
| 143 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 144 | /** Local variable naming macro. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 145 | #define _pool_var(v) _pool_##v |
| 146 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 147 | /** Queries whether pool has at least N_FREE free elements. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | always_inline uword |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 149 | pool_free_elts (void *v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 151 | pool_header_t *p = pool_header (v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 152 | uword n_free = 0; |
| 153 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 154 | if (v) |
| 155 | { |
| 156 | n_free += vec_len (p->free_indices); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 158 | /* Space left at end of vector? */ |
| 159 | n_free += vec_capacity (v, sizeof (p[0])) - vec_len (v); |
| 160 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | |
| 162 | return n_free; |
| 163 | } |
| 164 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 165 | /** Allocate an object E from a pool P (general version). |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 167 | First search free list. If nothing is free extend vector of objects. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | */ |
| 169 | #define pool_get_aligned(P,E,A) \ |
| 170 | do { \ |
| 171 | pool_header_t * _pool_var (p) = pool_header (P); \ |
| 172 | uword _pool_var (l); \ |
| 173 | \ |
| 174 | _pool_var (l) = 0; \ |
| 175 | if (P) \ |
| 176 | _pool_var (l) = vec_len (_pool_var (p)->free_indices); \ |
| 177 | \ |
| 178 | if (_pool_var (l) > 0) \ |
| 179 | { \ |
| 180 | /* Return free element from free list. */ \ |
| 181 | uword _pool_var (i) = _pool_var (p)->free_indices[_pool_var (l) - 1]; \ |
| 182 | (E) = (P) + _pool_var (i); \ |
| 183 | _pool_var (p)->free_bitmap = \ |
| 184 | clib_bitmap_andnoti (_pool_var (p)->free_bitmap, _pool_var (i)); \ |
| 185 | _vec_len (_pool_var (p)->free_indices) = _pool_var (l) - 1; \ |
| 186 | } \ |
| 187 | else \ |
| 188 | { \ |
| 189 | /* Nothing on free list, make a new element and return it. */ \ |
| 190 | P = _vec_resize (P, \ |
| 191 | /* length_increment */ 1, \ |
| 192 | /* new size */ (vec_len (P) + 1) * sizeof (P[0]), \ |
| 193 | pool_aligned_header_bytes, \ |
| 194 | /* align */ (A)); \ |
| 195 | E = vec_end (P) - 1; \ |
| 196 | } \ |
| 197 | } while (0) |
| 198 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 199 | /** Allocate an object E from a pool P (unspecified alignment). */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | #define pool_get(P,E) pool_get_aligned(P,E,0) |
| 201 | |
Dave Barach | 614ac5d | 2017-02-06 09:28:03 -0500 | [diff] [blame] | 202 | /** See if pool_get will expand the pool or not */ |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame^] | 203 | #define pool_get_aligned_will_expand(P,YESNO,A) \ |
Dave Barach | 614ac5d | 2017-02-06 09:28:03 -0500 | [diff] [blame] | 204 | do { \ |
| 205 | pool_header_t * _pool_var (p) = pool_header (P); \ |
| 206 | uword _pool_var (l); \ |
| 207 | \ |
| 208 | _pool_var (l) = 0; \ |
| 209 | if (P) \ |
| 210 | _pool_var (l) = vec_len (_pool_var (p)->free_indices); \ |
| 211 | \ |
| 212 | /* Free elements, certainly won't expand */ \ |
| 213 | if (_pool_var (l) > 0) \ |
| 214 | YESNO=0; \ |
| 215 | else \ |
| 216 | { \ |
| 217 | /* Nothing on free list, make a new element and return it. */ \ |
| 218 | YESNO = _vec_resize_will_expand \ |
| 219 | (P, \ |
| 220 | /* length_increment */ 1, \ |
| 221 | /* new size */ (vec_len (P) + 1) * sizeof (P[0]), \ |
| 222 | pool_aligned_header_bytes, \ |
| 223 | /* align */ (A)); \ |
| 224 | } \ |
| 225 | } while (0) |
| 226 | |
| 227 | #define pool_get_will_expand(P,YESNO) pool_get_aligned_will_expand(P,YESNO,0) |
| 228 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 229 | /** Use free bitmap to query whether given element is free. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | #define pool_is_free(P,E) \ |
| 231 | ({ \ |
| 232 | pool_header_t * _pool_var (p) = pool_header (P); \ |
| 233 | uword _pool_var (i) = (E) - (P); \ |
| 234 | (_pool_var (i) < vec_len (P)) ? clib_bitmap_get (_pool_var (p)->free_bitmap, _pool_i) : 1; \ |
| 235 | }) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 236 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 237 | /** Use free bitmap to query whether given index is free */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 238 | #define pool_is_free_index(P,I) pool_is_free((P),(P)+(I)) |
| 239 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 240 | /** Free an object E in pool P. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | #define pool_put(P,E) \ |
| 242 | do { \ |
| 243 | pool_header_t * _pool_var (p) = pool_header (P); \ |
| 244 | uword _pool_var (l) = (E) - (P); \ |
| 245 | ASSERT (vec_is_member (P, E)); \ |
| 246 | ASSERT (! pool_is_free (P, E)); \ |
| 247 | \ |
| 248 | /* Add element to free bitmap and to free list. */ \ |
| 249 | _pool_var (p)->free_bitmap = \ |
| 250 | clib_bitmap_ori (_pool_var (p)->free_bitmap, _pool_var (l)); \ |
| 251 | vec_add1 (_pool_var (p)->free_indices, _pool_var (l)); \ |
| 252 | } while (0) |
| 253 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 254 | /** Free pool element with given index. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 255 | #define pool_put_index(p,i) \ |
| 256 | do { \ |
| 257 | typeof (p) _e = (p) + (i); \ |
| 258 | pool_put (p, _e); \ |
| 259 | } while (0) |
| 260 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 261 | /** Allocate N more free elements to pool (general version). */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 262 | #define pool_alloc_aligned(P,N,A) \ |
| 263 | do { \ |
| 264 | pool_header_t * _p; \ |
| 265 | (P) = _vec_resize ((P), 0, (vec_len (P) + (N)) * sizeof (P[0]), \ |
| 266 | pool_aligned_header_bytes, \ |
| 267 | (A)); \ |
| 268 | _p = pool_header (P); \ |
| 269 | vec_resize (_p->free_indices, (N)); \ |
| 270 | _vec_len (_p->free_indices) -= (N); \ |
| 271 | } while (0) |
| 272 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 273 | /** Allocate N more free elements to pool (unspecified alignment). */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 274 | #define pool_alloc(P,N) pool_alloc_aligned(P,N,0) |
| 275 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 276 | /** Low-level free pool operator (do not call directly). */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 277 | always_inline void * |
| 278 | _pool_free (void *v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 280 | pool_header_t *p = pool_header (v); |
| 281 | if (!v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | return v; |
| 283 | clib_bitmap_free (p->free_bitmap); |
| 284 | vec_free (p->free_indices); |
| 285 | vec_free_h (v, pool_aligned_header_bytes); |
| 286 | return 0; |
| 287 | } |
| 288 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 289 | /** Free a pool. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 290 | #define pool_free(p) (p) = _pool_free(p) |
| 291 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 292 | /** Optimized iteration through pool. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 293 | |
| 294 | @param LO pointer to first element in chunk |
| 295 | @param HI pointer to last element in chunk |
| 296 | @param POOL pool to iterate across |
| 297 | @param BODY operation to perform |
| 298 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 299 | Optimized version which assumes that BODY is smart enough to |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 300 | process multiple (LOW,HI) chunks. See also pool_foreach(). |
| 301 | */ |
| 302 | #define pool_foreach_region(LO,HI,POOL,BODY) \ |
| 303 | do { \ |
| 304 | uword _pool_var (i), _pool_var (lo), _pool_var (hi), _pool_var (len); \ |
| 305 | uword _pool_var (bl), * _pool_var (b); \ |
| 306 | pool_header_t * _pool_var (p); \ |
| 307 | \ |
| 308 | _pool_var (p) = pool_header (POOL); \ |
| 309 | _pool_var (b) = (POOL) ? _pool_var (p)->free_bitmap : 0; \ |
| 310 | _pool_var (bl) = vec_len (_pool_var (b)); \ |
| 311 | _pool_var (len) = vec_len (POOL); \ |
| 312 | _pool_var (lo) = 0; \ |
| 313 | \ |
| 314 | for (_pool_var (i) = 0; \ |
| 315 | _pool_var (i) <= _pool_var (bl); \ |
| 316 | _pool_var (i)++) \ |
| 317 | { \ |
| 318 | uword _pool_var (m), _pool_var (f); \ |
| 319 | _pool_var (m) = (_pool_var (i) < _pool_var (bl) \ |
| 320 | ? _pool_var (b) [_pool_var (i)] \ |
| 321 | : 1); \ |
| 322 | while (_pool_var (m) != 0) \ |
| 323 | { \ |
| 324 | _pool_var (f) = first_set (_pool_var (m)); \ |
| 325 | _pool_var (hi) = (_pool_var (i) * BITS (_pool_var (b)[0]) \ |
| 326 | + min_log2 (_pool_var (f))); \ |
| 327 | _pool_var (hi) = (_pool_var (i) < _pool_var (bl) \ |
| 328 | ? _pool_var (hi) : _pool_var (len)); \ |
| 329 | _pool_var (m) ^= _pool_var (f); \ |
| 330 | if (_pool_var (hi) > _pool_var (lo)) \ |
| 331 | { \ |
| 332 | (LO) = _pool_var (lo); \ |
| 333 | (HI) = _pool_var (hi); \ |
| 334 | do { BODY; } while (0); \ |
| 335 | } \ |
| 336 | _pool_var (lo) = _pool_var (hi) + 1; \ |
| 337 | } \ |
| 338 | } \ |
| 339 | } while (0) |
| 340 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 341 | /** Iterate through pool. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 342 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 343 | @param VAR A variable of same type as pool vector to be used as an |
| 344 | iterator. |
| 345 | @param POOL The pool to iterate across. |
| 346 | @param BODY The operation to perform, typically a code block. See |
| 347 | the example below. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 348 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 349 | This macro will call @c BODY with each active pool element. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | |
| 351 | It is a bad idea to allocate or free pool element from within |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 352 | @c pool_foreach. Build a vector of indices and dispose of them later. |
Neale Ranns | 87df12d | 2017-02-18 08:16:41 -0800 | [diff] [blame] | 353 | Or call pool_flush. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 354 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 355 | |
| 356 | @par Example |
| 357 | @code{.c} |
| 358 | proc_t *procs; // a pool of processes. |
| 359 | proc_t *proc; // pointer to one process; used as the iterator. |
| 360 | |
| 361 | pool_foreach (proc, procs, ({ |
| 362 | if (proc->state != PROC_STATE_RUNNING) |
| 363 | continue; |
| 364 | |
| 365 | // check a running proc in some way |
| 366 | ... |
| 367 | })); |
| 368 | @endcode |
| 369 | |
| 370 | @warning Because @c pool_foreach is a macro, syntax errors can be |
| 371 | difficult to find inside @c BODY, let alone actual code bugs. One |
| 372 | can temporarily split a complex @c pool_foreach into a trivial |
| 373 | @c pool_foreach which builds a vector of active indices, and a |
| 374 | vec_foreach() (or plain for-loop) to walk the active index vector. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 375 | */ |
| 376 | #define pool_foreach(VAR,POOL,BODY) \ |
| 377 | do { \ |
| 378 | uword _pool_foreach_lo, _pool_foreach_hi; \ |
| 379 | pool_foreach_region (_pool_foreach_lo, _pool_foreach_hi, (POOL), \ |
| 380 | ({ \ |
| 381 | for ((VAR) = (POOL) + _pool_foreach_lo; \ |
| 382 | (VAR) < (POOL) + _pool_foreach_hi; \ |
| 383 | (VAR)++) \ |
| 384 | do { BODY; } while (0); \ |
| 385 | })); \ |
| 386 | } while (0) |
| 387 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 388 | /** Returns pointer to element at given index. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 389 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 390 | ASSERTs that the supplied index is valid. |
| 391 | Even though one can write correct code of the form |
| 392 | @code |
| 393 | p = pool_base + index; |
| 394 | @endcode |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 395 | use of @c pool_elt_at_index is strongly suggested. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 396 | */ |
| 397 | #define pool_elt_at_index(p,i) \ |
| 398 | ({ \ |
| 399 | typeof (p) _e = (p) + (i); \ |
| 400 | ASSERT (! pool_is_free (p, _e)); \ |
| 401 | _e; \ |
| 402 | }) |
| 403 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 404 | /** Return next occupied pool index after @c i, useful for safe iteration. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 405 | #define pool_next_index(P,I) \ |
| 406 | ({ \ |
| 407 | pool_header_t * _pool_var (p) = pool_header (P); \ |
| 408 | uword _pool_var (rv) = (I) + 1; \ |
| 409 | \ |
| 410 | _pool_var(rv) = \ |
| 411 | (_pool_var (rv) < vec_len (P) ? \ |
| 412 | clib_bitmap_next_clear (_pool_var (p)->free_bitmap, _pool_var(rv)) \ |
| 413 | : ~0); \ |
| 414 | _pool_var(rv); \ |
| 415 | }) |
| 416 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 417 | /** Iterate pool by index. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 418 | #define pool_foreach_index(i,v,body) \ |
| 419 | for ((i) = 0; (i) < vec_len (v); (i)++) \ |
| 420 | { \ |
| 421 | if (! pool_is_free_index ((v), (i))) \ |
| 422 | do { body; } while (0); \ |
| 423 | } |
| 424 | |
Neale Ranns | 87df12d | 2017-02-18 08:16:41 -0800 | [diff] [blame] | 425 | /** |
| 426 | * @brief Remove all elemenets from a pool in a safe way |
| 427 | * |
| 428 | * @param VAR each element in the pool |
| 429 | * @param POOL The pool to flush |
| 430 | * @param BODY The actions to perform on each element before it is returned to |
| 431 | * the pool. i.e. before it is 'freed' |
| 432 | */ |
| 433 | #define pool_flush(VAR, POOL, BODY) \ |
| 434 | { \ |
| 435 | uword *_pool_var(ii), *_pool_var(dv) = NULL; \ |
| 436 | \ |
| 437 | pool_foreach((VAR), (POOL), \ |
| 438 | ({ \ |
| 439 | vec_add1(_pool_var(dv), (VAR) - (POOL)); \ |
| 440 | })); \ |
| 441 | vec_foreach(_pool_var(ii), _pool_var(dv)) \ |
| 442 | { \ |
| 443 | (VAR) = pool_elt_at_index((POOL), *_pool_var(ii)); \ |
| 444 | do { BODY; } while (0); \ |
| 445 | pool_put((POOL), (VAR)); \ |
| 446 | } \ |
| 447 | vec_free(_pool_var(dv)); \ |
| 448 | } |
| 449 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 450 | #endif /* included_pool_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 451 | |
| 452 | /* |
| 453 | * fd.io coding-style-patch-verification: ON |
| 454 | * |
| 455 | * Local Variables: |
| 456 | * eval: (c-set-style "gnu") |
| 457 | * End: |
| 458 | */ |