summaryrefslogtreecommitdiffstats
path: root/src/vppinfra
diff options
context:
space:
mode:
authorDamjan Marion <damarion@cisco.com>2022-03-17 15:32:56 +0100
committerFlorin Coras <florin.coras@gmail.com>2022-03-17 20:31:14 +0000
commit5c45d1cf1b46c1ec83d50d032d6e7cad5fa3fefd (patch)
tree757f3fc0ad8a77b742857bcf1180792777447ee6 /src/vppinfra
parent1da361f9ea525663a1141c3665a67df5e81c4133 (diff)
vppinfra: vec_max_len, vec_mem_size use stored header size
Type: improvement Change-Id: I17778e89674da0e8204713302e2293377bdabcbc Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src/vppinfra')
-rw-r--r--src/vppinfra/hash.c4
-rw-r--r--src/vppinfra/heap.c6
-rw-r--r--src/vppinfra/pool.h2
-rw-r--r--src/vppinfra/vec_bootstrap.h61
4 files changed, 40 insertions, 33 deletions
diff --git a/src/vppinfra/hash.c b/src/vppinfra/hash.c
index f36ce831d25..7b8869254e1 100644
--- a/src/vppinfra/hash.c
+++ b/src/vppinfra/hash.c
@@ -1009,7 +1009,7 @@ hash_bytes (void *v)
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 @@ hash_bytes (void *v)
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 b54987abe5e..b724f88fdae 100644
--- a/src/vppinfra/heap.c
+++ b/src/vppinfra/heap.c
@@ -640,10 +640,10 @@ heap_bytes (void *v)
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 32b42b175b3..35c8e1e7b30 100644
--- a/src/vppinfra/pool.h
+++ b/src/vppinfra/pool.h
@@ -163,7 +163,7 @@ pool_header_bytes (void *v)
#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 d628d3827f9..34e50c9edb1 100644
--- a/src/vppinfra/vec_bootstrap.h
+++ b/src/vppinfra/vec_bootstrap.h
@@ -85,6 +85,14 @@ vec_header_bytes (uword header_bytes)
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 @@ u32 vec_len_not_inline (void *v);
* 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 @@ u32 vec_len_not_inline (void *v);
* @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)
-
-/** \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__ */
+
+always_inline uword
+_vec_max_len (void *v, uword elt_size)
+{
+ return v ? vec_mem_size (v) / elt_size : 0;
+}
+
+#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