From a4a28f04cb887ee690ccc9f563d06bf74fe18faf Mon Sep 17 00:00:00 2001 From: Damjan Marion Date: Thu, 17 Mar 2022 15:46:25 +0100 Subject: vppinfra: use stored vec header size to find header Type: refactor Change-Id: Iaa1e43c87c5725ab33ea8489bff2a7bda18b9c79 Signed-off-by: Damjan Marion --- src/vlib/node.c | 2 +- src/vppinfra/fifo.h | 2 +- src/vppinfra/hash.h | 9 +----- src/vppinfra/heap.h | 2 +- src/vppinfra/pool.h | 2 +- src/vppinfra/ring.h | 2 +- src/vppinfra/sparse_vec.h | 2 +- src/vppinfra/test_vec.c | 68 ++++++++++++++++++++++---------------------- src/vppinfra/vec.c | 2 +- src/vppinfra/vec.h | 18 ++++++------ src/vppinfra/vec_bootstrap.h | 28 +++--------------- 11 files changed, 56 insertions(+), 81 deletions(-) (limited to 'src') diff --git a/src/vlib/node.c b/src/vlib/node.c index 41b9ee73be6..2c408d6069a 100644 --- a/src/vlib/node.c +++ b/src/vlib/node.c @@ -364,7 +364,7 @@ register_node (vlib_main_t * vm, vlib_node_registration_t * r) 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 c1636c49841..496f5dd17c5 100644 --- a/src/vppinfra/fifo.h +++ b/src/vppinfra/fifo.h @@ -54,7 +54,7 @@ typedef struct 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 23b91b107a2..f5735bc92ee 100644 --- a/src/vppinfra/hash.h +++ b/src/vppinfra/hash.h @@ -96,18 +96,11 @@ typedef struct hash_header 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 22fc335c072..8b430644dbd 100644 --- a/src/vppinfra/heap.h +++ b/src/vppinfra/heap.h @@ -160,7 +160,7 @@ typedef struct 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 7d42f6c1e0d..1b1d54c1daa 100644 --- a/src/vppinfra/pool.h +++ b/src/vppinfra/pool.h @@ -71,7 +71,7 @@ typedef struct 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 be61dc44978..2f90b8269a6 100644 --- a/src/vppinfra/ring.h +++ b/src/vppinfra/ring.h @@ -29,7 +29,7 @@ typedef struct 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 2245be88262..4bc2cbdeaea 100644 --- a/src/vppinfra/sparse_vec.h +++ b/src/vppinfra/sparse_vec.h @@ -59,7 +59,7 @@ typedef struct 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 c6f97fb984d..28d945ee15f 100644 --- a/src/vppinfra/test_vec.c +++ b/src/vppinfra/test_vec.c @@ -207,33 +207,34 @@ dump_call_stats (uword * stats) 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 @@ validate_vec_hdr (elt_t * vec, uword hdr_bytes) 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 @@ validate_vec (elt_t * vec, uword hdr_bytes) 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 @@ validate_vec_resize_h (elt_t * vec, uword num_elts, uword hdr_bytes) 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 @@ validate_vec_resize_h (elt_t * vec, uword num_elts, uword hdr_bytes) } 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 @@ validate_vec_init_h (uword num_elts, uword hdr_bytes) { 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 97e2762ecbf..db84c1463a0 100644 --- a/src/vppinfra/vec.c +++ b/src/vppinfra/vec.c @@ -154,7 +154,7 @@ vec_resize_allocate_memory (void *v, __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 d19ff998137..e8eb586fca9 100644 --- a/src/vppinfra/vec.h +++ b/src/vppinfra/vec.h @@ -379,14 +379,16 @@ do { \ @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 34e50c9edb1..5dc1b6af540 100644 --- a/src/vppinfra/vec_bootstrap.h +++ b/src/vppinfra/vec_bootstrap.h @@ -89,7 +89,6 @@ 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; } @@ -100,9 +99,9 @@ vec_get_header_size (void *v) */ 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 @@ vec_header (void *v, uword header_bytes) */ 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 -- cgit 1.2.3-korg