nuclear@67: #include nuclear@67: #include nuclear@67: #include nuclear@67: #include "dynarr.h" nuclear@67: nuclear@67: /* The array descriptor keeps auxilliary information needed to manipulate nuclear@67: * the dynamic array. It's allocated adjacent to the array buffer. nuclear@67: */ nuclear@67: struct arrdesc { nuclear@67: int nelem, szelem; nuclear@67: int max_elem; nuclear@67: int bufsz; /* not including the descriptor */ nuclear@67: }; nuclear@67: nuclear@67: #define DESC(x) ((struct arrdesc*)((char*)(x) - sizeof(struct arrdesc))) nuclear@67: nuclear@67: void *dynarr_alloc(int elem, int szelem) nuclear@67: { nuclear@67: struct arrdesc *desc; nuclear@67: nuclear@67: if(!(desc = malloc(elem * szelem + sizeof *desc))) { nuclear@67: return 0; nuclear@67: } nuclear@67: desc->nelem = desc->max_elem = elem; nuclear@67: desc->szelem = szelem; nuclear@67: desc->bufsz = elem * szelem; nuclear@67: return (char*)desc + sizeof *desc; nuclear@67: } nuclear@67: nuclear@67: void dynarr_free(void *da) nuclear@67: { nuclear@67: if(da) { nuclear@67: free(DESC(da)); nuclear@67: } nuclear@67: } nuclear@67: nuclear@67: void *dynarr_resize(void *da, int elem) nuclear@67: { nuclear@67: int newsz; nuclear@67: void *tmp; nuclear@67: struct arrdesc *desc; nuclear@67: nuclear@67: if(!da) return 0; nuclear@67: desc = DESC(da); nuclear@67: nuclear@67: newsz = desc->szelem * elem; nuclear@67: nuclear@67: if(!(tmp = realloc(desc, newsz + sizeof *desc))) { nuclear@67: return 0; nuclear@67: } nuclear@67: desc = tmp; nuclear@67: nuclear@67: desc->nelem = desc->max_elem = elem; nuclear@67: desc->bufsz = newsz; nuclear@67: return (char*)desc + sizeof *desc; nuclear@67: } nuclear@67: nuclear@67: int dynarr_empty(void *da) nuclear@67: { nuclear@67: return DESC(da)->nelem ? 0 : 1; nuclear@67: } nuclear@67: nuclear@67: int dynarr_size(void *da) nuclear@67: { nuclear@67: return DESC(da)->nelem; nuclear@67: } nuclear@67: nuclear@67: nuclear@67: /* stack semantics */ nuclear@67: void *dynarr_push(void *da, void *item) nuclear@67: { nuclear@67: struct arrdesc *desc; nuclear@67: int nelem; nuclear@67: nuclear@67: desc = DESC(da); nuclear@67: nelem = desc->nelem; nuclear@67: nuclear@67: if(nelem >= desc->max_elem) { nuclear@67: /* need to resize */ nuclear@67: struct arrdesc *tmp; nuclear@67: int newsz = desc->max_elem ? desc->max_elem * 2 : 1; nuclear@67: nuclear@67: if(!(tmp = dynarr_resize(da, newsz))) { nuclear@67: fprintf(stderr, "failed to resize\n"); nuclear@67: return da; nuclear@67: } nuclear@67: da = tmp; nuclear@67: desc = DESC(da); nuclear@67: desc->nelem = nelem; nuclear@67: } nuclear@67: nuclear@67: memcpy((char*)da + desc->nelem++ * desc->szelem, item, desc->szelem); nuclear@67: return da; nuclear@67: } nuclear@67: nuclear@67: void *dynarr_pop(void *da) nuclear@67: { nuclear@67: struct arrdesc *desc; nuclear@67: int nelem; nuclear@67: nuclear@67: desc = DESC(da); nuclear@67: nelem = desc->nelem; nuclear@67: nuclear@67: if(!nelem) return da; nuclear@67: nuclear@67: if(nelem <= desc->max_elem / 3) { nuclear@67: /* reclaim space */ nuclear@67: struct arrdesc *tmp; nuclear@67: int newsz = desc->max_elem / 2; nuclear@67: nuclear@67: if(!(tmp = dynarr_resize(da, newsz))) { nuclear@67: fprintf(stderr, "failed to resize\n"); nuclear@67: return da; nuclear@67: } nuclear@67: da = tmp; nuclear@67: desc = DESC(da); nuclear@67: desc->nelem = nelem; nuclear@67: } nuclear@67: desc->nelem--; nuclear@67: nuclear@67: return da; nuclear@67: }