[PATCH 1/3] string: Add (x)memdup

Sascha Hauer s.hauer at pengutronix.de
Tue Nov 26 07:17:18 EST 2013


It's a common task to duplicate some memory. Add (x)memdup functions.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 include/string.h |  2 ++
 include/xfuncs.h |  1 +
 lib/string.c     | 14 ++++++++++++++
 lib/xfuncs.c     | 10 ++++++++++
 4 files changed, 27 insertions(+)

diff --git a/include/string.h b/include/string.h
index b906e15..a833da1 100644
--- a/include/string.h
+++ b/include/string.h
@@ -3,4 +3,6 @@
 
 #include <linux/string.h>
 
+void *memdup(const void *, size_t);
+
 #endif /* __STRING_H */
diff --git a/include/xfuncs.h b/include/xfuncs.h
index 261aaa5..8efc99d 100644
--- a/include/xfuncs.h
+++ b/include/xfuncs.h
@@ -8,5 +8,6 @@ void *xrealloc(void *ptr, size_t size);
 void *xzalloc(size_t size);
 char *xstrdup(const char *s);
 void* xmemalign(size_t alignment, size_t bytes);
+void* xmemdup(const void *orig, size_t size);
 
 #endif /* __XFUNCS_H */
diff --git a/lib/string.c b/lib/string.c
index eeec137..ceced7f 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -725,3 +725,17 @@ void *memchr_inv(const void *start, int c, size_t bytes)
 	return check_bytes8(start, value, bytes % 8);
 }
 EXPORT_SYMBOL(memchr_inv);
+
+void *memdup(const void *orig, size_t size)
+{
+	void *buf;
+
+	buf = malloc(size);
+	if (!buf)
+		return NULL;
+
+	memcpy(buf, orig, size);
+
+	return buf;
+}
+EXPORT_SYMBOL(memdup);
diff --git a/lib/xfuncs.c b/lib/xfuncs.c
index db85720..86d0013 100644
--- a/lib/xfuncs.c
+++ b/lib/xfuncs.c
@@ -75,3 +75,13 @@ void* xmemalign(size_t alignment, size_t bytes)
 	return p;
 }
 EXPORT_SYMBOL(xmemalign);
+
+void *xmemdup(const void *orig, size_t size)
+{
+	void *buf = xmalloc(size);
+
+	memcpy(buf, orig, size);
+
+	return buf;
+}
+EXPORT_SYMBOL(xmemdup);
-- 
1.8.4.2




More information about the barebox mailing list