Introduce os_memdup()

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>
This commit is contained in:
Johannes Berg 2017-03-07 10:17:23 +01:00 committed by Jouni Malinen
parent 60be144e45
commit dbdda355d0
4 changed files with 40 additions and 0 deletions

View 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); 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 * os_exec - Execute an external program
* @program: Path to the program * @program: Path to the program

View 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 #ifdef OS_NO_C_LIB_DEFINES
void * os_malloc(size_t size) void * os_malloc(size_t size)
{ {

View 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 #ifdef WPA_TRACE
#if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS) #if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS)
@ -540,6 +550,8 @@ static int testing_fail_alloc(void)
i++; i++;
if (i < res && os_strcmp(func[i], "os_strdup") == 0) if (i < res && os_strcmp(func[i], "os_strdup") == 0)
i++; i++;
if (i < res && os_strcmp(func[i], "os_memdup") == 0)
i++;
pos = wpa_trace_fail_func; pos = wpa_trace_fail_func;

View file

@ -283,3 +283,13 @@ int os_exec(const char *program, const char *arg, int wait_completion)
{ {
return -1; 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;
}