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

Simplified temporary resources

Also TMP_SAVED now uses the CLEANUP hook to allow for breaks and returns
This commit is contained in:
Maria Matejka 2024-05-08 18:55:01 +02:00
parent 76ca53e4f8
commit b5f803ce4a
4 changed files with 21 additions and 24 deletions

View File

@ -207,6 +207,7 @@ lp_save(linpool *m)
struct lp_state *p = lp_alloc(m, sizeof(struct lp_state));
ASSERT_DIE(m->current);
*p = (struct lp_state) {
.p = m,
.current = m->current,
.large = m->first_large,
.total_large = m->total_large,
@ -233,6 +234,7 @@ lp_restore(linpool *m, lp_state *p)
/* Move ptr to the saved pos and free all newer large chunks */
ASSERT_DIE(p->current);
ASSERT_DIE(p->p == m);
m->current = c = p->current;
m->ptr = (byte *) p;
m->end = c->data + LP_DATA_SIZE;

View File

@ -338,28 +338,22 @@ resource_init(void)
root_pool.r.class = &pool_class;
rp_init(&root_pool, the_bird_domain.the_bird, "Root");
tmp_init(&root_pool, the_bird_domain.the_bird);
tmp_init(&root_pool);
}
_Thread_local struct tmp_resources tmp_res;
_Thread_local linpool *tmp_linpool;
void
tmp_init(pool *p, struct domain_generic *dom)
tmp_init(pool *p)
{
tmp_res.lp = lp_new_default(p);
tmp_res.parent = p;
tmp_res.pool = rp_new(p, dom, "TMP");
tmp_res.domain = dom;
ASSERT_DIE(!tmp_linpool);
tmp_linpool = lp_new_default(p);
}
void
tmp_flush(void)
{
ASSERT_DIE(DG_IS_LOCKED(tmp_res.domain));
lp_flush(tmp_linpool);
rp_free(tmp_res.pool);
tmp_res.pool = rp_new(tmp_res.parent, tmp_res.domain, "TMP");
}

View File

@ -91,6 +91,7 @@ void mb_free(void *);
typedef struct linpool linpool;
typedef struct lp_state {
struct linpool *p;
void *current, *large;
uint total_large;
} lp_state;
@ -103,28 +104,28 @@ void lp_flush(linpool *); /* Free everything, but leave linpool */
lp_state *lp_save(linpool *m); /* Save state */
void lp_restore(linpool *m, lp_state *p); /* Restore state */
#define LP_SAVED(m) for (struct lp_state *_lp_state = lp_save(m); _lp_state; lp_restore(m, _lp_state), _lp_state = NULL)
#define TMP_SAVED LP_SAVED(tmp_linpool)
static inline void lp_saved_cleanup(struct lp_state **lps)
{
if (*lps)
lp_restore((*lps)->p, (*lps));
}
struct tmp_resources {
pool *pool, *parent;
linpool *lp;
struct domain_generic *domain;
};
#define LP_SAVED(m) for (CLEANUP(lp_saved_cleanup) struct lp_state *_lp_state = lp_save(m); _lp_state; lp_restore(m, _lp_state), _lp_state = NULL)
extern _Thread_local struct tmp_resources tmp_res;
#define lp_new_default lp_new
#define tmp_linpool tmp_res.lp
/* Thread-local temporary linpools */
extern _Thread_local linpool *tmp_linpool;
#define tmp_alloc(sz) lp_alloc(tmp_linpool, sz)
#define tmp_allocu(sz) lp_allocu(tmp_linpool, sz)
#define tmp_allocz(sz) lp_allocz(tmp_linpool, sz)
#define TMP_SAVED LP_SAVED(tmp_linpool)
void tmp_init(pool *p, struct domain_generic *dg);
void tmp_init(pool *p);
void tmp_flush(void);
#define lp_new_default lp_new
/* Slabs */
typedef struct slab slab;

View File

@ -792,7 +792,7 @@ bird_thread_main(void *arg)
birdloop_enter(thr->meta);
this_birdloop = thr->meta;
tmp_init(thr->pool, birdloop_domain(thr->meta));
tmp_init(thr->pool);
init_list(&thr->loops);
defer_init(lp_new(thr->pool));