38 #ifndef included_vec_h 39 #define included_vec_h 102 word length_increment,
116 #define _vec_resize(V,L,DB,HB,A) \ 117 _vec_resize_inline(V,L,DB,HB,clib_max((__alignof__((V)[0])),(A))) 120 _vec_resize_inline (
void *v,
121 word length_increment,
125 uword new_data_bytes, aligned_header_bytes;
129 new_data_bytes = data_bytes + aligned_header_bytes;
133 void *p = v - aligned_header_bytes;
141 vh->
len += length_increment;
164 _vec_resize_will_expand (
void *v,
165 word length_increment,
169 uword new_data_bytes, aligned_header_bytes;
173 new_data_bytes = data_bytes + aligned_header_bytes;
177 void *p = v - aligned_header_bytes;
212 #define _v(var) _vec_##var 226 #define vec_resize_ha(V,N,H,A) \ 229 word _v(l) = vec_len (V); \ 230 V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \ 242 #define vec_resize(V,N) vec_resize_ha(V,N,0,0) 255 #define vec_resize_aligned(V,N,A) vec_resize_ha(V,N,0,A) 266 #define vec_alloc_ha(V,N,H,A) \ 268 uword _v(l) = vec_len (V); \ 269 vec_resize_ha (V, N, H, A); \ 270 _vec_len (V) = _v(l); \ 280 #define vec_alloc(V,N) vec_alloc_ha(V,N,0,0) 289 #define vec_alloc_aligned(V,N,A) vec_alloc_ha(V,N,0,A) 298 #define vec_new_ha(T,N,H,A) \ 301 _vec_resize ((T *) 0, _v(n), _v(n) * sizeof (T), (H), (A)); \ 311 #define vec_new(T,N) vec_new_ha(T,N,0,0) 320 #define vec_new_aligned(T,N,A) vec_new_ha(T,N,0,A) 328 #define vec_free_h(V,H) \ 332 clib_mem_free (vec_header ((V), (H))); \ 341 #define vec_free(V) vec_free_h(V,0) 347 #define vec_free_header(h) clib_mem_free (h) 358 #define vec_dup_ha(V,H,A) \ 360 __typeof__ ((V)[0]) * _v(v) = 0; \ 361 uword _v(l) = vec_len (V); \ 364 vec_resize_ha (_v(v), _v(l), (H), (A)); \ 365 clib_memcpy_fast (_v(v), (V), _v(l) * sizeof ((V)[0]));\ 375 #define vec_dup(V) vec_dup_ha(V,0,0) 384 #define vec_dup_aligned(V,A) vec_dup_ha(V,0,A) 392 #define vec_copy(DST,SRC) clib_memcpy_fast (DST, SRC, vec_len (DST) * \ 401 #define vec_clone(NEW_V,OLD_V) \ 404 (NEW_V) = _vec_resize ((NEW_V), vec_len (OLD_V), \ 405 vec_len (OLD_V) * sizeof ((NEW_V)[0]), (0), (0)); \ 417 #define vec_validate_ha(V,I,H,A) \ 419 STATIC_ASSERT(A==0 || ((A % sizeof(V[0]))==0) || ((sizeof(V[0]) % A) == 0),\ 420 "vector validate aligned on incorrectly sized object"); \ 422 word _v(l) = vec_len (V); \ 423 if (_v(i) >= _v(l)) \ 425 vec_resize_ha ((V), 1 + (_v(i) - _v(l)), (H), (A)); \ 428 clib_memset ((V) + _v(l), 0, (1 + (_v(i) - _v(l))) * sizeof ((V)[0])); \ 439 #define vec_validate(V,I) vec_validate_ha(V,I,0,0) 450 #define vec_validate_aligned(V,I,A) vec_validate_ha(V,I,0,A) 462 #define vec_validate_init_empty_ha(V,I,INIT,H,A) \ 465 word _v(l) = vec_len (V); \ 466 if (_v(i) >= _v(l)) \ 468 vec_resize_ha ((V), 1 + (_v(i) - _v(l)), (H), (A)); \ 469 while (_v(l) <= _v(i)) \ 471 (V)[_v(l)] = (INIT); \ 486 #define vec_validate_init_empty(V,I,INIT) \ 487 vec_validate_init_empty_ha(V,I,INIT,0,0) 498 #define vec_validate_init_empty_aligned(V,I,INIT,A) \ 499 vec_validate_init_empty_ha(V,I,INIT,0,A) 509 #define vec_add1_ha(V,E,H,A) \ 511 word _v(l) = vec_len (V); \ 512 V = _vec_resize ((V), 1, (_v(l) + 1) * sizeof ((V)[0]), (H), (A)); \ 522 #define vec_add1(V,E) vec_add1_ha(V,E,0,0) 531 #define vec_add1_aligned(V,E,A) vec_add1_ha(V,E,0,A) 543 #define vec_add2_ha(V,P,N,H,A) \ 546 word _v(l) = vec_len (V); \ 547 V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \ 560 #define vec_add2(V,P,N) vec_add2_ha(V,P,N,0,0) 572 #define vec_add2_aligned(V,P,N,A) vec_add2_ha(V,P,N,0,A) 583 #define vec_add_ha(V,E,N,H,A) \ 586 word _v(l) = vec_len (V); \ 587 V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \ 588 clib_memcpy_fast ((V) + _v(l), (E), _v(n) * sizeof ((V)[0])); \ 598 #define vec_add(V,E,N) vec_add_ha(V,E,N,0,0) 608 #define vec_add_aligned(V,E,N,A) vec_add_ha(V,E,N,0,A) 617 uword _v(l) = vec_len (V); \ 618 ASSERT (_v(l) > 0); \ 620 _vec_len (V) = _v (l); \ 631 #define vec_pop2(V,E) \ 633 uword _v(l) = vec_len (V); \ 634 if (_v(l) > 0) (E) = vec_pop (V); \ 649 #define vec_insert_init_empty_ha(V,N,M,INIT,H,A) \ 651 word _v(l) = vec_len (V); \ 654 V = _vec_resize ((V), \ 656 (_v(l) + _v(n))*sizeof((V)[0]), \ 658 ASSERT (_v(m) <= _v(l)); \ 659 memmove ((V) + _v(m) + _v(n), \ 661 (_v(l) - _v(m)) * sizeof ((V)[0])); \ 662 clib_memset ((V) + _v(m), INIT, _v(n) * sizeof ((V)[0])); \ 675 #define vec_insert_ha(V,N,M,H,A) vec_insert_init_empty_ha(V,N,M,0,H,A) 685 #define vec_insert(V,N,M) vec_insert_ha(V,N,M,0,0) 696 #define vec_insert_aligned(V,N,M,A) vec_insert_ha(V,N,M,0,A) 708 #define vec_insert_init_empty(V,N,M,INIT) \ 709 vec_insert_init_empty_ha(V,N,M,INIT,0,0) 722 #define vec_insert_init_empty_aligned(V,N,M,INIT,A) \ 723 vec_insert_init_empty_ha(V,N,M,INIT,0,A) 737 #define vec_insert_elts_ha(V,E,N,M,H,A) \ 739 word _v(l) = vec_len (V); \ 742 V = _vec_resize ((V), \ 744 (_v(l) + _v(n))*sizeof((V)[0]), \ 746 ASSERT (_v(m) <= _v(l)); \ 747 memmove ((V) + _v(m) + _v(n), \ 749 (_v(l) - _v(m)) * sizeof ((V)[0])); \ 750 clib_memcpy_fast ((V) + _v(m), (E), \ 751 _v(n) * sizeof ((V)[0])); \ 763 #define vec_insert_elts(V,E,N,M) vec_insert_elts_ha(V,E,N,M,0,0) 775 #define vec_insert_elts_aligned(V,E,N,M,A) vec_insert_elts_ha(V,E,N,M,0,A) 784 #define vec_delete(V,N,M) \ 786 word _v(l) = vec_len (V); \ 790 if (_v(l) - _v(n) - _v(m) > 0) \ 791 memmove ((V) + _v(m), (V) + _v(m) + _v(n), \ 792 (_v(l) - _v(n) - _v(m)) * sizeof ((V)[0])); \ 795 clib_memset ((V) + _v(l) - _v(n), 0, _v(n) * sizeof ((V)[0])); \ 796 _vec_len (V) -= _v(n); \ 804 #define vec_del1(v,i) \ 806 uword _vec_del_l = _vec_len (v) - 1; \ 807 uword _vec_del_i = (i); \ 808 if (_vec_del_i < _vec_del_l) \ 809 (v)[_vec_del_i] = (v)[_vec_del_l]; \ 810 _vec_len (v) = _vec_del_l; \ 818 #define vec_append(v1,v2) \ 820 uword _v(l1) = vec_len (v1); \ 821 uword _v(l2) = vec_len (v2); \ 823 v1 = _vec_resize ((v1), _v(l2), \ 824 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, 0); \ 825 clib_memcpy_fast ((v1) + _v(l1), (v2), _v(l2) * sizeof ((v2)[0])); \ 834 #define vec_append_aligned(v1,v2,align) \ 836 uword _v(l1) = vec_len (v1); \ 837 uword _v(l2) = vec_len (v2); \ 839 v1 = _vec_resize ((v1), _v(l2), \ 840 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, align); \ 841 clib_memcpy_fast ((v1) + _v(l1), (v2), _v(l2) * sizeof ((v2)[0])); \ 849 #define vec_prepend(v1,v2) \ 851 uword _v(l1) = vec_len (v1); \ 852 uword _v(l2) = vec_len (v2); \ 854 v1 = _vec_resize ((v1), _v(l2), \ 855 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, 0); \ 856 memmove ((v1) + _v(l2), (v1), _v(l1) * sizeof ((v1)[0])); \ 857 clib_memcpy_fast ((v1), (v2), _v(l2) * sizeof ((v2)[0])); \ 866 #define vec_prepend_aligned(v1,v2,align) \ 868 uword _v(l1) = vec_len (v1); \ 869 uword _v(l2) = vec_len (v2); \ 871 v1 = _vec_resize ((v1), _v(l2), \ 872 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, align); \ 873 memmove ((v1) + _v(l2), (v1), _v(l1) * sizeof ((v1)[0])); \ 874 clib_memcpy_fast ((v1), (v2), _v(l2) * sizeof ((v2)[0])); \ 881 #define vec_zero(var) \ 884 clib_memset ((var), 0, vec_len (var) * sizeof ((var)[0])); \ 891 #define vec_set(v,val) \ 894 __typeof__ ((v)[0]) _val = (val); \ 895 for (_v(i) = 0; _v(i) < vec_len (v); _v(i)++) \ 909 #define vec_is_equal(v1,v2) \ 910 (vec_len (v1) == vec_len (v2) && ! memcmp ((v1), (v2), vec_len (v1) * sizeof ((v1)[0]))) 919 #define vec_cmp(v1,v2) \ 921 word _v(i), _v(cmp), _v(l); \ 922 _v(l) = clib_min (vec_len (v1), vec_len (v2)); \ 924 for (_v(i) = 0; _v(i) < _v(l); _v(i)++) { \ 925 _v(cmp) = (v1)[_v(i)] - (v2)[_v(i)]; \ 929 if (_v(cmp) == 0 && _v(l) > 0) \ 930 _v(cmp) = vec_len(v1) - vec_len(v2); \ 931 (_v(cmp) < 0 ? -1 : (_v(cmp) > 0 ? +1 : 0)); \ 940 #define vec_search(v,E) \ 943 while (_v(i) < vec_len(v)) \ 945 if ((v)[_v(i)] == E) \ 949 if (_v(i) == vec_len(v)) \ 961 #define vec_search_with_function(v,E,fn) \ 964 while (_v(i) < vec_len(v)) \ 966 if (0 != fn(&(v)[_v(i)], (E))) \ 970 if (_v(i) == vec_len(v)) \ 983 #define vec_sort_with_function(vec,f) \ 985 if (vec_len (vec) > 1) \ 986 qsort (vec, vec_len (vec), sizeof (vec[0]), (void *) (f)); \ 995 #define vec_validate_init_c_string(V, S, L) \ 997 vec_reset_length (V); \ 998 vec_validate ((V), (L)); \ 1000 clib_memcpy_fast ((V), (S), (L)); \ 1010 #define vec_c_string_is_terminated(V) \ 1011 (((V) != 0) && (vec_len (V) != 0) && ((V)[vec_len ((V)) - 1] == 0)) 1018 #define vec_terminate_c_string(V) \ 1020 u32 vl = vec_len ((V)); \ 1021 if (!vec_c_string_is_terminated(V)) \ 1023 vec_validate ((V), vl); \ uword clib_mem_is_vec_h(void *v, uword header_bytes)
Predicate function, says whether the supplied vector is a clib heap object (general version)...
Optimized string handling code, including c11-compliant "safe C library" variants.
static uword vec_header_bytes(uword header_bytes)
void * vec_resize_allocate_memory(void *v, word length_increment, uword data_bytes, uword header_bytes, uword data_align)
Low-level resize allocation function, usually not called directly.
static uword clib_mem_size(void *p)
static uword clib_mem_is_heap_object(void *p)
static uword clib_mem_is_vec(void *v)
Predicate function, says whether the supplied vector is a clib heap object.
Vector bootstrap header file.