Harmonize vec/pool_get_aligned object sizes and alignment requests
[vpp.git] / src / vppinfra / vec.h
index eed96d6..a029630 100644 (file)
@@ -145,6 +145,42 @@ _vec_resize (void *v,
                                               data_align));
 }
 
+/** \brief Determine if vector will resize with next allocation
+
+    @param v pointer to a vector
+    @param length_increment length increment in elements
+    @param data_bytes requested size in bytes
+    @param header_bytes header size in bytes (may be zero)
+    @param data_align alignment (may be zero)
+    @return 1 if vector will resize 0 otherwise
+*/
+
+always_inline int
+_vec_resize_will_expand (void *v,
+                        word length_increment,
+                        uword data_bytes, uword header_bytes,
+                        uword data_align)
+{
+  uword new_data_bytes, aligned_header_bytes;
+
+  aligned_header_bytes = vec_header_bytes (header_bytes);
+
+  new_data_bytes = data_bytes + aligned_header_bytes;
+
+  if (PREDICT_TRUE (v != 0))
+    {
+      void *p = v - aligned_header_bytes;
+
+      /* Vector header must start heap object. */
+      ASSERT (clib_mem_is_heap_object (p));
+
+      /* Typically we'll not need to resize. */
+      if (new_data_bytes <= clib_mem_size (p))
+       return 0;
+    }
+  return 1;
+}
+
 /** \brief Predicate function, says whether the supplied vector is a clib heap
     object (general version).
 
@@ -375,6 +411,8 @@ do {                                                                                \
 
 #define vec_validate_ha(V,I,H,A)                                       \
 do {                                                                   \
+  STATIC_ASSERT(A==0 || ((A % sizeof(V[0]))==0) || ((sizeof(V[0]) % A) == 0),\
+                "vector validate aligned on incorrectly sized object"); \
   word _v(i) = (I);                                                    \
   word _v(l) = vec_len (V);                                            \
   if (_v(i) >= _v(l))                                                  \
@@ -455,7 +493,7 @@ do {                                                                \
     @param A alignment (may be zero)
     @return V (value-result macro parameter)
 */
-#define vec_validate_init_empty_aligned(V,I,A) \
+#define vec_validate_init_empty_aligned(V,I,INIT,A) \
   vec_validate_init_empty_ha(V,I,INIT,0,A)
 
 /** \brief Add 1 element to end of vector (general version).
@@ -903,7 +941,7 @@ do {                                                \
   word _v(i) = 0;                                      \
   while (_v(i) < vec_len(v))                           \
   {                                                    \
-    if (v[_v(i)] == E)                                 \
+    if ((v)[_v(i)] == E)                                       \
       break;                                           \
     _v(i)++;                                           \
   }                                                    \