summaryrefslogtreecommitdiff
path: root/src/libdatastruct/genset.c
blob: a4e48e069362f6a3421ca3e2fdd51cdfbf666e18 (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
28
/* ISC license. */

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

void genset_init (genset *x, void *storage, uint32_t *freelist, uint32_t esize, uint32_t max)
{
  uint32_t i = max ;
  x->storage = (char *)storage ;
  x->freelist = freelist ;
  x->esize = esize ;
  x->max = max ;
  x->sp = max ;
  while (i--) freelist[i] = max - 1 - i ;
}

uint32_t genset_new (genset *x)
{
  return x->sp ? x->freelist[--x->sp] : (errno = ENOSPC, x->max) ;
}

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