@return v_prime pointer to resized vector, may or may not equal v
*/
+#define _vec_resize(V,L,DB,HB,A) \
+ _vec_resize_inline(V,L,DB,HB,clib_max((__alignof__((V)[0])),(A)))
+
always_inline void *
-_vec_resize (void *v,
- word length_increment,
- uword data_bytes, uword header_bytes, uword data_align)
+_vec_resize_inline (void *v,
+ word length_increment,
+ uword data_bytes, uword header_bytes, uword data_align)
{
vec_header_t *vh = _vec_find (v);
uword new_data_bytes, aligned_header_bytes;
data_align));
}
-/** \brief Low-level vector resize predicate
+/** \brief Determine if vector will resize with next allocation
@param v pointer to a vector
@param length_increment length increment in elements
@param data_bytes requested size in bytes
@param header_bytes header size in bytes (may be zero)
@param data_align alignment (may be zero)
- @return v_prime pointer to resized vector, may or may not equal v
+ @return 1 if vector will resize 0 otherwise
*/
always_inline int
uword data_bytes, uword header_bytes,
uword data_align)
{
- vec_header_t *vh = _vec_find (v);
uword new_data_bytes, aligned_header_bytes;
aligned_header_bytes = vec_header_bytes (header_bytes);
/* Typically we'll not need to resize. */
if (new_data_bytes <= clib_mem_size (p))
- {
- vh->len += length_increment;
- return 0;
- }
+ return 0;
}
return 1;
}
#define vec_validate_ha(V,I,H,A) \
do { \
+ STATIC_ASSERT(A==0 || ((A % sizeof(V[0]))==0) || ((sizeof(V[0]) % A) == 0),\
+ "vector validate aligned on incorrectly sized object"); \
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])); \
+ clib_memset ((V) + _v(l), 0, (1 + (_v(i) - _v(l))) * sizeof ((V)[0])); \
} \
} while (0)
@param A alignment (may be zero)
@return V (value-result macro parameter)
*/
-#define vec_validate_init_empty_aligned(V,I,A) \
+#define vec_validate_init_empty_aligned(V,I,INIT,A) \
vec_validate_init_empty_ha(V,I,INIT,0,A)
/** \brief Add 1 element to end of vector (general version).
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])); \
+ clib_memset ((V) + _v(m), INIT, _v(n) * sizeof ((V)[0])); \
} while (0)
/** \brief Insert N vector elements starting at element M,
(_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])); \
+ clib_memset ((V) + _v(l) - _v(n), 0, _v(n) * sizeof ((V)[0])); \
_vec_len (V) -= _v(n); \
} while (0)
#define vec_zero(var) \
do { \
if (var) \
- memset ((var), 0, vec_len (var) * sizeof ((var)[0])); \
+ clib_memset ((var), 0, vec_len (var) * sizeof ((var)[0])); \
} while (0)
/** \brief Set all vector elements to given value. Null-pointer tolerant.
word _v(i) = 0; \
while (_v(i) < vec_len(v)) \
{ \
- if (v[_v(i)] == E) \
+ if ((v)[_v(i)] == E) \
+ break; \
+ _v(i)++; \
+ } \
+ if (_v(i) == vec_len(v)) \
+ _v(i) = ~0; \
+ _v(i); \
+})
+
+/** \brief Search a vector for the index of the entry that matches.
+
+ @param v1 Pointer to a vector
+ @param v2 Pointer to entry to match
+ @param fn Comparison function !0 => match
+ @return index of match or ~0
+*/
+#define vec_search_with_function(v,E,fn) \
+({ \
+ word _v(i) = 0; \
+ while (_v(i) < vec_len(v)) \
+ { \
+ if (0 != fn(&(v)[_v(i)], (E))) \
break; \
_v(i)++; \
} \