]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Printf variant with a result allocated inside a pool / linpool
authorMaria Matejka <mq@ucw.cz>
Tue, 15 Mar 2022 10:21:46 +0000 (11:21 +0100)
committerMaria Matejka <mq@ucw.cz>
Tue, 15 Mar 2022 10:21:46 +0000 (11:21 +0100)
lib/printf.c
lib/resource.c
lib/resource.h
lib/string.h
nest/rt-table.c

index 236df427474ffa2bb11f7638367d492efb0809e9..424d545f915ece6f52e49ef4ac0b08e65ea80279 100644 (file)
@@ -568,3 +568,51 @@ buffer_puts(buffer *buf, const char *str)
 
   buf->pos = (bp < be) ? bp : buf->end;
 }
+
+#define POOL_PRINTF_MAXBUF     1024
+
+char *mb_vsprintf(pool *p, const char *fmt, va_list args)
+{
+  char buf[POOL_PRINTF_MAXBUF];
+  int count = bvsnprintf(buf, POOL_PRINTF_MAXBUF, fmt, args);
+
+  if (count < 0)
+    bug("Attempted to mb_vsprintf() a too long string");
+
+  char *out = mb_alloc(p, count + 1);
+  memcpy(out, buf, count + 1);
+  return out;
+}
+
+char *mb_sprintf(pool *p, const char *fmt, ...)
+{
+  va_list args;
+  char *out;
+  va_start(args, fmt);
+  out = mb_vsprintf(p, fmt, args);
+  va_end(args);
+  return out;
+}
+
+char *lp_vsprintf(linpool *p, const char *fmt, va_list args)
+{
+  char buf[POOL_PRINTF_MAXBUF];
+  int count = bvsnprintf(buf, POOL_PRINTF_MAXBUF, fmt, args);
+
+  if (count < 0)
+    bug("Attempted to mb_vsprintf() a too long string");
+
+  char *out = lp_alloc(p, count + 1);
+  memcpy(out, buf, count + 1);
+  return out;
+}
+
+char *lp_sprintf(linpool *p, const char *fmt, ...)
+{
+  va_list args;
+  char *out;
+  va_start(args, fmt);
+  out = lp_vsprintf(p, fmt, args);
+  va_end(args);
+  return out;
+}
index a179afe3d8aa26add53a6c5311a1bb6c8be278f5..89e559b4818a2ecae118e7e9b5370c6c05fedc65 100644 (file)
@@ -70,6 +70,20 @@ rp_new(pool *p, const char *name)
   return z;
 }
 
+pool *
+rp_newf(pool *p, const char *fmt, ...)
+{
+  pool *z = rp_new(p, NULL);
+
+  va_list args;
+  va_start(args, fmt);
+  z->name = mb_vsprintf(p, fmt, args);
+  va_end(args);
+
+  return z;
+}
+
+
 static void
 pool_free(resource *P)
 {
@@ -410,21 +424,6 @@ mb_realloc(void *m, unsigned size)
   return b->data;
 }
 
-/**
- * mb_move - move a memory block
- * @m: memory block
- * @p: target pool
- *
- * mb_move() moves the given memory block to another pool in the same way
- * as rmove() moves a plain resource.
- */
-void
-mb_move(void *m, pool *p)
-{
-  struct mblock *b = SKIP_BACK(struct mblock, data, m);
-  rmove(b, p);
-}
-
 
 /**
  * mb_free - free a memory block
index 313b01dca9ad0b8779b09619d46834f4bd643db9..ed9e109d987ba399f979f41cded6242e736d5301 100644 (file)
@@ -44,6 +44,7 @@ typedef struct pool pool;
 
 void resource_init(void);
 pool *rp_new(pool *, const char *);    /* Create new pool */
+pool *rp_newf(pool *, const char *, ...);      /* Create a new pool with a formatted string as its name */
 void rfree(void *);                    /* Free single resource */
 void rdump(void *);                    /* Dump to debug output */
 struct resmem rmemsize(void *res);             /* Return size of memory used by the resource */
@@ -59,7 +60,6 @@ extern pool root_pool;
 void *mb_alloc(pool *, unsigned size);
 void *mb_allocz(pool *, unsigned size);
 void *mb_realloc(void *m, unsigned size);
-void mb_move(void *, pool *);
 void mb_free(void *);
 
 /* Memory pools with linear allocation */
index 976b1c247111a8d3eed24f7aea9cf77aafe7ba81..2829943de0e9301cc94993fe1e3e248f4f50f4a0 100644 (file)
@@ -20,6 +20,11 @@ int bvsprintf(char *str, const char *fmt, va_list args);
 int bsnprintf(char *str, int size, const char *fmt, ...);
 int bvsnprintf(char *str, int size, const char *fmt, va_list args);
 
+char *mb_sprintf(pool *p, const char *fmt, ...);
+char *mb_vsprintf(pool *p, const char *fmt, va_list args);
+char *lp_sprintf(linpool *p, const char *fmt, ...);
+char *lp_vsprintf(linpool *p, const char *fmt, va_list args);
+
 int buffer_vprint(buffer *buf, const char *fmt, va_list args);
 int buffer_print(buffer *buf, const char *fmt, ...);
 void buffer_puts(buffer *buf, const char *str);
index b3ca3d050ed894f46c7614396fe6e69c6da4eedc..a1e49c215629eb92a13d7faf6ff9bdc4795970a0 100644 (file)
@@ -2094,12 +2094,7 @@ static struct resclass rt_class = {
 rtable *
 rt_setup(pool *pp, struct rtable_config *cf)
 {
-  int ns = strlen("Routing table ") + strlen(cf->name) + 1;
-  void *nb = mb_alloc(pp, ns);
-  ASSERT_DIE(ns - 1 == bsnprintf(nb, ns, "Routing table %s", cf->name));
-
-  pool *p = rp_new(pp, nb);
-  mb_move(nb, p);
+  pool *p = rp_newf(pp, "Routing table %s", cf->name);
 
   rtable *t = ralloc(p, &rt_class);
   t->rp = p;