Statistics
| Branch: | Revision:

iof-bird-daemon / lib / resource.c @ 7a2105be

History | View | Annotate | Download (2.35 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
#include <string.h>
12

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

    
16
struct pool {
17
  resource r;
18
  list inside;
19
  char *name;
20
};
21

    
22
static void pool_dump(resource *);
23
static void pool_free(resource *);
24

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

    
32
pool root_pool;
33

    
34
static int indent;
35

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

    
44
static void
45
pool_free(resource *P)
46
{
47
  pool *p = (pool *) P;
48
  resource *r, *rr;
49

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

    
59
static void
60
pool_dump(resource *P)
61
{
62
  pool *p = (pool *) P;
63
  resource *r;
64

    
65
  debug(" %s\n", p->name);
66
  indent += 3;
67
  WALK_LIST(r, p->inside)
68
    rdump(r);
69
  indent -= 3;
70
}
71

    
72
void
73
rfree(void *res)
74
{
75
  resource *r = res;
76

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

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

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

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

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

    
113
void
114
resource_init(void)
115
{
116
  root_pool.r.class = &pool_class;
117
  root_pool.name = "Root";
118
  init_list(&root_pool.inside);
119
}
120

    
121
/*
122
 *        Memory blocks.
123
 */
124

    
125
struct mblock {
126
  resource r;
127
  unsigned size;
128
  byte data[0];
129
};
130

    
131
static void mbl_free(resource *r)
132
{
133
}
134

    
135
static void mbl_debug(resource *r)
136
{
137
  struct mblock *m = (struct mblock *) r;
138

    
139
  debug("(size=%d)\n", m->size);
140
}
141

    
142
static struct resclass mb_class = {
143
  "Memory",
144
  0,
145
  mbl_free,
146
  mbl_debug,
147
};
148

    
149
void *
150
mb_alloc(pool *p, unsigned size)
151
{
152
  struct mblock *b = xmalloc(sizeof(struct mblock) + size);
153

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

    
160
void *
161
mb_allocz(pool *p, unsigned size)
162
{
163
  void *x = mb_alloc(p, size);
164
  bzero(x, size);
165
  return x;
166
}
167

    
168
void
169
mb_free(void *m)
170
{
171
  struct mblock *b = SKIP_BACK(struct mblock, data, m);
172
  rfree(b);
173
}