-/* routines to support generic grow-able arrays */
-
-struct dynamic_array_ {
- size_t entry_size;
- size_t grow_size;
- size_t allocated;
- size_t entries;
- void *a;
-};
-
-#define DYNARRAY_ITEM(da, index) ( (char *)(da).a + ( (da).entry_size * index ) )
-
-/* allocate and initialize a new generic dynamic array */
-static
-struct dynamic_array_ *dynarray_new_(size_t entry_size, size_t grow_size) {
- struct dynamic_array_ *da;
-
- if (entry_size == 0 || grow_size == 0) {
- fprintf(stderr, "%s: internal error: sizes cannot be zero\n", __func__);
- exit(EX_SOFTWARE);
- }
-
- da = calloc(1, sizeof *da);
- if (da == NULL) {
- fprintf(stderr, "%s():%s\n", "calloc", strerror(errno));
- return NULL;
- }
-
- da->entry_size = entry_size;
- da->grow_size = grow_size;
-
- da->a = malloc(da->entry_size * da->grow_size);
- if (da->a == NULL) {
- fprintf(stderr, "%s():%s\n", "malloc", strerror(errno));
- }
-
- da->allocated = grow_size;
-
- DEBUG_PRINTF("allocated new dynarray:%p a:%p entry_size:%zu\n", da, da->a, da->entry_size);
-
- return da;
-}
-
-/* copy item onto end of array */
-static
-void *dynarray_add_(struct dynamic_array_ *da, void *item) {
- void *dst;
-
- /* make room, make room */
- if (da->entries == da->allocated) {
- size_t new_allocated = da->allocated + da->grow_size;
- void *tmp_ptr = realloc(da->a, new_allocated * da->entry_size);
- if (tmp_ptr == NULL) {
- fprintf(stderr, "%s():%s\n", "realloc", strerror(errno));
- return NULL;
- }
- da->a = tmp_ptr;
- da->allocated = new_allocated;
-
- DEBUG_PRINTF("grew dynarray:%p\n", da);
- }
-
- dst = DYNARRAY_ITEM(*da, da->entries);
- memcpy(dst, item, da->entry_size);
-
- da->entries++;
-
- DEBUG_PRINTF("added dynarray:%p entry:%zu item:%p\n", da, da->entries, item);
-
- return dst;
-}
-
-