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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
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:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
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.
38 #ifndef included_vec_h
39 #define included_vec_h
41 #include <vppinfra/clib.h> /* word, etc */
42 #include <vppinfra/mem.h> /* clib_mem_free */
43 #include <vppinfra/string.h> /* memcpy, memmove */
44 #include <vppinfra/vec_bootstrap.h>
48 CLIB vectors are ubiquitous dynamically resized arrays with by user
49 defined "headers". Many CLIB data structures (e.g. hash, heap,
50 pool) are vectors with various different headers.
52 The memory layout looks like this:
55 user header (aligned to uword boundary)
56 vector length: number of elements
57 user's pointer-> vector element #0
62 The user pointer contains the address of vector element # 0. Null
63 pointer vectors are valid and mean a zero length vector.
65 You can reset the length of an allocated vector to zero via the
66 vec_reset_length(v) macro, or by setting the vector length field to
67 zero (e.g. _vec_len (v) = 0). Vec_reset_length(v) preferred: it
68 understands Null pointers.
70 Typically, the header is not present. Headers allow for other
71 data structures to be built atop CLIB vectors.
73 Users may specify the alignment for data elements via the
76 Vectors elements can be any C type e.g. (int, double, struct bar).
77 This is also true for data types built atop vectors (e.g. heap,
80 Many macros have _a variants supporting alignment of vector data
81 and _h variants supporting non zero length vector headers.
82 The _ha variants support both.
84 Standard programming error: memorize a pointer to the ith element
85 of a vector then expand it. Vectors expand by 3/2, so such code
86 may appear to work for a period of time. Memorize vector indices
90 /** \brief Low-level resize allocation function, usually not called directly
92 @param v pointer to a vector
93 @param length_increment length increment in elements
94 @param data_bytes requested size in bytes
95 @param header_bytes header size in bytes (may be zero)
96 @param data_align alignment (may be zero)
97 @return v_prime pointer to resized vector, may or may not equal v
99 void * vec_resize_allocate_memory (void * v,
100 word length_increment,
105 /** \brief Low-level vector resize function, usually not called directly
107 @param v pointer to a vector
108 @param length_increment length increment in elements
109 @param data_bytes requested size in bytes
110 @param header_bytes header size in bytes (may be zero)
111 @param data_align alignment (may be zero)
112 @return v_prime pointer to resized vector, may or may not equal v
116 _vec_resize (void * v,
117 word length_increment,
122 vec_header_t * vh = _vec_find (v);
123 uword new_data_bytes, aligned_header_bytes;
125 aligned_header_bytes = vec_header_bytes (header_bytes);
127 new_data_bytes = data_bytes + aligned_header_bytes;
129 if (PREDICT_TRUE (v != 0))
131 void * p = v - aligned_header_bytes;
133 /* Vector header must start heap object. */
134 ASSERT (clib_mem_is_heap_object (p));
136 /* Typically we'll not need to resize. */
137 if (new_data_bytes <= clib_mem_size (p))
139 vh->len += length_increment;
144 /* Slow path: call helper function. */
145 return vec_resize_allocate_memory (v, length_increment, data_bytes, header_bytes,
146 clib_max (sizeof (vec_header_t), data_align));
149 /** \brief Predicate function, says whether the supplied vector is a clib heap
150 object (general version).
152 @param v pointer to a vector
153 @param header_bytes vector header size in bytes (may be zero)
156 uword clib_mem_is_vec_h (void * v, uword header_bytes);
159 /** \brief Predicate function, says whether the supplied vector is a clib heap
162 @param v pointer to a vector
165 always_inline uword clib_mem_is_vec (void * v)
166 { return clib_mem_is_vec_h (v, 0); }
168 /* Local variable naming macro (prevents collisions with other macro naming). */
169 #define _v(var) _vec_##var
171 /** \brief Resize a vector (general version).
172 Add N elements to end of given vector V, return pointer to start of vector.
173 Vector will have room for H header bytes and will have user's data aligned
174 at alignment A (rounded to next power of 2).
176 @param V pointer to a vector
177 @param N number of elements to add
178 @param H header size in bytes (may be zero)
179 @param A alignment (may be zero)
180 @return V (value-result macro parameter)
183 #define vec_resize_ha(V,N,H,A) \
186 word _v(l) = vec_len (V); \
187 V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \
190 /** \brief Resize a vector (no header, unspecified alignment)
191 Add N elements to end of given vector V, return pointer to start of vector.
192 Vector will have room for H header bytes and will have user's data aligned
193 at alignment A (rounded to next power of 2).
195 @param V pointer to a vector
196 @param N number of elements to add
197 @return V (value-result macro parameter)
199 #define vec_resize(V,N) vec_resize_ha(V,N,0,0)
201 /** \brief Resize a vector (no header, alignment specified).
202 Add N elements to end of given vector V, return pointer to start of vector.
203 Vector will have room for H header bytes and will have user's data aligned
204 at alignment A (rounded to next power of 2).
206 @param V pointer to a vector
207 @param N number of elements to add
208 @param A alignment (may be zero)
209 @return V (value-result macro parameter)
212 #define vec_resize_aligned(V,N,A) vec_resize_ha(V,N,0,A)
214 /** \brief Allocate space for N more elements
216 @param V pointer to a vector
217 @param N number of elements to add
218 @param H header size in bytes (may be zero)
219 @param A alignment (may be zero)
220 @return V (value-result macro parameter)
223 #define vec_alloc_ha(V,N,H,A) \
225 uword _v(l) = vec_len (V); \
226 vec_resize_ha (V, N, H, A); \
227 _vec_len (V) = _v(l); \
230 /** \brief Allocate space for N more elements
231 (no header, unspecified alignment)
233 @param V pointer to a vector
234 @param N number of elements to add
235 @return V (value-result macro parameter)
237 #define vec_alloc(V,N) vec_alloc_ha(V,N,0,0)
239 /** \brief Allocate space for N more elements (no header, given alignment)
240 @param V pointer to a vector
241 @param N number of elements to add
242 @param A alignment (may be zero)
243 @return V (value-result macro parameter)
246 #define vec_alloc_aligned(V,N,A) vec_alloc_ha(V,N,0,A)
248 /** \brief Create new vector of given type and length (general version).
249 @param T type of elements in new vector
250 @param N number of elements to add
251 @param H header size in bytes (may be zero)
252 @param A alignment (may be zero)
255 #define vec_new_ha(T,N,H,A) \
258 _vec_resize ((T *) 0, _v(n), _v(n) * sizeof (T), (H), (A)); \
261 /** \brief Create new vector of given type and length
262 (unspecified alignment, no header).
264 @param T type of elements in new vector
265 @param N number of elements to add
268 #define vec_new(T,N) vec_new_ha(T,N,0,0)
269 /** \brief Create new vector of given type and length
270 (alignment specified, no header).
272 @param T type of elements in new vector
273 @param N number of elements to add
274 @param A alignment (may be zero)
277 #define vec_new_aligned(T,N,A) vec_new_ha(T,N,0,A)
279 /** \brief Free vector's memory (general version)
281 @param V pointer to a vector
282 @param H size of header in bytes
283 @return V (value-result parameter, V=0)
285 #define vec_free_h(V,H) \
289 clib_mem_free (vec_header ((V), (H))); \
294 /** \brief Free vector's memory (no header).
295 @param V pointer to a vector
296 @return V (value-result parameter, V=0)
298 #define vec_free(V) vec_free_h(V,0)
300 /**\brief Free vector user header (syntactic sugar)
301 @param h vector header
304 #define vec_free_header(h) clib_mem_free (h)
306 /** \brief Return copy of vector (general version).
308 @param V pointer to a vector
309 @param H size of header in bytes
310 @param A alignment (may be zero)
312 @return Vdup copy of vector
315 #define vec_dup_ha(V,H,A) \
317 __typeof__ ((V)[0]) * _v(v) = 0; \
318 uword _v(l) = vec_len (V); \
321 vec_resize_ha (_v(v), _v(l), (H), (A)); \
322 clib_memcpy (_v(v), (V), _v(l) * sizeof ((V)[0]));\
327 /** \brief Return copy of vector (no header, no alignment)
329 @param V pointer to a vector
330 @return Vdup copy of vector
332 #define vec_dup(V) vec_dup_ha(V,0,0)
334 /** \brief Return copy of vector (no header, alignment specified).
336 @param V pointer to a vector
337 @param A alignment (may be zero)
339 @return Vdup copy of vector
341 #define vec_dup_aligned(V,A) vec_dup_ha(V,0,A)
343 /** \brief Copy a vector, memcpy wrapper. Assumes sizeof(SRC[0]) ==
346 @param DST destination
349 #define vec_copy(DST,SRC) clib_memcpy (DST, SRC, vec_len (DST) * \
352 /** \brief Clone a vector. Make a new vector with the
353 same size as a given vector but possibly with a different type.
355 @param NEW_V pointer to new vector
356 @param OLD_V pointer to old vector
358 #define vec_clone(NEW_V,OLD_V) \
361 (NEW_V) = _vec_resize ((NEW_V), vec_len (OLD_V), \
362 vec_len (OLD_V) * sizeof ((NEW_V)[0]), (0), (0)); \
365 /** \brief Make sure vector is long enough for given index (general version).
367 @param V (possibly NULL) pointer to a vector.
368 @param I vector index which will be valid upon return
369 @param H header size in bytes (may be zero)
370 @param A alignment (may be zero)
371 @return V (value-result macro parameter)
374 #define vec_validate_ha(V,I,H,A) \
377 word _v(l) = vec_len (V); \
378 if (_v(i) >= _v(l)) \
380 vec_resize_ha ((V), 1 + (_v(i) - _v(l)), (H), (A)); \
381 /* Must zero new space since user may have previously \
382 used e.g. _vec_len (v) -= 10 */ \
383 memset ((V) + _v(l), 0, (1 + (_v(i) - _v(l))) * sizeof ((V)[0])); \
387 /** \brief Make sure vector is long enough for given index
388 (no header, unspecified alignment)
390 @param V (possibly NULL) pointer to a vector.
391 @param I vector index which will be valid upon return
392 @return V (value-result macro parameter)
394 #define vec_validate(V,I) vec_validate_ha(V,I,0,0)
396 /** \brief Make sure vector is long enough for given index
397 (no header, specified alignment)
399 @param V (possibly NULL) pointer to a vector.
400 @param I vector index which will be valid upon return
401 @param A alignment (may be zero)
402 @return V (value-result macro parameter)
405 #define vec_validate_aligned(V,I,A) vec_validate_ha(V,I,0,A)
407 /** \brief Make sure vector is long enough for given index
408 and initialize empty space (general version)
410 @param V (possibly NULL) pointer to a vector.
411 @param I vector index which will be valid upon return
412 @param INIT initial value (can be a complex expression!)
413 @param H header size in bytes (may be zero)
414 @param A alignment (may be zero)
415 @return V (value-result macro parameter)
417 #define vec_validate_init_empty_ha(V,I,INIT,H,A) \
420 word _v(l) = vec_len (V); \
421 if (_v(i) >= _v(l)) \
423 vec_resize_ha ((V), 1 + (_v(i) - _v(l)), (H), (A)); \
424 while (_v(l) <= _v(i)) \
426 (V)[_v(l)] = (INIT); \
432 /** \brief Make sure vector is long enough for given index
433 and initialize empty space (no header, unspecified alignment)
435 @param V (possibly NULL) pointer to a vector.
436 @param I vector index which will be valid upon return
437 @param INIT initial value (can be a complex expression!)
438 @param H header size in bytes (may be zero)
439 @param A alignment (may be zero)
440 @return V (value-result macro parameter)
443 #define vec_validate_init_empty(V,I,INIT) \
444 vec_validate_init_empty_ha(V,I,INIT,0,0)
446 /** \brief Make sure vector is long enough for given index
447 and initialize empty space (no header, alignment alignment)
449 @param V (possibly NULL) pointer to a vector.
450 @param I vector index which will be valid upon return
451 @param INIT initial value (can be a complex expression!)
452 @param H header size in bytes (may be zero)
453 @param A alignment (may be zero)
454 @return V (value-result macro parameter)
456 #define vec_validate_init_empty_aligned(V,I,A) \
457 vec_validate_init_empty_ha(V,I,INIT,0,A)
459 /** \brief Add 1 element to end of vector (general version).
461 @param V pointer to a vector
462 @param E element to add
463 @param H header size in bytes (may be zero)
464 @param A alignment (may be zero)
465 @return V (value-result macro parameter)
467 #define vec_add1_ha(V,E,H,A) \
469 word _v(l) = vec_len (V); \
470 V = _vec_resize ((V), 1, (_v(l) + 1) * sizeof ((V)[0]), (H), (A)); \
474 /** \brief Add 1 element to end of vector (unspecified alignment).
476 @param V pointer to a vector
477 @param E element to add
478 @return V (value-result macro parameter)
480 #define vec_add1(V,E) vec_add1_ha(V,E,0,0)
482 /** \brief Add 1 element to end of vector (alignment specified).
484 @param V pointer to a vector
485 @param E element to add
486 @param H header size in bytes (may be zero)
487 @param A alignment (may be zero)
488 @return V (value-result macro parameter)
490 #define vec_add1_aligned(V,E,A) vec_add1_ha(V,E,0,A)
492 /** \brief Add N elements to end of vector V,
493 return pointer to new elements in P. (general version)
495 @param V pointer to a vector
496 @param P pointer to new vector element(s)
497 @param N number of elements to add
498 @param H header size in bytes (may be zero)
499 @param A alignment (may be zero)
500 @return V and P (value-result macro parameters)
502 #define vec_add2_ha(V,P,N,H,A) \
505 word _v(l) = vec_len (V); \
506 V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \
510 /** \brief Add N elements to end of vector V,
511 return pointer to new elements in P. (no header, unspecified alignment)
513 @param V pointer to a vector
514 @param P pointer to new vector element(s)
515 @param N number of elements to add
516 @return V and P (value-result macro parameters)
519 #define vec_add2(V,P,N) vec_add2_ha(V,P,N,0,0)
521 /** \brief Add N elements to end of vector V,
522 return pointer to new elements in P. (no header, alignment specified)
524 @param V pointer to a vector
525 @param P pointer to new vector element(s)
526 @param N number of elements to add
527 @param A alignment (may be zero)
528 @return V and P (value-result macro parameters)
531 #define vec_add2_aligned(V,P,N,A) vec_add2_ha(V,P,N,0,A)
533 /** \brief Add N elements to end of vector V (general version)
535 @param V pointer to a vector
536 @param E pointer to element(s) to add
537 @param N number of elements to add
538 @param H header size in bytes (may be zero)
539 @param A alignment (may be zero)
540 @return V (value-result macro parameter)
542 #define vec_add_ha(V,E,N,H,A) \
545 word _v(l) = vec_len (V); \
546 V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \
547 clib_memcpy ((V) + _v(l), (E), _v(n) * sizeof ((V)[0])); \
550 /** \brief Add N elements to end of vector V (no header, unspecified alignment)
552 @param V pointer to a vector
553 @param E pointer to element(s) to add
554 @param N number of elements to add
555 @return V (value-result macro parameter)
557 #define vec_add(V,E,N) vec_add_ha(V,E,N,0,0)
559 /** \brief Add N elements to end of vector V (no header, specified alignment)
561 @param V pointer to a vector
562 @param E pointer to element(s) to add
563 @param N number of elements to add
564 @param A alignment (may be zero)
565 @return V (value-result macro parameter)
567 #define vec_add_aligned(V,E,N,A) vec_add_ha(V,E,N,0,A)
569 /** \brief Returns last element of a vector and decrements its length
571 @param V pointer to a vector
572 @return E element removed from the end of the vector
576 uword _v(l) = vec_len (V); \
577 ASSERT (_v(l) > 0); \
579 _vec_len (V) = _v (l); \
583 /** \brief Set E to the last element of a vector, decrement vector length
584 @param V pointer to a vector
585 @param E pointer to the last vector element
586 @return E element removed from the end of the vector
587 (value-result macro parameter
590 #define vec_pop2(V,E) \
592 uword _v(l) = vec_len (V); \
593 if (_v(l) > 0) (E) = vec_pop (V); \
597 /** \brief Insert N vector elements starting at element M,
598 initialize new elements (general version).
600 @param V (possibly NULL) pointer to a vector.
601 @param N number of elements to insert
602 @param M insertion point
603 @param INIT initial value (can be a complex expression!)
604 @param H header size in bytes (may be zero)
605 @param A alignment (may be zero)
606 @return V (value-result macro parameter)
608 #define vec_insert_init_empty_ha(V,N,M,INIT,H,A) \
610 word _v(l) = vec_len (V); \
613 V = _vec_resize ((V), \
615 (_v(l) + _v(n))*sizeof((V)[0]), \
617 ASSERT (_v(m) <= _v(l)); \
618 memmove ((V) + _v(m) + _v(n), \
620 (_v(l) - _v(m)) * sizeof ((V)[0])); \
621 memset ((V) + _v(m), INIT, _v(n) * sizeof ((V)[0])); \
624 /** \brief Insert N vector elements starting at element M,
625 initialize new elements to zero (general version)
627 @param V (possibly NULL) pointer to a vector.
628 @param N number of elements to insert
629 @param M insertion point
630 @param H header size in bytes (may be zero)
631 @param A alignment (may be zero)
632 @return V (value-result macro parameter)
634 #define vec_insert_ha(V,N,M,H,A) vec_insert_init_empty_ha(V,N,M,0,H,A)
636 /** \brief Insert N vector elements starting at element M,
637 initialize new elements to zero (no header, unspecified alignment)
639 @param V (possibly NULL) pointer to a vector.
640 @param N number of elements to insert
641 @param M insertion point
642 @return V (value-result macro parameter)
644 #define vec_insert(V,N,M) vec_insert_ha(V,N,M,0,0)
646 /** \brief Insert N vector elements starting at element M,
647 initialize new elements to zero (no header, alignment specified)
649 @param V (possibly NULL) pointer to a vector.
650 @param N number of elements to insert
651 @param M insertion point
652 @param A alignment (may be zero)
653 @return V (value-result macro parameter)
655 #define vec_insert_aligned(V,N,M,A) vec_insert_ha(V,N,M,0,A)
657 /** \brief Insert N vector elements starting at element M,
658 initialize new elements (no header, unspecified alignment)
660 @param V (possibly NULL) pointer to a vector.
661 @param N number of elements to insert
662 @param M insertion point
663 @param INIT initial value (can be a complex expression!)
664 @return V (value-result macro parameter)
667 #define vec_insert_init_empty(V,N,M,INIT) \
668 vec_insert_init_empty_ha(V,N,M,INIT,0,0)
669 /* Resize vector by N elements starting from element M, initialize new elements to INIT (alignment specified, no header). */
671 /** \brief Insert N vector elements starting at element M,
672 initialize new elements (no header, specified alignment)
674 @param V (possibly NULL) pointer to a vector.
675 @param N number of elements to insert
676 @param M insertion point
677 @param INIT initial value (can be a complex expression!)
678 @param A alignment (may be zero)
679 @return V (value-result macro parameter)
681 #define vec_insert_init_empty_aligned(V,N,M,INIT,A) \
682 vec_insert_init_empty_ha(V,N,M,INIT,0,A)
684 /** \brief Insert N vector elements starting at element M,
685 insert given elements (general version)
687 @param V (possibly NULL) pointer to a vector.
688 @param E element(s) to insert
689 @param N number of elements to insert
690 @param M insertion point
691 @param H header size in bytes (may be zero)
692 @param A alignment (may be zero)
693 @return V (value-result macro parameter)
696 #define vec_insert_elts_ha(V,E,N,M,H,A) \
698 word _v(l) = vec_len (V); \
701 V = _vec_resize ((V), \
703 (_v(l) + _v(n))*sizeof((V)[0]), \
705 ASSERT (_v(m) <= _v(l)); \
706 memmove ((V) + _v(m) + _v(n), \
708 (_v(l) - _v(m)) * sizeof ((V)[0])); \
709 clib_memcpy ((V) + _v(m), (E), \
710 _v(n) * sizeof ((V)[0])); \
713 /** \brief Insert N vector elements starting at element M,
714 insert given elements (no header, unspecified alignment)
716 @param V (possibly NULL) pointer to a vector.
717 @param E element(s) to insert
718 @param N number of elements to insert
719 @param M insertion point
720 @return V (value-result macro parameter)
722 #define vec_insert_elts(V,E,N,M) vec_insert_elts_ha(V,E,N,M,0,0)
724 /** \brief Insert N vector elements starting at element M,
725 insert given elements (no header, specified alignment)
727 @param V (possibly NULL) pointer to a vector.
728 @param E element(s) to insert
729 @param N number of elements to insert
730 @param M insertion point
731 @param A alignment (may be zero)
732 @return V (value-result macro parameter)
734 #define vec_insert_elts_aligned(V,E,N,M,A) vec_insert_elts_ha(V,E,N,M,0,A)
736 /** \brief Delete N elements starting at element M
738 @param V pointer to a vector
739 @param N number of elements to delete
740 @param M first element to delete
741 @return V (value-result macro parameter)
743 #define vec_delete(V,N,M) \
745 word _v(l) = vec_len (V); \
748 /* Copy over deleted elements. */ \
749 if (_v(l) - _v(n) - _v(m) > 0) \
750 memmove ((V) + _v(m), (V) + _v(m) + _v(n), \
751 (_v(l) - _v(n) - _v(m)) * sizeof ((V)[0])); \
752 /* Zero empty space at end (for future re-allocation). */ \
754 memset ((V) + _v(l) - _v(n), 0, _v(n) * sizeof ((V)[0])); \
755 _vec_len (V) -= _v(n); \
758 /** \brief Delete the element at index I
760 @param V pointer to a vector
761 @param I index to delete
763 #define vec_del1(v,i) \
765 uword _vec_del_l = _vec_len (v) - 1; \
766 uword _vec_del_i = (i); \
767 if (_vec_del_i < _vec_del_l) \
768 (v)[_vec_del_i] = (v)[_vec_del_l]; \
769 _vec_len (v) = _vec_del_l; \
772 /** \brief Append v2 after v1. Result in v1.
773 @param V1 target vector
774 @param V2 vector to append
777 #define vec_append(v1,v2) \
779 uword _v(l1) = vec_len (v1); \
780 uword _v(l2) = vec_len (v2); \
782 v1 = _vec_resize ((v1), _v(l2), \
783 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, 0); \
784 clib_memcpy ((v1) + _v(l1), (v2), _v(l2) * sizeof ((v2)[0])); \
787 /** \brief Append v2 after v1. Result in v1. Specified alignment.
788 @param V1 target vector
789 @param V2 vector to append
790 @param align required alignment
793 #define vec_append_aligned(v1,v2,align) \
795 uword _v(l1) = vec_len (v1); \
796 uword _v(l2) = vec_len (v2); \
798 v1 = _vec_resize ((v1), _v(l2), \
799 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, align); \
800 clib_memcpy ((v1) + _v(l1), (v2), _v(l2) * sizeof ((v2)[0])); \
803 /** \brief Prepend v2 before v1. Result in v1.
804 @param V1 target vector
805 @param V2 vector to prepend
808 #define vec_prepend(v1,v2) \
810 uword _v(l1) = vec_len (v1); \
811 uword _v(l2) = vec_len (v2); \
813 v1 = _vec_resize ((v1), _v(l2), \
814 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, 0); \
815 memmove ((v1) + _v(l2), (v1), _v(l1) * sizeof ((v1)[0])); \
816 clib_memcpy ((v1), (v2), _v(l2) * sizeof ((v2)[0])); \
819 /** \brief Prepend v2 before v1. Result in v1. Specified alignment
820 @param V1 target vector
821 @param V2 vector to prepend
822 @param align required alignment
825 #define vec_prepend_aligned(v1,v2,align) \
827 uword _v(l1) = vec_len (v1); \
828 uword _v(l2) = vec_len (v2); \
830 v1 = _vec_resize ((v1), _v(l2), \
831 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, align); \
832 memmove ((v1) + _v(l2), (v1), _v(l1) * sizeof ((v1)[0])); \
833 clib_memcpy ((v1), (v2), _v(l2) * sizeof ((v2)[0])); \
837 /** \brief Zero all vector elements. Null-pointer tolerant.
838 @param var Vector to zero
840 #define vec_zero(var) \
843 memset ((var), 0, vec_len (var) * sizeof ((var)[0])); \
846 /** \brief Set all vector elements to given value. Null-pointer tolerant.
847 @param v vector to set
848 @param val value for each vector element
850 #define vec_set(v,val) \
853 __typeof__ ((v)[0]) _val = (val); \
854 for (_v(i) = 0; _v(i) < vec_len (v); _v(i)++) \
859 #include <stdlib.h> /* for qsort */
862 /** \brief Compare two vectors, not NULL-pointer tolerant
864 @param v1 Pointer to a vector
865 @param v2 Pointer to a vector
866 @return 1 if equal, 0 if unequal
868 #define vec_is_equal(v1,v2) \
869 (vec_len (v1) == vec_len (v2) && ! memcmp ((v1), (v2), vec_len (v1) * sizeof ((v1)[0])))
871 /** \brief Compare two vectors (only applicable to vectors of signed numbers).
872 Used in qsort compare functions.
874 @param v1 Pointer to a vector
875 @param v2 Pointer to a vector
878 #define vec_cmp(v1,v2) \
880 word _v(i), _v(cmp), _v(l); \
881 _v(l) = clib_min (vec_len (v1), vec_len (v2)); \
883 for (_v(i) = 0; _v(i) < _v(l); _v(i)++) { \
884 _v(cmp) = (v1)[_v(i)] - (v2)[_v(i)]; \
888 if (_v(cmp) == 0 && _v(l) > 0) \
889 _v(cmp) = vec_len(v1) - vec_len(v2); \
890 (_v(cmp) < 0 ? -1 : (_v(cmp) > 0 ? +1 : 0)); \
893 /** \brief Sort a vector using the supplied element comparison function
895 @param vec vector to sort
896 @param f comparison function
898 #define vec_sort_with_function(vec,f) \
900 qsort (vec, vec_len (vec), sizeof (vec[0]), (void *) (f)); \
903 /** \brief Make a vector containing a NULL terminated c-string.
905 @param V (possibly NULL) pointer to a vector.
906 @param S pointer to string buffer.
907 @param L string length (NOT including the terminating NULL; a la strlen())
909 #define vec_validate_init_c_string(V, S, L) \
911 vec_reset_length (V); \
912 vec_validate ((V), (L)); \
914 clib_memcpy ((V), (S), (L)); \
921 @param V (possibly NULL) pointer to a vector.
922 @return BOOLEAN indicating if the vector c-string is null terminated.
924 #define vec_c_string_is_terminated(V) \
925 (((V) != 0) && (vec_len (V) != 0) && ((V)[vec_len ((V)) - 1] == 0))
927 /** \brief (If necessary) Null terminate a vector containing a c-string.
929 @param V (possibly NULL) pointer to a vector.
930 @return V (value-result macro parameter)
932 #define vec_terminate_c_string(V) \
934 u32 vl = vec_len ((V)); \
935 if (!vec_c_string_is_terminated(V)) \
937 vec_validate ((V), vl); \
942 #endif /* included_vec_h */