]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Linpools may use pages instead of xmalloc
authorMaria Matejka <mq@ucw.cz>
Fri, 3 Sep 2021 17:48:38 +0000 (19:48 +0200)
committerMaria Matejka <mq@ucw.cz>
Wed, 13 Oct 2021 17:00:36 +0000 (19:00 +0200)
lib/mempool.c
lib/resource.h

index 758882ce251251821ebcf483cdfadf2f46aef1c0..8f300b818618e2650329186ff83abe8f04a3cd06 100644 (file)
@@ -37,9 +37,10 @@ const int lp_chunk_size = sizeof(struct lp_chunk);
 struct linpool {
   resource r;
   byte *ptr, *end;
+  pool *p;
   struct lp_chunk *first, *current;            /* Normal (reusable) chunks */
   struct lp_chunk *first_large;                        /* Large chunks */
-  uint chunk_size, threshold, total, total_large;
+  uint chunk_size, threshold, total:31, use_pages:1, total_large;
 };
 
 static void lp_free(resource *);
@@ -69,6 +70,13 @@ linpool
 *lp_new(pool *p, uint blk)
 {
   linpool *m = ralloc(p, &lp_class);
+  m->p = p;
+  if (!blk)
+  {
+    m->use_pages = 1;
+    blk = page_size - lp_chunk_size;
+  }
+
   m->chunk_size = blk;
   m->threshold = 3*blk/4;
   return m;
@@ -121,7 +129,11 @@ lp_alloc(linpool *m, uint size)
          else
            {
              /* Need to allocate a new chunk */
-             c = xmalloc(sizeof(struct lp_chunk) + m->chunk_size);
+             if (m->use_pages)
+               c = alloc_page(m->p);
+             else
+               c = xmalloc(sizeof(struct lp_chunk) + m->chunk_size);
+
              m->total += m->chunk_size;
              c->next = NULL;
              c->size = m->chunk_size;
@@ -258,7 +270,10 @@ lp_free(resource *r)
   for(d=m->first; d; d = c)
     {
       c = d->next;
-      xfree(d);
+      if (m->use_pages)
+       free_page(m->p, d);
+      else
+       xfree(d);
     }
   for(d=m->first_large; d; d = c)
     {
index 597d6c17e2f641fe91d6aa7c8db348e636488900..26030aea6781b29cacefdac7ca2c04b0a106a75c 100644 (file)
@@ -76,7 +76,7 @@ void lp_restore(linpool *m, lp_state *p);     /* Restore state */
 extern const int lp_chunk_size;
 #define LP_GAS             1024
 #define LP_GOOD_SIZE(x)            (((x + LP_GAS - 1) & (~(LP_GAS - 1))) - lp_chunk_size)
-#define lp_new_default(p)   lp_new(p, LP_GOOD_SIZE(LP_GAS*4))
+#define lp_new_default(p)   lp_new(p, 0)
 
 /* Slabs */