skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

genset.c (607B)


      1 /* ISC license. */
      2 
      3 #include <errno.h>
      4 #include <skalibs/genset.h>
      5 
      6 void genset_init (genset *x, void *storage, uint32_t *freelist, uint32_t esize, uint32_t max)
      7 {
      8   uint32_t i = max ;
      9   x->storage = (char *)storage ;
     10   x->freelist = freelist ;
     11   x->esize = esize ;
     12   x->max = max ;
     13   x->sp = max ;
     14   while (i--) freelist[i] = max - 1 - i ;
     15 }
     16 
     17 uint32_t genset_new (genset *x)
     18 {
     19   return x->sp ? x->freelist[--x->sp] : (errno = ENOSPC, x->max) ;
     20 }
     21 
     22 int genset_delete (genset *x, uint32_t i)
     23 {
     24   if ((i >= x->max) || (x->sp >= x->max)) return (errno = EINVAL, 0) ;
     25   x->freelist[x->sp++] = i ;
     26   return 1 ;
     27 }