From 12eeb07ec0233349ed421e0804cba7820286a01a Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Mon, 3 May 2004 10:45:18 +0000 Subject: [PATCH] Added mp_strcat() and mp_multicat(). --- lib/pool-str.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/pool.c | 9 --------- lib/pools.h | 11 ++++++++++- 3 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 lib/pool-str.c diff --git a/lib/pool-str.c b/lib/pool-str.c new file mode 100644 index 00000000..8dc6d237 --- /dev/null +++ b/lib/pool-str.c @@ -0,0 +1,52 @@ +/* + * Sherlock Library -- Memory Pools (String Operations) + * + * (c) 2004 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include "lib/lib.h" +#include "lib/pools.h" + +#include +#include + +char * +mp_strdup(struct mempool *p, char *s) +{ + uns l = strlen(s) + 1; + char *t = mp_alloc_fast_noalign(p, l); + memcpy(t, s, l); + return t; +} + +char * +mp_multicat(struct mempool *p, ...) +{ + va_list args, a; + va_start(args, p); + char *x, *y; + uns cnt = 0; + a = args; + while (x = va_arg(a, char *)) + cnt++; + uns *sizes = alloca(cnt * sizeof(uns)); + uns len = 1; + cnt = 0; + a = args; + while (x = va_arg(a, char *)) + len += sizes[cnt++] = strlen(x); + char *buf = mp_alloc_fast_noalign(p, len); + y = buf; + a = args; + cnt = 0; + while (x = va_arg(a, char *)) + { + memcpy(y, x, sizes[cnt]); + y += sizes[cnt++]; + } + *y = 0; + return buf; +} diff --git a/lib/pool.c b/lib/pool.c index d581466b..8b7c3725 100644 --- a/lib/pool.c +++ b/lib/pool.c @@ -109,12 +109,3 @@ mp_alloc_zero(struct mempool *p, uns s) bzero(x, s); return x; } - -char * -mp_strdup(struct mempool *p, char *s) -{ - uns l = strlen(s) + 1; - char *t = mp_alloc_fast_noalign(p, l); - memcpy(t, s, l); - return t; -} diff --git a/lib/pools.h b/lib/pools.h index ff9250dc..540e8e60 100644 --- a/lib/pools.h +++ b/lib/pools.h @@ -26,7 +26,6 @@ void mp_delete(struct mempool *); void mp_flush(struct mempool *); void *mp_alloc(struct mempool *, uns); void *mp_alloc_zero(struct mempool *, uns); -char *mp_strdup(struct mempool *, char *); static inline void *mp_alloc_fast(struct mempool *p, uns l) { @@ -61,4 +60,14 @@ mp_end_string(struct mempool *p, void *stop) p->free = stop; } +/* pool-str.c */ + +char *mp_strdup(struct mempool *, char *); +char *mp_multicat(struct mempool *, ...); +static inline char * +mp_strcat(struct mempool *mp, char *x, char *y) +{ + mp_multicat(mp, x, y, NULL); +} + #endif -- 2.39.2