blob: a94c1a19ba7ba35a4b16bf09982058229b69dc6e [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). */
Damjan Marion299571a2022-03-19 00:07:52 +010058 u8 hdr_size; /**< header size divided by VEC_MIN_ALIGN */
Damjan Marion24738582022-03-31 15:12:20 +020059 u8 log2_align : 7; /**< data alignment */
60 u8 default_heap : 1; /**< vector uses default heap */
Damjan Marion3cfd5292022-03-18 15:48:12 +010061 u8 vpad[2]; /**< pad to 8 bytes */
Ed Warnickecb9cada2015-12-08 15:45:58 -070062 u8 vector_data[0]; /**< Vector data . */
63} vec_header_t;
64
Damjan Marion299571a2022-03-19 00:07:52 +010065#define VEC_MIN_ALIGN 8
Damjan Marion1da361f2022-03-16 17:57:29 +010066
Ed Warnickecb9cada2015-12-08 15:45:58 -070067/** \brief Find the vector header
68
69 Given the user's pointer to a vector, find the corresponding
Dave Barachc3799992016-08-15 11:12:27 -040070 vector header
Ed Warnickecb9cada2015-12-08 15:45:58 -070071
72 @param v pointer to a vector
73 @return pointer to the vector's vector_header_t
74*/
75#define _vec_find(v) ((vec_header_t *) (v) - 1)
Damjan Marion24738582022-03-31 15:12:20 +020076#define _vec_heap(v) (((void **) (_vec_find (v)))[-1])
Ed Warnickecb9cada2015-12-08 15:45:58 -070077
Damjan Marion299571a2022-03-19 00:07:52 +010078always_inline uword __vec_align (uword data_align, uword configuered_align);
79always_inline uword __vec_elt_sz (uword elt_sz, int is_void);
80
Ed Warnickecb9cada2015-12-08 15:45:58 -070081#define _vec_round_size(s) \
82 (((s) + sizeof (uword) - 1) &~ (sizeof (uword) - 1))
Damjan Marion299571a2022-03-19 00:07:52 +010083#define _vec_is_void(P) \
84 __builtin_types_compatible_p (__typeof__ ((P)[0]), void)
85#define _vec_elt_sz(V) __vec_elt_sz (sizeof ((V)[0]), _vec_is_void (V))
86#define _vec_align(V, A) __vec_align (__alignof__((V)[0]), A)
Ed Warnickecb9cada2015-12-08 15:45:58 -070087
Damjan Marion79934e82022-04-05 12:40:31 +020088always_inline __clib_nosanitize_addr uword
Damjan Marion5c45d1c2022-03-17 15:32:56 +010089vec_get_header_size (void *v)
90{
Damjan Marion299571a2022-03-19 00:07:52 +010091 uword header_size = _vec_find (v)->hdr_size * VEC_MIN_ALIGN;
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 (rvalue-only, NULL tolerant)
Dave Barachc3799992016-08-15 11:12:27 -0400120
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 vec_len (v) checks for NULL, but cannot be used as an lvalue.
122 If in doubt, use vec_len...
123*/
124
Damjan Marion8bea5892022-04-04 22:40:45 +0200125static_always_inline u32
126__vec_len (void *v)
127{
128 return _vec_find (v)->len;
129}
130
131#define _vec_len(v) __vec_len ((void *) (v))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132#define vec_len(v) ((v) ? _vec_len(v) : 0)
Damjan Marion8bea5892022-04-04 22:40:45 +0200133
Dave Barache09ae012020-08-19 06:59:53 -0400134u32 vec_len_not_inline (void *v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136/** \brief Number of data bytes in vector. */
137
138#define vec_bytes(v) (vec_len (v) * sizeof (v[0]))
139
Florin Coras41a1bbf2022-03-11 10:58:55 -0800140/**
141 * Return size of memory allocated for the vector
142 *
143 * @param v vector
Florin Coras41a1bbf2022-03-11 10:58:55 -0800144 * @return memory size allocated for the vector
145 */
Damjan Marion5c45d1c2022-03-17 15:32:56 +0100146
Damjan Marion299571a2022-03-19 00:07:52 +0100147uword vec_mem_size (void *v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148
Florin Coras41a1bbf2022-03-11 10:58:55 -0800149/**
150 * Number of elements that can fit into generic vector
151 *
152 * @param v vector
153 * @param b extra header bytes
154 * @return number of elements that can fit into vector
155 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156
Damjan Marion5c45d1c2022-03-17 15:32:56 +0100157always_inline uword
Damjan Marion299571a2022-03-19 00:07:52 +0100158vec_max_bytes (void *v)
Damjan Marion5c45d1c2022-03-17 15:32:56 +0100159{
Damjan Marion299571a2022-03-19 00:07:52 +0100160 return v ? vec_mem_size (v) - vec_get_header_size (v) : 0;
Damjan Marion5c45d1c2022-03-17 15:32:56 +0100161}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162
Damjan Marion299571a2022-03-19 00:07:52 +0100163always_inline uword
164_vec_max_len (void *v, uword elt_sz)
165{
166 return vec_max_bytes (v) / elt_sz;
167}
168
169#define vec_max_len(v) _vec_max_len (v, _vec_elt_sz (v))
Damjan Marion5c45d1c2022-03-17 15:32:56 +0100170
171always_inline void
Damjan Marion299571a2022-03-19 00:07:52 +0100172_vec_set_len (void *v, uword len, uword elt_sz)
Damjan Marion5c45d1c2022-03-17 15:32:56 +0100173{
174 ASSERT (v);
Damjan Marion299571a2022-03-19 00:07:52 +0100175 ASSERT (len <= _vec_max_len (v, elt_sz));
176 uword old_len = _vec_len (v);
Damjan Marion5c45d1c2022-03-17 15:32:56 +0100177
Damjan Marion299571a2022-03-19 00:07:52 +0100178 if (len > old_len)
Damjan Marion79934e82022-04-05 12:40:31 +0200179 clib_mem_unpoison (v + old_len * elt_sz, (len - old_len) * elt_sz);
Damjan Marion299571a2022-03-19 00:07:52 +0100180 else if (len > old_len)
Damjan Marion79934e82022-04-05 12:40:31 +0200181 clib_mem_poison (v + len * elt_sz, (old_len - len) * elt_sz);
Damjan Marion299571a2022-03-19 00:07:52 +0100182
Damjan Marion8bea5892022-04-04 22:40:45 +0200183 _vec_find (v)->len = len;
Damjan Marion5c45d1c2022-03-17 15:32:56 +0100184}
185
Damjan Marion299571a2022-03-19 00:07:52 +0100186#define vec_set_len(v, l) _vec_set_len ((void *) v, l, _vec_elt_sz (v))
Damjan Marion8bea5892022-04-04 22:40:45 +0200187#define vec_inc_len(v, l) vec_set_len (v, _vec_len (v) + (l))
188#define vec_dec_len(v, l) vec_set_len (v, _vec_len (v) - (l))
Benoît Ganne8a4bfda2019-07-22 14:21:46 +0200189
190/** \brief Reset vector length to zero
191 NULL-pointer tolerant
192*/
Benoît Ganne8a4bfda2019-07-22 14:21:46 +0200193#define vec_reset_length(v) do { if (v) vec_set_len (v, 0); } while (0)
194
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195/** \brief End (last data address) of vector. */
196#define vec_end(v) ((v) + vec_len (v))
197
198/** \brief True if given pointer is within given vector. */
199#define vec_is_member(v,e) ((e) >= (v) && (e) < vec_end (v))
200
201/** \brief Get vector value at index i checking that i is in bounds. */
202#define vec_elt_at_index(v,i) \
203({ \
204 ASSERT ((i) < vec_len (v)); \
205 (v) + (i); \
206})
207
208/** \brief Get vector value at index i */
209#define vec_elt(v,i) (vec_elt_at_index(v,i))[0]
210
211/** \brief Vector iterator */
212#define vec_foreach(var,vec) for (var = (vec); var < vec_end (vec); var++)
213
214/** \brief Vector iterator (reverse) */
Neale Ranns9d33cfc2022-01-09 13:24:47 +0000215#define vec_foreach_backwards(var, vec) \
216 if (vec) \
217 for (var = vec_end (vec) - 1; var >= (vec); var--)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218
219/** \brief Iterate over vector indices. */
220#define vec_foreach_index(var,v) for ((var) = 0; (var) < vec_len (v); (var)++)
221
Neale Rannsfc5dda32018-07-27 00:44:52 -0700222/** \brief Iterate over vector indices (reverse). */
Neale Ranns9d33cfc2022-01-09 13:24:47 +0000223#define vec_foreach_index_backwards(var, v) \
224 if (v) \
225 for ((var) = vec_len ((v)) - 1; (var) >= 0; (var)--)
Neale Rannsfc5dda32018-07-27 00:44:52 -0700226
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227#endif /* included_clib_vec_bootstrap_h */
Dave Barachc3799992016-08-15 11:12:27 -0400228
229/*
230 * fd.io coding-style-patch-verification: ON
231 *
232 * Local Variables:
233 * eval: (c-set-style "gnu")
234 * End:
235 */