20 #define VEC_REALLOC_SIZE 8
22 typedef struct vec_hdr {
33 #define _V(_x) _v_##_x
43 static inline vec_hdr_t *
46 return (vec_hdr_t *)((vec) ?
CNE_PTR_SUB(vec,
sizeof(vec_hdr_t)) : NULL);
57 #define vec_end(vec) &(vec)[vec_header(vec)->len]
71 #define vec_init(hv, al, es) \
74 _V(hx) = (vec_hdr_t *)(hv); \
78 _V(hx)->rsvd[0] = 0; \
79 _V(hx)->rsvd[1] = 0; \
80 _V(hx)->rsvd[2] = 0; \
81 (void *)_V(hx)->data; \
95 _vec_realloc_data(
void *vec, uint32_t nelem, uint32_t esize)
98 uint32_t sz = (nelem * esize) +
sizeof(vec_hdr_t);
100 h = (vec) ? vec_header(vec) : NULL;
108 return (
void *)h->data;
123 #define _vec_realloc(vec, nelem, esize) _vec_realloc_data((vec), nelem, esize)
131 #define vec_free(v) \
134 free(vec_header(v)); \
148 #define vec_alloc(vec, nelem) \
151 CNE_ERR("Vector all ready allocated\n"); \
153 (vec) = _vec_realloc((vec), nelem, sizeof((vec)[0])); \
165 #define vec_foreach(var, vec) for ((var) = (vec); (vec) && (var < vec_end(vec)); var++)
167 #define vec_foreach_ptr(var, vec) \
168 for (uint32_t _V(_i) = 0; (vec) && (var = (vec)[_V(_i)], _V(_i) < vec_len(vec)); _V(_i)++)
180 #define vec_calc_size(n, _t) \
181 CNE_ALIGN_CEIL((n * sizeof(_t)) + sizeof(vec_hdr_t), CNE_CACHE_LINE_SIZE)
191 #define vec_len(v) ((v) ? vec_header(v)->len : 0)
202 vec_set_len(
void *v,
int len)
204 vec_hdr_t *hdr = (vec_hdr_t *)vec_header(v);
218 #define vec_max_len(v) ((v) ? vec_header(v)->alen : 0)
226 #define vec_dec_len(v) ((v) ? vec_header(v)->len-- : 0)
238 #define vec_at_index(vec, n) (vec)[(n)]
250 #define vec_addr_at_index(vec, n) &(vec)[(n)]
262 #define vec_pop(vec) ((vec) ? (vec)[--vec_header(vec)->len] : 0)
270 #define vec_inc_len(v) ((v) ? vec_header(v)->len++ : 0)
273 _vec_find_index(
void **vec,
void *v)
275 for (uint32_t i = 0; i < vec_len(vec); i++) {
276 if (v == vec_at_index(vec, i))
282 #define vec_find_index(vec, v) _vec_find_index((void **)vec, (void *)v)
294 #define vec_add(vec, v) \
296 if ((vec) == NULL) { \
297 (vec) = _vec_realloc(NULL, VEC_REALLOC_SIZE, sizeof((vec)[0])); \
298 (vec) = vec_init(vec_header((vec)), VEC_REALLOC_SIZE, sizeof((vec)[0])); \
300 vec_hdr_t *_V(h) = vec_header((vec)); \
301 int _V(l) = _V(h)->len; \
302 if (_V(h)->len >= _V(h)->alen) { \
303 _V(h)->alen += VEC_REALLOC_SIZE; \
304 (vec) = _vec_realloc((vec), _V(h)->alen, _V(h)->esize); \
305 _V(h) = vec_header((vec)); \
307 (vec)[_V(h)->len++] = (v); \
311 #define vec_full(vec) \
313 vec_hdr_t *_V(h) = vec_header(vec); \
314 (_V(h)->len >= _V(h)->alen) ? true : false; \
329 #define vec_move_at_index(t, f, n) \
331 vec_add(t, vec_at_index(f, n)); \
332 vec_at_index(f, n) = 0; \
345 #define vec_copy_at_index(to, from, idx) \
347 typeof(from) v = vec_at_index(from, idx); \
360 vec_remove(
void *v, uint32_t nb)
362 vec_hdr_t *h = vec_header(v);
364 if (h && h->len && nb <= h->len) {
368 memmove(v,
CNE_PTR_ADD(v, (nb * h->esize)), n * h->esize);
382 vec_dump(
const char *msg,
void *vec)
386 cne_printf(
" [orange]%-8s[] ", msg ? msg :
"");
#define CNE_PTR_ADD(ptr, x)
#define CNE_PTR_SUB(ptr, x)
#define __cne_always_inline
CNDP_API int cne_printf(const char *fmt,...)