diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-18 18:55:44 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-18 18:55:44 +0000 |
commit | 3534b428629be185e096be99e3bd5fdfe32d5544 (patch) | |
tree | 210ef3198ed66bc7f7b7bf6a85e4579f455e5a36 /src/libdatastruct/genset.c | |
download | skalibs-3534b428629be185e096be99e3bd5fdfe32d5544.tar.xz |
initial commit with rc for skalibs-2.0.0.0
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 ; +} |