summaryrefslogtreecommitdiff
path: root/lib/resource.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/resource.c')
-rw-r--r--lib/resource.c188
1 files changed, 50 insertions, 138 deletions
diff --git a/lib/resource.c b/lib/resource.c
index 9a4c7717..94b8d019 100644
--- a/lib/resource.c
+++ b/lib/resource.c
@@ -15,7 +15,6 @@
#include "lib/resource.h"
#include "lib/string.h"
#include "lib/rcu.h"
-#include "lib/io-loop.h"
/**
* DOC: Resource pools
@@ -31,7 +30,7 @@
* is freed upon shutdown of the module.
*/
-static void pool_dump(resource *);
+static void pool_dump(resource *, unsigned);
static void pool_free(resource *);
static resource *pool_lookup(resource *, unsigned long);
static struct resmem pool_memsize(resource *P);
@@ -47,44 +46,43 @@ static struct resclass pool_class = {
pool root_pool;
-static int indent;
-
/**
* rp_new - create a resource pool
* @p: parent pool
- * @l: loop to assign
* @name: pool name (to be included in debugging dumps)
*
* rp_new() creates a new resource pool inside the specified
* parent pool.
*/
pool *
-rp_new(pool *p, struct birdloop *loop, const char *name)
+rp_new(pool *p, const char *name)
{
- ASSERT_DIE(birdloop_inside(p->loop));
- ASSERT_DIE(birdloop_inside(loop));
-
pool *z = ralloc(p, &pool_class);
- z->loop = loop;
z->name = name;
init_list(&z->inside);
return z;
}
-_Thread_local static pool *pool_parent = NULL;
+pool *
+rp_newf(pool *p, const char *fmt, ...)
+{
+ pool *z = rp_new(p, NULL);
+
+ va_list args;
+ va_start(args, fmt);
+ z->name = mb_vsprintf(p, fmt, args);
+ va_end(args);
+
+ return z;
+}
+
static void
pool_free(resource *P)
{
- ASSERT_DIE(pool_parent);
-
pool *p = (pool *) P;
- ASSERT_DIE(birdloop_inside(p->loop));
-
- pool *parent = pool_parent;
- pool_parent = p;
-
resource *r, *rr;
+
r = HEAD(p->inside);
while (rr = (resource *) r->n.next)
{
@@ -92,73 +90,23 @@ pool_free(resource *P)
xfree(r);
r = rr;
}
-
- pool_parent = parent;
-}
-
-void
-rp_free(pool *p, pool *parent)
-{
- ASSERT_DIE(pool_parent == NULL);
- pool_parent = parent;
- rfree(p);
- ASSERT_DIE(pool_parent == parent);
- pool_parent = NULL;
}
static void
-pool_dump_locked(pool *p)
+pool_dump(resource *P, unsigned indent)
{
+ pool *p = (pool *) P;
resource *r;
-
+
debug("%s\n", p->name);
- indent += 3;
WALK_LIST(r, p->inside)
- rdump(r);
- indent -= 3;
-}
-
-static void
-pool_dump(resource *P)
-{
- pool *p = (pool *) P;
-
- if (p->loop != pool_parent->loop)
- birdloop_enter(p->loop);
-
- pool *parent = pool_parent;
- pool_parent = p;
-
- pool_dump_locked(p);
-
- pool_parent = parent;
-
- if (p->loop != pool_parent->loop)
- birdloop_leave(p->loop);
-}
-
-void
-rp_dump(pool *p)
-{
- int inside = birdloop_inside(p->loop);
- if (!inside)
- birdloop_enter(p->loop);
-
- ASSERT_DIE(pool_parent == NULL);
- pool_parent = p;
-
- pool_dump_locked(p);
-
- ASSERT_DIE(pool_parent == p);
- pool_parent = NULL;
-
- if (!inside)
- birdloop_leave(p->loop);
+ rdump(r, indent + 3);
}
static struct resmem
-pool_memsize_locked(pool *p)
+pool_memsize(resource *P)
{
+ pool *p = (pool *) P;
resource *r;
struct resmem sum = {
.effective = 0,
@@ -175,46 +123,6 @@ pool_memsize_locked(pool *p)
return sum;
}
-static struct resmem
-pool_memsize(resource *P)
-{
- pool *p = (pool *) P;
-
- pool *parent = pool_parent;
- pool_parent = p;
-
- if (p->loop != parent->loop)
- birdloop_enter(p->loop);
-
- struct resmem sum = pool_memsize_locked(p);
-
- if (p->loop != parent->loop)
- birdloop_leave(p->loop);
-
- pool_parent = parent;
-
- return sum;
-}
-
-struct resmem
-rp_memsize(pool *p)
-{
- int inside = birdloop_inside(p->loop);
- if (!inside)
- birdloop_enter(p->loop);
-
- ASSERT_DIE(pool_parent == NULL);
- pool_parent = p;
- struct resmem sum = pool_memsize_locked(p);
- ASSERT_DIE(pool_parent == p);
- pool_parent = NULL;
-
- if (!inside)
- birdloop_leave(p->loop);
-
- return sum;
-}
-
static resource *
pool_lookup(resource *P, unsigned long a)
{
@@ -282,7 +190,7 @@ rfree(void *res)
* It works by calling a class-specific dump function.
*/
void
-rdump(void *res)
+rdump(void *res, unsigned indent)
{
char x[16];
resource *r = res;
@@ -292,7 +200,7 @@ rdump(void *res)
if (r)
{
debug("%s ", r->class->name);
- r->class->dump(r);
+ r->class->dump(r, indent);
}
else
debug("NULL\n");
@@ -325,15 +233,12 @@ rmemsize(void *res)
void *
ralloc(pool *p, struct resclass *c)
{
- ASSERT_DIE(p);
- ASSERT_DIE(birdloop_inside(p->loop));
-
resource *r = xmalloc(c->size);
bzero(r, c->size);
r->class = c;
- add_tail(&p->inside, &r->n);
-
+ if (p)
+ add_tail(&p->inside, &r->n);
return r;
}
@@ -355,7 +260,7 @@ rlookup(unsigned long a)
debug("Looking up %08lx\n", a);
if (r = pool_lookup(&root_pool.r, a))
- rdump(r);
+ rdump(r, 3);
else
debug("Not found.\n");
}
@@ -371,12 +276,33 @@ void
resource_init(void)
{
rcu_init();
+ resource_sys_init();
root_pool.r.class = &pool_class;
root_pool.name = "Root";
init_list(&root_pool.inside);
+ tmp_init(&root_pool);
}
+_Thread_local struct tmp_resources tmp_res;
+
+void
+tmp_init(pool *p)
+{
+ tmp_res.lp = lp_new_default(p);
+ tmp_res.parent = p;
+ tmp_res.pool = rp_new(p, "TMP");
+}
+
+void
+tmp_flush(void)
+{
+ lp_flush(tmp_linpool);
+ rfree(tmp_res.pool);
+ tmp_res.pool = rp_new(tmp_res.parent, "TMP");
+}
+
+
/**
* DOC: Memory blocks
*
@@ -401,7 +327,7 @@ static void mbl_free(resource *r UNUSED)
{
}
-static void mbl_debug(resource *r)
+static void mbl_debug(resource *r, unsigned indent UNUSED)
{
struct mblock *m = (struct mblock *) r;
@@ -509,21 +435,6 @@ mb_realloc(void *m, unsigned size)
return b->data;
}
-/**
- * mb_move - move a memory block
- * @m: memory block
- * @p: target pool
- *
- * mb_move() moves the given memory block to another pool in the same way
- * as rmove() moves a plain resource.
- */
-void
-mb_move(void *m, pool *p)
-{
- struct mblock *b = SKIP_BACK(struct mblock, data, m);
- rmove(b, p);
-}
-
/**
* mb_free - free a memory block
@@ -542,6 +453,7 @@ mb_free(void *m)
}
+
#define STEP_UP(x) ((x) + (x)/2 + 4)
void