lib: Add memdup()
Add a function to duplicate a memory region, a little like strdup(). Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
parent
0caf37e973
commit
930c887e0f
@ -129,6 +129,19 @@ extern void * memchr(const void *,int,__kernel_size_t);
|
|||||||
void *memchr_inv(const void *, int, size_t);
|
void *memchr_inv(const void *, int, size_t);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* memdup() - allocate a buffer and copy in the contents
|
||||||
|
*
|
||||||
|
* Note that this returns a valid pointer even if @len is 0
|
||||||
|
*
|
||||||
|
* @src: data to copy in
|
||||||
|
* @len: number of bytes to copy
|
||||||
|
* @return allocated buffer with the copied contents, or NULL if not enough
|
||||||
|
* memory is available
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
char *memdup(const void *src, size_t len);
|
||||||
|
|
||||||
unsigned long ustrtoul(const char *cp, char **endp, unsigned int base);
|
unsigned long ustrtoul(const char *cp, char **endp, unsigned int base);
|
||||||
unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base);
|
unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base);
|
||||||
|
|
||||||
|
13
lib/string.c
13
lib/string.c
@ -659,6 +659,19 @@ void * memscan(void * addr, int c, size_t size)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
char *memdup(const void *src, size_t len)
|
||||||
|
{
|
||||||
|
char *p;
|
||||||
|
|
||||||
|
p = malloc(len);
|
||||||
|
if (!p)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
memcpy(p, src, len);
|
||||||
|
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
#ifndef __HAVE_ARCH_STRSTR
|
#ifndef __HAVE_ARCH_STRSTR
|
||||||
/**
|
/**
|
||||||
* strstr - Find the first substring in a %NUL terminated string
|
* strstr - Find the first substring in a %NUL terminated string
|
||||||
|
@ -23,6 +23,8 @@
|
|||||||
/* Allow for copying up to 32 bytes */
|
/* Allow for copying up to 32 bytes */
|
||||||
#define BUFLEN (SWEEP + 33)
|
#define BUFLEN (SWEEP + 33)
|
||||||
|
|
||||||
|
#define TEST_STR "hello"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* init_buffer() - initialize buffer
|
* init_buffer() - initialize buffer
|
||||||
*
|
*
|
||||||
@ -193,3 +195,33 @@ static int lib_memmove(struct unit_test_state *uts)
|
|||||||
}
|
}
|
||||||
|
|
||||||
LIB_TEST(lib_memmove, 0);
|
LIB_TEST(lib_memmove, 0);
|
||||||
|
|
||||||
|
/** lib_memdup() - unit test for memdup() */
|
||||||
|
static int lib_memdup(struct unit_test_state *uts)
|
||||||
|
{
|
||||||
|
char buf[BUFLEN];
|
||||||
|
size_t len;
|
||||||
|
char *p, *q;
|
||||||
|
|
||||||
|
/* Zero size should do nothing */
|
||||||
|
p = memdup(NULL, 0);
|
||||||
|
ut_assertnonnull(p);
|
||||||
|
free(p);
|
||||||
|
|
||||||
|
p = memdup(buf, 0);
|
||||||
|
ut_assertnonnull(p);
|
||||||
|
free(p);
|
||||||
|
|
||||||
|
strcpy(buf, TEST_STR);
|
||||||
|
len = sizeof(TEST_STR);
|
||||||
|
p = memdup(buf, len);
|
||||||
|
ut_asserteq_mem(p, buf, len);
|
||||||
|
|
||||||
|
q = memdup(p, len);
|
||||||
|
ut_asserteq_mem(q, buf, len);
|
||||||
|
free(q);
|
||||||
|
free(p);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
LIB_TEST(lib_memdup, 0);
|
||||||
|
Loading…
Reference in New Issue
Block a user