From 89c92abb7a696c03080be2339852ab1c83795218 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Wed, 8 May 2024 18:55:01 +0200 Subject: [PATCH] Simplified temporary resources Also TMP_SAVED now uses the CLEANUP hook to allow for breaks and returns --- lib/mempool.c | 2 ++ lib/resource.c | 16 +++++----------- lib/resource.h | 25 +++++++++++++------------ sysdep/unix/io-loop.c | 2 +- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/lib/mempool.c b/lib/mempool.c index da1dc857..ce513cfc 100644 --- a/lib/mempool.c +++ b/lib/mempool.c @@ -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; diff --git a/lib/resource.c b/lib/resource.c index 441e5e3d..b89f772c 100644 --- a/lib/resource.c +++ b/lib/resource.c @@ -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"); } diff --git a/lib/resource.h b/lib/resource.h index abcf46fa..1c5519a2 100644 --- a/lib/resource.h +++ b/lib/resource.h @@ -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; diff --git a/sysdep/unix/io-loop.c b/sysdep/unix/io-loop.c index 0853204e..f777d210 100644 --- a/sysdep/unix/io-loop.c +++ b/sysdep/unix/io-loop.c @@ -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));