0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-22 09:41:54 +00:00

Lib: resource management converted to a typed list

This commit is contained in:
Maria Matejka 2019-10-24 12:43:37 +02:00
parent 72ce0b10bc
commit ceeb22ec27
2 changed files with 15 additions and 17 deletions

View File

@ -30,7 +30,7 @@
struct pool { struct pool {
resource r; resource r;
list inside; TLIST(resource) inside;
const char *name; const char *name;
}; };
@ -65,7 +65,7 @@ rp_new(pool *p, const char *name)
{ {
pool *z = ralloc(p, &pool_class); pool *z = ralloc(p, &pool_class);
z->name = name; z->name = name;
init_list(&z->inside); INIT_TLIST(&z->inside);
return z; return z;
} }
@ -73,14 +73,12 @@ static void
pool_free(resource *P) pool_free(resource *P)
{ {
pool *p = (pool *) P; pool *p = (pool *) P;
resource *r, *rr; resource *r;
r = HEAD(p->inside); WALK_TLIST_DELSAFE(r, p->inside)
while (rr = (resource *) r->n.next)
{ {
r->class->free(r); r->class->free(r);
xfree(r); xfree(r);
r = rr;
} }
} }
@ -136,9 +134,9 @@ void rmove(void *res, pool *p)
if (r) if (r)
{ {
if (r->n.next) if (TNODE_IN_LIST(r))
rem_node(&r->n); TREM_NODE(r);
add_tail(&p->inside, &r->n); TADD_TAIL(&p->inside, r);
} }
} }
@ -160,8 +158,8 @@ rfree(void *res)
if (!r) if (!r)
return; return;
if (r->n.next) if (TNODE_IN_LIST(r))
rem_node(&r->n); TREM_NODE(r);
r->class->free(r); r->class->free(r);
r->class = NULL; r->class = NULL;
xfree(r); xfree(r);
@ -222,7 +220,7 @@ ralloc(pool *p, struct resclass *c)
r->class = c; r->class = c;
if (p) if (p)
add_tail(&p->inside, &r->n); TADD_TAIL(&p->inside, r);
return r; return r;
} }
@ -261,7 +259,7 @@ resource_init(void)
{ {
root_pool.r.class = &pool_class; root_pool.r.class = &pool_class;
root_pool.name = "Root"; root_pool.name = "Root";
init_list(&root_pool.inside); INIT_TLIST(&root_pool.inside);
} }
/** /**
@ -340,7 +338,7 @@ mb_alloc(pool *p, unsigned size)
struct mblock *b = xmalloc(sizeof(struct mblock) + size); struct mblock *b = xmalloc(sizeof(struct mblock) + size);
b->r.class = &mb_class; b->r.class = &mb_class;
add_tail(&p->inside, &b->r.n); TADD_TAIL(&p->inside, &b->r);
b->size = size; b->size = size;
return b->data; return b->data;
} }
@ -387,7 +385,7 @@ mb_realloc(void *m, unsigned size)
struct mblock *b = SKIP_BACK(struct mblock, data, m); struct mblock *b = SKIP_BACK(struct mblock, data, m);
b = xrealloc(b, sizeof(struct mblock) + size); b = xrealloc(b, sizeof(struct mblock) + size);
replace_node(&b->r.n, &b->r.n); TFIX_NODE(&b->r);
b->size = size; b->size = size;
return b->data; return b->data;
} }

View File

@ -14,8 +14,8 @@
/* Resource */ /* Resource */
typedef struct resource { typedef struct resource {
node n; /* Inside resource pool */ TLIST_NODE(struct resource); /* Inside resource pool */
struct resclass *class; /* Resource class */ struct resclass *class; /* Resource class */
} resource; } resource;
/* Resource class */ /* Resource class */