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 | #if CLIB_VEC64 > 0 |
| 58 | u64 len; |
| 59 | #else |
| 60 | u32 len; /**< Number of elements in vector (NOT its allocated length). */ |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 61 | u32 dlmalloc_header_offset; /**< offset to memory allocator offset */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | #endif |
| 63 | u8 vector_data[0]; /**< Vector data . */ |
| 64 | } vec_header_t; |
| 65 | |
| 66 | /** \brief Find the vector header |
| 67 | |
| 68 | Given the user's pointer to a vector, find the corresponding |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 69 | vector header |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | |
| 71 | @param v pointer to a vector |
| 72 | @return pointer to the vector's vector_header_t |
| 73 | */ |
| 74 | #define _vec_find(v) ((vec_header_t *) (v) - 1) |
| 75 | |
| 76 | #define _vec_round_size(s) \ |
| 77 | (((s) + sizeof (uword) - 1) &~ (sizeof (uword) - 1)) |
| 78 | |
| 79 | always_inline uword |
| 80 | vec_header_bytes (uword header_bytes) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 81 | { |
| 82 | return round_pow2 (header_bytes + sizeof (vec_header_t), |
| 83 | sizeof (vec_header_t)); |
| 84 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | |
| 86 | /** \brief Find a user vector header |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 87 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | Finds the user header of a vector with unspecified alignment given |
| 89 | the user pointer to the vector. |
| 90 | */ |
| 91 | |
| 92 | always_inline void * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 93 | vec_header (void *v, uword header_bytes) |
| 94 | { |
| 95 | return v - vec_header_bytes (header_bytes); |
| 96 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | |
| 98 | /** \brief Find the end of user vector header |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 99 | |
| 100 | Finds the end of the user header of a vector with unspecified |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | alignment given the user pointer to the vector. |
| 102 | */ |
| 103 | |
| 104 | always_inline void * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 105 | vec_header_end (void *v, uword header_bytes) |
| 106 | { |
| 107 | return v + vec_header_bytes (header_bytes); |
| 108 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | |
| 110 | always_inline uword |
| 111 | vec_aligned_header_bytes (uword header_bytes, uword align) |
| 112 | { |
| 113 | return round_pow2 (header_bytes + sizeof (vec_header_t), align); |
| 114 | } |
| 115 | |
| 116 | always_inline void * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 117 | vec_aligned_header (void *v, uword header_bytes, uword align) |
| 118 | { |
| 119 | return v - vec_aligned_header_bytes (header_bytes, align); |
| 120 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | |
| 122 | always_inline void * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 123 | vec_aligned_header_end (void *v, uword header_bytes, uword align) |
| 124 | { |
| 125 | return v + vec_aligned_header_bytes (header_bytes, align); |
| 126 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | |
| 128 | |
| 129 | /** \brief Number of elements in vector (lvalue-capable) |
| 130 | |
| 131 | _vec_len (v) does not check for null, but can be used as a lvalue |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 132 | (e.g. _vec_len (v) = 99). |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | */ |
| 134 | |
| 135 | #define _vec_len(v) (_vec_find(v)->len) |
| 136 | |
| 137 | /** \brief Number of elements in vector (rvalue-only, NULL tolerant) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 138 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | vec_len (v) checks for NULL, but cannot be used as an lvalue. |
| 140 | If in doubt, use vec_len... |
| 141 | */ |
| 142 | |
| 143 | #define vec_len(v) ((v) ? _vec_len(v) : 0) |
| 144 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 145 | /** \brief Number of data bytes in vector. */ |
| 146 | |
| 147 | #define vec_bytes(v) (vec_len (v) * sizeof (v[0])) |
| 148 | |
| 149 | /** \brief Total number of bytes that can fit in vector with current allocation. */ |
| 150 | |
| 151 | #define vec_capacity(v,b) \ |
| 152 | ({ \ |
| 153 | void * _vec_capacity_v = (void *) (v); \ |
| 154 | uword _vec_capacity_b = (b); \ |
| 155 | _vec_capacity_b = sizeof (vec_header_t) + _vec_round_size (_vec_capacity_b); \ |
| 156 | _vec_capacity_v ? clib_mem_size (_vec_capacity_v - _vec_capacity_b) : 0; \ |
| 157 | }) |
| 158 | |
| 159 | /** \brief Total number of elements that can fit into vector. */ |
| 160 | #define vec_max_len(v) (vec_capacity(v,0) / sizeof (v[0])) |
| 161 | |
Benoît Ganne | 8a4bfda | 2019-07-22 14:21:46 +0200 | [diff] [blame] | 162 | /** \brief Set vector length to a user-defined value */ |
Benoît Ganne | 70d5d4f | 2019-10-22 18:19:00 +0200 | [diff] [blame^] | 163 | #ifndef __COVERITY__ /* Coverity gets confused by ASSERT() */ |
Benoît Ganne | 8a4bfda | 2019-07-22 14:21:46 +0200 | [diff] [blame] | 164 | #define vec_set_len(v, l) do { \ |
| 165 | ASSERT(v); \ |
| 166 | ASSERT((l) <= vec_max_len(v)); \ |
| 167 | _vec_len(v) = (l); \ |
| 168 | } while (0) |
Benoît Ganne | 70d5d4f | 2019-10-22 18:19:00 +0200 | [diff] [blame^] | 169 | #else /* __COVERITY__ */ |
| 170 | #define vec_set_len(v, l) do { \ |
| 171 | _vec_len(v) = (l); \ |
| 172 | } while (0) |
| 173 | #endif /* __COVERITY__ */ |
Benoît Ganne | 8a4bfda | 2019-07-22 14:21:46 +0200 | [diff] [blame] | 174 | |
| 175 | /** \brief Reset vector length to zero |
| 176 | NULL-pointer tolerant |
| 177 | */ |
Benoît Ganne | 8a4bfda | 2019-07-22 14:21:46 +0200 | [diff] [blame] | 178 | #define vec_reset_length(v) do { if (v) vec_set_len (v, 0); } while (0) |
| 179 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | /** \brief End (last data address) of vector. */ |
| 181 | #define vec_end(v) ((v) + vec_len (v)) |
| 182 | |
| 183 | /** \brief True if given pointer is within given vector. */ |
| 184 | #define vec_is_member(v,e) ((e) >= (v) && (e) < vec_end (v)) |
| 185 | |
| 186 | /** \brief Get vector value at index i checking that i is in bounds. */ |
| 187 | #define vec_elt_at_index(v,i) \ |
| 188 | ({ \ |
| 189 | ASSERT ((i) < vec_len (v)); \ |
| 190 | (v) + (i); \ |
| 191 | }) |
| 192 | |
| 193 | /** \brief Get vector value at index i */ |
| 194 | #define vec_elt(v,i) (vec_elt_at_index(v,i))[0] |
| 195 | |
| 196 | /** \brief Vector iterator */ |
| 197 | #define vec_foreach(var,vec) for (var = (vec); var < vec_end (vec); var++) |
| 198 | |
| 199 | /** \brief Vector iterator (reverse) */ |
| 200 | #define vec_foreach_backwards(var,vec) \ |
| 201 | for (var = vec_end (vec) - 1; var >= (vec); var--) |
| 202 | |
| 203 | /** \brief Iterate over vector indices. */ |
| 204 | #define vec_foreach_index(var,v) for ((var) = 0; (var) < vec_len (v); (var)++) |
| 205 | |
Neale Ranns | fc5dda3 | 2018-07-27 00:44:52 -0700 | [diff] [blame] | 206 | /** \brief Iterate over vector indices (reverse). */ |
| 207 | #define vec_foreach_index_backwards(var,v) \ |
| 208 | for ((var) = vec_len((v)) - 1; (var) >= 0; (var)--) |
| 209 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | #endif /* included_clib_vec_bootstrap_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 211 | |
| 212 | /* |
| 213 | * fd.io coding-style-patch-verification: ON |
| 214 | * |
| 215 | * Local Variables: |
| 216 | * eval: (c-set-style "gnu") |
| 217 | * End: |
| 218 | */ |