summaryrefslogtreecommitdiff
path: root/src/libdatastruct/genset.c
blob: c8fc36e4493c6b998adca715a828903e0b642a3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* ISC license. */

#include <errno.h>
#include <skalibs/genset.h>

void genset_init (genset *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 *x)
{
  return x->sp ? x->freelist[--x->sp] : (errno = ENOSPC, x->max) ;
}

int genset_delete (genset *x, unsigned int i)
{
  if ((i >= x->max) || (x->sp >= x->max)) return (errno = EINVAL, 0) ;
  x->freelist[x->sp++] = i ;
  return 1 ;
}