Mercurial > ~dholland > hg > ag > index.cgi
view help2html/array.c @ 8:ec2b657edf13
Add explicit lint-comment-style fallthrough annotations.
GCC now assumes that if you don't have these you're making a mistake,
which is annoying.
XXX: This changeset updates the AG output files only (by hand) and is
XXX: abusive - rebuilding them will erase the change. However, I need
XXX: to get things to build before I can try to get AG to issue the
XXX: annotations itself, so this seems like a reasonable expedient.
author | David A. Holland |
---|---|
date | Mon, 30 May 2022 23:51:43 -0400 |
parents | 13d2b8934445 |
children | 60b08b68c750 |
line wrap: on
line source
#include <stdlib.h> #include "must.h" #include "array.h" //////////////////////////////////////////////////////////// void array_init(struct array *a) { a->v = NULL; a->num = a->max = 0; } struct array *array_create(void) { struct array *a; a = must_malloc(sizeof(*a)); array_init(a); return a; } void array_cleanup(struct array *a) { free(a->v); a->v = NULL; a->num = a->max = 0; } void array_destroy(struct array *a) { array_cleanup(a); free(a); } //////////////////////////////////////////////////////////// /* * I wish there were a way to check that these were the same as the * inline versions, but apparently allowing such things to diverge is * a feature of C99. Bleh. */ unsigned array_num(const struct array *a) { return a->num; } void *array_get(const struct array *a, unsigned ix) { assert(ix < a->num); return a->v[ix]; } void array_set(struct array *a, unsigned ix, void *ptr) { assert(ix < a->num); a->v[ix] = ptr; } //////////////////////////////////////////////////////////// unsigned array_add(struct array *a, void *ptr) { unsigned ix; ix = a->num; array_setsize(a, a->num+1); a->v[ix] = ptr; return ix; } void array_setsize(struct array *a, unsigned newnum) { unsigned newmax; void **newptr; if (newnum > a->max) { newmax = a->max; if (newmax == 0) { newmax = 4; } while (newmax < newnum) { newmax *= 2; } newptr = must_realloc(a->v, newmax*sizeof(void *)); a->v = newptr; a->max = newmax; } a->num = newnum; /* Note: it's the user's responsibility to initialize the new space */ } void array_nonulls(struct array *a) { unsigned i, j; for (i=j=0; i<a->num; i++) { if (a->v[i] != NULL) { if (i != j) { a->v[j] = a->v[i]; } j++; } } a->num = j; } static int (*array_usersort)(void *a, void *b); static int array_sortthunk(const void *av, const void *bv) { /* av and bv point to elements of the array */ void *a = *(void *const *)av; void *b = *(void *const *)bv; return array_usersort(a, b); } void array_sort(struct array *a, int (*f)(void *a, void *b)) { array_usersort = f; qsort(a->v, a->num, sizeof(void *), array_sortthunk); }