Statistics
| Branch: | Revision:

iof-bird-daemon / lib / slab.c @ 18c8241a

History | View | Annotate | Download (1.26 KB)

1
/*
2
 *        BIRD Resource Manager -- SLABs
3
 *
4
 *        (c) 1998 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#include <stdlib.h>
10
#include <string.h>
11

    
12
#include "nest/bird.h"
13
#include "lib/resource.h"
14

    
15
/*
16
 *        These are only fake, soon to change...
17
 */
18

    
19
struct sl_obj {
20
  node n;
21
  byte data[0];
22
};
23

    
24
struct slab {
25
  resource r;
26
  unsigned size;
27
  list objs;
28
};
29

    
30
void slab_free(resource *r);
31
void slab_dump(resource *r);
32

    
33
struct resclass sl_class = {
34
  "Slab",
35
  sizeof(struct slab),
36
  slab_free,
37
  slab_dump
38
};
39

    
40
slab *
41
sl_new(pool *p, unsigned size)
42
{
43
  slab *s = ralloc(p, &sl_class);
44
  s->size = size;
45
  init_list(&s->objs);
46
  return s;
47
}
48

    
49
void *
50
sl_alloc(slab *s)
51
{
52
  struct sl_obj *o = xmalloc(sizeof(struct sl_obj) + s->size);
53

    
54
  add_tail(&s->objs, &o->n);
55
  return o->data;
56
}
57

    
58
void
59
sl_free(slab *s, void *oo)
60
{
61
  struct sl_obj *o = SKIP_BACK(struct sl_obj, data, oo);
62

    
63
  rem_node(&o->n);
64
  xfree(o);
65
}
66

    
67
void
68
slab_free(resource *r)
69
{
70
  slab *s = (slab *) r;
71
  struct sl_obj *o, *p;
72

    
73
  for(o = HEAD(s->objs); p = (struct sl_obj *) o->n.next; o = p)
74
    xfree(o);
75
}
76

    
77
void
78
slab_dump(resource *r)
79
{
80
  slab *s = (slab *) r;
81
  int cnt = 0;
82
  struct sl_obj *o;
83

    
84
  WALK_LIST(o, s->objs)
85
    cnt++;
86
  debug("(%d objects per %d bytes)\n", cnt, s->size);
87
}