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,
164 uword new_data_bytes, aligned_header_bytes;
168 new_data_bytes = data_bytes + aligned_header_bytes;
172 void *p = v - aligned_header_bytes;
207 #define _v(var) _vec_##var 221 #define vec_resize_ha(V,N,H,A) \ 224 word _v(l) = vec_len (V); \ 225 V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \ 237 #define vec_resize(V,N) vec_resize_ha(V,N,0,0) 250 #define vec_resize_aligned(V,N,A) vec_resize_ha(V,N,0,A) 261 #define vec_alloc_ha(V,N,H,A) \ 263 uword _v(l) = vec_len (V); \ 264 vec_resize_ha (V, N, H, A); \ 265 _vec_len (V) = _v(l); \ 275 #define vec_alloc(V,N) vec_alloc_ha(V,N,0,0) 284 #define vec_alloc_aligned(V,N,A) vec_alloc_ha(V,N,0,A) 293 #define vec_new_ha(T,N,H,A) \ 296 _vec_resize ((T *) 0, _v(n), _v(n) * sizeof (T), (H), (A)); \ 306 #define vec_new(T,N) vec_new_ha(T,N,0,0) 315 #define vec_new_aligned(T,N,A) vec_new_ha(T,N,0,A) 323 #define vec_free_h(V,H) \ 327 clib_mem_free (vec_header ((V), (H))); \ 336 #define vec_free(V) vec_free_h(V,0) 342 #define vec_free_header(h) clib_mem_free (h) 353 #define vec_dup_ha(V,H,A) \ 355 __typeof__ ((V)[0]) * _v(v) = 0; \ 356 uword _v(l) = vec_len (V); \ 359 vec_resize_ha (_v(v), _v(l), (H), (A)); \ 360 clib_memcpy (_v(v), (V), _v(l) * sizeof ((V)[0]));\ 370 #define vec_dup(V) vec_dup_ha(V,0,0) 379 #define vec_dup_aligned(V,A) vec_dup_ha(V,0,A) 387 #define vec_copy(DST,SRC) clib_memcpy (DST, SRC, vec_len (DST) * \ 396 #define vec_clone(NEW_V,OLD_V) \ 399 (NEW_V) = _vec_resize ((NEW_V), vec_len (OLD_V), \ 400 vec_len (OLD_V) * sizeof ((NEW_V)[0]), (0), (0)); \ 412 #define vec_validate_ha(V,I,H,A) \ 414 STATIC_ASSERT(A==0 || ((A % sizeof(V[0]))==0) || ((sizeof(V[0]) % A) == 0),\ 415 "vector validate aligned on incorrectly sized object"); \ 417 word _v(l) = vec_len (V); \ 418 if (_v(i) >= _v(l)) \ 420 vec_resize_ha ((V), 1 + (_v(i) - _v(l)), (H), (A)); \ 423 memset ((V) + _v(l), 0, (1 + (_v(i) - _v(l))) * sizeof ((V)[0])); \ 434 #define vec_validate(V,I) vec_validate_ha(V,I,0,0) 445 #define vec_validate_aligned(V,I,A) vec_validate_ha(V,I,0,A) 457 #define vec_validate_init_empty_ha(V,I,INIT,H,A) \ 460 word _v(l) = vec_len (V); \ 461 if (_v(i) >= _v(l)) \ 463 vec_resize_ha ((V), 1 + (_v(i) - _v(l)), (H), (A)); \ 464 while (_v(l) <= _v(i)) \ 466 (V)[_v(l)] = (INIT); \ 483 #define vec_validate_init_empty(V,I,INIT) \ 484 vec_validate_init_empty_ha(V,I,INIT,0,0) 496 #define vec_validate_init_empty_aligned(V,I,INIT,A) \ 497 vec_validate_init_empty_ha(V,I,INIT,0,A) 507 #define vec_add1_ha(V,E,H,A) \ 509 word _v(l) = vec_len (V); \ 510 V = _vec_resize ((V), 1, (_v(l) + 1) * sizeof ((V)[0]), (H), (A)); \ 520 #define vec_add1(V,E) vec_add1_ha(V,E,0,0) 530 #define vec_add1_aligned(V,E,A) vec_add1_ha(V,E,0,A) 542 #define vec_add2_ha(V,P,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)); \ 559 #define vec_add2(V,P,N) vec_add2_ha(V,P,N,0,0) 571 #define vec_add2_aligned(V,P,N,A) vec_add2_ha(V,P,N,0,A) 582 #define vec_add_ha(V,E,N,H,A) \ 585 word _v(l) = vec_len (V); \ 586 V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \ 587 clib_memcpy ((V) + _v(l), (E), _v(n) * sizeof ((V)[0])); \ 597 #define vec_add(V,E,N) vec_add_ha(V,E,N,0,0) 607 #define vec_add_aligned(V,E,N,A) vec_add_ha(V,E,N,0,A) 616 uword _v(l) = vec_len (V); \ 617 ASSERT (_v(l) > 0); \ 619 _vec_len (V) = _v (l); \ 630 #define vec_pop2(V,E) \ 632 uword _v(l) = vec_len (V); \ 633 if (_v(l) > 0) (E) = vec_pop (V); \ 648 #define vec_insert_init_empty_ha(V,N,M,INIT,H,A) \ 650 word _v(l) = vec_len (V); \ 653 V = _vec_resize ((V), \ 655 (_v(l) + _v(n))*sizeof((V)[0]), \ 657 ASSERT (_v(m) <= _v(l)); \ 658 memmove ((V) + _v(m) + _v(n), \ 660 (_v(l) - _v(m)) * sizeof ((V)[0])); \ 661 memset ((V) + _v(m), INIT, _v(n) * sizeof ((V)[0])); \ 674 #define vec_insert_ha(V,N,M,H,A) vec_insert_init_empty_ha(V,N,M,0,H,A) 684 #define vec_insert(V,N,M) vec_insert_ha(V,N,M,0,0) 695 #define vec_insert_aligned(V,N,M,A) vec_insert_ha(V,N,M,0,A) 707 #define vec_insert_init_empty(V,N,M,INIT) \ 708 vec_insert_init_empty_ha(V,N,M,INIT,0,0) 721 #define vec_insert_init_empty_aligned(V,N,M,INIT,A) \ 722 vec_insert_init_empty_ha(V,N,M,INIT,0,A) 736 #define vec_insert_elts_ha(V,E,N,M,H,A) \ 738 word _v(l) = vec_len (V); \ 741 V = _vec_resize ((V), \ 743 (_v(l) + _v(n))*sizeof((V)[0]), \ 745 ASSERT (_v(m) <= _v(l)); \ 746 memmove ((V) + _v(m) + _v(n), \ 748 (_v(l) - _v(m)) * sizeof ((V)[0])); \ 749 clib_memcpy ((V) + _v(m), (E), \ 750 _v(n) * sizeof ((V)[0])); \ 762 #define vec_insert_elts(V,E,N,M) vec_insert_elts_ha(V,E,N,M,0,0) 774 #define vec_insert_elts_aligned(V,E,N,M,A) vec_insert_elts_ha(V,E,N,M,0,A) 783 #define vec_delete(V,N,M) \ 785 word _v(l) = vec_len (V); \ 789 if (_v(l) - _v(n) - _v(m) > 0) \ 790 memmove ((V) + _v(m), (V) + _v(m) + _v(n), \ 791 (_v(l) - _v(n) - _v(m)) * sizeof ((V)[0])); \ 794 memset ((V) + _v(l) - _v(n), 0, _v(n) * sizeof ((V)[0])); \ 795 _vec_len (V) -= _v(n); \ 803 #define vec_del1(v,i) \ 805 uword _vec_del_l = _vec_len (v) - 1; \ 806 uword _vec_del_i = (i); \ 807 if (_vec_del_i < _vec_del_l) \ 808 (v)[_vec_del_i] = (v)[_vec_del_l]; \ 809 _vec_len (v) = _vec_del_l; \ 817 #define vec_append(v1,v2) \ 819 uword _v(l1) = vec_len (v1); \ 820 uword _v(l2) = vec_len (v2); \ 822 v1 = _vec_resize ((v1), _v(l2), \ 823 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, 0); \ 824 clib_memcpy ((v1) + _v(l1), (v2), _v(l2) * sizeof ((v2)[0])); \ 833 #define vec_append_aligned(v1,v2,align) \ 835 uword _v(l1) = vec_len (v1); \ 836 uword _v(l2) = vec_len (v2); \ 838 v1 = _vec_resize ((v1), _v(l2), \ 839 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, align); \ 840 clib_memcpy ((v1) + _v(l1), (v2), _v(l2) * sizeof ((v2)[0])); \ 848 #define vec_prepend(v1,v2) \ 850 uword _v(l1) = vec_len (v1); \ 851 uword _v(l2) = vec_len (v2); \ 853 v1 = _vec_resize ((v1), _v(l2), \ 854 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, 0); \ 855 memmove ((v1) + _v(l2), (v1), _v(l1) * sizeof ((v1)[0])); \ 856 clib_memcpy ((v1), (v2), _v(l2) * sizeof ((v2)[0])); \ 865 #define vec_prepend_aligned(v1,v2,align) \ 867 uword _v(l1) = vec_len (v1); \ 868 uword _v(l2) = vec_len (v2); \ 870 v1 = _vec_resize ((v1), _v(l2), \ 871 (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, align); \ 872 memmove ((v1) + _v(l2), (v1), _v(l1) * sizeof ((v1)[0])); \ 873 clib_memcpy ((v1), (v2), _v(l2) * sizeof ((v2)[0])); \ 880 #define vec_zero(var) \ 883 memset ((var), 0, vec_len (var) * sizeof ((var)[0])); \ 890 #define vec_set(v,val) \ 893 __typeof__ ((v)[0]) _val = (val); \ 894 for (_v(i) = 0; _v(i) < vec_len (v); _v(i)++) \ 908 #define vec_is_equal(v1,v2) \ 909 (vec_len (v1) == vec_len (v2) && ! memcmp ((v1), (v2), vec_len (v1) * sizeof ((v1)[0]))) 918 #define vec_cmp(v1,v2) \ 920 word _v(i), _v(cmp), _v(l); \ 921 _v(l) = clib_min (vec_len (v1), vec_len (v2)); \ 923 for (_v(i) = 0; _v(i) < _v(l); _v(i)++) { \ 924 _v(cmp) = (v1)[_v(i)] - (v2)[_v(i)]; \ 928 if (_v(cmp) == 0 && _v(l) > 0) \ 929 _v(cmp) = vec_len(v1) - vec_len(v2); \ 930 (_v(cmp) < 0 ? -1 : (_v(cmp) > 0 ? +1 : 0)); \ 939 #define vec_search(v,E) \ 942 while (_v(i) < vec_len(v)) \ 944 if ((v)[_v(i)] == E) \ 948 if (_v(i) == vec_len(v)) \ 958 #define vec_sort_with_function(vec,f) \ 960 qsort (vec, vec_len (vec), sizeof (vec[0]), (void *) (f)); \ 969 #define vec_validate_init_c_string(V, S, L) \ 971 vec_reset_length (V); \ 972 vec_validate ((V), (L)); \ 974 clib_memcpy ((V), (S), (L)); \ 984 #define vec_c_string_is_terminated(V) \ 985 (((V) != 0) && (vec_len (V) != 0) && ((V)[vec_len ((V)) - 1] == 0)) 992 #define vec_terminate_c_string(V) \ 994 u32 vl = vec_len ((V)); \ 995 if (!vec_c_string_is_terminated(V)) \ 997 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.