38 #ifndef included_pool_h 39 #define included_pool_h 58 #define pool_aligned_header_bytes \ 59 vec_aligned_header_bytes (sizeof (pool_header_t), sizeof (void *)) 88 #define pool_validate_index(v,i) \ 90 uword __pool_validate_index = (i); \ 91 vec_validate_ha ((v), __pool_validate_index, \ 92 pool_aligned_header_bytes, 0); \ 93 pool_header_validate_index ((v), __pool_validate_index); \ 109 #define pool_len(p) vec_len(p) 114 #define _pool_len(p) _vec_len(p) 129 #define pool_bytes(P) (vec_bytes (P) + pool_header_bytes (P)) 132 #define _pool_var(v) _pool_##v 155 #define pool_get_aligned(P,E,A) \ 157 pool_header_t * _pool_var (p) = pool_header (P); \ 158 uword _pool_var (l); \ 162 _pool_var (l) = vec_len (_pool_var (p)->free_indices); \ 164 if (_pool_var (l) > 0) \ 167 uword _pool_var (i) = _pool_var (p)->free_indices[_pool_var (l) - 1]; \ 168 (E) = (P) + _pool_var (i); \ 169 _pool_var (p)->free_bitmap = \ 170 clib_bitmap_andnoti (_pool_var (p)->free_bitmap, _pool_var (i)); \ 171 _vec_len (_pool_var (p)->free_indices) = _pool_var (l) - 1; \ 176 P = _vec_resize (P, \ 178 (vec_len (P) + 1) * sizeof (P[0]), \ 179 pool_aligned_header_bytes, \ 181 E = vec_end (P) - 1; \ 186 #define pool_get(P,E) pool_get_aligned(P,E,0) 189 #define pool_is_free(P,E) \ 191 pool_header_t * _pool_var (p) = pool_header (P); \ 192 uword _pool_var (i) = (E) - (P); \ 193 (_pool_var (i) < vec_len (P)) ? clib_bitmap_get (_pool_var (p)->free_bitmap, _pool_i) : 1; \ 197 #define pool_is_free_index(P,I) pool_is_free((P),(P)+(I)) 200 #define pool_put(P,E) \ 202 pool_header_t * _pool_var (p) = pool_header (P); \ 203 uword _pool_var (l) = (E) - (P); \ 204 ASSERT (vec_is_member (P, E)); \ 205 ASSERT (! pool_is_free (P, E)); \ 208 _pool_var (p)->free_bitmap = \ 209 clib_bitmap_ori (_pool_var (p)->free_bitmap, _pool_var (l)); \ 210 vec_add1 (_pool_var (p)->free_indices, _pool_var (l)); \ 214 #define pool_put_index(p,i) \ 216 typeof (p) _e = (p) + (i); \ 221 #define pool_alloc_aligned(P,N,A) \ 223 pool_header_t * _p; \ 224 (P) = _vec_resize ((P), 0, (vec_len (P) + (N)) * sizeof (P[0]), \ 225 pool_aligned_header_bytes, \ 227 _p = pool_header (P); \ 228 vec_resize (_p->free_indices, (N)); \ 229 _vec_len (_p->free_indices) -= (N); \ 233 #define pool_alloc(P,N) pool_alloc_aligned(P,N,0) 248 #define pool_free(p) (p) = _pool_free(p) 260 #define pool_foreach_region(LO,HI,POOL,BODY) \ 262 uword _pool_var (i), _pool_var (lo), _pool_var (hi), _pool_var (len); \ 263 uword _pool_var (bl), * _pool_var (b); \ 264 pool_header_t * _pool_var (p); \ 266 _pool_var (p) = pool_header (POOL); \ 267 _pool_var (b) = (POOL) ? _pool_var (p)->free_bitmap : 0; \ 268 _pool_var (bl) = vec_len (_pool_var (b)); \ 269 _pool_var (len) = vec_len (POOL); \ 270 _pool_var (lo) = 0; \ 272 for (_pool_var (i) = 0; \ 273 _pool_var (i) <= _pool_var (bl); \ 276 uword _pool_var (m), _pool_var (f); \ 277 _pool_var (m) = (_pool_var (i) < _pool_var (bl) \ 278 ? _pool_var (b) [_pool_var (i)] \ 280 while (_pool_var (m) != 0) \ 282 _pool_var (f) = first_set (_pool_var (m)); \ 283 _pool_var (hi) = (_pool_var (i) * BITS (_pool_var (b)[0]) \ 284 + min_log2 (_pool_var (f))); \ 285 _pool_var (hi) = (_pool_var (i) < _pool_var (bl) \ 286 ? _pool_var (hi) : _pool_var (len)); \ 287 _pool_var (m) ^= _pool_var (f); \ 288 if (_pool_var (hi) > _pool_var (lo)) \ 290 (LO) = _pool_var (lo); \ 291 (HI) = _pool_var (hi); \ 292 do { BODY; } while (0); \ 294 _pool_var (lo) = _pool_var (hi) + 1; \ 328 #define pool_foreach(VAR,POOL,BODY) \ 330 uword _pool_foreach_lo, _pool_foreach_hi; \ 331 pool_foreach_region (_pool_foreach_lo, _pool_foreach_hi, (POOL), \ 333 for ((VAR) = (POOL) + _pool_foreach_lo; \ 334 (VAR) < (POOL) + _pool_foreach_hi; \ 336 do { BODY; } while (0); \ 346 #define pool_elt_at_index(p,i) \ 348 typeof (p) _e = (p) + (i); \ 349 ASSERT (! pool_is_free (p, _e)); \ 354 #define pool_next_index(P,I) \ 356 pool_header_t * _pool_var (p) = pool_header (P); \ 357 uword _pool_var (rv) = (I) + 1; \ 360 (_pool_var (rv) < vec_len (P) ? \ 361 clib_bitmap_next_clear (_pool_var (p)->free_bitmap, _pool_var(rv)) \ 366 #define pool_foreach_index(i,v,body) \ 367 for ((i) = 0; (i) < vec_len (v); (i)++) \ 369 if (! pool_is_free_index ((v), (i))) \ 370 do { body; } while (0); \ #define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
always_inline uword pool_free_elts(void *v)
sll srl srl sll sra u16x4 i
always_inline uword clib_bitmap_count_set_bits(uword *ai)
always_inline uword pool_header_bytes(void *v)
always_inline void * vec_aligned_header(void *v, uword header_bytes, uword align)
#define vec_bytes(v)
Number of data bytes in vector.
always_inline uword pool_elts(void *v)
always_inline void pool_validate(void *v)
always_inline uword clib_bitmap_get(uword *ai, uword i)
#define pool_aligned_header_bytes
#define vec_free(V)
Free vector's memory (no header).
#define vec_capacity(v, b)
Total number of bytes that can fit in vector with current allocation.
#define clib_bitmap_free(v)
always_inline void pool_header_validate_index(void *v, uword index)
always_inline pool_header_t * pool_header(void *v)
#define vec_free_h(V, H)
Free vector's memory (general version)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)