X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvppinfra%2Fpool.h;h=116c1cd132176314a3f6f227121da00f7f6ca903;hb=b2c31b685fd2cf28436ca32bc93e23eb24c74878;hp=db950d27d1855df7c66a15eea355298338ca645a;hpb=a690fdbfe179e0ea65818c03b52535bf9210efd0;p=vpp.git diff --git a/src/vppinfra/pool.h b/src/vppinfra/pool.h index db950d27d18..116c1cd1321 100644 --- a/src/vppinfra/pool.h +++ b/src/vppinfra/pool.h @@ -46,7 +46,6 @@ #include #include -#include typedef struct @@ -423,6 +422,20 @@ _pool_free (void *v) return 0; } +static_always_inline uword +pool_get_first_index (void *pool) +{ + pool_header_t *h = pool_header (pool); + return clib_bitmap_first_clear (h->free_bitmap); +} + +static_always_inline uword +pool_get_next_index (void *pool, uword last) +{ + pool_header_t *h = pool_header (pool); + return clib_bitmap_next_clear (h->free_bitmap, last + 1); +} + /** Free a pool. */ #define pool_free(p) (p) = _pool_free(p) @@ -510,17 +523,16 @@ do { \ @c pool_foreach which builds a vector of active indices, and a vec_foreach() (or plain for-loop) to walk the active index vector. */ -#define pool_foreach(VAR,POOL,BODY) \ -do { \ - uword _pool_foreach_lo, _pool_foreach_hi; \ - pool_foreach_region (_pool_foreach_lo, _pool_foreach_hi, (POOL), \ - ({ \ - for ((VAR) = (POOL) + _pool_foreach_lo; \ - (VAR) < (POOL) + _pool_foreach_hi; \ - (VAR)++) \ - do { BODY; } while (0); \ - })); \ -} while (0) + +#define pool_foreach(VAR,POOL) \ + if (POOL) \ + for (VAR = POOL + pool_get_first_index (POOL); \ + VAR < vec_end (POOL); \ + VAR = POOL + pool_get_next_index (POOL, VAR - POOL)) + +#define pool_foreach_old(VAR,POOL,BODY) \ + pool_foreach(VAR,POOL) \ + { BODY; } /** Returns pointer to element at given index. @@ -554,13 +566,16 @@ do { \ _pool_var(rv); \ }) +#define pool_foreach_index(i,v) \ + if (v) \ + for (i = pool_get_first_index (v); \ + i < vec_len (v); \ + i = pool_get_next_index (v, i)) \ + /** Iterate pool by index. */ -#define pool_foreach_index(i,v,body) \ - for ((i) = 0; (i) < vec_len (v); (i)++) \ - { \ - if (! pool_is_free_index ((v), (i))) \ - do { body; } while (0); \ - } +#define pool_foreach_index_old(i,v,body) \ + pool_foreach_index (i,v) \ + { body; } /** * @brief Remove all elements from a pool in a safe way @@ -574,10 +589,10 @@ do { \ { \ uword *_pool_var(ii), *_pool_var(dv) = NULL; \ \ - pool_foreach((VAR), (POOL), \ - ({ \ + pool_foreach((VAR), (POOL)) \ + { \ vec_add1(_pool_var(dv), (VAR) - (POOL)); \ - })); \ + } \ vec_foreach(_pool_var(ii), _pool_var(dv)) \ { \ (VAR) = pool_elt_at_index((POOL), *_pool_var(ii)); \