vppinfra: vector allocator rework
[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 hdr_size;        /**< header size divided by VEC_MIN_ALIGN */
59   u8 log2_align;      /**< data alignment */
60   u8 vpad[2];         /**< pad to 8 bytes */
61   u8 vector_data[0];  /**< Vector data . */
62 } vec_header_t;
63
64 #define VEC_MIN_ALIGN 8
65
66 /** \brief Find the vector header
67
68     Given the user's pointer to a vector, find the corresponding
69     vector header
70
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 always_inline uword __vec_align (uword data_align, uword configuered_align);
77 always_inline uword __vec_elt_sz (uword elt_sz, int is_void);
78
79 #define _vec_round_size(s) \
80   (((s) + sizeof (uword) - 1) &~ (sizeof (uword) - 1))
81 #define _vec_is_void(P)                                                       \
82   __builtin_types_compatible_p (__typeof__ ((P)[0]), void)
83 #define _vec_elt_sz(V)   __vec_elt_sz (sizeof ((V)[0]), _vec_is_void (V))
84 #define _vec_align(V, A) __vec_align (__alignof__((V)[0]), A)
85
86 always_inline CLIB_NOSANITIZE_ADDR uword
87 vec_get_header_size (void *v)
88 {
89   uword header_size = _vec_find (v)->hdr_size * VEC_MIN_ALIGN;
90   return header_size;
91 }
92
93 /** \brief Find a user vector header
94
95     Finds the user header of a vector with unspecified alignment given
96     the user pointer to the vector.
97 */
98
99 always_inline void *
100 vec_header (void *v)
101 {
102   return v ? v - vec_get_header_size (v) : 0;
103 }
104
105 /** \brief Find the end of user vector header
106
107     Finds the end of the user header of a vector with unspecified
108     alignment given the user pointer to the vector.
109 */
110
111 always_inline void *
112 vec_header_end (void *v)
113 {
114   return v + vec_get_header_size (v);
115 }
116
117 /** \brief Number of elements in vector (lvalue-capable)
118
119    _vec_len (v) does not check for null, but can be used as an lvalue
120    (e.g. _vec_len (v) = 99).
121 */
122
123 #define _vec_len(v)     (_vec_find(v)->len)
124
125 /** \brief Number of elements in vector (rvalue-only, NULL tolerant)
126
127     vec_len (v) checks for NULL, but cannot be used as an lvalue.
128     If in doubt, use vec_len...
129 */
130
131 #define vec_len(v)      ((v) ? _vec_len(v) : 0)
132 u32 vec_len_not_inline (void *v);
133
134 /** \brief Number of data bytes in vector. */
135
136 #define vec_bytes(v) (vec_len (v) * sizeof (v[0]))
137
138 /**
139  * Return size of memory allocated for the vector
140  *
141  * @param v vector
142  * @return memory size allocated for the vector
143  */
144
145 uword vec_mem_size (void *v);
146
147 /**
148  * Number of elements that can fit into generic vector
149  *
150  * @param v vector
151  * @param b extra header bytes
152  * @return number of elements that can fit into vector
153  */
154
155 always_inline uword
156 vec_max_bytes (void *v)
157 {
158   return v ? vec_mem_size (v) - vec_get_header_size (v) : 0;
159 }
160
161 always_inline uword
162 _vec_max_len (void *v, uword elt_sz)
163 {
164   return vec_max_bytes (v) / elt_sz;
165 }
166
167 #define vec_max_len(v) _vec_max_len (v, _vec_elt_sz (v))
168
169 always_inline void
170 _vec_set_len (void *v, uword len, uword elt_sz)
171 {
172   ASSERT (v);
173   ASSERT (len <= _vec_max_len (v, elt_sz));
174   uword old_len = _vec_len (v);
175
176   if (len > old_len)
177     CLIB_MEM_UNPOISON (v + old_len * elt_sz, (len - old_len) * elt_sz);
178   else if (len > old_len)
179     CLIB_MEM_POISON (v + len * elt_sz, (old_len - len) * elt_sz);
180
181   _vec_len (v) = len;
182 }
183
184 #define vec_set_len(v, l) _vec_set_len ((void *) v, l, _vec_elt_sz (v))
185
186 /** \brief Reset vector length to zero
187     NULL-pointer tolerant
188 */
189 #define vec_reset_length(v) do { if (v) vec_set_len (v, 0); } while (0)
190
191 /** \brief End (last data address) of vector. */
192 #define vec_end(v)      ((v) + vec_len (v))
193
194 /** \brief True if given pointer is within given vector. */
195 #define vec_is_member(v,e) ((e) >= (v) && (e) < vec_end (v))
196
197 /** \brief Get vector value at index i checking that i is in bounds. */
198 #define vec_elt_at_index(v,i)                   \
199 ({                                              \
200   ASSERT ((i) < vec_len (v));                   \
201   (v) + (i);                                    \
202 })
203
204 /** \brief Get vector value at index i */
205 #define vec_elt(v,i) (vec_elt_at_index(v,i))[0]
206
207 /** \brief Vector iterator */
208 #define vec_foreach(var,vec) for (var = (vec); var < vec_end (vec); var++)
209
210 /** \brief Vector iterator (reverse) */
211 #define vec_foreach_backwards(var, vec)                                       \
212   if (vec)                                                                    \
213     for (var = vec_end (vec) - 1; var >= (vec); var--)
214
215 /** \brief Iterate over vector indices. */
216 #define vec_foreach_index(var,v) for ((var) = 0; (var) < vec_len (v); (var)++)
217
218 /** \brief Iterate over vector indices (reverse). */
219 #define vec_foreach_index_backwards(var, v)                                   \
220   if (v)                                                                      \
221     for ((var) = vec_len ((v)) - 1; (var) >= 0; (var)--)
222
223 #endif /* included_clib_vec_bootstrap_h */
224
225 /*
226  * fd.io coding-style-patch-verification: ON
227  *
228  * Local Variables:
229  * eval: (c-set-style "gnu")
230  * End:
231  */