blob: bb6ac84e73486ff6321f691a42cdb6acd63b5852 [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 Marion1da361f2022-03-16 17:57:29 +010058 u8 hdr_size; /**< header size divided by VEC_HEADER_ROUND */
59 u8 log2_align; /**< data alignment */
Damjan Marion3cfd5292022-03-18 15:48:12 +010060 u8 vpad[2]; /**< pad to 8 bytes */
Ed Warnickecb9cada2015-12-08 15:45:58 -070061 u8 vector_data[0]; /**< Vector data . */
62} vec_header_t;
63
Damjan Marion1da361f2022-03-16 17:57:29 +010064#define VEC_HEADER_ROUND 8
65
Ed Warnickecb9cada2015-12-08 15:45:58 -070066/** \brief Find the vector header
67
68 Given the user's pointer to a vector, find the corresponding
Dave Barachc3799992016-08-15 11:12:27 -040069 vector header
Ed Warnickecb9cada2015-12-08 15:45:58 -070070
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
79always_inline uword
80vec_header_bytes (uword header_bytes)
Dave Barachc3799992016-08-15 11:12:27 -040081{
Damjan Marion1da361f2022-03-16 17:57:29 +010082 return round_pow2 (header_bytes + sizeof (vec_header_t), VEC_HEADER_ROUND);
Dave Barachc3799992016-08-15 11:12:27 -040083}
Ed Warnickecb9cada2015-12-08 15:45:58 -070084
Damjan Marion5c45d1c2022-03-17 15:32:56 +010085always_inline uword
86vec_get_header_size (void *v)
87{
88 uword header_size = _vec_find (v)->hdr_size * VEC_HEADER_ROUND;
Damjan Marion5c45d1c2022-03-17 15:32:56 +010089 return header_size;
90}
91
Ed Warnickecb9cada2015-12-08 15:45:58 -070092/** \brief Find a user vector header
Dave Barachc3799992016-08-15 11:12:27 -040093
Ed Warnickecb9cada2015-12-08 15:45:58 -070094 Finds the user header of a vector with unspecified alignment given
95 the user pointer to the vector.
96*/
97
98always_inline void *
Damjan Mariona4a28f02022-03-17 15:46:25 +010099vec_header (void *v)
Dave Barachc3799992016-08-15 11:12:27 -0400100{
Damjan Mariona4a28f02022-03-17 15:46:25 +0100101 return v ? v - vec_get_header_size (v) : 0;
Dave Barachc3799992016-08-15 11:12:27 -0400102}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103
104/** \brief Find the end of user vector header
Dave Barachc3799992016-08-15 11:12:27 -0400105
106 Finds the end of the user header of a vector with unspecified
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 alignment given the user pointer to the vector.
108*/
109
110always_inline void *
Damjan Mariona4a28f02022-03-17 15:46:25 +0100111vec_header_end (void *v)
Dave Barachc3799992016-08-15 11:12:27 -0400112{
Damjan Mariona4a28f02022-03-17 15:46:25 +0100113 return v + vec_get_header_size (v);
Dave Barachc3799992016-08-15 11:12:27 -0400114}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116/** \brief Number of elements in vector (lvalue-capable)
117
Dave Baracha690fdb2020-01-21 12:34:55 -0500118 _vec_len (v) does not check for null, but can be used as an lvalue
Dave Barachc3799992016-08-15 11:12:27 -0400119 (e.g. _vec_len (v) = 99).
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120*/
121
122#define _vec_len(v) (_vec_find(v)->len)
123
124/** \brief Number of elements in vector (rvalue-only, NULL tolerant)
Dave Barachc3799992016-08-15 11:12:27 -0400125
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126 vec_len (v) checks for NULL, but cannot be used as an lvalue.
127 If in doubt, use vec_len...
128*/
129
130#define vec_len(v) ((v) ? _vec_len(v) : 0)
Dave Barache09ae012020-08-19 06:59:53 -0400131u32 vec_len_not_inline (void *v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133/** \brief Number of data bytes in vector. */
134
135#define vec_bytes(v) (vec_len (v) * sizeof (v[0]))
136
Florin Coras41a1bbf2022-03-11 10:58:55 -0800137/**
138 * Return size of memory allocated for the vector
139 *
140 * @param v vector
Florin Coras41a1bbf2022-03-11 10:58:55 -0800141 * @return memory size allocated for the vector
142 */
Damjan Marion5c45d1c2022-03-17 15:32:56 +0100143
144always_inline uword
145vec_mem_size (void *v)
146{
147 return v ? clib_mem_size (v - vec_get_header_size (v)) : 0;
148}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
Florin Coras41a1bbf2022-03-11 10:58:55 -0800150/**
151 * Number of elements that can fit into generic vector
152 *
153 * @param v vector
154 * @param b extra header bytes
155 * @return number of elements that can fit into vector
156 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157
Damjan Marion5c45d1c2022-03-17 15:32:56 +0100158always_inline uword
159_vec_max_len (void *v, uword elt_size)
160{
161 return v ? vec_mem_size (v) / elt_size : 0;
162}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163
Damjan Marion5c45d1c2022-03-17 15:32:56 +0100164#define vec_max_len(v) _vec_max_len (v, sizeof ((v)[0]))
165
166always_inline void
167_vec_set_len (void *v, uword len, uword elt_size)
168{
169 ASSERT (v);
170 ASSERT (len <= vec_max_len (v));
171
172 CLIB_MEM_POISON_LEN (v, _vec_len (v) * elt_size, len * elt_size);
173 _vec_len (v) = len;
174}
175
176#define vec_set_len(v, l) _vec_set_len ((void *) v, l, sizeof ((v)[0]))
Benoît Ganne8a4bfda2019-07-22 14:21:46 +0200177
178/** \brief Reset vector length to zero
179 NULL-pointer tolerant
180*/
Benoît Ganne8a4bfda2019-07-22 14:21:46 +0200181#define vec_reset_length(v) do { if (v) vec_set_len (v, 0); } while (0)
182
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183/** \brief End (last data address) of vector. */
184#define vec_end(v) ((v) + vec_len (v))
185
186/** \brief True if given pointer is within given vector. */
187#define vec_is_member(v,e) ((e) >= (v) && (e) < vec_end (v))
188
189/** \brief Get vector value at index i checking that i is in bounds. */
190#define vec_elt_at_index(v,i) \
191({ \
192 ASSERT ((i) < vec_len (v)); \
193 (v) + (i); \
194})
195
196/** \brief Get vector value at index i */
197#define vec_elt(v,i) (vec_elt_at_index(v,i))[0]
198
199/** \brief Vector iterator */
200#define vec_foreach(var,vec) for (var = (vec); var < vec_end (vec); var++)
201
202/** \brief Vector iterator (reverse) */
Neale Ranns9d33cfc2022-01-09 13:24:47 +0000203#define vec_foreach_backwards(var, vec) \
204 if (vec) \
205 for (var = vec_end (vec) - 1; var >= (vec); var--)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206
207/** \brief Iterate over vector indices. */
208#define vec_foreach_index(var,v) for ((var) = 0; (var) < vec_len (v); (var)++)
209
Neale Rannsfc5dda32018-07-27 00:44:52 -0700210/** \brief Iterate over vector indices (reverse). */
Neale Ranns9d33cfc2022-01-09 13:24:47 +0000211#define vec_foreach_index_backwards(var, v) \
212 if (v) \
213 for ((var) = vec_len ((v)) - 1; (var) >= 0; (var)--)
Neale Rannsfc5dda32018-07-27 00:44:52 -0700214
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215#endif /* included_clib_vec_bootstrap_h */
Dave Barachc3799992016-08-15 11:12:27 -0400216
217/*
218 * fd.io coding-style-patch-verification: ON
219 *
220 * Local Variables:
221 * eval: (c-set-style "gnu")
222 * End:
223 */