vppinfra: vec_max_len, vec_mem_size use stored header size
Type: improvement
Change-Id: I17778e89674da0e8204713302e2293377bdabcbc
Signed-off-by: Damjan Marion <damarion@cisco.com>
diff --git a/src/vppinfra/hash.c b/src/vppinfra/hash.c
index f36ce83..7b88692 100644
--- a/src/vppinfra/hash.c
+++ b/src/vppinfra/hash.c
@@ -1009,7 +1009,7 @@
if (!v)
return 0;
- bytes = vec_mem_size (v, hash_header_bytes (v));
+ bytes = vec_mem_size (v);
for (i = 0; i < hash_capacity (v); i++)
{
@@ -1019,7 +1019,7 @@
if (h->log2_pair_size > 0)
bytes += 1 << indirect_pair_get_log2_bytes (&p->indirect);
else
- bytes += vec_mem_size (p->indirect.pairs, 0);
+ bytes += vec_mem_size (p->indirect.pairs);
}
}
return bytes;
diff --git a/src/vppinfra/heap.c b/src/vppinfra/heap.c
index b54987a..b724f88 100644
--- a/src/vppinfra/heap.c
+++ b/src/vppinfra/heap.c
@@ -640,10 +640,10 @@
bytes = sizeof (h[0]);
bytes += vec_len (v) * sizeof (h->elt_bytes);
for (b = 0; b < vec_len (h->free_lists); b++)
- bytes += vec_mem_size (h->free_lists[b], 0);
+ bytes += vec_mem_size (h->free_lists[b]);
bytes += vec_bytes (h->free_lists);
- bytes += vec_mem_size (h->elts, 0);
- bytes += vec_mem_size (h->free_elts, 0);
+ bytes += vec_mem_size (h->elts);
+ bytes += vec_mem_size (h->free_elts);
bytes += vec_bytes (h->used_elt_bitmap);
return bytes;
diff --git a/src/vppinfra/pool.h b/src/vppinfra/pool.h
index 32b42b1..35c8e1e 100644
--- a/src/vppinfra/pool.h
+++ b/src/vppinfra/pool.h
@@ -163,7 +163,7 @@
#define _pool_var(v) _pool_##v
/** Number of elements that can fit into pool with current allocation */
-#define pool_max_len(P) vec_max_elts (P, pool_aligned_header_bytes)
+#define pool_max_len(P) vec_max_len (P)
/** Number of free elements in pool */
#define pool_free_elts(P) \
diff --git a/src/vppinfra/vec_bootstrap.h b/src/vppinfra/vec_bootstrap.h
index d628d38..34e50c9 100644
--- a/src/vppinfra/vec_bootstrap.h
+++ b/src/vppinfra/vec_bootstrap.h
@@ -85,6 +85,14 @@
return round_pow2 (header_bytes + sizeof (vec_header_t), VEC_HEADER_ROUND);
}
+always_inline uword
+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;
+}
+
/** \brief Find a user vector header
Finds the user header of a vector with unspecified alignment given
@@ -167,16 +175,14 @@
* Return size of memory allocated for the vector
*
* @param v vector
- * @param b extra header bytes
* @return memory size allocated for the vector
*/
-#define vec_mem_size(v, b) \
- ({ \
- void *_vec_mem_v = (void *) (v); \
- uword _vec_mem_b = (b); \
- _vec_mem_b = sizeof (vec_header_t) + _vec_round_size (_vec_mem_b); \
- _vec_mem_v ? clib_mem_size (_vec_mem_v - _vec_mem_b) : 0; \
- })
+
+always_inline uword
+vec_mem_size (void *v)
+{
+ return v ? clib_mem_size (v - vec_get_header_size (v)) : 0;
+}
/**
* Number of elements that can fit into generic vector
@@ -185,25 +191,26 @@
* @param b extra header bytes
* @return number of elements that can fit into vector
*/
-#define vec_max_elts(v, b) \
- (v ? (vec_mem_size (v, b) - vec_header_bytes (b)) / sizeof (v[0]) : 0)
-/** \brief Total number of elements that can fit into vector. */
-#define vec_max_len(v) vec_max_elts (v, 0)
+always_inline uword
+_vec_max_len (void *v, uword elt_size)
+{
+ return v ? vec_mem_size (v) / elt_size : 0;
+}
-/** \brief Set vector length to a user-defined value */
-#ifndef __COVERITY__ /* Coverity gets confused by ASSERT() */
-#define vec_set_len(v, l) do { \
- ASSERT(v); \
- ASSERT((l) <= vec_max_len(v)); \
- CLIB_MEM_POISON_LEN((void *)(v), _vec_len(v) * sizeof((v)[0]), (l) * sizeof((v)[0])); \
- _vec_len(v) = (l); \
-} while (0)
-#else /* __COVERITY__ */
-#define vec_set_len(v, l) do { \
- _vec_len(v) = (l); \
-} while (0)
-#endif /* __COVERITY__ */
+#define vec_max_len(v) _vec_max_len (v, sizeof ((v)[0]))
+
+always_inline void
+_vec_set_len (void *v, uword len, uword elt_size)
+{
+ ASSERT (v);
+ ASSERT (len <= vec_max_len (v));
+
+ CLIB_MEM_POISON_LEN (v, _vec_len (v) * elt_size, len * elt_size);
+ _vec_len (v) = len;
+}
+
+#define vec_set_len(v, l) _vec_set_len ((void *) v, l, sizeof ((v)[0]))
/** \brief Reset vector length to zero
NULL-pointer tolerant