summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMaria Matejka <mq@ucw.cz>2022-03-15 11:21:46 +0100
committerMaria Matejka <mq@ucw.cz>2022-03-15 11:21:46 +0100
commitc53f547a0bf437fb95923b2a0b9ac497e474aef1 (patch)
treec4fd70c83ef16b8d343c42a6f77616ee56e8bba2 /lib
parent3c42f7af6a23de3f135235521318301e5b34f2de (diff)
Printf variant with a result allocated inside a pool / linpool
Diffstat (limited to 'lib')
-rw-r--r--lib/printf.c48
-rw-r--r--lib/resource.c29
-rw-r--r--lib/resource.h2
-rw-r--r--lib/string.h5
4 files changed, 68 insertions, 16 deletions
diff --git a/lib/printf.c b/lib/printf.c
index 236df427..424d545f 100644
--- a/lib/printf.c
+++ b/lib/printf.c
@@ -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;
+}
diff --git a/lib/resource.c b/lib/resource.c
index a179afe3..89e559b4 100644
--- a/lib/resource.c
+++ b/lib/resource.c
@@ -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
diff --git a/lib/resource.h b/lib/resource.h
index 313b01dc..ed9e109d 100644
--- a/lib/resource.h
+++ b/lib/resource.h
@@ -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 */
diff --git a/lib/string.h b/lib/string.h
index 976b1c24..2829943d 100644
--- a/lib/string.h
+++ b/lib/string.h
@@ -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);