resource.c 7.56 KB
Newer Older
1 2 3
/*
 *	BIRD Resource Manager
 *
4
 *	(c) 1998--2000 Martin Mares <mj@ucw.cz>
5 6 7 8 9 10 11 12 13
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include <stdio.h>
#include <stdlib.h>

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

16 17 18 19 20 21 22 23 24 25 26 27 28 29
/**
 * DOC: Resource pools
 *
 * Resource pools (&pool) are just containers holding a list of
 * other resources. Freeing a pool causes all the listed resources
 * to be freed as well. Each existing &resource is linked to some pool
 * except for a root pool which isn't linked anywhere, so all the
 * resources form a tree structure with internal nodes corresponding
 * to pools and leaves being the other resources.
 *
 * Example: Almost all modules of BIRD have their private pool which
 * is freed upon shutdown of the module.
 */

30 31 32
struct pool {
  resource r;
  list inside;
33
  char *name;
34 35
};

36 37
static void pool_dump(resource *);
static void pool_free(resource *);
38
static resource *pool_lookup(resource *, unsigned long);
39 40 41 42 43

static struct resclass pool_class = {
  "Pool",
  sizeof(pool),
  pool_free,
44 45
  pool_dump,
  pool_lookup
46 47 48 49 50 51
};

pool root_pool;

static int indent;

52 53 54 55 56 57 58 59
/**
 * rp_new - create a resource pool
 * @p: parent pool
 * @name: pool name (to be included in debugging dumps)
 *
 * rp_new() creates a new resource pool inside the specified
 * parent pool.
 */
60
pool *
61
rp_new(pool *p, char *name)
62 63
{
  pool *z = ralloc(p, &pool_class);
64
  z->name = name;
65 66 67 68
  init_list(&z->inside);
  return z;
}

69
static void
70 71 72 73 74 75 76 77 78 79 80 81 82 83
pool_free(resource *P)
{
  pool *p = (pool *) P;
  resource *r, *rr;

  r = HEAD(p->inside);
  while (rr = (resource *) r->n.next)
    {
      r->class->free(r);
      xfree(r);
      r = rr;
    }
}

84
static void
85 86 87 88 89
pool_dump(resource *P)
{
  pool *p = (pool *) P;
  resource *r;

90
  debug("%s\n", p->name);
91 92 93 94 95 96
  indent += 3;
  WALK_LIST(r, p->inside)
    rdump(r);
  indent -= 3;
}

97 98 99 100 101 102 103 104 105 106 107 108
static resource *
pool_lookup(resource *P, unsigned long a)
{
  pool *p = (pool *) P;
  resource *r, *q;

  WALK_LIST(r, p->inside)
    if (r->class->lookup && (q = r->class->lookup(r, a)))
      return q;
  return NULL;
}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
/**
 * rmove - move a resource
 * @res: resource
 * @p: pool to move the resource to
 *
 * rmove() moves a resource from one pool to another.
 */

void rmove(void *res, pool *p)
{
  resource *r = res;

  if (r)
    {
      if (r->n.next)
        rem_node(&r->n);
      add_tail(&p->inside, &r->n);
    }
}

129 130 131 132 133 134 135 136 137 138
/**
 * rfree - free a resource
 * @res: resource
 *
 * rfree() frees the given resource and all information associated
 * with it. In case it's a resource pool, it also frees all the objects
 * living inside the pool.
 *
 * It works by calling a class-specific freeing function.
 */
139 140 141 142 143 144 145 146 147 148 149 150 151 152
void
rfree(void *res)
{
  resource *r = res;

  if (r)
    {
      if (r->n.next)
	rem_node(&r->n);
      r->class->free(r);
      xfree(r);
    }
}

153 154 155 156 157 158 159 160 161
/**
 * rdump - dump a resource
 * @res: resource
 *
 * This function prints out all available information about the given
 * resource to the debugging output.
 *
 * It works by calling a class-specific dump function.
 */
162 163 164 165 166 167
void
rdump(void *res)
{
  char x[16];
  resource *r = res;

168 169
  bsprintf(x, "%%%ds%%p ", indent);
  debug(x, "", r);
170 171
  if (r)
    {
172
      debug("%s ", r->class->name);
173 174 175 176 177 178
      r->class->dump(r);
    }
  else
    debug("NULL\n");
}

179 180 181 182 183 184 185 186 187 188
/**
 * ralloc - create a resource
 * @p: pool to create the resource in
 * @c: class of the new resource
 *
 * This function is called by the resource classes to create a new
 * resource of the specified class and link it to the given pool.
 * Size of the resource structure is taken from the @size field
 * of the &resclass.
 */
189 190 191 192 193 194 195 196 197 198
void *
ralloc(pool *p, struct resclass *c)
{
  resource *r = xmalloc(c->size);

  r->class = c;
  add_tail(&p->inside, &r->n);
  return r;
}

199 200 201 202 203 204 205 206 207 208 209
/**
 * rlookup - look up a memory location
 * @a: memory address
 *
 * This function examines all existing resources to see whether
 * the address @a is inside any resource. It's used for debugging
 * purposes only.
 *
 * It works by calling a class-specific lookup function for each
 * resource.
 */
210 211 212 213 214 215 216 217 218 219 220 221
void
rlookup(unsigned long a)
{
  resource *r;

  debug("Looking up %08lx\n", a);
  if (r = pool_lookup(&root_pool.r, a))
    rdump(r);
  else
    debug("Not found.\n");
}

222 223 224 225 226 227 228
/**
 * resource_init - initialize the resource manager
 *
 * This function is called during BIRD startup. It initializes
 * all data structures of the resource manager and creates the
 * root pool.
 */
229 230 231 232
void
resource_init(void)
{
  root_pool.r.class = &pool_class;
233
  root_pool.name = "Root";
234 235 236
  init_list(&root_pool.inside);
}

237 238 239 240 241 242 243 244 245 246 247
/**
 * DOC: Memory blocks
 *
 * Memory blocks are pieces of contiguous allocated memory.
 * They are a bit non-standard since they are represented not by a pointer
 * to &resource, but by a void pointer to the start of data of the
 * memory block. All memory block functions know how to locate the header
 * given the data pointer.
 *
 * Example: All "unique" data structures such as hash tables are allocated
 * as memory blocks.
248 249 250 251 252 253 254 255
 */

struct mblock {
  resource r;
  unsigned size;
  byte data[0];
};

Martin Mareš's avatar
Martin Mareš committed
256
static void mbl_free(resource *r UNUSED)
257 258 259
{
}

260
static void mbl_debug(resource *r)
261 262 263 264 265 266
{
  struct mblock *m = (struct mblock *) r;

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

267 268 269 270 271 272 273 274 275 276
static resource *
mbl_lookup(resource *r, unsigned long a)
{
  struct mblock *m = (struct mblock *) r;

  if ((unsigned long) m->data <= a && (unsigned long) m->data + m->size > a)
    return r;
  return NULL;
}

277
static struct resclass mb_class = {
278 279 280 281
  "Memory",
  0,
  mbl_free,
  mbl_debug,
282
  mbl_lookup
283 284
};

285 286 287 288 289 290 291 292 293 294 295 296 297
/**
 * mb_alloc - allocate a memory block
 * @p: pool
 * @size: size of the block
 *
 * mb_alloc() allocates memory of a given size and creates
 * a memory block resource representing this memory chunk
 * in the pool @p.
 *
 * Please note that mb_alloc() returns a pointer to the memory
 * chunk, not to the resource, hence you have to free it using
 * mb_free(), not rfree().
 */
298 299 300 301 302 303 304 305 306 307 308
void *
mb_alloc(pool *p, unsigned size)
{
  struct mblock *b = xmalloc(sizeof(struct mblock) + size);

  b->r.class = &mb_class;
  add_tail(&p->inside, &b->r.n);
  b->size = size;
  return b->data;
}

309 310 311 312 313 314 315 316 317 318 319 320 321
/**
 * mb_allocz - allocate and clear a memory block
 * @p: pool
 * @size: size of the block
 *
 * mb_allocz() allocates memory of a given size, initializes it to
 * zeroes and creates a memory block resource representing this memory
 * chunk in the pool @p.
 *
 * Please note that mb_alloc() returns a pointer to the memory
 * chunk, not to the resource, hence you have to free it using
 * mb_free(), not rfree().
 */
322 323 324 325 326 327 328 329
void *
mb_allocz(pool *p, unsigned size)
{
  void *x = mb_alloc(p, size);
  bzero(x, size);
  return x;
}

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
/**
 * mb_realloc - reallocate a memory block
 * @p: pool
 * @m: memory block
 * @size: new size of the block
 *
 * mb_realloc() changes the size of the memory block @m to a given size.
 * The contents will be unchanged to the minimum of the old and new sizes;
 * newly allocated memory will be uninitialized. If @m is NULL, the call
 * is equivalent to mb_alloc(@p, @size).
 *
 * Like mb_alloc(), mb_realloc() also returns a pointer to the memory
 * chunk , not to the resource, hence you have to free it using
 * mb_free(), not rfree().
 */
void *
mb_realloc(pool *p, void *m, unsigned size)
{
  struct mblock *ob = NULL;

  if (m)
    {
      ob = SKIP_BACK(struct mblock, data, m);
      if (ob->r.n.next)
	rem_node(&ob->r.n);
    }

  struct mblock *b = xrealloc(ob, sizeof(struct mblock) + size);

  b->r.class = &mb_class;
  add_tail(&p->inside, &b->r.n);
  b->size = size;
  return b->data;
}


366 367 368 369 370 371
/**
 * mb_free - free a memory block
 * @m: memory block
 *
 * mb_free() frees all memory associated with the block @m.
 */
372 373 374 375 376 377
void
mb_free(void *m)
{
  struct mblock *b = SKIP_BACK(struct mblock, data, m);
  rfree(b);
}
378