blob: 879703c8f6a0c29acf0b3e90109ea8840d211ebe [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Barachc3799992016-08-15 11:12:27 -040041/** \file
Paul Vinciguerraec11b132018-09-24 05:25:00 -070042 Vector bootstrap header file
Ed Warnickecb9cada2015-12-08 15:45:58 -070043*/
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 Vinciguerraec11b132018-09-24 05:25:00 -070050 Bookkeeping header preceding vector elements in memory.
Dave Barachc3799992016-08-15 11:12:27 -040051 User header information may preceed standard vec header.
Ed Warnickecb9cada2015-12-08 15:45:58 -070052 If you change u32 len -> u64 len, single vectors can
53 exceed 2**32 elements. Clib heaps are vectors. */
54
Dave Barachc3799992016-08-15 11:12:27 -040055typedef struct
56{
Ed Warnickecb9cada2015-12-08 15:45:58 -070057#if CLIB_VEC64 > 0
58 u64 len;
59#else
60 u32 len; /**< Number of elements in vector (NOT its allocated length). */
Dave Baracha690fdb2020-01-21 12:34:55 -050061 u8 numa_id; /**< NUMA id */
62 u8 vpad[3]; /**< pad to 8 bytes */
Ed Warnickecb9cada2015-12-08 15:45:58 -070063#endif
64 u8 vector_data[0]; /**< Vector data . */
65} vec_header_t;
66
Dave Baracha690fdb2020-01-21 12:34:55 -050067#define VEC_NUMA_UNSPECIFIED (0xFF)
68
Ed Warnickecb9cada2015-12-08 15:45:58 -070069/** \brief Find the vector header
70
71 Given the user's pointer to a vector, find the corresponding
Dave Barachc3799992016-08-15 11:12:27 -040072 vector header
Ed Warnickecb9cada2015-12-08 15:45:58 -070073
74 @param v pointer to a vector
75 @return pointer to the vector's vector_header_t
76*/
77#define _vec_find(v) ((vec_header_t *) (v) - 1)
78
79#define _vec_round_size(s) \
80 (((s) + sizeof (uword) - 1) &~ (sizeof (uword) - 1))
81
82always_inline uword
83vec_header_bytes (uword header_bytes)
Dave Barachc3799992016-08-15 11:12:27 -040084{
85 return round_pow2 (header_bytes + sizeof (vec_header_t),
86 sizeof (vec_header_t));
87}
Ed Warnickecb9cada2015-12-08 15:45:58 -070088
89/** \brief Find a user vector header
Dave Barachc3799992016-08-15 11:12:27 -040090
Ed Warnickecb9cada2015-12-08 15:45:58 -070091 Finds the user header of a vector with unspecified alignment given
92 the user pointer to the vector.
93*/
94
95always_inline void *
Dave Barachc3799992016-08-15 11:12:27 -040096vec_header (void *v, uword header_bytes)
97{
98 return v - vec_header_bytes (header_bytes);
99}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
101/** \brief Find the end of user vector header
Dave Barachc3799992016-08-15 11:12:27 -0400102
103 Finds the end of the user header of a vector with unspecified
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104 alignment given the user pointer to the vector.
105*/
106
107always_inline void *
Dave Barachc3799992016-08-15 11:12:27 -0400108vec_header_end (void *v, uword header_bytes)
109{
110 return v + vec_header_bytes (header_bytes);
111}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112
113always_inline uword
114vec_aligned_header_bytes (uword header_bytes, uword align)
115{
116 return round_pow2 (header_bytes + sizeof (vec_header_t), align);
117}
118
119always_inline void *
Dave Barachc3799992016-08-15 11:12:27 -0400120vec_aligned_header (void *v, uword header_bytes, uword align)
121{
122 return v - vec_aligned_header_bytes (header_bytes, align);
123}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124
125always_inline void *
Dave Barachc3799992016-08-15 11:12:27 -0400126vec_aligned_header_end (void *v, uword header_bytes, uword align)
127{
128 return v + vec_aligned_header_bytes (header_bytes, align);
129}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130
131
132/** \brief Number of elements in vector (lvalue-capable)
133
Dave Baracha690fdb2020-01-21 12:34:55 -0500134 _vec_len (v) does not check for null, but can be used as an lvalue
Dave Barachc3799992016-08-15 11:12:27 -0400135 (e.g. _vec_len (v) = 99).
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136*/
137
138#define _vec_len(v) (_vec_find(v)->len)
139
140/** \brief Number of elements in vector (rvalue-only, NULL tolerant)
Dave Barachc3799992016-08-15 11:12:27 -0400141
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142 vec_len (v) checks for NULL, but cannot be used as an lvalue.
143 If in doubt, use vec_len...
144*/
145
146#define vec_len(v) ((v) ? _vec_len(v) : 0)
147
Dave Baracha690fdb2020-01-21 12:34:55 -0500148/** \brief Vector's NUMA id (lvalue-capable)
149
150 _vec_numa(v) does not check for null, but can be used as an lvalue
151 (e.g. _vec_numa(v) = 1).
152*/
153
154#define _vec_numa(v) (_vec_find(v)->numa_id)
155
156/** \brief Return vector's NUMA ID (rvalue-only, NULL tolerant)
157 vec_numa(v) checks for NULL, but cannot be used as an lvalue.
158*/
159#define vec_numa(v) ((v) ? _vec_numa(v) : 0)
160
161
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162/** \brief Number of data bytes in vector. */
163
164#define vec_bytes(v) (vec_len (v) * sizeof (v[0]))
165
166/** \brief Total number of bytes that can fit in vector with current allocation. */
167
168#define vec_capacity(v,b) \
169({ \
170 void * _vec_capacity_v = (void *) (v); \
171 uword _vec_capacity_b = (b); \
172 _vec_capacity_b = sizeof (vec_header_t) + _vec_round_size (_vec_capacity_b); \
173 _vec_capacity_v ? clib_mem_size (_vec_capacity_v - _vec_capacity_b) : 0; \
174})
175
176/** \brief Total number of elements that can fit into vector. */
177#define vec_max_len(v) (vec_capacity(v,0) / sizeof (v[0]))
178
Benoît Ganne8a4bfda2019-07-22 14:21:46 +0200179/** \brief Set vector length to a user-defined value */
Benoît Ganne70d5d4f2019-10-22 18:19:00 +0200180#ifndef __COVERITY__ /* Coverity gets confused by ASSERT() */
Benoît Ganne8a4bfda2019-07-22 14:21:46 +0200181#define vec_set_len(v, l) do { \
182 ASSERT(v); \
183 ASSERT((l) <= vec_max_len(v)); \
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200184 CLIB_MEM_POISON_LEN((void *)(v), _vec_len(v) * sizeof((v)[0]), (l) * sizeof((v)[0])); \
Benoît Ganne8a4bfda2019-07-22 14:21:46 +0200185 _vec_len(v) = (l); \
186} while (0)
Benoît Ganne70d5d4f2019-10-22 18:19:00 +0200187#else /* __COVERITY__ */
188#define vec_set_len(v, l) do { \
189 _vec_len(v) = (l); \
190} while (0)
191#endif /* __COVERITY__ */
Benoît Ganne8a4bfda2019-07-22 14:21:46 +0200192
193/** \brief Reset vector length to zero
194 NULL-pointer tolerant
195*/
Benoît Ganne8a4bfda2019-07-22 14:21:46 +0200196#define vec_reset_length(v) do { if (v) vec_set_len (v, 0); } while (0)
197
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198/** \brief End (last data address) of vector. */
199#define vec_end(v) ((v) + vec_len (v))
200
201/** \brief True if given pointer is within given vector. */
202#define vec_is_member(v,e) ((e) >= (v) && (e) < vec_end (v))
203
204/** \brief Get vector value at index i checking that i is in bounds. */
205#define vec_elt_at_index(v,i) \
206({ \
207 ASSERT ((i) < vec_len (v)); \
208 (v) + (i); \
209})
210
211/** \brief Get vector value at index i */
212#define vec_elt(v,i) (vec_elt_at_index(v,i))[0]
213
214/** \brief Vector iterator */
215#define vec_foreach(var,vec) for (var = (vec); var < vec_end (vec); var++)
216
217/** \brief Vector iterator (reverse) */
218#define vec_foreach_backwards(var,vec) \
219for (var = vec_end (vec) - 1; var >= (vec); var--)
220
221/** \brief Iterate over vector indices. */
222#define vec_foreach_index(var,v) for ((var) = 0; (var) < vec_len (v); (var)++)
223
Neale Rannsfc5dda32018-07-27 00:44:52 -0700224/** \brief Iterate over vector indices (reverse). */
225#define vec_foreach_index_backwards(var,v) \
226 for ((var) = vec_len((v)) - 1; (var) >= 0; (var)--)
227
Dave Baracha690fdb2020-01-21 12:34:55 -0500228/** \brief return the NUMA index for a vector */
229always_inline uword
230vec_get_numa (void *v)
231{
232 vec_header_t *vh;
233 if (v == 0)
234 return 0;
235 vh = _vec_find (v);
236 return vh->numa_id;
237}
238
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239#endif /* included_clib_vec_bootstrap_h */
Dave Barachc3799992016-08-15 11:12:27 -0400240
241/*
242 * fd.io coding-style-patch-verification: ON
243 *
244 * Local Variables:
245 * eval: (c-set-style "gnu")
246 * End:
247 */