Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus |
| 17 | |
| 18 | Permission is hereby granted, free of charge, to any person obtaining |
| 19 | a copy of this software and associated documentation files (the |
| 20 | "Software"), to deal in the Software without restriction, including |
| 21 | without limitation the rights to use, copy, modify, merge, publish, |
| 22 | distribute, sublicense, and/or sell copies of the Software, and to |
| 23 | permit persons to whom the Software is furnished to do so, subject to |
| 24 | the following conditions: |
| 25 | |
| 26 | The above copyright notice and this permission notice shall be |
| 27 | included in all copies or substantial portions of the Software. |
| 28 | |
| 29 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 30 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 32 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 33 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 34 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 35 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 | */ |
| 37 | |
| 38 | #ifndef included_clib_vec_bootstrap_h |
| 39 | #define included_clib_vec_bootstrap_h |
| 40 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 41 | /** \file |
Paul Vinciguerra | ec11b13 | 2018-09-24 05:25:00 -0700 | [diff] [blame] | 42 | Vector bootstrap header file |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | */ |
| 44 | |
| 45 | /* Bootstrap include so that #include <vppinfra/mem.h> can include e.g. |
| 46 | <vppinfra/mheap.h> which depends on <vppinfra/vec.h>. */ |
| 47 | |
| 48 | /** \brief vector header structure |
| 49 | |
Paul Vinciguerra | ec11b13 | 2018-09-24 05:25:00 -0700 | [diff] [blame] | 50 | Bookkeeping header preceding vector elements in memory. |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 51 | User header information may preceed standard vec header. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | If you change u32 len -> u64 len, single vectors can |
| 53 | exceed 2**32 elements. Clib heaps are vectors. */ |
| 54 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 55 | typedef struct |
| 56 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | u32 len; /**< Number of elements in vector (NOT its allocated length). */ |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 58 | u8 hdr_size; /**< header size divided by VEC_MIN_ALIGN */ |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 59 | u8 log2_align : 7; /**< data alignment */ |
| 60 | u8 default_heap : 1; /**< vector uses default heap */ |
Damjan Marion | e4fa1d2 | 2022-04-11 18:41:49 +0200 | [diff] [blame] | 61 | u8 grow_elts; /**< number of elts vector can grow without realloc */ |
| 62 | u8 vpad[1]; /**< pad to 8 bytes */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | u8 vector_data[0]; /**< Vector data . */ |
| 64 | } vec_header_t; |
| 65 | |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 66 | #define VEC_MIN_ALIGN 8 |
Damjan Marion | 1da361f | 2022-03-16 17:57:29 +0100 | [diff] [blame] | 67 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | /** \brief Find the vector header |
| 69 | |
| 70 | Given the user's pointer to a vector, find the corresponding |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 71 | vector header |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | |
| 73 | @param v pointer to a vector |
| 74 | @return pointer to the vector's vector_header_t |
| 75 | */ |
| 76 | #define _vec_find(v) ((vec_header_t *) (v) - 1) |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 77 | #define _vec_heap(v) (((void **) (_vec_find (v)))[-1]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 79 | always_inline uword __vec_align (uword data_align, uword configuered_align); |
| 80 | always_inline uword __vec_elt_sz (uword elt_sz, int is_void); |
| 81 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | #define _vec_round_size(s) \ |
| 83 | (((s) + sizeof (uword) - 1) &~ (sizeof (uword) - 1)) |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 84 | #define _vec_is_void(P) \ |
| 85 | __builtin_types_compatible_p (__typeof__ ((P)[0]), void) |
| 86 | #define _vec_elt_sz(V) __vec_elt_sz (sizeof ((V)[0]), _vec_is_void (V)) |
| 87 | #define _vec_align(V, A) __vec_align (__alignof__((V)[0]), A) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 89 | always_inline __clib_nosanitize_addr uword |
Damjan Marion | 5c45d1c | 2022-03-17 15:32:56 +0100 | [diff] [blame] | 90 | vec_get_header_size (void *v) |
| 91 | { |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 92 | uword header_size = _vec_find (v)->hdr_size * VEC_MIN_ALIGN; |
Damjan Marion | 5c45d1c | 2022-03-17 15:32:56 +0100 | [diff] [blame] | 93 | return header_size; |
| 94 | } |
| 95 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | /** \brief Find a user vector header |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 97 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | Finds the user header of a vector with unspecified alignment given |
| 99 | the user pointer to the vector. |
| 100 | */ |
| 101 | |
| 102 | always_inline void * |
Damjan Marion | a4a28f0 | 2022-03-17 15:46:25 +0100 | [diff] [blame] | 103 | vec_header (void *v) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 104 | { |
Damjan Marion | a4a28f0 | 2022-03-17 15:46:25 +0100 | [diff] [blame] | 105 | return v ? v - vec_get_header_size (v) : 0; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 106 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | |
| 108 | /** \brief Find the end of user vector header |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 109 | |
| 110 | Finds the end of the user header of a vector with unspecified |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | alignment given the user pointer to the vector. |
| 112 | */ |
| 113 | |
| 114 | always_inline void * |
Damjan Marion | a4a28f0 | 2022-03-17 15:46:25 +0100 | [diff] [blame] | 115 | vec_header_end (void *v) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 116 | { |
Damjan Marion | a4a28f0 | 2022-03-17 15:46:25 +0100 | [diff] [blame] | 117 | return v + vec_get_header_size (v); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 118 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | /** \brief Number of elements in vector (rvalue-only, NULL tolerant) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 121 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | vec_len (v) checks for NULL, but cannot be used as an lvalue. |
| 123 | If in doubt, use vec_len... |
| 124 | */ |
| 125 | |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 126 | static_always_inline u32 |
| 127 | __vec_len (void *v) |
| 128 | { |
| 129 | return _vec_find (v)->len; |
| 130 | } |
| 131 | |
| 132 | #define _vec_len(v) __vec_len ((void *) (v)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | #define vec_len(v) ((v) ? _vec_len(v) : 0) |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 134 | |
Dave Barach | e09ae01 | 2020-08-19 06:59:53 -0400 | [diff] [blame] | 135 | u32 vec_len_not_inline (void *v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | /** \brief Number of data bytes in vector. */ |
| 138 | |
| 139 | #define vec_bytes(v) (vec_len (v) * sizeof (v[0])) |
| 140 | |
Florin Coras | 41a1bbf | 2022-03-11 10:58:55 -0800 | [diff] [blame] | 141 | /** |
| 142 | * Return size of memory allocated for the vector |
| 143 | * |
| 144 | * @param v vector |
Florin Coras | 41a1bbf | 2022-03-11 10:58:55 -0800 | [diff] [blame] | 145 | * @return memory size allocated for the vector |
| 146 | */ |
Damjan Marion | 5c45d1c | 2022-03-17 15:32:56 +0100 | [diff] [blame] | 147 | |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 148 | uword vec_mem_size (void *v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | |
Florin Coras | 41a1bbf | 2022-03-11 10:58:55 -0800 | [diff] [blame] | 150 | /** |
| 151 | * Number of elements that can fit into generic vector |
| 152 | * |
| 153 | * @param v vector |
| 154 | * @param b extra header bytes |
| 155 | * @return number of elements that can fit into vector |
| 156 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | |
Damjan Marion | 5c45d1c | 2022-03-17 15:32:56 +0100 | [diff] [blame] | 158 | always_inline uword |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 159 | vec_max_bytes (void *v) |
Damjan Marion | 5c45d1c | 2022-03-17 15:32:56 +0100 | [diff] [blame] | 160 | { |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 161 | return v ? vec_mem_size (v) - vec_get_header_size (v) : 0; |
Damjan Marion | 5c45d1c | 2022-03-17 15:32:56 +0100 | [diff] [blame] | 162 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 164 | always_inline uword |
| 165 | _vec_max_len (void *v, uword elt_sz) |
| 166 | { |
| 167 | return vec_max_bytes (v) / elt_sz; |
| 168 | } |
| 169 | |
| 170 | #define vec_max_len(v) _vec_max_len (v, _vec_elt_sz (v)) |
Damjan Marion | 5c45d1c | 2022-03-17 15:32:56 +0100 | [diff] [blame] | 171 | |
Damjan Marion | e4fa1d2 | 2022-04-11 18:41:49 +0200 | [diff] [blame] | 172 | static_always_inline void |
| 173 | _vec_set_grow_elts (void *v, uword n_elts) |
| 174 | { |
| 175 | uword max = pow2_mask (BITS (_vec_find (0)->grow_elts)); |
| 176 | |
| 177 | if (PREDICT_FALSE (n_elts > max)) |
| 178 | n_elts = max; |
| 179 | |
| 180 | _vec_find (v)->grow_elts = n_elts; |
| 181 | } |
| 182 | |
Damjan Marion | 5c45d1c | 2022-03-17 15:32:56 +0100 | [diff] [blame] | 183 | always_inline void |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 184 | _vec_set_len (void *v, uword len, uword elt_sz) |
Damjan Marion | 5c45d1c | 2022-03-17 15:32:56 +0100 | [diff] [blame] | 185 | { |
| 186 | ASSERT (v); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 187 | ASSERT (len <= _vec_max_len (v, elt_sz)); |
| 188 | uword old_len = _vec_len (v); |
Damjan Marion | e4fa1d2 | 2022-04-11 18:41:49 +0200 | [diff] [blame] | 189 | uword grow_elts = _vec_find (v)->grow_elts; |
Damjan Marion | 5c45d1c | 2022-03-17 15:32:56 +0100 | [diff] [blame] | 190 | |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 191 | if (len > old_len) |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 192 | clib_mem_unpoison (v + old_len * elt_sz, (len - old_len) * elt_sz); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 193 | else if (len > old_len) |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 194 | clib_mem_poison (v + len * elt_sz, (old_len - len) * elt_sz); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 195 | |
Damjan Marion | e4fa1d2 | 2022-04-11 18:41:49 +0200 | [diff] [blame] | 196 | _vec_set_grow_elts (v, old_len + grow_elts - len); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 197 | _vec_find (v)->len = len; |
Damjan Marion | 5c45d1c | 2022-03-17 15:32:56 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 200 | #define vec_set_len(v, l) _vec_set_len ((void *) v, l, _vec_elt_sz (v)) |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 201 | #define vec_inc_len(v, l) vec_set_len (v, _vec_len (v) + (l)) |
| 202 | #define vec_dec_len(v, l) vec_set_len (v, _vec_len (v) - (l)) |
Benoît Ganne | 8a4bfda | 2019-07-22 14:21:46 +0200 | [diff] [blame] | 203 | |
| 204 | /** \brief Reset vector length to zero |
| 205 | NULL-pointer tolerant |
| 206 | */ |
Benoît Ganne | 8a4bfda | 2019-07-22 14:21:46 +0200 | [diff] [blame] | 207 | #define vec_reset_length(v) do { if (v) vec_set_len (v, 0); } while (0) |
| 208 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | /** \brief End (last data address) of vector. */ |
| 210 | #define vec_end(v) ((v) + vec_len (v)) |
| 211 | |
| 212 | /** \brief True if given pointer is within given vector. */ |
| 213 | #define vec_is_member(v,e) ((e) >= (v) && (e) < vec_end (v)) |
| 214 | |
| 215 | /** \brief Get vector value at index i checking that i is in bounds. */ |
| 216 | #define vec_elt_at_index(v,i) \ |
| 217 | ({ \ |
| 218 | ASSERT ((i) < vec_len (v)); \ |
| 219 | (v) + (i); \ |
| 220 | }) |
| 221 | |
| 222 | /** \brief Get vector value at index i */ |
| 223 | #define vec_elt(v,i) (vec_elt_at_index(v,i))[0] |
| 224 | |
| 225 | /** \brief Vector iterator */ |
| 226 | #define vec_foreach(var,vec) for (var = (vec); var < vec_end (vec); var++) |
| 227 | |
| 228 | /** \brief Vector iterator (reverse) */ |
Neale Ranns | 9d33cfc | 2022-01-09 13:24:47 +0000 | [diff] [blame] | 229 | #define vec_foreach_backwards(var, vec) \ |
| 230 | if (vec) \ |
| 231 | for (var = vec_end (vec) - 1; var >= (vec); var--) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | |
| 233 | /** \brief Iterate over vector indices. */ |
| 234 | #define vec_foreach_index(var,v) for ((var) = 0; (var) < vec_len (v); (var)++) |
| 235 | |
Neale Ranns | fc5dda3 | 2018-07-27 00:44:52 -0700 | [diff] [blame] | 236 | /** \brief Iterate over vector indices (reverse). */ |
Neale Ranns | 9d33cfc | 2022-01-09 13:24:47 +0000 | [diff] [blame] | 237 | #define vec_foreach_index_backwards(var, v) \ |
| 238 | if (v) \ |
| 239 | for ((var) = vec_len ((v)) - 1; (var) >= 0; (var)--) |
Neale Ranns | fc5dda3 | 2018-07-27 00:44:52 -0700 | [diff] [blame] | 240 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | #endif /* included_clib_vec_bootstrap_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 242 | |
| 243 | /* |
| 244 | * fd.io coding-style-patch-verification: ON |
| 245 | * |
| 246 | * Local Variables: |
| 247 | * eval: (c-set-style "gnu") |
| 248 | * End: |
| 249 | */ |