diff options
author | Damjan Marion <damarion@cisco.com> | 2022-03-17 15:32:56 +0100 |
---|---|---|
committer | Florin Coras <florin.coras@gmail.com> | 2022-03-17 20:31:14 +0000 |
commit | 5c45d1cf1b46c1ec83d50d032d6e7cad5fa3fefd (patch) | |
tree | 757f3fc0ad8a77b742857bcf1180792777447ee6 /src/vppinfra/vec_bootstrap.h | |
parent | 1da361f9ea525663a1141c3665a67df5e81c4133 (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/vec_bootstrap.h')
-rw-r--r-- | src/vppinfra/vec_bootstrap.h | 61 |
1 files changed, 34 insertions, 27 deletions
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 |