From 75a80e71a0d3a08f4bc561f16702c36d45e32c31 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 5 May 2009 07:03:02 -0700 Subject: [PATCH] pool: New function pool_strdup0. This function is the pool analogue of xmemdup0, except that it is only appropriate for use with strings. --- src/libpspp/pool.c | 13 +++++++++++++ src/libpspp/pool.h | 1 + 2 files changed, 14 insertions(+) diff --git a/src/libpspp/pool.c b/src/libpspp/pool.c index 88d3535d..dbdd71b6 100644 --- a/src/libpspp/pool.c +++ b/src/libpspp/pool.c @@ -364,6 +364,19 @@ pool_strdup (struct pool *pool, const char *string) return pool_clone_unaligned (pool, string, strlen (string) + 1); } +/* Duplicates the SIZE bytes of STRING, plus a trailing 0 byte, + and returns a pointer to the duplicate. For use only with + strings, because the returned pointere may not be aligned + properly for other types. */ +char * +pool_strdup0 (struct pool *pool, const char *string, size_t size) +{ + char *new_string = pool_alloc_unaligned (pool, size + 1); + memcpy (new_string, string, size); + new_string[size] = '\0'; + return new_string; +} + /* Formats FORMAT with the given ARGS in memory allocated from POOL and returns the formatted string. */ char * diff --git a/src/libpspp/pool.h b/src/libpspp/pool.h index e6c885e4..fc24b2f9 100644 --- a/src/libpspp/pool.h +++ b/src/libpspp/pool.h @@ -62,6 +62,7 @@ void *pool_clone (struct pool *, const void *, size_t) MALLOC_LIKE; void *pool_alloc_unaligned (struct pool *, size_t) MALLOC_LIKE; void *pool_clone_unaligned (struct pool *, const void *, size_t) MALLOC_LIKE; char *pool_strdup (struct pool *, const char *) MALLOC_LIKE; +char *pool_strdup0 (struct pool *, const char *, size_t) MALLOC_LIKE; char *pool_vasprintf (struct pool *, const char *, va_list) MALLOC_LIKE PRINTF_FORMAT (2, 0); char *pool_asprintf (struct pool *, const char *, ...) -- 2.30.2