summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/pool.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/vppinfra/pool.h')
-rw-r--r--src/vppinfra/pool.h26
1 files changed, 26 insertions, 0 deletions
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 */
/*