]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Simplified temporary resources
authorMaria Matejka <mq@ucw.cz>
Wed, 8 May 2024 16:55:01 +0000 (18:55 +0200)
committerMaria Matejka <mq@ucw.cz>
Fri, 17 May 2024 07:12:52 +0000 (09:12 +0200)
Also TMP_SAVED now uses the CLEANUP hook to allow for breaks and returns

lib/mempool.c
lib/resource.c
lib/resource.h
sysdep/unix/io-loop.c

index da1dc857fbad2d764eed82ed6d6b507fb9839968..ce513cfc5657aef6a17a33f9c5c5d2d977f81fbd 100644 (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;
index 441e5e3d0c8f4ef54b477429a4d20d47645f58be..b89f772c1dfdc682b63fcf92de624ead9a854666 100644 (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");
 }
 
 
index abcf46fa7b2f04368ae5b7798eafd14f4a5dabd0..1c5519a27d8bfabe3687d5aa9c5808b037bc05c3 100644 (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)
+
+#define lp_new_default lp_new
 
-extern _Thread_local struct tmp_resources tmp_res;
+/* Thread-local temporary linpools */
 
-#define tmp_linpool    tmp_res.lp
+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;
index d4eb033511b6e5a23ae32886ded34e48d5a35056..72892daa888684cea27fde58634ec0d6ab4c2208 100644 (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));