From d2f8089ecba7131c856bf05a85f85eee15b90e54 Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Tue, 9 Oct 2012 12:51:27 +0200 Subject: [PATCH] shl: misc: add shl_dup() helper shl_dup() duplicates a memory region similar to strdup() but without reading the data length via strlen(). Signed-off-by: David Herrmann --- src/shl_misc.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/shl_misc.h b/src/shl_misc.h index 9afd058..bf955d5 100644 --- a/src/shl_misc.h +++ b/src/shl_misc.h @@ -61,6 +61,22 @@ static inline int shl_strtou(const char *input, unsigned int *output) return 0; } +static inline int shl_dup(void **out, const void *data, size_t size) +{ + void *cpy; + + if (!data || !size) + return -EINVAL; + + cpy = malloc(size); + if (!cpy) + return -ENOMEM; + + memcpy(cpy, data, size); + *out = cpy; + return 0; +} + /* TODO: xkbcommon should provide these flags! * We currently copy them into each library API we use so we need to keep * them in sync. Currently, they're used in uterm-input and tsm-vte. */ -- 2.7.4