vppinfra: refactor uword bitmaps
[vpp.git] / src / vppinfra / pool.h
index 8330d64..968614e 100644 (file)
@@ -172,6 +172,9 @@ _pool_get (void **pp, void **ep, uword align, int zero, uword elt_sz)
   uword len = 0;
   void *p = pp[0];
   void *e;
+  vec_attr_t va = { .hdr_sz = sizeof (pool_header_t),
+                   .elt_sz = elt_sz,
+                   .align = align };
 
   if (p)
     {
@@ -199,8 +202,7 @@ _pool_get (void **pp, void **ep, uword align, int zero, uword elt_sz)
   len = vec_len (p);
 
   /* Nothing on free list, make a new element and return it. */
-  p =
-    _vec_realloc_inline (p, len + 1, elt_sz, sizeof (pool_header_t), align, 0);
+  p = _vec_realloc_internal (p, len + 1, &va);
   e = p + len * elt_sz;
 
   _vec_update_pointer (pp, p);
@@ -293,8 +295,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);
@@ -312,6 +315,10 @@ _pool_alloc (void **pp, uword n_elts, uword align, void *heap, uword elt_sz)
 {
   pool_header_t *ph = pool_header (pp[0]);
   uword len = vec_len (pp[0]);
+  const vec_attr_t va = { .hdr_sz = sizeof (pool_header_t),
+                         .elt_sz = elt_sz,
+                         .align = align,
+                         .heap = heap };
 
   if (ph && ph->max_elts)
     {
@@ -319,15 +326,14 @@ _pool_alloc (void **pp, uword n_elts, uword align, void *heap, uword elt_sz)
       os_out_of_memory ();
     }
 
-  pp[0] = _vec_realloc_inline (pp[0], len + n_elts, elt_sz,
-                              sizeof (pool_header_t), align, heap);
+  pp[0] = _vec_resize_internal (pp[0], len + n_elts, &va);
   _vec_set_len (pp[0], len, elt_sz);
   clib_mem_poison (pp[0] + len * elt_sz, n_elts * 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)                                   \
@@ -342,6 +348,9 @@ _pool_dup (void *p, uword align, uword elt_sz)
 {
   pool_header_t *nph, *ph = pool_header (p);
   uword len = vec_len (p);
+  const vec_attr_t va = { .hdr_sz = sizeof (pool_header_t),
+                         .elt_sz = elt_sz,
+                         .align = align };
   void *n;
 
   if (ph && ph->max_elts)
@@ -350,7 +359,7 @@ _pool_dup (void *p, uword align, uword elt_sz)
       os_out_of_memory ();
     }
 
-  n = _vec_realloc_inline (0, len, elt_sz, sizeof (pool_header_t), align, 0);
+  n = _vec_alloc_internal (len, &va);
   nph = pool_header (n);
   clib_memset_u8 (nph, 0, sizeof (vec_header_t));
 
@@ -547,11 +556,17 @@ 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)))
 
 /**
  * @brief Remove all elements from a pool in a safe way