vppinfra: use stored vec header size to find header

Type: refactor
Change-Id: Iaa1e43c87c5725ab33ea8489bff2a7bda18b9c79
Signed-off-by: Damjan Marion <damarion@cisco.com>
diff --git a/src/vlib/node.c b/src/vlib/node.c
index 41b9ee7..2c408d6 100644
--- a/src/vlib/node.c
+++ b/src/vlib/node.c
@@ -364,7 +364,7 @@
   vec_add1 (nm->nodes, n);
 
   /* Name is always a vector so it can be formatted with %v. */
-  if (clib_mem_is_heap_object (vec_header (r->name, 0)))
+  if (clib_mem_is_heap_object (vec_header (r->name)))
     n->name = vec_dup ((u8 *) r->name);
   else
     n->name = format (0, "%s", r->name);
diff --git a/src/vppinfra/fifo.h b/src/vppinfra/fifo.h
index c1636c4..496f5dd 100644
--- a/src/vppinfra/fifo.h
+++ b/src/vppinfra/fifo.h
@@ -54,7 +54,7 @@
 always_inline clib_fifo_header_t *
 clib_fifo_header (void *f)
 {
-  return vec_header (f, sizeof (clib_fifo_header_t));
+  return vec_header (f);
 }
 
 /* Aliases. */
diff --git a/src/vppinfra/hash.h b/src/vppinfra/hash.h
index 23b91b1..f5735bc 100644
--- a/src/vppinfra/hash.h
+++ b/src/vppinfra/hash.h
@@ -96,18 +96,11 @@
   uword *is_user;
 } hash_t;
 
-/* Hash header size in bytes */
-always_inline uword
-hash_header_bytes (void *v)
-{
-  return sizeof (hash_t);
-}
-
 /* Returns a pointer to the hash header given the vector pointer */
 always_inline hash_t *
 hash_header (void *v)
 {
-  return vec_header (v, hash_header_bytes (v));
+  return vec_header (v);
 }
 
 /* Number of elements in the hash table */
diff --git a/src/vppinfra/heap.h b/src/vppinfra/heap.h
index 22fc335..8b43064 100644
--- a/src/vppinfra/heap.h
+++ b/src/vppinfra/heap.h
@@ -160,7 +160,7 @@
 always_inline heap_header_t *
 heap_header (void *v)
 {
-  return vec_header (v, sizeof (heap_header_t));
+  return vec_header (v);
 }
 
 always_inline uword
diff --git a/src/vppinfra/pool.h b/src/vppinfra/pool.h
index 7d42f6c..1b1d54c 100644
--- a/src/vppinfra/pool.h
+++ b/src/vppinfra/pool.h
@@ -71,7 +71,7 @@
 always_inline pool_header_t *
 pool_header (void *v)
 {
-  return vec_aligned_header (v, sizeof (pool_header_t), sizeof (void *));
+  return vec_header (v);
 }
 
 extern void _pool_init_fixed (void **, u32, u32);
diff --git a/src/vppinfra/ring.h b/src/vppinfra/ring.h
index be61dc4..2f90b82 100644
--- a/src/vppinfra/ring.h
+++ b/src/vppinfra/ring.h
@@ -29,7 +29,7 @@
 always_inline clib_ring_header_t *
 clib_ring_header (void *v)
 {
-  return vec_aligned_header (v, sizeof (clib_ring_header_t), sizeof (void *));
+  return vec_header (v);
 }
 
 always_inline void
diff --git a/src/vppinfra/sparse_vec.h b/src/vppinfra/sparse_vec.h
index 2245be8..4bc2cbd 100644
--- a/src/vppinfra/sparse_vec.h
+++ b/src/vppinfra/sparse_vec.h
@@ -59,7 +59,7 @@
 always_inline sparse_vec_header_t *
 sparse_vec_header (void *v)
 {
-  return vec_header (v, sizeof (sparse_vec_header_t));
+  return vec_header (v);
 }
 
 /* Index 0 is always used to mark indices that are not valid in
diff --git a/src/vppinfra/test_vec.c b/src/vppinfra/test_vec.c
index c6f97fb..28d945e 100644
--- a/src/vppinfra/test_vec.c
+++ b/src/vppinfra/test_vec.c
@@ -207,33 +207,34 @@
    more sensible value later. */
 #define MAX_VEC_LEN 10
 
-#define create_random_vec_wh(elt_type, len, hdr_bytes, seed)			\
-({										\
-  elt_type * _v(v) = NULL;							\
-  uword _v(l) = (len);								\
-  uword _v(h) = (hdr_bytes);							\
-  u8 * _v(hdr);									\
-										\
-  if (_v(l) == 0)								\
-    goto __done__;								\
-										\
-  /* ~0 means select random length between 0 and MAX_VEC_LEN. */		\
-  if (_v(l) == ~0)								\
-    _v(l) = bounded_random_u32 (&(seed), 0, MAX_VEC_LEN);			\
-										\
-  _v(v) = _vec_resize (NULL, _v(l), _v(l) * sizeof (elt_type), _v(h), 0);	\
-  fill_with_random_data (_v(v), vec_bytes (_v(v)), (seed));			\
-										\
-  /* Fill header with random data as well. */					\
-  if (_v(h) > 0)								\
-    {										\
-      _v(hdr) = vec_header (_v(v), _v(h));					\
-      fill_with_random_data (_v(hdr), _v(h), (seed));				\
-    }										\
-										\
-__done__:									\
-  _v(v);									\
-})
+#define create_random_vec_wh(elt_type, len, hdr_bytes, seed)                  \
+  ({                                                                          \
+    elt_type *_v (v) = NULL;                                                  \
+    uword _v (l) = (len);                                                     \
+    uword _v (h) = (hdr_bytes);                                               \
+    u8 *_v (hdr);                                                             \
+                                                                              \
+    if (_v (l) == 0)                                                          \
+      goto __done__;                                                          \
+                                                                              \
+    /* ~0 means select random length between 0 and MAX_VEC_LEN. */            \
+    if (_v (l) == ~0)                                                         \
+      _v (l) = bounded_random_u32 (&(seed), 0, MAX_VEC_LEN);                  \
+                                                                              \
+    _v (v) =                                                                  \
+      _vec_resize (NULL, _v (l), _v (l) * sizeof (elt_type), _v (h), 0);      \
+    fill_with_random_data (_v (v), vec_bytes (_v (v)), (seed));               \
+                                                                              \
+    /* Fill header with random data as well. */                               \
+    if (_v (h) > 0)                                                           \
+      {                                                                       \
+	_v (hdr) = vec_header (_v (v));                                       \
+	fill_with_random_data (_v (hdr), _v (h), (seed));                     \
+      }                                                                       \
+                                                                              \
+  __done__:                                                                   \
+    _v (v);                                                                   \
+  })
 
 #define create_random_vec(elt_type, len, seed) \
 create_random_vec_wh (elt_type, len, 0, seed)
@@ -274,8 +275,8 @@
     return;
 
   vh = _vec_find (vec);
-  hdr = vec_header (vec, hdr_bytes);
-  hdr_end = vec_header_end (hdr, hdr_bytes);
+  hdr = vec_header (vec);
+  hdr_end = vec_header_end (hdr);
 
   ASSERT (hdr_end == (u8 *) vec);
   ASSERT ((u8 *) vh - (u8 *) hdr >= hdr_bytes);
@@ -335,8 +336,7 @@
   else
     {
       if (hdr_bytes > 0)
-	VERBOSE3 ("Header: %U\n",
-		  format_hex_bytes, vec_header (vec, sizeof (vec[0])),
+	VERBOSE3 ("Header: %U\n", format_hex_bytes, vec_header (vec),
 		  sizeof (vec[0]));
 
       VERBOSE3 ("%U\n\n",
@@ -371,7 +371,7 @@
   len1 = vec_len (vec);
 
   if (vec)
-    hdr = vec_header (vec, hdr_bytes);
+    hdr = vec_header (vec);
 
   hash = compute_vec_hash (0, vec);
   hdr_hash = compute_mem_hash (0, hdr, hdr_bytes);
@@ -391,7 +391,7 @@
     }
 
   if (vec)
-    hdr = vec_header (vec, hdr_bytes);
+    hdr = vec_header (vec);
 
   ASSERT (compute_vec_hash (hash, vec) == 0);
   ASSERT (compute_mem_hash (hdr_hash, hdr, hdr_bytes) == 0);
@@ -687,7 +687,7 @@
     {
       if (i == 0)
 	{
-	  ptr = (u8 *) vec_header (new, hdr_bytes);
+	  ptr = (u8 *) vec_header (new);
 	  end = ptr + hdr_bytes;
 	}
       else
diff --git a/src/vppinfra/vec.c b/src/vppinfra/vec.c
index 97e2762..db84c14 100644
--- a/src/vppinfra/vec.c
+++ b/src/vppinfra/vec.c
@@ -154,7 +154,7 @@
 __clib_export uword
 clib_mem_is_vec_h (void *v, uword header_bytes)
 {
-  return clib_mem_is_heap_object (vec_header (v, header_bytes));
+  return clib_mem_is_heap_object (vec_header (v));
 }
 
 __clib_export u32
diff --git a/src/vppinfra/vec.h b/src/vppinfra/vec.h
index d19ff99..e8eb586 100644
--- a/src/vppinfra/vec.h
+++ b/src/vppinfra/vec.h
@@ -379,14 +379,16 @@
     @param H size of header in bytes
     @return V (value-result parameter, V=0)
 */
-#define vec_free_h(V,H)				\
-do {						\
-  if (V)					\
-    {						\
-      clib_mem_free (vec_header ((V), (H)));	\
-      V = 0;					\
-    }						\
-} while (0)
+#define vec_free_h(V, H)                                                      \
+  do                                                                          \
+    {                                                                         \
+      if (V)                                                                  \
+	{                                                                     \
+	  clib_mem_free (vec_header ((V)));                                   \
+	  V = 0;                                                              \
+	}                                                                     \
+    }                                                                         \
+  while (0)
 
 /** \brief Free vector's memory (no header).
     @param V pointer to a vector
diff --git a/src/vppinfra/vec_bootstrap.h b/src/vppinfra/vec_bootstrap.h
index 34e50c9..5dc1b6a 100644
--- a/src/vppinfra/vec_bootstrap.h
+++ b/src/vppinfra/vec_bootstrap.h
@@ -89,7 +89,6 @@
 vec_get_header_size (void *v)
 {
   uword header_size = _vec_find (v)->hdr_size * VEC_HEADER_ROUND;
-  ASSERT (header_size >= vec_header_bytes (0));
   return header_size;
 }
 
@@ -100,9 +99,9 @@
 */
 
 always_inline void *
-vec_header (void *v, uword header_bytes)
+vec_header (void *v)
 {
-  return v - vec_header_bytes (header_bytes);
+  return v ? v - vec_get_header_size (v) : 0;
 }
 
 /** \brief Find the end of user vector header
@@ -112,30 +111,11 @@
 */
 
 always_inline void *
-vec_header_end (void *v, uword header_bytes)
+vec_header_end (void *v)
 {
-  return v + vec_header_bytes (header_bytes);
+  return v + vec_get_header_size (v);
 }
 
-always_inline uword
-vec_aligned_header_bytes (uword header_bytes, uword align)
-{
-  return round_pow2 (header_bytes + sizeof (vec_header_t), align);
-}
-
-always_inline void *
-vec_aligned_header (void *v, uword header_bytes, uword align)
-{
-  return v - vec_aligned_header_bytes (header_bytes, align);
-}
-
-always_inline void *
-vec_aligned_header_end (void *v, uword header_bytes, uword align)
-{
-  return v + vec_aligned_header_bytes (header_bytes, align);
-}
-
-
 /** \brief Number of elements in vector (lvalue-capable)
 
    _vec_len (v) does not check for null, but can be used as an lvalue