Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.11 KB)

1
/*
2
 *        BIRD Resource Manager
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 <stdio.h>
10
#include <stdlib.h>
11

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

    
15
struct pool {
16
  resource r;
17
  list inside;
18
};
19

    
20
void pool_dump(resource *);
21
void pool_free(resource *);
22

    
23
static struct resclass pool_class = {
24
  "Pool",
25
  sizeof(pool),
26
  pool_free,
27
  pool_dump
28
};
29

    
30
pool root_pool;
31

    
32
static int indent;
33

    
34
pool *
35
rp_new(pool *p)
36
{
37
  pool *z = ralloc(p, &pool_class);
38
  init_list(&z->inside);
39
  return z;
40
}
41

    
42
void
43
pool_free(resource *P)
44
{
45
  pool *p = (pool *) P;
46
  resource *r, *rr;
47

    
48
  r = HEAD(p->inside);
49
  while (rr = (resource *) r->n.next)
50
    {
51
      r->class->free(r);
52
      xfree(r);
53
      r = rr;
54
    }
55
}
56

    
57
void
58
pool_dump(resource *P)
59
{
60
  pool *p = (pool *) P;
61
  resource *r;
62

    
63
  debug("\n");
64
  indent += 3;
65
  WALK_LIST(r, p->inside)
66
    rdump(r);
67
  indent -= 3;
68
}
69

    
70
void
71
rfree(void *res)
72
{
73
  resource *r = res;
74

    
75
  if (r)
76
    {
77
      if (r->n.next)
78
        rem_node(&r->n);
79
      r->class->free(r);
80
      xfree(r);
81
    }
82
}
83

    
84
void
85
rdump(void *res)
86
{
87
  char x[16];
88
  resource *r = res;
89

    
90
  sprintf(x, "%%%ds%%08x ", indent);
91
  debug(x, "", (int) r);
92
  if (r)
93
    {
94
      debug("%-6s", r->class->name);
95
      r->class->dump(r);
96
    }
97
  else
98
    debug("NULL\n");
99
}
100

    
101
void *
102
ralloc(pool *p, struct resclass *c)
103
{
104
  resource *r = xmalloc(c->size);
105

    
106
  r->class = c;
107
  add_tail(&p->inside, &r->n);
108
  return r;
109
}
110

    
111
void
112
resource_init(void)
113
{
114
  root_pool.r.class = &pool_class;
115
  init_list(&root_pool.inside);
116
}
117

    
118
/*
119
 *        Memory blocks.
120
 */
121

    
122
struct mblock {
123
  resource r;
124
  unsigned size;
125
  byte data[0];
126
};
127

    
128
void mbl_free(resource *r)
129
{
130
}
131

    
132
void mbl_debug(resource *r)
133
{
134
  struct mblock *m = (struct mblock *) r;
135

    
136
  debug("(size=%d)\n", m->size);
137
}
138

    
139
struct resclass mb_class = {
140
  "Memory",
141
  0,
142
  mbl_free,
143
  mbl_debug,
144
};
145

    
146
void *
147
mb_alloc(pool *p, unsigned size)
148
{
149
  struct mblock *b = xmalloc(sizeof(struct mblock) + size);
150

    
151
  b->r.class = &mb_class;
152
  add_tail(&p->inside, &b->r.n);
153
  b->size = size;
154
  return b->data;
155
}
156

    
157
void
158
mb_free(void *m)
159
{
160
  struct mblock *b = SKIP_BACK(struct mblock, data, m);
161
  rfree(b);
162
}