From 41a1bbffc6238b0ed52b55f58ba884fe2f26e11f Mon Sep 17 00:00:00 2001 From: Florin Coras Date: Fri, 11 Mar 2022 10:58:55 -0800 Subject: [PATCH] vppinfra: fix vec capacity Rename vec_capacity to vec_mem_size as it returned the size of the underlying memory allocation not the number of bytes that can be used for vector elements. Add new vec_max_elts macro that returns number of elements that can fit into generic vector. Type: fix Signed-off-by: Florin Coras Change-Id: I2e53a2bfa6e56a89af62d6ddc073ead58b8c49bb --- src/vppinfra/hash.c | 4 ++-- src/vppinfra/heap.c | 6 +++--- src/vppinfra/pool.h | 5 +---- src/vppinfra/vec_bootstrap.h | 34 ++++++++++++++++++++++++---------- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/vppinfra/hash.c b/src/vppinfra/hash.c index c7774bb87ef..63dab989852 100644 --- a/src/vppinfra/hash.c +++ b/src/vppinfra/hash.c @@ -1003,7 +1003,7 @@ hash_bytes (void *v) if (!v) return 0; - bytes = vec_capacity (v, hash_header_bytes (v)); + bytes = vec_mem_size (v, hash_header_bytes (v)); for (i = 0; i < hash_capacity (v); i++) { @@ -1013,7 +1013,7 @@ hash_bytes (void *v) if (h->log2_pair_size > 0) bytes += 1 << indirect_pair_get_log2_bytes (&p->indirect); else - bytes += vec_capacity (p->indirect.pairs, 0); + bytes += vec_mem_size (p->indirect.pairs, 0); } } return bytes; diff --git a/src/vppinfra/heap.c b/src/vppinfra/heap.c index bc22da1d8f6..b54987abe5e 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_capacity (h->free_lists[b], 0); + bytes += vec_mem_size (h->free_lists[b], 0); bytes += vec_bytes (h->free_lists); - bytes += vec_capacity (h->elts, 0); - bytes += vec_capacity (h->free_elts, 0); + bytes += vec_mem_size (h->elts, 0); + bytes += vec_mem_size (h->free_elts, 0); bytes += vec_bytes (h->used_elt_bitmap); return bytes; diff --git a/src/vppinfra/pool.h b/src/vppinfra/pool.h index 284e23d8e0a..32b42b175b3 100644 --- a/src/vppinfra/pool.h +++ b/src/vppinfra/pool.h @@ -162,11 +162,8 @@ pool_header_bytes (void *v) /** Local variable naming macro. */ #define _pool_var(v) _pool_##v -/** Number of bytes that can fit into pool with current allocation */ -#define pool_capacity(P) vec_capacity (P, pool_aligned_header_bytes) - /** Number of elements that can fit into pool with current allocation */ -#define pool_max_len(P) (pool_capacity (P) / sizeof (P[0])) +#define pool_max_len(P) vec_max_elts (P, pool_aligned_header_bytes) /** 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 e14c4be8e52..f2940288110 100644 --- a/src/vppinfra/vec_bootstrap.h +++ b/src/vppinfra/vec_bootstrap.h @@ -160,19 +160,33 @@ u32 vec_len_not_inline (void *v); #define vec_bytes(v) (vec_len (v) * sizeof (v[0])) -/** \brief Total number of bytes that can fit in vector with current allocation. */ +/** + * 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; \ + }) -#define vec_capacity(v,b) \ -({ \ - void * _vec_capacity_v = (void *) (v); \ - uword _vec_capacity_b = (b); \ - _vec_capacity_b = sizeof (vec_header_t) + _vec_round_size (_vec_capacity_b); \ - _vec_capacity_v ? clib_mem_size (_vec_capacity_v - _vec_capacity_b) : 0; \ -}) +/** + * Number of elements that can fit into generic vector + * + * @param v vector + * @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) \ - ((v) ? (vec_capacity (v,0) - vec_header_bytes (0)) / sizeof (v[0]) : 0) +#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() */