38 #ifndef included_vec_h 39 #define included_vec_h 100 word length_increment,
115 _vec_resize (
void *v,
116 word length_increment,
120 uword new_data_bytes, aligned_header_bytes;
124 new_data_bytes = data_bytes + aligned_header_bytes;
128 void *p = v - aligned_header_bytes;
136 vh->
len += length_increment;
159 _vec_resize_will_expand (
void *v,
160 word length_increment,
165 uword new_data_bytes, aligned_header_bytes;
169 new_data_bytes = data_bytes + aligned_header_bytes;
173 void *p = v - aligned_header_bytes;
181 vh->
len += length_increment;
211 #define _v(var) _vec_##var 225 #define vec_resize_ha(V,N,H,A) \ 228 word _v(l) = vec_len (V); \ 229 V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \ 241 #define vec_resize(V,N) vec_resize_ha(V,N,0,0) 254 #define vec_resize_aligned(V,N,A) vec_resize_ha(V,N,0,A) 265 #define vec_alloc_ha(V,N,H,A) \ 267 uword _v(l) = vec_len (V); \ 268 vec_resize_ha (V, N, H, A); \ 269 _vec_len (V) = _v(l); \ 279 #define vec_alloc(V,N) vec_alloc_ha(V,N,0,0) 288 #define vec_alloc_aligned(V,N,A) vec_alloc_ha(V,N,0,A) 297 #define vec_new_ha(T,N,H,A) \ 300 _vec_resize ((T *) 0, _v(n), _v(n) * sizeof (T), (H), (A)); \ 310 #define vec_new(T,N) vec_new_ha(T,N,0,0) 319 #define vec_new_aligned(T,N,A) vec_new_ha(T,N,0,A) 327 #define vec_free_h(V,H) \ 331 clib_mem_free (vec_header ((V), (H))); \ 340 #define vec_free(V) vec_free_h(V,0) 346 #define vec_free_header(h) clib_mem_free (h) 357 #define vec_dup_ha(V,H,A) \ 359 __typeof__ ((V)[0]) * _v(v) = 0; \ 360 uword _v(l) = vec_len (V); \ 363 vec_resize_ha (_v(v), _v(l), (H), (A)); \ 364 clib_memcpy (_v(v), (V), _v(l) * sizeof ((V)[0]));\ 374 #define vec_dup(V) vec_dup_ha(V,0,0) 383 #define vec_dup_aligned(V,A) vec_dup_ha(V,0,A) 391 #define vec_copy(DST,SRC) clib_memcpy (DST, SRC, vec_len (DST) * \ 400 #define vec_clone(NEW_V,OLD_V) \ 403 (NEW_V) = _vec_resize ((NEW_V), vec_len (OLD_V), \ 404 vec_len (OLD_V) * sizeof ((NEW_V)[0]), (0), (0)); \ 416 #define vec_validate_ha(V,I,H,A) \ 419 word _v(l) = vec_len (V); \ 420 if (_v(i) >= _v(l)) \ 422 vec_resize_ha ((V), 1 + (_v(i) - _v(l)), (H), (A)); \ 425 memset ((V) + _v(l), 0, (1 + (_v(i) - _v(l))) * sizeof ((V)[0])); \ 436 #define vec_validate(V,I) vec_validate_ha(V,I,0,0) 447 #define vec_validate_aligned(V,I,A) vec_validate_ha(V,I,0,A) 459 #define vec_validate_init_empty_ha(V,I,INIT,H,A) \ 462 word _v(l) = vec_len (V); \ 463 if (_v(i) >= _v(l)) \ 465 vec_resize_ha ((V), 1 + (_v(i) - _v(l)), (H), (A)); \ 466 while (_v(l) <= _v(i)) \ 468 (V)[_v(l)] = (INIT); \ 485 #define vec_validate_init_empty(V,I,INIT) \ 486 vec_validate_init_empty_ha(V,I,INIT,0,0) 498 #define vec_validate_init_empty_aligned(V,I,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) 532 #define vec_add1_aligned(V,E,A) vec_add1_ha(V,E,0,A) 544 #define vec_add2_ha(V,P,N,H,A) \ 547 word _v(l) = vec_len (V); \ 548 V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \ 561 #define vec_add2(V,P,N) vec_add2_ha(V,P,N,0,0) 573 #define vec_add2_aligned(V,P,N,A) vec_add2_ha(V,P,N,0,A) 584 #define vec_add_ha(V,E,N,H,A) \ 587 word _v(l) = vec_len (V); \ 588 V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \ 589 clib_memcpy ((V) + _v(l), (E), _v(n) * sizeof ((V)[0])); \ 599 #define vec_add(V,E,N) vec_add_ha(V,E,N,0,0) 609 #define vec_add_aligned(V,E,N,A) vec_add_ha(V,E,N,0,A) 618 uword _v(l) = vec_len (V); \ 619 ASSERT (_v(l) > 0); \ 621 _vec_len (V) = _v (l); \ 632 #define vec_pop2(V,E) \ 634 uword _v(l) = vec_len (V); \ 635 if (_v(l) > 0) (E) = vec_pop (V); \ 650 #define vec_insert_init_empty_ha(V,N,M,INIT,H,A) \ 652 word _v(l) = vec_len (V); \ 655 V = _vec_resize ((V), \ 657 (_v(l) + _v(n))*sizeof((V)[0]), \ 659 ASSERT (_v(m) <= _v(l)); \ 660 memmove ((V) + _v(m) + _v(n), \ 662 (_v(l) - _v(m)) * sizeof ((V)[0])); \ 663 memset ((V) + _v(m), INIT, _v(n) * sizeof ((V)[0])); \ 676 #define vec_insert_ha(V,N,M,H,A) vec_insert_init_empty_ha(V,N,M,0,H,A) 686 #define vec_insert(V,N,M) vec_insert_ha(V,N,M,0,0) 697 #define vec_insert_aligned(V,N,M,A) vec_insert_ha(V,N,M,0,A) 709 #define vec_insert_init_empty(V,N,M,INIT) \ 710 vec_insert_init_empty_ha(V,N,M,INIT,0,0) 723 #define vec_insert_init_empty_aligned(V,N,M,INIT,A) \ 724 vec_insert_init_empty_ha(V,N,M,INIT,0,A) 738 #define vec_insert_elts_ha(V,E,N,M,H,A) \ 740 word _v(l) = vec_len (V); \ 743 V = _vec_resize ((V), \ 745 (_v(l) + _v(n))*sizeof((V)[0]), \ 747 ASSERT (_v(m) <= _v(l)); \ 748 memmove ((V) + _v(m) + _v(n), \ 750 (_v(l) - _v(m)) * sizeof ((V)[0])); \ 751 clib_memcpy ((V) + _v(m), (E), \ 752 _v(n) * sizeof ((V)[0])); \ 764 #define vec_insert_elts(V,E,N,M) vec_insert_elts_ha(V,E,N,M,0,0) 776 #define vec_insert_elts_aligned(V,E,N,M,A) vec_insert_elts_ha(V,E,N,M,0,A) 785 #define vec_delete(V,N,M) \ 787 word _v(l) = vec_len (V); \ 791 if (_v(l) - _v(n) - _v(m) > 0) \ 792 memmove ((V) + _v(m), (V) + _v(m) + _v(n), \ 793 (_v(l) - _v(n) - _v(m)) * sizeof ((V)[0])); \ 796 memset ((V) + _v(l) - _v(n), 0, _v(n) * sizeof ((V)[0])); \ 797 _vec_len (V) -= _v(n); \ 805 #define vec_del1(v,i) \ 807 uword _vec_del_l = _vec_len (v) - 1; \ 808 uword _vec_del_i = (i); \ 809 if (_vec_del_i < _vec_del_l) \ 810 (v)[_vec_del_i] = (v)[_vec_del_l]; \ 811 _vec_len (v) = _vec_del_l; \ 819 #define vec_append(v1,v2) \ 821 uword _v(l1) = vec_len (v1); \ 822 uword _v(l2) = vec_len (v2); \ 824 v1 = _vec_resize ((v1), _v(l2), \ 825 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, 0); \ 826 clib_memcpy ((v1) + _v(l1), (v2), _v(l2) * sizeof ((v2)[0])); \ 835 #define vec_append_aligned(v1,v2,align) \ 837 uword _v(l1) = vec_len (v1); \ 838 uword _v(l2) = vec_len (v2); \ 840 v1 = _vec_resize ((v1), _v(l2), \ 841 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, align); \ 842 clib_memcpy ((v1) + _v(l1), (v2), _v(l2) * sizeof ((v2)[0])); \ 850 #define vec_prepend(v1,v2) \ 852 uword _v(l1) = vec_len (v1); \ 853 uword _v(l2) = vec_len (v2); \ 855 v1 = _vec_resize ((v1), _v(l2), \ 856 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, 0); \ 857 memmove ((v1) + _v(l2), (v1), _v(l1) * sizeof ((v1)[0])); \ 858 clib_memcpy ((v1), (v2), _v(l2) * sizeof ((v2)[0])); \ 867 #define vec_prepend_aligned(v1,v2,align) \ 869 uword _v(l1) = vec_len (v1); \ 870 uword _v(l2) = vec_len (v2); \ 872 v1 = _vec_resize ((v1), _v(l2), \ 873 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, align); \ 874 memmove ((v1) + _v(l2), (v1), _v(l1) * sizeof ((v1)[0])); \ 875 clib_memcpy ((v1), (v2), _v(l2) * sizeof ((v2)[0])); \ 882 #define vec_zero(var) \ 885 memset ((var), 0, vec_len (var) * sizeof ((var)[0])); \ 892 #define vec_set(v,val) \ 895 __typeof__ ((v)[0]) _val = (val); \ 896 for (_v(i) = 0; _v(i) < vec_len (v); _v(i)++) \ 910 #define vec_is_equal(v1,v2) \ 911 (vec_len (v1) == vec_len (v2) && ! memcmp ((v1), (v2), vec_len (v1) * sizeof ((v1)[0]))) 920 #define vec_cmp(v1,v2) \ 922 word _v(i), _v(cmp), _v(l); \ 923 _v(l) = clib_min (vec_len (v1), vec_len (v2)); \ 925 for (_v(i) = 0; _v(i) < _v(l); _v(i)++) { \ 926 _v(cmp) = (v1)[_v(i)] - (v2)[_v(i)]; \ 930 if (_v(cmp) == 0 && _v(l) > 0) \ 931 _v(cmp) = vec_len(v1) - vec_len(v2); \ 932 (_v(cmp) < 0 ? -1 : (_v(cmp) > 0 ? +1 : 0)); \ 941 #define vec_search(v,E) \ 944 while (_v(i) < vec_len(v)) \ 950 if (_v(i) == vec_len(v)) \ 960 #define vec_sort_with_function(vec,f) \ 962 qsort (vec, vec_len (vec), sizeof (vec[0]), (void *) (f)); \ 971 #define vec_validate_init_c_string(V, S, L) \ 973 vec_reset_length (V); \ 974 vec_validate ((V), (L)); \ 976 clib_memcpy ((V), (S), (L)); \ 986 #define vec_c_string_is_terminated(V) \ 987 (((V) != 0) && (vec_len (V) != 0) && ((V)[vec_len ((V)) - 1] == 0)) 994 #define vec_terminate_c_string(V) \ 996 u32 vl = vec_len ((V)); \ 997 if (!vec_c_string_is_terminated(V)) \ 999 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)...
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 bootsrap header file.