]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
Introduce os_memdup()
authorJohannes Berg <johannes.berg@intel.com>
Tue, 7 Mar 2017 09:17:23 +0000 (10:17 +0100)
committerJouni Malinen <j@w1.fi>
Tue, 7 Mar 2017 11:18:49 +0000 (13:18 +0200)
This can be used to clean the code and reduce size by converting
os_malloc() followed by os_memcpy() cases to use a single function call.

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
src/utils/os.h
src/utils/os_none.c
src/utils/os_unix.c
src/utils/os_win32.c

index e8f0b792738ab1ea4db4c1f9af561c764188027c..21ba5c3ff85b337042c88bf7244402911e97df52 100644 (file)
@@ -614,6 +614,18 @@ size_t os_strlcpy(char *dest, const char *src, size_t siz);
  */
 int os_memcmp_const(const void *a, const void *b, size_t len);
 
+
+/**
+ * os_memdup - Allocate duplicate of passed memory chunk
+ * @src: Source buffer to duplicate
+ * @len: Length of source buffer
+ * Returns: %NULL if allocation failed, copy of src buffer otherwise
+ *
+ * This function allocates a memory block like os_malloc() would, and
+ * copies the given source buffer into it.
+ */
+void * os_memdup(const void *src, size_t len);
+
 /**
  * os_exec - Execute an external program
  * @program: Path to the program
index 0c3214d32ce50f2b9d77b6d3b9fb43b0e45190cb..e74f206a2c5a291e5679fa581f93a7d61367e636 100644 (file)
@@ -114,6 +114,12 @@ void * os_zalloc(size_t size)
 }
 
 
+void * os_memdup(const void *src, size_t n)
+{
+       return NULL;
+}
+
+
 #ifdef OS_NO_C_LIB_DEFINES
 void * os_malloc(size_t size)
 {
index 26fd172b78ba2c1709a112abd23ce71f8ab2e135..1894fcdb0cf25c9d101e3ffd02c68d6122d56b29 100644 (file)
@@ -508,6 +508,16 @@ int os_memcmp_const(const void *a, const void *b, size_t len)
 }
 
 
+void * os_memdup(const void *src, size_t len)
+{
+       void *r = os_malloc(len);
+
+       if (r)
+               os_memcpy(r, src, len);
+       return r;
+}
+
+
 #ifdef WPA_TRACE
 
 #if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS)
@@ -540,6 +550,8 @@ static int testing_fail_alloc(void)
                i++;
        if (i < res && os_strcmp(func[i], "os_strdup") == 0)
                i++;
+       if (i < res && os_strcmp(func[i], "os_memdup") == 0)
+               i++;
 
        pos = wpa_trace_fail_func;
 
index dea27b9f2ad846c709f6074b4ff0985b0970aade..f9e4b308ea5fdeca16d2a1fa938a5d8dddc9187d 100644 (file)
@@ -283,3 +283,13 @@ int os_exec(const char *program, const char *arg, int wait_completion)
 {
        return -1;
 }
+
+
+void * os_memdup(const void *src, size_t len)
+{
+       void *r = os_malloc(len);
+
+       if (r)
+               os_memcpy(r, src, len);
+       return r;
+}