blob: 3b8c770744cbb23ecc75ff272537328aa409a95c [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
Ed Warnickecb9cada2015-12-08 15:45:58 -070042 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.
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#if CLIB_VEC64 > 0
58 u64 len;
59#else
60 u32 len; /**< Number of elements in vector (NOT its allocated length). */
61#endif
62 u8 vector_data[0]; /**< Vector data . */
63} vec_header_t;
64
65/** \brief Find the vector header
66
67 Given the user's pointer to a vector, find the corresponding
Dave Barachc3799992016-08-15 11:12:27 -040068 vector header
Ed Warnickecb9cada2015-12-08 15:45:58 -070069
70 @param v pointer to a vector
71 @return pointer to the vector's vector_header_t
72*/
73#define _vec_find(v) ((vec_header_t *) (v) - 1)
74
75#define _vec_round_size(s) \
76 (((s) + sizeof (uword) - 1) &~ (sizeof (uword) - 1))
77
78always_inline uword
79vec_header_bytes (uword header_bytes)
Dave Barachc3799992016-08-15 11:12:27 -040080{
81 return round_pow2 (header_bytes + sizeof (vec_header_t),
82 sizeof (vec_header_t));
83}
Ed Warnickecb9cada2015-12-08 15:45:58 -070084
85/** \brief Find a user vector header
Dave Barachc3799992016-08-15 11:12:27 -040086
Ed Warnickecb9cada2015-12-08 15:45:58 -070087 Finds the user header of a vector with unspecified alignment given
88 the user pointer to the vector.
89*/
90
91always_inline void *
Dave Barachc3799992016-08-15 11:12:27 -040092vec_header (void *v, uword header_bytes)
93{
94 return v - vec_header_bytes (header_bytes);
95}
Ed Warnickecb9cada2015-12-08 15:45:58 -070096
97/** \brief Find the end of user vector header
Dave Barachc3799992016-08-15 11:12:27 -040098
99 Finds the end of the user header of a vector with unspecified
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 alignment given the user pointer to the vector.
101*/
102
103always_inline void *
Dave Barachc3799992016-08-15 11:12:27 -0400104vec_header_end (void *v, uword header_bytes)
105{
106 return v + vec_header_bytes (header_bytes);
107}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
109always_inline uword
110vec_aligned_header_bytes (uword header_bytes, uword align)
111{
112 return round_pow2 (header_bytes + sizeof (vec_header_t), align);
113}
114
115always_inline void *
Dave Barachc3799992016-08-15 11:12:27 -0400116vec_aligned_header (void *v, uword header_bytes, uword align)
117{
118 return v - vec_aligned_header_bytes (header_bytes, align);
119}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120
121always_inline void *
Dave Barachc3799992016-08-15 11:12:27 -0400122vec_aligned_header_end (void *v, uword header_bytes, uword align)
123{
124 return v + vec_aligned_header_bytes (header_bytes, align);
125}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126
127
128/** \brief Number of elements in vector (lvalue-capable)
129
130 _vec_len (v) does not check for null, but can be used as a lvalue
Dave Barachc3799992016-08-15 11:12:27 -0400131 (e.g. _vec_len (v) = 99).
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132*/
133
134#define _vec_len(v) (_vec_find(v)->len)
135
136/** \brief Number of elements in vector (rvalue-only, NULL tolerant)
Dave Barachc3799992016-08-15 11:12:27 -0400137
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 vec_len (v) checks for NULL, but cannot be used as an lvalue.
139 If in doubt, use vec_len...
140*/
141
142#define vec_len(v) ((v) ? _vec_len(v) : 0)
143
Dave Barachc3799992016-08-15 11:12:27 -0400144/** \brief Reset vector length to zero
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145 NULL-pointer tolerant
146*/
147
148#define vec_reset_length(v) do { if (v) _vec_len (v) = 0; } while (0)
149
150/** \brief Number of data bytes in vector. */
151
152#define vec_bytes(v) (vec_len (v) * sizeof (v[0]))
153
154/** \brief Total number of bytes that can fit in vector with current allocation. */
155
156#define vec_capacity(v,b) \
157({ \
158 void * _vec_capacity_v = (void *) (v); \
159 uword _vec_capacity_b = (b); \
160 _vec_capacity_b = sizeof (vec_header_t) + _vec_round_size (_vec_capacity_b); \
161 _vec_capacity_v ? clib_mem_size (_vec_capacity_v - _vec_capacity_b) : 0; \
162})
163
164/** \brief Total number of elements that can fit into vector. */
165#define vec_max_len(v) (vec_capacity(v,0) / sizeof (v[0]))
166
167/** \brief End (last data address) of vector. */
168#define vec_end(v) ((v) + vec_len (v))
169
170/** \brief True if given pointer is within given vector. */
171#define vec_is_member(v,e) ((e) >= (v) && (e) < vec_end (v))
172
173/** \brief Get vector value at index i checking that i is in bounds. */
174#define vec_elt_at_index(v,i) \
175({ \
176 ASSERT ((i) < vec_len (v)); \
177 (v) + (i); \
178})
179
180/** \brief Get vector value at index i */
181#define vec_elt(v,i) (vec_elt_at_index(v,i))[0]
182
183/** \brief Vector iterator */
184#define vec_foreach(var,vec) for (var = (vec); var < vec_end (vec); var++)
185
186/** \brief Vector iterator (reverse) */
187#define vec_foreach_backwards(var,vec) \
188for (var = vec_end (vec) - 1; var >= (vec); var--)
189
190/** \brief Iterate over vector indices. */
191#define vec_foreach_index(var,v) for ((var) = 0; (var) < vec_len (v); (var)++)
192
193#endif /* included_clib_vec_bootstrap_h */
Dave Barachc3799992016-08-15 11:12:27 -0400194
195/*
196 * fd.io coding-style-patch-verification: ON
197 *
198 * Local Variables:
199 * eval: (c-set-style "gnu")
200 * End:
201 */