44 #ifndef included_pool_h 45 #define included_pool_h 62 #define pool_aligned_header_bytes \ 63 vec_aligned_header_bytes (sizeof (pool_header_t), sizeof (void *)) 97 #define pool_validate_index(v,i) \ 99 uword __pool_validate_index = (i); \ 100 vec_validate_ha ((v), __pool_validate_index, \ 101 pool_aligned_header_bytes, 0); \ 102 pool_header_validate_index ((v), __pool_validate_index); \ 121 #define pool_len(p) vec_len(p) 127 #define _pool_len(p) _vec_len(p) 142 #define pool_bytes(P) (vec_bytes (P) + pool_header_bytes (P)) 145 #define _pool_var(v) _pool_##v 169 #define pool_get_aligned(P,E,A) \ 171 pool_header_t * _pool_var (p) = pool_header (P); \ 172 uword _pool_var (l); \ 176 _pool_var (l) = vec_len (_pool_var (p)->free_indices); \ 178 if (_pool_var (l) > 0) \ 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; \ 190 P = _vec_resize (P, \ 192 (vec_len (P) + 1) * sizeof (P[0]), \ 193 pool_aligned_header_bytes, \ 195 E = vec_end (P) - 1; \ 200 #define pool_get(P,E) pool_get_aligned(P,E,0) 203 #define pool_is_free(P,E) \ 205 pool_header_t * _pool_var (p) = pool_header (P); \ 206 uword _pool_var (i) = (E) - (P); \ 207 (_pool_var (i) < vec_len (P)) ? clib_bitmap_get (_pool_var (p)->free_bitmap, _pool_i) : 1; \ 211 #define pool_is_free_index(P,I) pool_is_free((P),(P)+(I)) 214 #define pool_put(P,E) \ 216 pool_header_t * _pool_var (p) = pool_header (P); \ 217 uword _pool_var (l) = (E) - (P); \ 218 ASSERT (vec_is_member (P, E)); \ 219 ASSERT (! pool_is_free (P, E)); \ 222 _pool_var (p)->free_bitmap = \ 223 clib_bitmap_ori (_pool_var (p)->free_bitmap, _pool_var (l)); \ 224 vec_add1 (_pool_var (p)->free_indices, _pool_var (l)); \ 228 #define pool_put_index(p,i) \ 230 typeof (p) _e = (p) + (i); \ 235 #define pool_alloc_aligned(P,N,A) \ 237 pool_header_t * _p; \ 238 (P) = _vec_resize ((P), 0, (vec_len (P) + (N)) * sizeof (P[0]), \ 239 pool_aligned_header_bytes, \ 241 _p = pool_header (P); \ 242 vec_resize (_p->free_indices, (N)); \ 243 _vec_len (_p->free_indices) -= (N); \ 247 #define pool_alloc(P,N) pool_alloc_aligned(P,N,0) 263 #define pool_free(p) (p) = _pool_free(p) 275 #define pool_foreach_region(LO,HI,POOL,BODY) \ 277 uword _pool_var (i), _pool_var (lo), _pool_var (hi), _pool_var (len); \ 278 uword _pool_var (bl), * _pool_var (b); \ 279 pool_header_t * _pool_var (p); \ 281 _pool_var (p) = pool_header (POOL); \ 282 _pool_var (b) = (POOL) ? _pool_var (p)->free_bitmap : 0; \ 283 _pool_var (bl) = vec_len (_pool_var (b)); \ 284 _pool_var (len) = vec_len (POOL); \ 285 _pool_var (lo) = 0; \ 287 for (_pool_var (i) = 0; \ 288 _pool_var (i) <= _pool_var (bl); \ 291 uword _pool_var (m), _pool_var (f); \ 292 _pool_var (m) = (_pool_var (i) < _pool_var (bl) \ 293 ? _pool_var (b) [_pool_var (i)] \ 295 while (_pool_var (m) != 0) \ 297 _pool_var (f) = first_set (_pool_var (m)); \ 298 _pool_var (hi) = (_pool_var (i) * BITS (_pool_var (b)[0]) \ 299 + min_log2 (_pool_var (f))); \ 300 _pool_var (hi) = (_pool_var (i) < _pool_var (bl) \ 301 ? _pool_var (hi) : _pool_var (len)); \ 302 _pool_var (m) ^= _pool_var (f); \ 303 if (_pool_var (hi) > _pool_var (lo)) \ 305 (LO) = _pool_var (lo); \ 306 (HI) = _pool_var (hi); \ 307 do { BODY; } while (0); \ 309 _pool_var (lo) = _pool_var (hi) + 1; \ 348 #define pool_foreach(VAR,POOL,BODY) \ 350 uword _pool_foreach_lo, _pool_foreach_hi; \ 351 pool_foreach_region (_pool_foreach_lo, _pool_foreach_hi, (POOL), \ 353 for ((VAR) = (POOL) + _pool_foreach_lo; \ 354 (VAR) < (POOL) + _pool_foreach_hi; \ 356 do { BODY; } while (0); \ 369 #define pool_elt_at_index(p,i) \ 371 typeof (p) _e = (p) + (i); \ 372 ASSERT (! pool_is_free (p, _e)); \ 377 #define pool_next_index(P,I) \ 379 pool_header_t * _pool_var (p) = pool_header (P); \ 380 uword _pool_var (rv) = (I) + 1; \ 383 (_pool_var (rv) < vec_len (P) ? \ 384 clib_bitmap_next_clear (_pool_var (p)->free_bitmap, _pool_var(rv)) \ 390 #define pool_foreach_index(i,v,body) \ 391 for ((i) = 0; (i) < vec_len (v); (i)++) \ 393 if (! pool_is_free_index ((v), (i))) \ 394 do { body; } while (0); \ #define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
sll srl srl sll sra u16x4 i
static void pool_header_validate_index(void *v, uword index)
#define vec_bytes(v)
Number of data bytes in vector.
static pool_header_t * pool_header(void *v)
Get pool header from user pool pointer.
#define pool_aligned_header_bytes
Align pool header so that pointers are naturally aligned.
static uword pool_header_bytes(void *v)
Memory usage of pool header.
static void pool_validate(void *v)
Validate a pool.
#define vec_free(V)
Free vector's memory (no header).
static void * vec_aligned_header(void *v, uword header_bytes, uword align)
#define vec_capacity(v, b)
Total number of bytes that can fit in vector with current allocation.
static uword clib_bitmap_get(uword *ai, uword i)
Gets the ith bit value from a bitmap.
Bitmaps built as vectors of machine words.
#define clib_bitmap_free(v)
Free a bitmap.
static uword clib_bitmap_count_set_bits(uword *ai)
Return the number of set bits in a bitmap.
#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)
static uword pool_free_elts(void *v)
Queries whether pool has at least N_FREE free elements.
static uword pool_elts(void *v)
Number of active elements in a pool.