blob: 5dc1b6af5409eeec53b96a216c0e15401e7503cf [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 u32 len; /**< Number of elements in vector (NOT its allocated length). */
Dave Baracha690fdb2020-01-21 12:34:55 -050058 u8 numa_id; /**< NUMA id */
Damjan Marion1da361f2022-03-16 17:57:29 +010059 u8 hdr_size; /**< header size divided by VEC_HEADER_ROUND */
60 u8 log2_align; /**< data alignment */
61 u8 vpad[1]; /**< pad to 8 bytes */
Ed Warnickecb9cada2015-12-08 15:45:58 -070062 u8 vector_data[0]; /**< Vector data . */
63} vec_header_t;
64
Dave Baracha690fdb2020-01-21 12:34:55 -050065#define VEC_NUMA_UNSPECIFIED (0xFF)
66
Damjan Marion1da361f2022-03-16 17:57:29 +010067#define VEC_HEADER_ROUND 8
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{
Damjan Marion1da361f2022-03-16 17:57:29 +010085 return round_pow2 (header_bytes + sizeof (vec_header_t), VEC_HEADER_ROUND);
Dave Barachc3799992016-08-15 11:12:27 -040086}
Ed Warnickecb9cada2015-12-08 15:45:58 -070087
Damjan Marion5c45d1c2022-03-17 15:32:56 +010088always_inline uword
89vec_get_header_size (void *v)
90{
91 uword header_size = _vec_find (v)->hdr_size * VEC_HEADER_ROUND;
Damjan Marion5c45d1c2022-03-17 15:32:56 +010092 return header_size;
93}
94
Ed Warnickecb9cada2015-12-08 15:45:58 -070095/** \brief Find a user vector header
Dave Barachc3799992016-08-15 11:12:27 -040096
Ed Warnickecb9cada2015-12-08 15:45:58 -070097 Finds the user header of a vector with unspecified alignment given
98 the user pointer to the vector.
99*/
100
101always_inline void *
Damjan Mariona4a28f02022-03-17 15:46:25 +0100102vec_header (void *v)
Dave Barachc3799992016-08-15 11:12:27 -0400103{
Damjan Mariona4a28f02022-03-17 15:46:25 +0100104 return v ? v - vec_get_header_size (v) : 0;
Dave Barachc3799992016-08-15 11:12:27 -0400105}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
107/** \brief Find the end of user vector header
Dave Barachc3799992016-08-15 11:12:27 -0400108
109 Finds the end of the user header of a vector with unspecified
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 alignment given the user pointer to the vector.
111*/
112
113always_inline void *
Damjan Mariona4a28f02022-03-17 15:46:25 +0100114vec_header_end (void *v)
Dave Barachc3799992016-08-15 11:12:27 -0400115{
Damjan Mariona4a28f02022-03-17 15:46:25 +0100116 return v + vec_get_header_size (v);
Dave Barachc3799992016-08-15 11:12:27 -0400117}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119/** \brief Number of elements in vector (lvalue-capable)
120
Dave Baracha690fdb2020-01-21 12:34:55 -0500121 _vec_len (v) does not check for null, but can be used as an lvalue
Dave Barachc3799992016-08-15 11:12:27 -0400122 (e.g. _vec_len (v) = 99).
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123*/
124
125#define _vec_len(v) (_vec_find(v)->len)
126
127/** \brief Number of elements in vector (rvalue-only, NULL tolerant)
Dave Barachc3799992016-08-15 11:12:27 -0400128
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129 vec_len (v) checks for NULL, but cannot be used as an lvalue.
130 If in doubt, use vec_len...
131*/
132
133#define vec_len(v) ((v) ? _vec_len(v) : 0)
Dave Barache09ae012020-08-19 06:59:53 -0400134u32 vec_len_not_inline (void *v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135
Dave Baracha690fdb2020-01-21 12:34:55 -0500136/** \brief Vector's NUMA id (lvalue-capable)
137
138 _vec_numa(v) does not check for null, but can be used as an lvalue
139 (e.g. _vec_numa(v) = 1).
140*/
141
142#define _vec_numa(v) (_vec_find(v)->numa_id)
143
144/** \brief Return vector's NUMA ID (rvalue-only, NULL tolerant)
145 vec_numa(v) checks for NULL, but cannot be used as an lvalue.
146*/
147#define vec_numa(v) ((v) ? _vec_numa(v) : 0)
148
149
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150/** \brief Number of data bytes in vector. */
151
152#define vec_bytes(v) (vec_len (v) * sizeof (v[0]))
153
Florin Coras41a1bbf2022-03-11 10:58:55 -0800154/**
155 * Return size of memory allocated for the vector
156 *
157 * @param v vector
Florin Coras41a1bbf2022-03-11 10:58:55 -0800158 * @return memory size allocated for the vector
159 */
Damjan Marion5c45d1c2022-03-17 15:32:56 +0100160
161always_inline uword
162vec_mem_size (void *v)
163{
164 return v ? clib_mem_size (v - vec_get_header_size (v)) : 0;
165}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166
Florin Coras41a1bbf2022-03-11 10:58:55 -0800167/**
168 * Number of elements that can fit into generic vector
169 *
170 * @param v vector
171 * @param b extra header bytes
172 * @return number of elements that can fit into vector
173 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174
Damjan Marion5c45d1c2022-03-17 15:32:56 +0100175always_inline uword
176_vec_max_len (void *v, uword elt_size)
177{
178 return v ? vec_mem_size (v) / elt_size : 0;
179}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180
Damjan Marion5c45d1c2022-03-17 15:32:56 +0100181#define vec_max_len(v) _vec_max_len (v, sizeof ((v)[0]))
182
183always_inline void
184_vec_set_len (void *v, uword len, uword elt_size)
185{
186 ASSERT (v);
187 ASSERT (len <= vec_max_len (v));
188
189 CLIB_MEM_POISON_LEN (v, _vec_len (v) * elt_size, len * elt_size);
190 _vec_len (v) = len;
191}
192
193#define vec_set_len(v, l) _vec_set_len ((void *) v, l, sizeof ((v)[0]))
Benoît Ganne8a4bfda2019-07-22 14:21:46 +0200194
195/** \brief Reset vector length to zero
196 NULL-pointer tolerant
197*/
Benoît Ganne8a4bfda2019-07-22 14:21:46 +0200198#define vec_reset_length(v) do { if (v) vec_set_len (v, 0); } while (0)
199
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200/** \brief End (last data address) of vector. */
201#define vec_end(v) ((v) + vec_len (v))
202
203/** \brief True if given pointer is within given vector. */
204#define vec_is_member(v,e) ((e) >= (v) && (e) < vec_end (v))
205
206/** \brief Get vector value at index i checking that i is in bounds. */
207#define vec_elt_at_index(v,i) \
208({ \
209 ASSERT ((i) < vec_len (v)); \
210 (v) + (i); \
211})
212
213/** \brief Get vector value at index i */
214#define vec_elt(v,i) (vec_elt_at_index(v,i))[0]
215
216/** \brief Vector iterator */
217#define vec_foreach(var,vec) for (var = (vec); var < vec_end (vec); var++)
218
219/** \brief Vector iterator (reverse) */
Neale Ranns9d33cfc2022-01-09 13:24:47 +0000220#define vec_foreach_backwards(var, vec) \
221 if (vec) \
222 for (var = vec_end (vec) - 1; var >= (vec); var--)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223
224/** \brief Iterate over vector indices. */
225#define vec_foreach_index(var,v) for ((var) = 0; (var) < vec_len (v); (var)++)
226
Neale Rannsfc5dda32018-07-27 00:44:52 -0700227/** \brief Iterate over vector indices (reverse). */
Neale Ranns9d33cfc2022-01-09 13:24:47 +0000228#define vec_foreach_index_backwards(var, v) \
229 if (v) \
230 for ((var) = vec_len ((v)) - 1; (var) >= 0; (var)--)
Neale Rannsfc5dda32018-07-27 00:44:52 -0700231
Dave Baracha690fdb2020-01-21 12:34:55 -0500232/** \brief return the NUMA index for a vector */
233always_inline uword
234vec_get_numa (void *v)
235{
236 vec_header_t *vh;
237 if (v == 0)
238 return 0;
239 vh = _vec_find (v);
240 return vh->numa_id;
241}
242
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243#endif /* included_clib_vec_bootstrap_h */
Dave Barachc3799992016-08-15 11:12:27 -0400244
245/*
246 * fd.io coding-style-patch-verification: ON
247 *
248 * Local Variables:
249 * eval: (c-set-style "gnu")
250 * End:
251 */