X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvppinfra%2Fpool.h;h=57838e1c609ec27467fb5742be989cf5d482b246;hb=87df12d5de67600414ae80b891e8a0f89e89ce5c;hp=586d13e73a9ed0dcaa8e93a75e89f1672ede4a92;hpb=4008ac998f43265451281cb6e759cd6184e50bed;p=vpp.git diff --git a/src/vppinfra/pool.h b/src/vppinfra/pool.h index 586d13e73a9..57838e1c609 100644 --- a/src/vppinfra/pool.h +++ b/src/vppinfra/pool.h @@ -350,6 +350,7 @@ do { \ It is a bad idea to allocate or free pool element from within @c pool_foreach. Build a vector of indices and dispose of them later. + Or call pool_flush. @par Example @@ -421,6 +422,31 @@ do { \ do { body; } while (0); \ } +/** + * @brief Remove all elemenets from a pool in a safe way + * + * @param VAR each element in the pool + * @param POOL The pool to flush + * @param BODY The actions to perform on each element before it is returned to + * the pool. i.e. before it is 'freed' + */ +#define pool_flush(VAR, POOL, BODY) \ +{ \ + uword *_pool_var(ii), *_pool_var(dv) = NULL; \ + \ + 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)); \ + do { BODY; } while (0); \ + pool_put((POOL), (VAR)); \ + } \ + vec_free(_pool_var(dv)); \ +} + #endif /* included_pool_h */ /*