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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
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:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
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.
38 #ifndef included_clib_vec_bootstrap_h
39 #define included_clib_vec_bootstrap_h
42 Vector bootstrap header file
45 /* Bootstrap include so that #include <vppinfra/mem.h> can include e.g.
46 <vppinfra/mheap.h> which depends on <vppinfra/vec.h>. */
48 /** \brief vector header structure
50 Bookkeeping header preceding vector elements in memory.
51 User header information may preceed standard vec header.
52 If you change u32 len -> u64 len, single vectors can
53 exceed 2**32 elements. Clib heaps are vectors. */
57 u32 len; /**< Number of elements in vector (NOT its allocated length). */
58 u8 numa_id; /**< NUMA id */
59 u8 hdr_size; /**< header size divided by VEC_HEADER_ROUND */
60 u8 log2_align; /**< data alignment */
61 u8 vpad[1]; /**< pad to 8 bytes */
62 u8 vector_data[0]; /**< Vector data . */
65 #define VEC_NUMA_UNSPECIFIED (0xFF)
67 #define VEC_HEADER_ROUND 8
69 /** \brief Find the vector header
71 Given the user's pointer to a vector, find the corresponding
74 @param v pointer to a vector
75 @return pointer to the vector's vector_header_t
77 #define _vec_find(v) ((vec_header_t *) (v) - 1)
79 #define _vec_round_size(s) \
80 (((s) + sizeof (uword) - 1) &~ (sizeof (uword) - 1))
83 vec_header_bytes (uword header_bytes)
85 return round_pow2 (header_bytes + sizeof (vec_header_t), VEC_HEADER_ROUND);
89 vec_get_header_size (void *v)
91 uword header_size = _vec_find (v)->hdr_size * VEC_HEADER_ROUND;
92 ASSERT (header_size >= vec_header_bytes (0));
96 /** \brief Find a user vector header
98 Finds the user header of a vector with unspecified alignment given
99 the user pointer to the vector.
103 vec_header (void *v, uword header_bytes)
105 return v - vec_header_bytes (header_bytes);
108 /** \brief Find the end of user vector header
110 Finds the end of the user header of a vector with unspecified
111 alignment given the user pointer to the vector.
115 vec_header_end (void *v, uword header_bytes)
117 return v + vec_header_bytes (header_bytes);
121 vec_aligned_header_bytes (uword header_bytes, uword align)
123 return round_pow2 (header_bytes + sizeof (vec_header_t), align);
127 vec_aligned_header (void *v, uword header_bytes, uword align)
129 return v - vec_aligned_header_bytes (header_bytes, align);
133 vec_aligned_header_end (void *v, uword header_bytes, uword align)
135 return v + vec_aligned_header_bytes (header_bytes, align);
139 /** \brief Number of elements in vector (lvalue-capable)
141 _vec_len (v) does not check for null, but can be used as an lvalue
142 (e.g. _vec_len (v) = 99).
145 #define _vec_len(v) (_vec_find(v)->len)
147 /** \brief Number of elements in vector (rvalue-only, NULL tolerant)
149 vec_len (v) checks for NULL, but cannot be used as an lvalue.
150 If in doubt, use vec_len...
153 #define vec_len(v) ((v) ? _vec_len(v) : 0)
154 u32 vec_len_not_inline (void *v);
156 /** \brief Vector's NUMA id (lvalue-capable)
158 _vec_numa(v) does not check for null, but can be used as an lvalue
159 (e.g. _vec_numa(v) = 1).
162 #define _vec_numa(v) (_vec_find(v)->numa_id)
164 /** \brief Return vector's NUMA ID (rvalue-only, NULL tolerant)
165 vec_numa(v) checks for NULL, but cannot be used as an lvalue.
167 #define vec_numa(v) ((v) ? _vec_numa(v) : 0)
170 /** \brief Number of data bytes in vector. */
172 #define vec_bytes(v) (vec_len (v) * sizeof (v[0]))
175 * Return size of memory allocated for the vector
178 * @return memory size allocated for the vector
182 vec_mem_size (void *v)
184 return v ? clib_mem_size (v - vec_get_header_size (v)) : 0;
188 * Number of elements that can fit into generic vector
191 * @param b extra header bytes
192 * @return number of elements that can fit into vector
196 _vec_max_len (void *v, uword elt_size)
198 return v ? vec_mem_size (v) / elt_size : 0;
201 #define vec_max_len(v) _vec_max_len (v, sizeof ((v)[0]))
204 _vec_set_len (void *v, uword len, uword elt_size)
207 ASSERT (len <= vec_max_len (v));
209 CLIB_MEM_POISON_LEN (v, _vec_len (v) * elt_size, len * elt_size);
213 #define vec_set_len(v, l) _vec_set_len ((void *) v, l, sizeof ((v)[0]))
215 /** \brief Reset vector length to zero
216 NULL-pointer tolerant
218 #define vec_reset_length(v) do { if (v) vec_set_len (v, 0); } while (0)
220 /** \brief End (last data address) of vector. */
221 #define vec_end(v) ((v) + vec_len (v))
223 /** \brief True if given pointer is within given vector. */
224 #define vec_is_member(v,e) ((e) >= (v) && (e) < vec_end (v))
226 /** \brief Get vector value at index i checking that i is in bounds. */
227 #define vec_elt_at_index(v,i) \
229 ASSERT ((i) < vec_len (v)); \
233 /** \brief Get vector value at index i */
234 #define vec_elt(v,i) (vec_elt_at_index(v,i))[0]
236 /** \brief Vector iterator */
237 #define vec_foreach(var,vec) for (var = (vec); var < vec_end (vec); var++)
239 /** \brief Vector iterator (reverse) */
240 #define vec_foreach_backwards(var, vec) \
242 for (var = vec_end (vec) - 1; var >= (vec); var--)
244 /** \brief Iterate over vector indices. */
245 #define vec_foreach_index(var,v) for ((var) = 0; (var) < vec_len (v); (var)++)
247 /** \brief Iterate over vector indices (reverse). */
248 #define vec_foreach_index_backwards(var, v) \
250 for ((var) = vec_len ((v)) - 1; (var) >= 0; (var)--)
252 /** \brief return the NUMA index for a vector */
254 vec_get_numa (void *v)
263 #endif /* included_clib_vec_bootstrap_h */
266 * fd.io coding-style-patch-verification: ON
269 * eval: (c-set-style "gnu")