+eina_memdup
authorMike Blumenkrantz <zmike@osg.samsung.com>
Mon, 15 Dec 2014 19:30:43 +0000 (14:30 -0500)
committerMike Blumenkrantz <zmike@osg.samsung.com>
Mon, 15 Dec 2014 19:32:42 +0000 (14:32 -0500)
for those times when three lines of code should be one

@feature

src/lib/eina/eina_str.c
src/lib/eina/eina_str.h

index be88c75ec3662af1bc53cd19aee0f51704a2aa20..770a64404955892c73feac213fb0f3aee40b804f 100644 (file)
@@ -662,3 +662,16 @@ eina_str_toupper(char **str)
    for (p = *str; (*p); p++)
       *p = toupper((unsigned char)(*p));
 }
+
+EAPI unsigned char *
+eina_memdup(unsigned char *mem, size_t size, Eina_Bool terminate)
+{
+   unsigned char *ret;
+
+   terminate = !!terminate;
+   ret = malloc(size + terminate);
+   memcpy(ret, mem, size);
+   if (terminate)
+     ret[size] = 0;
+   return ret;
+}
index dae592bac66aac51e746bfb1189347a0f971d63f..f3e9f9f53e79d3986caa17497f165ffcee07cc78 100644 (file)
@@ -345,6 +345,15 @@ static inline size_t eina_str_join(char *dst, size_t size, char sep, const char
 
 static inline size_t eina_strlen_bounded(const char *str, size_t maxlen) EINA_PURE EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1);
 
+/**
+ * @brief memory duplication function with optional termination for strings
+ * @param mem The memory to copy
+ * @param size The size of @p mem
+ * @param terminate If true, the returned memory will be nul terminated with '\0'
+ * @return the copied memory, must be freed
+ * @since 1.13
+ */
+EAPI unsigned char *eina_memdup(unsigned char *mem, size_t size, Eina_Bool terminate);
 #include "eina_inline_str.x"
 
 /**