d628d3827f92490c32131a342b343558e43565fa
[vpp.git] / src / vppinfra / vec_bootstrap.h
1 /*
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
41 /** \file
42     Vector bootstrap header file
43 */
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
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. */
54
55 typedef struct
56 {
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 . */
63 } vec_header_t;
64
65 #define VEC_NUMA_UNSPECIFIED (0xFF)
66
67 #define VEC_HEADER_ROUND 8
68
69 /** \brief Find the vector header
70
71     Given the user's pointer to a vector, find the corresponding
72     vector header
73
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
82 always_inline uword
83 vec_header_bytes (uword header_bytes)
84 {
85   return round_pow2 (header_bytes + sizeof (vec_header_t), VEC_HEADER_ROUND);
86 }
87
88 /** \brief Find a user vector header
89
90     Finds the user header of a vector with unspecified alignment given
91     the user pointer to the vector.
92 */
93
94 always_inline void *
95 vec_header (void *v, uword header_bytes)
96 {
97   return v - vec_header_bytes (header_bytes);
98 }
99
100 /** \brief Find the end of user vector header
101
102     Finds the end of the user header of a vector with unspecified
103     alignment given the user pointer to the vector.
104 */
105
106 always_inline void *
107 vec_header_end (void *v, uword header_bytes)
108 {
109   return v + vec_header_bytes (header_bytes);
110 }
111
112 always_inline uword
113 vec_aligned_header_bytes (uword header_bytes, uword align)
114 {
115   return round_pow2 (header_bytes + sizeof (vec_header_t), align);
116 }
117
118 always_inline void *
119 vec_aligned_header (void *v, uword header_bytes, uword align)
120 {
121   return v - vec_aligned_header_bytes (header_bytes, align);
122 }
123
124 always_inline void *
125 vec_aligned_header_end (void *v, uword header_bytes, uword align)
126 {
127   return v + vec_aligned_header_bytes (header_bytes, align);
128 }
129
130
131 /** \brief Number of elements in vector (lvalue-capable)
132
133    _vec_len (v) does not check for null, but can be used as an lvalue
134    (e.g. _vec_len (v) = 99).
135 */
136
137 #define _vec_len(v)     (_vec_find(v)->len)
138
139 /** \brief Number of elements in vector (rvalue-only, NULL tolerant)
140
141     vec_len (v) checks for NULL, but cannot be used as an lvalue.
142     If in doubt, use vec_len...
143 */
144
145 #define vec_len(v)      ((v) ? _vec_len(v) : 0)
146 u32 vec_len_not_inline (void *v);
147
148 /** \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
162 /** \brief Number of data bytes in vector. */
163
164 #define vec_bytes(v) (vec_len (v) * sizeof (v[0]))
165
166 /**
167  * Return size of memory allocated for the vector
168  *
169  * @param v vector
170  * @param b extra header bytes
171  * @return memory size allocated for the vector
172  */
173 #define vec_mem_size(v, b)                                                    \
174   ({                                                                          \
175     void *_vec_mem_v = (void *) (v);                                          \
176     uword _vec_mem_b = (b);                                                   \
177     _vec_mem_b = sizeof (vec_header_t) + _vec_round_size (_vec_mem_b);        \
178     _vec_mem_v ? clib_mem_size (_vec_mem_v - _vec_mem_b) : 0;                 \
179   })
180
181 /**
182  * Number of elements that can fit into generic vector
183  *
184  * @param v vector
185  * @param b extra header bytes
186  * @return number of elements that can fit into vector
187  */
188 #define vec_max_elts(v, b)                                                    \
189   (v ? (vec_mem_size (v, b) - vec_header_bytes (b)) / sizeof (v[0]) : 0)
190
191 /** \brief Total number of elements that can fit into vector. */
192 #define vec_max_len(v) vec_max_elts (v, 0)
193
194 /** \brief Set vector length to a user-defined value */
195 #ifndef __COVERITY__            /* Coverity gets confused by ASSERT() */
196 #define vec_set_len(v, l) do {     \
197     ASSERT(v);                     \
198     ASSERT((l) <= vec_max_len(v)); \
199     CLIB_MEM_POISON_LEN((void *)(v), _vec_len(v) * sizeof((v)[0]), (l) * sizeof((v)[0])); \
200     _vec_len(v) = (l);             \
201 } while (0)
202 #else /* __COVERITY__ */
203 #define vec_set_len(v, l) do {     \
204     _vec_len(v) = (l);             \
205 } while (0)
206 #endif /* __COVERITY__ */
207
208 /** \brief Reset vector length to zero
209     NULL-pointer tolerant
210 */
211 #define vec_reset_length(v) do { if (v) vec_set_len (v, 0); } while (0)
212
213 /** \brief End (last data address) of vector. */
214 #define vec_end(v)      ((v) + vec_len (v))
215
216 /** \brief True if given pointer is within given vector. */
217 #define vec_is_member(v,e) ((e) >= (v) && (e) < vec_end (v))
218
219 /** \brief Get vector value at index i checking that i is in bounds. */
220 #define vec_elt_at_index(v,i)                   \
221 ({                                              \
222   ASSERT ((i) < vec_len (v));                   \
223   (v) + (i);                                    \
224 })
225
226 /** \brief Get vector value at index i */
227 #define vec_elt(v,i) (vec_elt_at_index(v,i))[0]
228
229 /** \brief Vector iterator */
230 #define vec_foreach(var,vec) for (var = (vec); var < vec_end (vec); var++)
231
232 /** \brief Vector iterator (reverse) */
233 #define vec_foreach_backwards(var, vec)                                       \
234   if (vec)                                                                    \
235     for (var = vec_end (vec) - 1; var >= (vec); var--)
236
237 /** \brief Iterate over vector indices. */
238 #define vec_foreach_index(var,v) for ((var) = 0; (var) < vec_len (v); (var)++)
239
240 /** \brief Iterate over vector indices (reverse). */
241 #define vec_foreach_index_backwards(var, v)                                   \
242   if (v)                                                                      \
243     for ((var) = vec_len ((v)) - 1; (var) >= 0; (var)--)
244
245 /** \brief return the NUMA index for a vector */
246 always_inline uword
247 vec_get_numa (void *v)
248 {
249   vec_header_t *vh;
250   if (v == 0)
251     return 0;
252   vh = _vec_find (v);
253   return vh->numa_id;
254 }
255
256 #endif /* included_clib_vec_bootstrap_h */
257
258 /*
259  * fd.io coding-style-patch-verification: ON
260  *
261  * Local Variables:
262  * eval: (c-set-style "gnu")
263  * End:
264  */