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;
171 #define _v(var) _vec_##var 185 #define vec_resize_ha(V,N,H,A) \ 188 word _v(l) = vec_len (V); \ 189 V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \ 201 #define vec_resize(V,N) vec_resize_ha(V,N,0,0) 214 #define vec_resize_aligned(V,N,A) vec_resize_ha(V,N,0,A) 225 #define vec_alloc_ha(V,N,H,A) \ 227 uword _v(l) = vec_len (V); \ 228 vec_resize_ha (V, N, H, A); \ 229 _vec_len (V) = _v(l); \ 239 #define vec_alloc(V,N) vec_alloc_ha(V,N,0,0) 248 #define vec_alloc_aligned(V,N,A) vec_alloc_ha(V,N,0,A) 257 #define vec_new_ha(T,N,H,A) \ 260 _vec_resize ((T *) 0, _v(n), _v(n) * sizeof (T), (H), (A)); \ 270 #define vec_new(T,N) vec_new_ha(T,N,0,0) 279 #define vec_new_aligned(T,N,A) vec_new_ha(T,N,0,A) 287 #define vec_free_h(V,H) \ 291 clib_mem_free (vec_header ((V), (H))); \ 300 #define vec_free(V) vec_free_h(V,0) 306 #define vec_free_header(h) clib_mem_free (h) 317 #define vec_dup_ha(V,H,A) \ 319 __typeof__ ((V)[0]) * _v(v) = 0; \ 320 uword _v(l) = vec_len (V); \ 323 vec_resize_ha (_v(v), _v(l), (H), (A)); \ 324 clib_memcpy (_v(v), (V), _v(l) * sizeof ((V)[0]));\ 334 #define vec_dup(V) vec_dup_ha(V,0,0) 343 #define vec_dup_aligned(V,A) vec_dup_ha(V,0,A) 351 #define vec_copy(DST,SRC) clib_memcpy (DST, SRC, vec_len (DST) * \ 360 #define vec_clone(NEW_V,OLD_V) \ 363 (NEW_V) = _vec_resize ((NEW_V), vec_len (OLD_V), \ 364 vec_len (OLD_V) * sizeof ((NEW_V)[0]), (0), (0)); \ 376 #define vec_validate_ha(V,I,H,A) \ 379 word _v(l) = vec_len (V); \ 380 if (_v(i) >= _v(l)) \ 382 vec_resize_ha ((V), 1 + (_v(i) - _v(l)), (H), (A)); \ 385 memset ((V) + _v(l), 0, (1 + (_v(i) - _v(l))) * sizeof ((V)[0])); \ 396 #define vec_validate(V,I) vec_validate_ha(V,I,0,0) 407 #define vec_validate_aligned(V,I,A) vec_validate_ha(V,I,0,A) 419 #define vec_validate_init_empty_ha(V,I,INIT,H,A) \ 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)); \ 426 while (_v(l) <= _v(i)) \ 428 (V)[_v(l)] = (INIT); \ 445 #define vec_validate_init_empty(V,I,INIT) \ 446 vec_validate_init_empty_ha(V,I,INIT,0,0) 458 #define vec_validate_init_empty_aligned(V,I,A) \ 459 vec_validate_init_empty_ha(V,I,INIT,0,A) 469 #define vec_add1_ha(V,E,H,A) \ 471 word _v(l) = vec_len (V); \ 472 V = _vec_resize ((V), 1, (_v(l) + 1) * sizeof ((V)[0]), (H), (A)); \ 482 #define vec_add1(V,E) vec_add1_ha(V,E,0,0) 492 #define vec_add1_aligned(V,E,A) vec_add1_ha(V,E,0,A) 504 #define vec_add2_ha(V,P,N,H,A) \ 507 word _v(l) = vec_len (V); \ 508 V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \ 521 #define vec_add2(V,P,N) vec_add2_ha(V,P,N,0,0) 533 #define vec_add2_aligned(V,P,N,A) vec_add2_ha(V,P,N,0,A) 544 #define vec_add_ha(V,E,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)); \ 549 clib_memcpy ((V) + _v(l), (E), _v(n) * sizeof ((V)[0])); \ 559 #define vec_add(V,E,N) vec_add_ha(V,E,N,0,0) 569 #define vec_add_aligned(V,E,N,A) vec_add_ha(V,E,N,0,A) 578 uword _v(l) = vec_len (V); \ 579 ASSERT (_v(l) > 0); \ 581 _vec_len (V) = _v (l); \ 592 #define vec_pop2(V,E) \ 594 uword _v(l) = vec_len (V); \ 595 if (_v(l) > 0) (E) = vec_pop (V); \ 610 #define vec_insert_init_empty_ha(V,N,M,INIT,H,A) \ 612 word _v(l) = vec_len (V); \ 615 V = _vec_resize ((V), \ 617 (_v(l) + _v(n))*sizeof((V)[0]), \ 619 ASSERT (_v(m) <= _v(l)); \ 620 memmove ((V) + _v(m) + _v(n), \ 622 (_v(l) - _v(m)) * sizeof ((V)[0])); \ 623 memset ((V) + _v(m), INIT, _v(n) * sizeof ((V)[0])); \ 636 #define vec_insert_ha(V,N,M,H,A) vec_insert_init_empty_ha(V,N,M,0,H,A) 646 #define vec_insert(V,N,M) vec_insert_ha(V,N,M,0,0) 657 #define vec_insert_aligned(V,N,M,A) vec_insert_ha(V,N,M,0,A) 669 #define vec_insert_init_empty(V,N,M,INIT) \ 670 vec_insert_init_empty_ha(V,N,M,INIT,0,0) 683 #define vec_insert_init_empty_aligned(V,N,M,INIT,A) \ 684 vec_insert_init_empty_ha(V,N,M,INIT,0,A) 698 #define vec_insert_elts_ha(V,E,N,M,H,A) \ 700 word _v(l) = vec_len (V); \ 703 V = _vec_resize ((V), \ 705 (_v(l) + _v(n))*sizeof((V)[0]), \ 707 ASSERT (_v(m) <= _v(l)); \ 708 memmove ((V) + _v(m) + _v(n), \ 710 (_v(l) - _v(m)) * sizeof ((V)[0])); \ 711 clib_memcpy ((V) + _v(m), (E), \ 712 _v(n) * sizeof ((V)[0])); \ 724 #define vec_insert_elts(V,E,N,M) vec_insert_elts_ha(V,E,N,M,0,0) 736 #define vec_insert_elts_aligned(V,E,N,M,A) vec_insert_elts_ha(V,E,N,M,0,A) 745 #define vec_delete(V,N,M) \ 747 word _v(l) = vec_len (V); \ 751 if (_v(l) - _v(n) - _v(m) > 0) \ 752 memmove ((V) + _v(m), (V) + _v(m) + _v(n), \ 753 (_v(l) - _v(n) - _v(m)) * sizeof ((V)[0])); \ 756 memset ((V) + _v(l) - _v(n), 0, _v(n) * sizeof ((V)[0])); \ 757 _vec_len (V) -= _v(n); \ 765 #define vec_del1(v,i) \ 767 uword _vec_del_l = _vec_len (v) - 1; \ 768 uword _vec_del_i = (i); \ 769 if (_vec_del_i < _vec_del_l) \ 770 (v)[_vec_del_i] = (v)[_vec_del_l]; \ 771 _vec_len (v) = _vec_del_l; \ 779 #define vec_append(v1,v2) \ 781 uword _v(l1) = vec_len (v1); \ 782 uword _v(l2) = vec_len (v2); \ 784 v1 = _vec_resize ((v1), _v(l2), \ 785 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, 0); \ 786 clib_memcpy ((v1) + _v(l1), (v2), _v(l2) * sizeof ((v2)[0])); \ 795 #define vec_append_aligned(v1,v2,align) \ 797 uword _v(l1) = vec_len (v1); \ 798 uword _v(l2) = vec_len (v2); \ 800 v1 = _vec_resize ((v1), _v(l2), \ 801 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, align); \ 802 clib_memcpy ((v1) + _v(l1), (v2), _v(l2) * sizeof ((v2)[0])); \ 810 #define vec_prepend(v1,v2) \ 812 uword _v(l1) = vec_len (v1); \ 813 uword _v(l2) = vec_len (v2); \ 815 v1 = _vec_resize ((v1), _v(l2), \ 816 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, 0); \ 817 memmove ((v1) + _v(l2), (v1), _v(l1) * sizeof ((v1)[0])); \ 818 clib_memcpy ((v1), (v2), _v(l2) * sizeof ((v2)[0])); \ 827 #define vec_prepend_aligned(v1,v2,align) \ 829 uword _v(l1) = vec_len (v1); \ 830 uword _v(l2) = vec_len (v2); \ 832 v1 = _vec_resize ((v1), _v(l2), \ 833 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, align); \ 834 memmove ((v1) + _v(l2), (v1), _v(l1) * sizeof ((v1)[0])); \ 835 clib_memcpy ((v1), (v2), _v(l2) * sizeof ((v2)[0])); \ 842 #define vec_zero(var) \ 845 memset ((var), 0, vec_len (var) * sizeof ((var)[0])); \ 852 #define vec_set(v,val) \ 855 __typeof__ ((v)[0]) _val = (val); \ 856 for (_v(i) = 0; _v(i) < vec_len (v); _v(i)++) \ 870 #define vec_is_equal(v1,v2) \ 871 (vec_len (v1) == vec_len (v2) && ! memcmp ((v1), (v2), vec_len (v1) * sizeof ((v1)[0]))) 880 #define vec_cmp(v1,v2) \ 882 word _v(i), _v(cmp), _v(l); \ 883 _v(l) = clib_min (vec_len (v1), vec_len (v2)); \ 885 for (_v(i) = 0; _v(i) < _v(l); _v(i)++) { \ 886 _v(cmp) = (v1)[_v(i)] - (v2)[_v(i)]; \ 890 if (_v(cmp) == 0 && _v(l) > 0) \ 891 _v(cmp) = vec_len(v1) - vec_len(v2); \ 892 (_v(cmp) < 0 ? -1 : (_v(cmp) > 0 ? +1 : 0)); \ 901 #define vec_search(v,E) \ 904 while (_v(i) < vec_len(v)) \ 910 if (_v(i) == vec_len(v)) \ 920 #define vec_sort_with_function(vec,f) \ 922 qsort (vec, vec_len (vec), sizeof (vec[0]), (void *) (f)); \ 931 #define vec_validate_init_c_string(V, S, L) \ 933 vec_reset_length (V); \ 934 vec_validate ((V), (L)); \ 936 clib_memcpy ((V), (S), (L)); \ 946 #define vec_c_string_is_terminated(V) \ 947 (((V) != 0) && (vec_len (V) != 0) && ((V)[vec_len ((V)) - 1] == 0)) 954 #define vec_terminate_c_string(V) \ 956 u32 vl = vec_len ((V)); \ 957 if (!vec_c_string_is_terminated(V)) \ 959 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.