return (x);
}
+/*----- Resizing arrays ---------------------------------------------------*/
+
+void *vec__grow(void *p, size_t esz, size_t *sz_inout)
+{
+ size_t sz = *sz_inout, want;
+
+ sz = sz ? 2*sz : 32;
+ want = sz*esz;
+ p = realloc(p, want);
+ if (!p) bail("out of memory allocating %zu bytes", want);
+ *sz_inout = sz; return (p);
+}
+
/*----- System utilities --------------------------------------------------*/
void sit(double t)
} while (0)
/* Free the vector VV. It's safe to free a vector multiple times. */
+extern void *vec__grow(void *p, size_t esz, size_t *sz_inout);
+ /* Extend the buffer P, which currently has space for *SZ_INOUT
+ * elements, each ESZ bytes in size, so that there's space for at
+ * least one one more; return the new buffer address, and update
+ * *SZ_INOUT with the new size.
+ */
+
#define VEC_PUSH(p, vv) do { \
- size_t _want; \
- if ((vv)->n >= (vv)->sz) { \
- (vv)->sz = (vv)->sz ? 2*(vv)->sz : 32; \
- _want = (vv)->sz*sizeof(*(vv)->v); \
- (vv)->v = realloc((vv)->v, _want); \
- if (!(vv)->v) bail("out of memory allocating %zu bytes", _want); \
- } \
+ if ((vv)->n >= (vv)->sz) \
+ (vv)->v = vec__grow((vv)->v, sizeof(*(vv)->v), &(vv)->sz); \
(p) = &(vv)->v[(vv)->n++]; \
} while (0)
/* Add an initialized element to the end of vector VV, storing its