diff options
Diffstat (limited to 'src/libdatastruct/genset.c')
-rw-r--r-- | src/libdatastruct/genset.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/libdatastruct/genset.c b/src/libdatastruct/genset.c new file mode 100644 index 0000000..8d750b7 --- /dev/null +++ b/src/libdatastruct/genset.c @@ -0,0 +1,27 @@ +/* ISC license. */ + +#include <errno.h> +#include <skalibs/genset.h> + +void genset_init (genset_ref x, void *storage, unsigned int *freelist, unsigned int esize, unsigned int max) +{ + register unsigned int i = 0 ; + x->storage = (char *)storage ; + x->freelist = freelist ; + x->esize = esize ; + x->max = max ; + x->sp = max ; + for (; i < max ; i++) freelist[i] = max - 1 - i ; +} + +unsigned int genset_new (genset_ref x) +{ + return x->sp ? x->freelist[--x->sp] : (errno = ENOSPC, x->max) ; +} + +int genset_delete (genset_ref x, unsigned int i) +{ + if ((i >= x->max) || (x->sp >= x->max)) return (errno = EINVAL, 0) ; + x->freelist[x->sp++] = i ; + return 1 ; +} |