FD.io VPP  v16.06
Vector Packet Processing
vec_bootstrap.h
Go to the documentation of this file.
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 bootsrap 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  Bookeeping 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 #if CLIB_VEC64 > 0
57  u64 len;
58 #else
59  u32 len; /**< Number of elements in vector (NOT its allocated length). */
60 #endif
61  u8 vector_data[0]; /**< Vector data . */
62 } vec_header_t;
63 
64 /** \brief Find the vector header
65 
66  Given the user's pointer to a vector, find the corresponding
67  vector header
68 
69  @param v pointer to a vector
70  @return pointer to the vector's vector_header_t
71 */
72 #define _vec_find(v) ((vec_header_t *) (v) - 1)
73 
74 #define _vec_round_size(s) \
75  (((s) + sizeof (uword) - 1) &~ (sizeof (uword) - 1))
76 
78 vec_header_bytes (uword header_bytes)
79 { return round_pow2 (header_bytes + sizeof (vec_header_t), sizeof (vec_header_t)); }
80 
81 /** \brief Find a user vector header
82 
83  Finds the user header of a vector with unspecified alignment given
84  the user pointer to the vector.
85 */
86 
87 always_inline void *
88 vec_header (void * v, uword header_bytes)
89 { return v - vec_header_bytes (header_bytes); }
90 
91 /** \brief Find the end of user vector header
92 
93  Finds the end of the user header of a vector with unspecified
94  alignment given the user pointer to the vector.
95 */
96 
97 always_inline void *
98 vec_header_end (void * v, uword header_bytes)
99 { return v + vec_header_bytes (header_bytes); }
100 
102 vec_aligned_header_bytes (uword header_bytes, uword align)
103 {
104  return round_pow2 (header_bytes + sizeof (vec_header_t), align);
105 }
106 
107 always_inline void *
108 vec_aligned_header (void * v, uword header_bytes, uword align)
109 { return v - vec_aligned_header_bytes (header_bytes, align); }
110 
111 always_inline void *
112 vec_aligned_header_end (void * v, uword header_bytes, uword align)
113 { return v + vec_aligned_header_bytes (header_bytes, align); }
114 
115 
116 /** \brief Number of elements in vector (lvalue-capable)
117 
118  _vec_len (v) does not check for null, but can be used as a lvalue
119  (e.g. _vec_len (v) = 99).
120 */
121 
122 #define _vec_len(v) (_vec_find(v)->len)
123 
124 /** \brief Number of elements in vector (rvalue-only, NULL tolerant)
125 
126  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)
131 
132 /** \brief Reset vector length to zero
133  NULL-pointer tolerant
134 */
135 
136 #define vec_reset_length(v) do { if (v) _vec_len (v) = 0; } while (0)
137 
138 /** \brief Number of data bytes in vector. */
139 
140 #define vec_bytes(v) (vec_len (v) * sizeof (v[0]))
141 
142 /** \brief Total number of bytes that can fit in vector with current allocation. */
143 
144 #define vec_capacity(v,b) \
145 ({ \
146  void * _vec_capacity_v = (void *) (v); \
147  uword _vec_capacity_b = (b); \
148  _vec_capacity_b = sizeof (vec_header_t) + _vec_round_size (_vec_capacity_b); \
149  _vec_capacity_v ? clib_mem_size (_vec_capacity_v - _vec_capacity_b) : 0; \
150 })
151 
152 /** \brief Total number of elements that can fit into vector. */
153 #define vec_max_len(v) (vec_capacity(v,0) / sizeof (v[0]))
154 
155 /** \brief End (last data address) of vector. */
156 #define vec_end(v) ((v) + vec_len (v))
157 
158 /** \brief True if given pointer is within given vector. */
159 #define vec_is_member(v,e) ((e) >= (v) && (e) < vec_end (v))
160 
161 /** \brief Get vector value at index i checking that i is in bounds. */
162 #define vec_elt_at_index(v,i) \
163 ({ \
164  ASSERT ((i) < vec_len (v)); \
165  (v) + (i); \
166 })
167 
168 /** \brief Get vector value at index i */
169 #define vec_elt(v,i) (vec_elt_at_index(v,i))[0]
170 
171 /** \brief Vector iterator */
172 #define vec_foreach(var,vec) for (var = (vec); var < vec_end (vec); var++)
173 
174 /** \brief Vector iterator (reverse) */
175 #define vec_foreach_backwards(var,vec) \
176 for (var = vec_end (vec) - 1; var >= (vec); var--)
177 
178 /** \brief Iterate over vector indices. */
179 #define vec_foreach_index(var,v) for ((var) = 0; (var) < vec_len (v); (var)++)
180 
181 #endif /* included_clib_vec_bootstrap_h */
always_inline uword round_pow2(uword x, uword pow2)
Definition: clib.h:255
always_inline void * vec_aligned_header(void *v, uword header_bytes, uword align)
#define always_inline
Definition: clib.h:84
unsigned long u64
Definition: types.h:89
u32 len
Number of elements in vector (NOT its allocated length).
Definition: vec_bootstrap.h:59
unsigned int u32
Definition: types.h:88
vector header structure
Definition: vec_bootstrap.h:55
u64 uword
Definition: types.h:112
unsigned char u8
Definition: types.h:56
always_inline void * vec_aligned_header_end(void *v, uword header_bytes, uword align)
always_inline void * vec_header_end(void *v, uword header_bytes)
Find the end of user vector header.
Definition: vec_bootstrap.h:98
always_inline void * vec_header(void *v, uword header_bytes)
Find a user vector header.
Definition: vec_bootstrap.h:88
always_inline uword vec_aligned_header_bytes(uword header_bytes, uword align)
always_inline uword vec_header_bytes(uword header_bytes)
Definition: vec_bootstrap.h:78