38 #ifndef included_vec_h 39 #define included_vec_h 100 word length_increment,
116 _vec_resize (
void * v,
117 word length_increment,
123 uword new_data_bytes, aligned_header_bytes;
127 new_data_bytes = data_bytes + aligned_header_bytes;
131 void * p = v - aligned_header_bytes;
139 vh->
len += length_increment;
169 #define _v(var) _vec_##var 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)); \ 199 #define vec_resize(V,N) vec_resize_ha(V,N,0,0) 212 #define vec_resize_aligned(V,N,A) vec_resize_ha(V,N,0,A) 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); \ 237 #define vec_alloc(V,N) vec_alloc_ha(V,N,0,0) 246 #define vec_alloc_aligned(V,N,A) vec_alloc_ha(V,N,0,A) 255 #define vec_new_ha(T,N,H,A) \ 258 _vec_resize ((T *) 0, _v(n), _v(n) * sizeof (T), (H), (A)); \ 268 #define vec_new(T,N) vec_new_ha(T,N,0,0) 277 #define vec_new_aligned(T,N,A) vec_new_ha(T,N,0,A) 285 #define vec_free_h(V,H) \ 289 clib_mem_free (vec_header ((V), (H))); \ 298 #define vec_free(V) vec_free_h(V,0) 304 #define vec_free_header(h) clib_mem_free (h) 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]));\ 332 #define vec_dup(V) vec_dup_ha(V,0,0) 341 #define vec_dup_aligned(V,A) vec_dup_ha(V,0,A) 349 #define vec_copy(DST,SRC) clib_memcpy (DST, SRC, vec_len (DST) * \ 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)); \ 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)); \ 383 memset ((V) + _v(l), 0, (1 + (_v(i) - _v(l))) * sizeof ((V)[0])); \ 394 #define vec_validate(V,I) vec_validate_ha(V,I,0,0) 405 #define vec_validate_aligned(V,I,A) vec_validate_ha(V,I,0,A) 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); \ 443 #define vec_validate_init_empty(V,I,INIT) \ 444 vec_validate_init_empty_ha(V,I,INIT,0,0) 456 #define vec_validate_init_empty_aligned(V,I,A) \ 457 vec_validate_init_empty_ha(V,I,INIT,0,A) 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)); \ 480 #define vec_add1(V,E) vec_add1_ha(V,E,0,0) 490 #define vec_add1_aligned(V,E,A) vec_add1_ha(V,E,0,A) 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)); \ 519 #define vec_add2(V,P,N) vec_add2_ha(V,P,N,0,0) 531 #define vec_add2_aligned(V,P,N,A) vec_add2_ha(V,P,N,0,A) 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])); \ 557 #define vec_add(V,E,N) vec_add_ha(V,E,N,0,0) 567 #define vec_add_aligned(V,E,N,A) vec_add_ha(V,E,N,0,A) 576 uword _v(l) = vec_len (V); \ 577 ASSERT (_v(l) > 0); \ 579 _vec_len (V) = _v (l); \ 590 #define vec_pop2(V,E) \ 592 uword _v(l) = vec_len (V); \ 593 if (_v(l) > 0) (E) = vec_pop (V); \ 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])); \ 634 #define vec_insert_ha(V,N,M,H,A) vec_insert_init_empty_ha(V,N,M,0,H,A) 644 #define vec_insert(V,N,M) vec_insert_ha(V,N,M,0,0) 655 #define vec_insert_aligned(V,N,M,A) vec_insert_ha(V,N,M,0,A) 667 #define vec_insert_init_empty(V,N,M,INIT) \ 668 vec_insert_init_empty_ha(V,N,M,INIT,0,0) 681 #define vec_insert_init_empty_aligned(V,N,M,INIT,A) \ 682 vec_insert_init_empty_ha(V,N,M,INIT,0,A) 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])); \ 722 #define vec_insert_elts(V,E,N,M) vec_insert_elts_ha(V,E,N,M,0,0) 734 #define vec_insert_elts_aligned(V,E,N,M,A) vec_insert_elts_ha(V,E,N,M,0,A) 743 #define vec_delete(V,N,M) \ 745 word _v(l) = vec_len (V); \ 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])); \ 754 memset ((V) + _v(l) - _v(n), 0, _v(n) * sizeof ((V)[0])); \ 755 _vec_len (V) -= _v(n); \ 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; \ 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])); \ 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])); \ 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])); \ 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])); \ 840 #define vec_zero(var) \ 843 memset ((var), 0, vec_len (var) * sizeof ((var)[0])); \ 850 #define vec_set(v,val) \ 853 __typeof__ ((v)[0]) _val = (val); \ 854 for (_v(i) = 0; _v(i) < vec_len (v); _v(i)++) \ 868 #define vec_is_equal(v1,v2) \ 869 (vec_len (v1) == vec_len (v2) && ! memcmp ((v1), (v2), vec_len (v1) * sizeof ((v1)[0]))) 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)); \ 898 #define vec_sort_with_function(vec,f) \ 900 qsort (vec, vec_len (vec), sizeof (vec[0]), (void *) (f)); \ 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)); \ 924 #define vec_c_string_is_terminated(V) \ 925 (((V) != 0) && (vec_len (V) != 0) && ((V)[vec_len ((V)) - 1] == 0)) 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); \ 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)...
always_inline uword clib_mem_size(void *p)
always_inline uword clib_mem_is_vec(void *v)
Predicate function, says whether the supplied vector is a clib heap object.
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.
always_inline uword clib_mem_is_heap_object(void *p)
Vector bootsrap header file.
always_inline uword vec_header_bytes(uword header_bytes)