5 #include "buffer.h" /* force_assert() */
7 static inline size_t vector_align_size(size_t s
) {
8 size_t a
= (s
+ 16) & ((size_t)~15);
9 return (a
< s
) ? s
: a
;
12 void vector_free(void *data
);
13 void *vector_malloc(size_t sz
);
14 void *vector_realloc(void *data
, size_t elem_size
, size_t size
, size_t used
);
16 #define DEFINE_TYPED_VECTOR(name, entry, release) \
17 typedef struct vector_ ## name { \
22 static inline void vector_ ## name ## _init(vector_ ## name *v) { \
24 v->used = v->size = 0; \
26 static inline vector_ ## name *vector_ ## name ## _alloc() { \
27 vector_ ## name *v = vector_malloc(sizeof(*v)); \
28 force_assert(NULL != v); \
29 vector_ ## name ## _init(v); \
32 static inline void vector_ ## name ## _clear(vector_ ## name *v) { \
34 vector_ ## name vcopy = *v; \
35 vector_ ## name ## _init(v); \
36 if (release) for (ndx = 0; ndx < vcopy.used; ++ndx) release(vcopy.data[ndx]); \
37 vector_free(vcopy.data); \
39 static inline void vector_ ## name ## _free(vector_ ## name *v) { \
41 vector_ ## name ## _clear(v); \
45 static inline void vector_ ## name ## _reserve(vector_ ## name *v, size_t p) { \
46 force_assert(v->used < SIZE_MAX - p); \
47 if (v->size < v->used + p) { \
48 v->size = vector_align_size(v->used + p); \
49 v->data = vector_realloc(v->data, sizeof(entry), v->size, v->used); \
52 static inline void vector_ ## name ## _push(vector_ ## name *v, entry e) { \
53 vector_ ## name ## _reserve(v, 1); \
54 v->data[v->used++] = e; \
56 static inline entry vector_ ## name ## _pop(vector_ ## name *v) { \
57 force_assert(v->used > 0); \
58 return v->data[--v->used]; \
60 struct vector_ ## name /* expect trailing semicolon */ \
61 /* end of DEFINE_TYPED_VECTOR */
63 #define DEFINE_TYPED_VECTOR_NO_RELEASE(name, entry) \
64 DEFINE_TYPED_VECTOR(name, entry, ((void(*)(entry)) NULL)) \
65 /* end of DEFINE_TYPED_VECTOR_NO_RELEASE */