FD.io VPP  v18.01-8-g0eacf49
Vector Packet Processing
vec.h File Reference

CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers". More...

+ Include dependency graph for vec.h:

Go to the source code of this file.

Macros

#define vec_resize_ha(V, N, H, A)
 Resize a vector (general version). More...
 
#define vec_resize(V, N)   vec_resize_ha(V,N,0,0)
 Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V, return pointer to start of vector. More...
 
#define vec_resize_aligned(V, N, A)   vec_resize_ha(V,N,0,A)
 Resize a vector (no header, alignment specified). More...
 
#define vec_alloc_ha(V, N, H, A)
 Allocate space for N more elements. More...
 
#define vec_alloc(V, N)   vec_alloc_ha(V,N,0,0)
 Allocate space for N more elements (no header, unspecified alignment) More...
 
#define vec_alloc_aligned(V, N, A)   vec_alloc_ha(V,N,0,A)
 Allocate space for N more elements (no header, given alignment) More...
 
#define vec_new_ha(T, N, H, A)
 Create new vector of given type and length (general version). More...
 
#define vec_new(T, N)   vec_new_ha(T,N,0,0)
 Create new vector of given type and length (unspecified alignment, no header). More...
 
#define vec_new_aligned(T, N, A)   vec_new_ha(T,N,0,A)
 Create new vector of given type and length (alignment specified, no header). More...
 
#define vec_free_h(V, H)
 Free vector's memory (general version) More...
 
#define vec_free(V)   vec_free_h(V,0)
 Free vector's memory (no header). More...
 
#define vec_free_header(h)   clib_mem_free (h)
 Free vector user header (syntactic sugar) More...
 
#define vec_dup_ha(V, H, A)
 Return copy of vector (general version). More...
 
#define vec_dup(V)   vec_dup_ha(V,0,0)
 Return copy of vector (no header, no alignment) More...
 
#define vec_dup_aligned(V, A)   vec_dup_ha(V,0,A)
 Return copy of vector (no header, alignment specified). More...
 
#define vec_copy(DST, SRC)
 Copy a vector, memcpy wrapper. More...
 
#define vec_clone(NEW_V, OLD_V)
 Clone a vector. More...
 
#define vec_validate_ha(V, I, H, A)
 Make sure vector is long enough for given index (general version). More...
 
#define vec_validate(V, I)   vec_validate_ha(V,I,0,0)
 Make sure vector is long enough for given index (no header, unspecified alignment) More...
 
#define vec_validate_aligned(V, I, A)   vec_validate_ha(V,I,0,A)
 Make sure vector is long enough for given index (no header, specified alignment) More...
 
#define vec_validate_init_empty_ha(V, I, INIT, H, A)
 Make sure vector is long enough for given index and initialize empty space (general version) More...
 
#define vec_validate_init_empty(V, I, INIT)   vec_validate_init_empty_ha(V,I,INIT,0,0)
 Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment) More...
 
#define vec_validate_init_empty_aligned(V, I, INIT, A)   vec_validate_init_empty_ha(V,I,INIT,0,A)
 Make sure vector is long enough for given index and initialize empty space (no header, alignment alignment) More...
 
#define vec_add1_ha(V, E, H, A)
 Add 1 element to end of vector (general version). More...
 
#define vec_add1(V, E)   vec_add1_ha(V,E,0,0)
 Add 1 element to end of vector (unspecified alignment). More...
 
#define vec_add1_aligned(V, E, A)   vec_add1_ha(V,E,0,A)
 Add 1 element to end of vector (alignment specified). More...
 
#define vec_add2_ha(V, P, N, H, A)
 Add N elements to end of vector V, return pointer to new elements in P. More...
 
#define vec_add2(V, P, N)   vec_add2_ha(V,P,N,0,0)
 Add N elements to end of vector V, return pointer to new elements in P. More...
 
#define vec_add2_aligned(V, P, N, A)   vec_add2_ha(V,P,N,0,A)
 Add N elements to end of vector V, return pointer to new elements in P. More...
 
#define vec_add_ha(V, E, N, H, A)
 Add N elements to end of vector V (general version) More...
 
#define vec_add(V, E, N)   vec_add_ha(V,E,N,0,0)
 Add N elements to end of vector V (no header, unspecified alignment) More...
 
#define vec_add_aligned(V, E, N, A)   vec_add_ha(V,E,N,0,A)
 Add N elements to end of vector V (no header, specified alignment) More...
 
#define vec_pop(V)
 Returns last element of a vector and decrements its length. More...
 
#define vec_pop2(V, E)
 Set E to the last element of a vector, decrement vector length. More...
 
#define vec_insert_init_empty_ha(V, N, M, INIT, H, A)
 Insert N vector elements starting at element M, initialize new elements (general version). More...
 
#define vec_insert_ha(V, N, M, H, A)   vec_insert_init_empty_ha(V,N,M,0,H,A)
 Insert N vector elements starting at element M, initialize new elements to zero (general version) More...
 
#define vec_insert(V, N, M)   vec_insert_ha(V,N,M,0,0)
 Insert N vector elements starting at element M, initialize new elements to zero (no header, unspecified alignment) More...
 
#define vec_insert_aligned(V, N, M, A)   vec_insert_ha(V,N,M,0,A)
 Insert N vector elements starting at element M, initialize new elements to zero (no header, alignment specified) More...
 
#define vec_insert_init_empty(V, N, M, INIT)   vec_insert_init_empty_ha(V,N,M,INIT,0,0)
 Insert N vector elements starting at element M, initialize new elements (no header, unspecified alignment) More...
 
#define vec_insert_init_empty_aligned(V, N, M, INIT, A)   vec_insert_init_empty_ha(V,N,M,INIT,0,A)
 Insert N vector elements starting at element M, initialize new elements (no header, specified alignment) More...
 
#define vec_insert_elts_ha(V, E, N, M, H, A)
 Insert N vector elements starting at element M, insert given elements (general version) More...
 
#define vec_insert_elts(V, E, N, M)   vec_insert_elts_ha(V,E,N,M,0,0)
 Insert N vector elements starting at element M, insert given elements (no header, unspecified alignment) More...
 
#define vec_insert_elts_aligned(V, E, N, M, A)   vec_insert_elts_ha(V,E,N,M,0,A)
 Insert N vector elements starting at element M, insert given elements (no header, specified alignment) More...
 
#define vec_delete(V, N, M)
 Delete N elements starting at element M. More...
 
#define vec_del1(v, i)
 Delete the element at index I. More...
 
#define vec_append(v1, v2)
 Append v2 after v1. More...
 
#define vec_append_aligned(v1, v2, align)
 Append v2 after v1. More...
 
#define vec_prepend(v1, v2)
 Prepend v2 before v1. More...
 
#define vec_prepend_aligned(v1, v2, align)
 Prepend v2 before v1. More...
 
#define vec_zero(var)
 Zero all vector elements. More...
 
#define vec_set(v, val)
 Set all vector elements to given value. More...
 
#define vec_is_equal(v1, v2)   (vec_len (v1) == vec_len (v2) && ! memcmp ((v1), (v2), vec_len (v1) * sizeof ((v1)[0])))
 Compare two vectors, not NULL-pointer tolerant. More...
 
#define vec_cmp(v1, v2)
 Compare two vectors (only applicable to vectors of signed numbers). More...
 
#define vec_search(v, E)
 Search a vector for the index of the entry that matches. More...
 
#define vec_sort_with_function(vec, f)
 Sort a vector using the supplied element comparison function. More...
 
#define vec_validate_init_c_string(V, S, L)
 Make a vector containing a NULL terminated c-string. More...
 
#define vec_c_string_is_terminated(V)   (((V) != 0) && (vec_len (V) != 0) && ((V)[vec_len ((V)) - 1] == 0))
 Test whether a vector is a NULL terminated c-string. More...
 
#define vec_terminate_c_string(V)
 (If necessary) NULL terminate a vector containing a c-string. More...
 

Functions

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. More...
 
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). More...
 
static uword clib_mem_is_vec (void *v)
 Predicate function, says whether the supplied vector is a clib heap object. More...
 

Detailed Description

CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".

Many CLIB data structures (e.g. hash, heap, pool) are vectors with various different headers.

The memory layout looks like this:

user header (aligned to uword boundary)
vector length: number of elements
user's pointer-> vector element #0
vector element #1
...

The user pointer contains the address of vector element # 0. Null pointer vectors are valid and mean a zero length vector.

You can reset the length of an allocated vector to zero via the vec_reset_length(v) macro, or by setting the vector length field to zero (e.g. _vec_len (v) = 0). Vec_reset_length(v) preferred: it understands Null pointers.

Typically, the header is not present. Headers allow for other data structures to be built atop CLIB vectors.

Users may specify the alignment for data elements via the vec_*_aligned macros.

Vectors elements can be any C type e.g. (int, double, struct bar). This is also true for data types built atop vectors (e.g. heap, pool, etc.).

Many macros have _a variants supporting alignment of vector data and _h variants supporting non zero length vector headers. The _ha variants support both.

Standard programming error: memorize a pointer to the ith element of a vector then expand it. Vectors expand by 3/2, so such code may appear to work for a period of time. Memorize vector indices which are invariant.

Definition in file vec.h.

Macro Definition Documentation

#define vec_add (   V,
  E,
 
)    vec_add_ha(V,E,N,0,0)

Add N elements to end of vector V (no header, unspecified alignment)

Parameters
Vpointer to a vector
Epointer to element(s) to add
Nnumber of elements to add
Returns
V (value-result macro parameter)

Definition at line 595 of file vec.h.

#define vec_add1 (   V,
 
)    vec_add1_ha(V,E,0,0)

Add 1 element to end of vector (unspecified alignment).

Parameters
Vpointer to a vector
Eelement to add
Returns
V (value-result macro parameter)

Definition at line 518 of file vec.h.

#define vec_add1_aligned (   V,
  E,
 
)    vec_add1_ha(V,E,0,A)

Add 1 element to end of vector (alignment specified).

Parameters
Vpointer to a vector
Eelement to add
Hheader size in bytes (may be zero)
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 528 of file vec.h.

#define vec_add1_ha (   V,
  E,
  H,
 
)
Value:
do { \
word _v(l) = vec_len (V); \
V = _vec_resize ((V), 1, (_v(l) + 1) * sizeof ((V)[0]), (H), (A)); \
(V)[_v(l)] = (E); \
} while (0)
#define H(b, c, d)
Definition: md5.c:46
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Add 1 element to end of vector (general version).

Parameters
Vpointer to a vector
Eelement to add
Hheader size in bytes (may be zero)
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 505 of file vec.h.

#define vec_add2 (   V,
  P,
 
)    vec_add2_ha(V,P,N,0,0)

Add N elements to end of vector V, return pointer to new elements in P.

(no header, unspecified alignment)

Parameters
Vpointer to a vector
Ppointer to new vector element(s)
Nnumber of elements to add
Returns
V and P (value-result macro parameters)

Definition at line 557 of file vec.h.

#define vec_add2_aligned (   V,
  P,
  N,
 
)    vec_add2_ha(V,P,N,0,A)

Add N elements to end of vector V, return pointer to new elements in P.

(no header, alignment specified)

Parameters
Vpointer to a vector
Ppointer to new vector element(s)
Nnumber of elements to add
Aalignment (may be zero)
Returns
V and P (value-result macro parameters)

Definition at line 569 of file vec.h.

#define vec_add2_ha (   V,
  P,
  N,
  H,
 
)
Value:
do { \
word _v(n) = (N); \
word _v(l) = vec_len (V); \
V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \
P = (V) + _v(l); \
} while (0)
#define H(b, c, d)
Definition: md5.c:46
#define P(fmt,...)
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Add N elements to end of vector V, return pointer to new elements in P.

(general version)

Parameters
Vpointer to a vector
Ppointer to new vector element(s)
Nnumber of elements to add
Hheader size in bytes (may be zero)
Aalignment (may be zero)
Returns
V and P (value-result macro parameters)

Definition at line 540 of file vec.h.

#define vec_add_aligned (   V,
  E,
  N,
 
)    vec_add_ha(V,E,N,0,A)

Add N elements to end of vector V (no header, specified alignment)

Parameters
Vpointer to a vector
Epointer to element(s) to add
Nnumber of elements to add
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 605 of file vec.h.

#define vec_add_ha (   V,
  E,
  N,
  H,
 
)
Value:
do { \
word _v(n) = (N); \
word _v(l) = vec_len (V); \
V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \
clib_memcpy ((V) + _v(l), (E), _v(n) * sizeof ((V)[0])); \
} while (0)
#define H(b, c, d)
Definition: md5.c:46
#define clib_memcpy(a, b, c)
Definition: string.h:75
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Add N elements to end of vector V (general version)

Parameters
Vpointer to a vector
Epointer to element(s) to add
Nnumber of elements to add
Hheader size in bytes (may be zero)
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 580 of file vec.h.

#define vec_alloc (   V,
 
)    vec_alloc_ha(V,N,0,0)

Allocate space for N more elements (no header, unspecified alignment)

Parameters
Vpointer to a vector
Nnumber of elements to add
Returns
V (value-result macro parameter)

Definition at line 275 of file vec.h.

#define vec_alloc_aligned (   V,
  N,
 
)    vec_alloc_ha(V,N,0,A)

Allocate space for N more elements (no header, given alignment)

Parameters
Vpointer to a vector
Nnumber of elements to add
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 284 of file vec.h.

#define vec_alloc_ha (   V,
  N,
  H,
 
)
Value:
do { \
uword _v(l) = vec_len (V); \
vec_resize_ha (V, N, H, A); \
_vec_len (V) = _v(l); \
} while (0)
#define H(b, c, d)
Definition: md5.c:46
#define vec_resize_ha(V, N, H, A)
Resize a vector (general version).
Definition: vec.h:221
u64 uword
Definition: types.h:112
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Allocate space for N more elements.

Parameters
Vpointer to a vector
Nnumber of elements to add
Hheader size in bytes (may be zero)
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 261 of file vec.h.

#define vec_append (   v1,
  v2 
)
Value:
do { \
uword _v(l1) = vec_len (v1); \
uword _v(l2) = vec_len (v2); \
\
v1 = _vec_resize ((v1), _v(l2), \
(_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, 0); \
clib_memcpy ((v1) + _v(l1), (v2), _v(l2) * sizeof ((v2)[0])); \
} while (0)
#define clib_memcpy(a, b, c)
Definition: string.h:75
u64 uword
Definition: types.h:112
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Append v2 after v1.

Result in v1.

Parameters
V1target vector
V2vector to append

Definition at line 815 of file vec.h.

#define vec_append_aligned (   v1,
  v2,
  align 
)
Value:
do { \
uword _v(l1) = vec_len (v1); \
uword _v(l2) = vec_len (v2); \
\
v1 = _vec_resize ((v1), _v(l2), \
(_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, align); \
clib_memcpy ((v1) + _v(l1), (v2), _v(l2) * sizeof ((v2)[0])); \
} while (0)
#define clib_memcpy(a, b, c)
Definition: string.h:75
u64 uword
Definition: types.h:112
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Append v2 after v1.

Result in v1. Specified alignment.

Parameters
V1target vector
V2vector to append
alignrequired alignment

Definition at line 831 of file vec.h.

#define vec_c_string_is_terminated (   V)    (((V) != 0) && (vec_len (V) != 0) && ((V)[vec_len ((V)) - 1] == 0))

Test whether a vector is a NULL terminated c-string.

Parameters
V(possibly NULL) pointer to a vector.
Returns
BOOLEAN indicating if the vector c-string is null terminated.

Definition at line 982 of file vec.h.

#define vec_clone (   NEW_V,
  OLD_V 
)
Value:
do { \
(NEW_V) = 0; \
(NEW_V) = _vec_resize ((NEW_V), vec_len (OLD_V), \
vec_len (OLD_V) * sizeof ((NEW_V)[0]), (0), (0)); \
} while (0)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Clone a vector.

Make a new vector with the same size as a given vector but possibly with a different type.

Parameters
NEW_Vpointer to new vector
OLD_Vpointer to old vector

Definition at line 396 of file vec.h.

#define vec_cmp (   v1,
  v2 
)
Value:
({ \
word _v(i), _v(cmp), _v(l); \
_v(l) = clib_min (vec_len (v1), vec_len (v2)); \
_v(cmp) = 0; \
for (_v(i) = 0; _v(i) < _v(l); _v(i)++) { \
_v(cmp) = (v1)[_v(i)] - (v2)[_v(i)]; \
if (_v(cmp)) \
break; \
} \
if (_v(cmp) == 0 && _v(l) > 0) \
_v(cmp) = vec_len(v1) - vec_len(v2); \
(_v(cmp) < 0 ? -1 : (_v(cmp) > 0 ? +1 : 0)); \
})
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
#define clib_min(x, y)
Definition: clib.h:340
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Compare two vectors (only applicable to vectors of signed numbers).

Used in qsort compare functions.

Parameters
v1Pointer to a vector
v2Pointer to a vector
Returns
-1, 0, +1

Definition at line 916 of file vec.h.

#define vec_copy (   DST,
  SRC 
)
Value:
clib_memcpy (DST, SRC, vec_len (DST) * \
sizeof ((DST)[0]))
#define clib_memcpy(a, b, c)
Definition: string.h:75
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Copy a vector, memcpy wrapper.

Assumes sizeof(SRC[0]) == sizeof(DST[0])

Parameters
DSTdestination
SRCsource

Definition at line 387 of file vec.h.

#define vec_del1 (   v,
  i 
)
Value:
do { \
uword _vec_del_l = _vec_len (v) - 1; \
uword _vec_del_i = (i); \
if (_vec_del_i < _vec_del_l) \
(v)[_vec_del_i] = (v)[_vec_del_l]; \
_vec_len (v) = _vec_del_l; \
} while (0)
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
#define v
Definition: acl.c:341
u64 uword
Definition: types.h:112

Delete the element at index I.

Parameters
Vpointer to a vector
Iindex to delete

Definition at line 801 of file vec.h.

#define vec_delete (   V,
  N,
  M 
)
Value:
do { \
word _v(l) = vec_len (V); \
word _v(n) = (N); \
word _v(m) = (M); \
/* Copy over deleted elements. */ \
if (_v(l) - _v(n) - _v(m) > 0) \
memmove ((V) + _v(m), (V) + _v(m) + _v(n), \
(_v(l) - _v(n) - _v(m)) * sizeof ((V)[0])); \
/* Zero empty space at end (for future re-allocation). */ \
if (_v(n) > 0) \
memset ((V) + _v(l) - _v(n), 0, _v(n) * sizeof ((V)[0])); \
_vec_len (V) -= _v(n); \
} while (0)
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define M(T, mp)

Delete N elements starting at element M.

Parameters
Vpointer to a vector
Nnumber of elements to delete
Mfirst element to delete
Returns
V (value-result macro parameter)

Definition at line 781 of file vec.h.

#define vec_dup (   V)    vec_dup_ha(V,0,0)

Return copy of vector (no header, no alignment)

Parameters
Vpointer to a vector
Returns
Vdup copy of vector

Definition at line 370 of file vec.h.

#define vec_dup_aligned (   V,
 
)    vec_dup_ha(V,0,A)

Return copy of vector (no header, alignment specified).

Parameters
Vpointer to a vector
Aalignment (may be zero)
Returns
Vdup copy of vector

Definition at line 379 of file vec.h.

#define vec_dup_ha (   V,
  H,
 
)
Value:
({ \
__typeof__ ((V)[0]) * _v(v) = 0; \
uword _v(l) = vec_len (V); \
if (_v(l) > 0) \
{ \
vec_resize_ha (_v(v), _v(l), (H), (A)); \
clib_memcpy (_v(v), (V), _v(l) * sizeof ((V)[0]));\
} \
_v(v); \
})
#define H(b, c, d)
Definition: md5.c:46
#define v
Definition: acl.c:341
#define clib_memcpy(a, b, c)
Definition: string.h:75
#define vec_resize_ha(V, N, H, A)
Resize a vector (general version).
Definition: vec.h:221
u64 uword
Definition: types.h:112
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Return copy of vector (general version).

Parameters
Vpointer to a vector
Hsize of header in bytes
Aalignment (may be zero)
Returns
Vdup copy of vector

Definition at line 353 of file vec.h.

#define vec_free (   V)    vec_free_h(V,0)

Free vector's memory (no header).

Parameters
Vpointer to a vector
Returns
V (value-result parameter, V=0)

Definition at line 336 of file vec.h.

#define vec_free_h (   V,
  H 
)
Value:
do { \
if (V) \
{ \
V = 0; \
} \
} while (0)
#define H(b, c, d)
Definition: md5.c:46
static void clib_mem_free(void *p)
Definition: mem.h:179
static void * vec_header(void *v, uword header_bytes)
Find a user vector header.
Definition: vec_bootstrap.h:92

Free vector's memory (general version)

Parameters
Vpointer to a vector
Hsize of header in bytes
Returns
V (value-result parameter, V=0)

Definition at line 323 of file vec.h.

#define vec_free_header (   h)    clib_mem_free (h)

Free vector user header (syntactic sugar)

Parameters
hvector header

Definition at line 342 of file vec.h.

#define vec_insert (   V,
  N,
  M 
)    vec_insert_ha(V,N,M,0,0)

Insert N vector elements starting at element M, initialize new elements to zero (no header, unspecified alignment)

Parameters
V(possibly NULL) pointer to a vector.
Nnumber of elements to insert
Minsertion point
Returns
V (value-result macro parameter)

Definition at line 682 of file vec.h.

#define vec_insert_aligned (   V,
  N,
  M,
 
)    vec_insert_ha(V,N,M,0,A)

Insert N vector elements starting at element M, initialize new elements to zero (no header, alignment specified)

Parameters
V(possibly NULL) pointer to a vector.
Nnumber of elements to insert
Minsertion point
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 693 of file vec.h.

#define vec_insert_elts (   V,
  E,
  N,
  M 
)    vec_insert_elts_ha(V,E,N,M,0,0)

Insert N vector elements starting at element M, insert given elements (no header, unspecified alignment)

Parameters
V(possibly NULL) pointer to a vector.
Eelement(s) to insert
Nnumber of elements to insert
Minsertion point
Returns
V (value-result macro parameter)

Definition at line 760 of file vec.h.

#define vec_insert_elts_aligned (   V,
  E,
  N,
  M,
 
)    vec_insert_elts_ha(V,E,N,M,0,A)

Insert N vector elements starting at element M, insert given elements (no header, specified alignment)

Parameters
V(possibly NULL) pointer to a vector.
Eelement(s) to insert
Nnumber of elements to insert
Minsertion point
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 772 of file vec.h.

#define vec_insert_elts_ha (   V,
  E,
  N,
  M,
  H,
 
)
Value:
do { \
word _v(l) = vec_len (V); \
word _v(n) = (N); \
word _v(m) = (M); \
V = _vec_resize ((V), \
_v(n), \
(_v(l) + _v(n))*sizeof((V)[0]), \
(H), (A)); \
ASSERT (_v(m) <= _v(l)); \
memmove ((V) + _v(m) + _v(n), \
(V) + _v(m), \
(_v(l) - _v(m)) * sizeof ((V)[0])); \
clib_memcpy ((V) + _v(m), (E), \
_v(n) * sizeof ((V)[0])); \
} while (0)
#define H(b, c, d)
Definition: md5.c:46
#define clib_memcpy(a, b, c)
Definition: string.h:75
#define ASSERT(truth)
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define M(T, mp)

Insert N vector elements starting at element M, insert given elements (general version)

Parameters
V(possibly NULL) pointer to a vector.
Eelement(s) to insert
Nnumber of elements to insert
Minsertion point
Hheader size in bytes (may be zero)
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 734 of file vec.h.

#define vec_insert_ha (   V,
  N,
  M,
  H,
 
)    vec_insert_init_empty_ha(V,N,M,0,H,A)

Insert N vector elements starting at element M, initialize new elements to zero (general version)

Parameters
V(possibly NULL) pointer to a vector.
Nnumber of elements to insert
Minsertion point
Hheader size in bytes (may be zero)
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 672 of file vec.h.

#define vec_insert_init_empty (   V,
  N,
  M,
  INIT 
)    vec_insert_init_empty_ha(V,N,M,INIT,0,0)

Insert N vector elements starting at element M, initialize new elements (no header, unspecified alignment)

Parameters
V(possibly NULL) pointer to a vector.
Nnumber of elements to insert
Minsertion point
INITinitial value (can be a complex expression!)
Returns
V (value-result macro parameter)

Definition at line 705 of file vec.h.

#define vec_insert_init_empty_aligned (   V,
  N,
  M,
  INIT,
 
)    vec_insert_init_empty_ha(V,N,M,INIT,0,A)

Insert N vector elements starting at element M, initialize new elements (no header, specified alignment)

Parameters
V(possibly NULL) pointer to a vector.
Nnumber of elements to insert
Minsertion point
INITinitial value (can be a complex expression!)
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 719 of file vec.h.

#define vec_insert_init_empty_ha (   V,
  N,
  M,
  INIT,
  H,
 
)
Value:
do { \
word _v(l) = vec_len (V); \
word _v(n) = (N); \
word _v(m) = (M); \
V = _vec_resize ((V), \
_v(n), \
(_v(l) + _v(n))*sizeof((V)[0]), \
(H), (A)); \
ASSERT (_v(m) <= _v(l)); \
memmove ((V) + _v(m) + _v(n), \
(V) + _v(m), \
(_v(l) - _v(m)) * sizeof ((V)[0])); \
memset ((V) + _v(m), INIT, _v(n) * sizeof ((V)[0])); \
} while (0)
#define H(b, c, d)
Definition: md5.c:46
#define ASSERT(truth)
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define M(T, mp)

Insert N vector elements starting at element M, initialize new elements (general version).

Parameters
V(possibly NULL) pointer to a vector.
Nnumber of elements to insert
Minsertion point
INITinitial value (can be a complex expression!)
Hheader size in bytes (may be zero)
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 646 of file vec.h.

#define vec_is_equal (   v1,
  v2 
)    (vec_len (v1) == vec_len (v2) && ! memcmp ((v1), (v2), vec_len (v1) * sizeof ((v1)[0])))

Compare two vectors, not NULL-pointer tolerant.

Parameters
v1Pointer to a vector
v2Pointer to a vector
Returns
1 if equal, 0 if unequal

Definition at line 906 of file vec.h.

#define vec_new (   T,
 
)    vec_new_ha(T,N,0,0)

Create new vector of given type and length (unspecified alignment, no header).

Parameters
Ttype of elements in new vector
Nnumber of elements to add
Returns
V new vector

Definition at line 306 of file vec.h.

#define vec_new_aligned (   T,
  N,
 
)    vec_new_ha(T,N,0,A)

Create new vector of given type and length (alignment specified, no header).

Parameters
Ttype of elements in new vector
Nnumber of elements to add
Aalignment (may be zero)
Returns
V new vector

Definition at line 315 of file vec.h.

#define vec_new_ha (   T,
  N,
  H,
 
)
Value:
({ \
word _v(n) = (N); \
_vec_resize ((T *) 0, _v(n), _v(n) * sizeof (T), (H), (A)); \
})
#define H(b, c, d)
Definition: md5.c:46
i64 word
Definition: types.h:111

Create new vector of given type and length (general version).

Parameters
Ttype of elements in new vector
Nnumber of elements to add
Hheader size in bytes (may be zero)
Aalignment (may be zero)
Returns
V new vector

Definition at line 293 of file vec.h.

#define vec_pop (   V)
Value:
({ \
uword _v(l) = vec_len (V); \
ASSERT (_v(l) > 0); \
_v(l) -= 1; \
_vec_len (V) = _v (l); \
(V)[_v(l)]; \
})
#define ASSERT(truth)
u64 uword
Definition: types.h:112
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Returns last element of a vector and decrements its length.

Parameters
Vpointer to a vector
Returns
E element removed from the end of the vector

Definition at line 612 of file vec.h.

#define vec_pop2 (   V,
 
)
Value:
({ \
uword _v(l) = vec_len (V); \
if (_v(l) > 0) (E) = vec_pop (V); \
_v(l) > 0; \
})
#define vec_pop(V)
Returns last element of a vector and decrements its length.
Definition: vec.h:612
u64 uword
Definition: types.h:112
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Set E to the last element of a vector, decrement vector length.

Parameters
Vpointer to a vector
Epointer to the last vector element
Returns
E element removed from the end of the vector (value-result macro parameter

Definition at line 628 of file vec.h.

#define vec_prepend (   v1,
  v2 
)
Value:
do { \
uword _v(l1) = vec_len (v1); \
uword _v(l2) = vec_len (v2); \
\
v1 = _vec_resize ((v1), _v(l2), \
(_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, 0); \
memmove ((v1) + _v(l2), (v1), _v(l1) * sizeof ((v1)[0])); \
clib_memcpy ((v1), (v2), _v(l2) * sizeof ((v2)[0])); \
} while (0)
#define clib_memcpy(a, b, c)
Definition: string.h:75
u64 uword
Definition: types.h:112
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Prepend v2 before v1.

Result in v1.

Parameters
V1target vector
V2vector to prepend

Definition at line 846 of file vec.h.

#define vec_prepend_aligned (   v1,
  v2,
  align 
)
Value:
do { \
uword _v(l1) = vec_len (v1); \
uword _v(l2) = vec_len (v2); \
\
v1 = _vec_resize ((v1), _v(l2), \
(_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, align); \
memmove ((v1) + _v(l2), (v1), _v(l1) * sizeof ((v1)[0])); \
clib_memcpy ((v1), (v2), _v(l2) * sizeof ((v2)[0])); \
} while (0)
#define clib_memcpy(a, b, c)
Definition: string.h:75
u64 uword
Definition: types.h:112
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Prepend v2 before v1.

Result in v1. Specified alignment

Parameters
V1target vector
V2vector to prepend
alignrequired alignment

Definition at line 863 of file vec.h.

#define vec_resize (   V,
 
)    vec_resize_ha(V,N,0,0)

Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V, return pointer to start of vector.

Vector will have room for H header bytes and will have user's data aligned at alignment A (rounded to next power of 2).

Parameters
Vpointer to a vector
Nnumber of elements to add
Returns
V (value-result macro parameter)

Definition at line 237 of file vec.h.

#define vec_resize_aligned (   V,
  N,
 
)    vec_resize_ha(V,N,0,A)

Resize a vector (no header, alignment specified).

Add N elements to end of given vector V, return pointer to start of vector. Vector will have room for H header bytes and will have user's data aligned at alignment A (rounded to next power of 2).

Parameters
Vpointer to a vector
Nnumber of elements to add
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 250 of file vec.h.

#define vec_resize_ha (   V,
  N,
  H,
 
)
Value:
do { \
word _v(n) = (N); \
word _v(l) = vec_len (V); \
V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \
} while (0)
#define H(b, c, d)
Definition: md5.c:46
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Resize a vector (general version).

Add N elements to end of given vector V, return pointer to start of vector. Vector will have room for H header bytes and will have user's data aligned at alignment A (rounded to next power of 2).

Parameters
Vpointer to a vector
Nnumber of elements to add
Hheader size in bytes (may be zero)
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 221 of file vec.h.

#define vec_search (   v,
 
)
Value:
({ \
word _v(i) = 0; \
while (_v(i) < vec_len(v)) \
{ \
if ((v)[_v(i)] == E) \
break; \
_v(i)++; \
} \
if (_v(i) == vec_len(v)) \
_v(i) = ~0; \
_v(i); \
})
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
#define v
Definition: acl.c:341
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Search a vector for the index of the entry that matches.

Parameters
v1Pointer to a vector
v2Entry to match
Returns
index of match or ~0

Definition at line 937 of file vec.h.

#define vec_set (   v,
  val 
)
Value:
do { \
word _v(i); \
__typeof__ ((v)[0]) _val = (val); \
for (_v(i) = 0; _v(i) < vec_len (v); _v(i)++) \
(v)[_v(i)] = _val; \
} while (0)
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
#define v
Definition: acl.c:341
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Set all vector elements to given value.

Null-pointer tolerant.

Parameters
vvector to set
valvalue for each vector element

Definition at line 888 of file vec.h.

#define vec_sort_with_function (   vec,
 
)
Value:
do { \
qsort (vec, vec_len (vec), sizeof (vec[0]), (void *) (f)); \
} while (0)
void qsort(void *base, uword n, uword size, int(*compar)(const void *, const void *))
Definition: qsort.c:56
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Sort a vector using the supplied element comparison function.

Parameters
vecvector to sort
fcomparison function

Definition at line 956 of file vec.h.

#define vec_terminate_c_string (   V)
Value:
do { \
u32 vl = vec_len ((V)); \
{ \
vec_validate ((V), vl); \
(V)[vl] = 0; \
} \
} while (0)
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:432
u32 vl(void *p)
Definition: threads.c:30
#define vec_c_string_is_terminated(V)
Test whether a vector is a NULL terminated c-string.
Definition: vec.h:982
unsigned int u32
Definition: types.h:88
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

(If necessary) NULL terminate a vector containing a c-string.

Parameters
V(possibly NULL) pointer to a vector.
Returns
V (value-result macro parameter)

Definition at line 990 of file vec.h.

#define vec_validate (   V,
  I 
)    vec_validate_ha(V,I,0,0)

Make sure vector is long enough for given index (no header, unspecified alignment)

Parameters
V(possibly NULL) pointer to a vector.
Ivector index which will be valid upon return
Returns
V (value-result macro parameter)

Definition at line 432 of file vec.h.

#define vec_validate_aligned (   V,
  I,
 
)    vec_validate_ha(V,I,0,A)

Make sure vector is long enough for given index (no header, specified alignment)

Parameters
V(possibly NULL) pointer to a vector.
Ivector index which will be valid upon return
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 443 of file vec.h.

#define vec_validate_ha (   V,
  I,
  H,
 
)
Value:
do { \
word _v(i) = (I); \
word _v(l) = vec_len (V); \
if (_v(i) >= _v(l)) \
{ \
vec_resize_ha ((V), 1 + (_v(i) - _v(l)), (H), (A)); \
/* Must zero new space since user may have previously \
used e.g. _vec_len (v) -= 10 */ \
memset ((V) + _v(l), 0, (1 + (_v(i) - _v(l))) * sizeof ((V)[0])); \
} \
} while (0)
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
#define H(b, c, d)
Definition: md5.c:46
#define vec_resize_ha(V, N, H, A)
Resize a vector (general version).
Definition: vec.h:221
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define I(b, c, d)
Definition: md5.c:47

Make sure vector is long enough for given index (general version).

Parameters
V(possibly NULL) pointer to a vector.
Ivector index which will be valid upon return
Hheader size in bytes (may be zero)
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 412 of file vec.h.

#define vec_validate_init_c_string (   V,
  S,
 
)
Value:
do { \
vec_validate ((V), (L)); \
if ((S) && (L)) \
clib_memcpy ((V), (S), (L)); \
(V)[(L)] = 0; \
} while (0)
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:432
#define S(mp)
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
#define clib_memcpy(a, b, c)
Definition: string.h:75

Make a vector containing a NULL terminated c-string.

Parameters
V(possibly NULL) pointer to a vector.
Spointer to string buffer.
Lstring length (NOT including the terminating NULL; a la strlen())

Definition at line 967 of file vec.h.

#define vec_validate_init_empty (   V,
  I,
  INIT 
)    vec_validate_init_empty_ha(V,I,INIT,0,0)

Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)

Parameters
V(possibly NULL) pointer to a vector.
Ivector index which will be valid upon return
INITinitial value (can be a complex expression!)
Hheader size in bytes (may be zero)
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 481 of file vec.h.

#define vec_validate_init_empty_aligned (   V,
  I,
  INIT,
 
)    vec_validate_init_empty_ha(V,I,INIT,0,A)

Make sure vector is long enough for given index and initialize empty space (no header, alignment alignment)

Parameters
V(possibly NULL) pointer to a vector.
Ivector index which will be valid upon return
INITinitial value (can be a complex expression!)
Hheader size in bytes (may be zero)
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 494 of file vec.h.

#define vec_validate_init_empty_ha (   V,
  I,
  INIT,
  H,
 
)
Value:
do { \
word _v(i) = (I); \
word _v(l) = vec_len (V); \
if (_v(i) >= _v(l)) \
{ \
vec_resize_ha ((V), 1 + (_v(i) - _v(l)), (H), (A)); \
while (_v(l) <= _v(i)) \
{ \
(V)[_v(l)] = (INIT); \
_v(l)++; \
} \
} \
} while (0)
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
#define H(b, c, d)
Definition: md5.c:46
#define vec_resize_ha(V, N, H, A)
Resize a vector (general version).
Definition: vec.h:221
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define I(b, c, d)
Definition: md5.c:47

Make sure vector is long enough for given index and initialize empty space (general version)

Parameters
V(possibly NULL) pointer to a vector.
Ivector index which will be valid upon return
INITinitial value (can be a complex expression!)
Hheader size in bytes (may be zero)
Aalignment (may be zero)
Returns
V (value-result macro parameter)

Definition at line 455 of file vec.h.

#define vec_zero (   var)
Value:
do { \
if (var) \
memset ((var), 0, vec_len (var) * sizeof ((var)[0])); \
} while (0)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Zero all vector elements.

Null-pointer tolerant.

Parameters
varVector to zero

Definition at line 878 of file vec.h.

Function Documentation

static uword clib_mem_is_vec ( void *  v)
inlinestatic

Predicate function, says whether the supplied vector is a clib heap object.

Parameters
vpointer to a vector
Returns
0 or 1

Definition at line 201 of file vec.h.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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).

Parameters
vpointer to a vector
header_bytesvector header size in bytes (may be zero)
Returns
0 or 1

Definition at line 109 of file vec.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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.

Parameters
vpointer to a vector
length_incrementlength increment in elements
data_bytesrequested size in bytes
header_bytesheader size in bytes (may be zero)
data_alignalignment (may be zero)
Returns
v_prime pointer to resized vector, may or may not equal v

Definition at line 44 of file vec.c.

+ Here is the call graph for this function: