api: fix [un]formatting in vpp/api/types.c
[vpp.git] / src / vppinfra / pool.h
index 2a35629..07c9269 100644 (file)
@@ -268,7 +268,8 @@ _pool_put_will_expand (void *p, uword index, uword elt_sz)
   return 0;
 }
 
-#define pool_put_will_expand(P, E) _pool_put_will_expand (P, (E) - (P), sizeof ((P)[0])
+#define pool_put_will_expand(P, E)                                            \
+  _pool_put_will_expand (P, (E) - (P), sizeof ((P)[0]))
 
 /** Use free bitmap to query whether given element is free. */
 static_always_inline int
@@ -295,8 +296,9 @@ _pool_put_index (void *p, uword index, uword elt_sz)
   /* Preallocated pool? */
   if (ph->max_elts)
     {
-      ph->free_indices[_vec_len (ph->free_indices)] = index;
-      vec_inc_len (ph->free_indices, 1);
+      u32 len = _vec_len (ph->free_indices);
+      vec_set_len (ph->free_indices, len + 1);
+      ph->free_indices[len] = index;
     }
   else
     vec_add1 (ph->free_indices, index);
@@ -332,7 +334,7 @@ _pool_alloc (void **pp, uword n_elts, uword align, void *heap, uword elt_sz)
   ph = pool_header (pp[0]);
   vec_resize (ph->free_indices, n_elts);
   vec_dec_len (ph->free_indices, n_elts);
-  clib_bitmap_vec_validate (ph->free_bitmap, len + n_elts - 1);
+  clib_bitmap_validate (ph->free_bitmap, (len + n_elts) ?: 1);
 }
 
 #define pool_alloc_aligned_heap(P, N, A, H)                                   \
@@ -555,11 +557,25 @@ 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))        \
+#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 from s to e */
+#define pool_foreach_stepping_index(i, s, e, v)                               \
+  for ((i) =                                                                  \
+        (pool_is_free_index ((v), (s)) ? pool_get_next_index ((v), (s)) :    \
+                                               (s));                               \
+       (i) < (e); (i) = pool_get_next_index ((v), (i)))
+
+/* works only for pool of pointers, e is declared inside macro */
+#define pool_foreach_pointer(e, p)                                            \
+  if (p)                                                                      \
+    for (typeof ((p)[0]) *_t = (p) + pool_get_first_index (p), (e) = *_t,     \
+                        *_end = vec_end (p);                                 \
+        _t < _end; _t = (p) + pool_get_next_index (p, _t - (p)),             \
+                        (e) = _t < _end ? *_t : (e))
 
 /**
  * @brief Remove all elements from a pool in a safe way