Printf variant with a result allocated inside a pool / linpool

This commit is contained in:
Maria Matejka 2022-03-15 11:21:46 +01:00
parent 3c42f7af6a
commit c53f547a0b
5 changed files with 69 additions and 22 deletions

View file

@ -568,3 +568,51 @@ buffer_puts(buffer *buf, const char *str)
buf->pos = (bp < be) ? bp : buf->end; 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;
}

View file

@ -70,6 +70,20 @@ rp_new(pool *p, const char *name)
return z; 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 static void
pool_free(resource *P) pool_free(resource *P)
{ {
@ -410,21 +424,6 @@ mb_realloc(void *m, unsigned size)
return b->data; 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 * mb_free - free a memory block

View file

@ -44,6 +44,7 @@ typedef struct pool pool;
void resource_init(void); void resource_init(void);
pool *rp_new(pool *, const char *); /* Create new pool */ 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 rfree(void *); /* Free single resource */
void rdump(void *); /* Dump to debug output */ void rdump(void *); /* Dump to debug output */
struct resmem rmemsize(void *res); /* Return size of memory used by the resource */ 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_alloc(pool *, unsigned size);
void *mb_allocz(pool *, unsigned size); void *mb_allocz(pool *, unsigned size);
void *mb_realloc(void *m, unsigned size); void *mb_realloc(void *m, unsigned size);
void mb_move(void *, pool *);
void mb_free(void *); void mb_free(void *);
/* Memory pools with linear allocation */ /* Memory pools with linear allocation */

View 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 bsnprintf(char *str, int size, const char *fmt, ...);
int bvsnprintf(char *str, int size, const char *fmt, va_list args); 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_vprint(buffer *buf, const char *fmt, va_list args);
int buffer_print(buffer *buf, const char *fmt, ...); int buffer_print(buffer *buf, const char *fmt, ...);
void buffer_puts(buffer *buf, const char *str); void buffer_puts(buffer *buf, const char *str);

View file

@ -2094,12 +2094,7 @@ static struct resclass rt_class = {
rtable * rtable *
rt_setup(pool *pp, struct rtable_config *cf) rt_setup(pool *pp, struct rtable_config *cf)
{ {
int ns = strlen("Routing table ") + strlen(cf->name) + 1; pool *p = rp_newf(pp, "Routing table %s", cf->name);
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);
rtable *t = ralloc(p, &rt_class); rtable *t = ralloc(p, &rt_class);
t->rp = p; t->rp = p;