forked from osmocom/wireshark
Add memdup function to wmem.
svn path=/trunk/; revision=49857
This commit is contained in:
parent
edd3dfd6f8
commit
226b39f1d7
|
@ -99,6 +99,17 @@ wmem_realloc(wmem_allocator_t *allocator, void *ptr, const size_t size)
|
|||
return allocator->realloc(allocator->private_data, ptr, size);
|
||||
}
|
||||
|
||||
void *
|
||||
wmem_memdup(wmem_allocator_t *allocator, const void *source, const size_t size)
|
||||
{
|
||||
void *dest;
|
||||
|
||||
dest = wmem_alloc(allocator, size);
|
||||
memcpy(dest, source, size);
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
static void
|
||||
wmem_free_all_real(wmem_allocator_t *allocator, gboolean final)
|
||||
{
|
||||
|
|
|
@ -135,6 +135,18 @@ void *
|
|||
wmem_realloc(wmem_allocator_t *allocator, void *ptr, const size_t size)
|
||||
G_GNUC_MALLOC;
|
||||
|
||||
/** Copies a block of memory.
|
||||
*
|
||||
* @param allocator The allocator object to use to allocate memory to copy into.
|
||||
* @param source The pointer to the memory block to copy.
|
||||
* @param size The amount of memory to copy.
|
||||
* @return The location of the memory copy.
|
||||
*/
|
||||
WS_DLL_PUBLIC
|
||||
void *
|
||||
wmem_memdup(wmem_allocator_t *allocator, const void *source, const size_t size)
|
||||
G_GNUC_MALLOC;
|
||||
|
||||
/** Frees all the memory allocated in a pool. Depending on the allocator
|
||||
* implementation used this can be significantly cheaper than calling
|
||||
* wmem_free() on all the individual blocks. It also doesn't require you to have
|
||||
|
|
|
@ -313,6 +313,30 @@ wmem_test_allocator_strict(void)
|
|||
|
||||
/* UTILITY TESTING FUNCTIONS (/wmem/utils/) */
|
||||
|
||||
static void
|
||||
wmem_test_miscutls(void)
|
||||
{
|
||||
wmem_allocator_t *allocator;
|
||||
const char *source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
char *ret;
|
||||
|
||||
allocator = wmem_allocator_force_new(WMEM_ALLOCATOR_STRICT);
|
||||
|
||||
ret = (char*) wmem_memdup(allocator, source, 5);
|
||||
ret[4] = '\0';
|
||||
g_assert_cmpstr(ret, ==, "ABCD");
|
||||
|
||||
ret = (char*) wmem_memdup(allocator, source, 1);
|
||||
g_assert(ret[0] == 'A');
|
||||
wmem_strict_check_canaries(allocator);
|
||||
|
||||
ret = (char*) wmem_memdup(allocator, source, 10);
|
||||
ret[9] = '\0';
|
||||
g_assert_cmpstr(ret, ==, "ABCDEFGHI");
|
||||
|
||||
wmem_destroy_allocator(allocator);
|
||||
}
|
||||
|
||||
static void
|
||||
wmem_test_strutls(void)
|
||||
{
|
||||
|
@ -536,6 +560,7 @@ main(int argc, char **argv)
|
|||
g_test_add_func("/wmem/allocator/times", wmem_time_allocators);
|
||||
g_test_add_func("/wmem/allocator/callbacks", wmem_test_allocator_callbacks);
|
||||
|
||||
g_test_add_func("/wmem/utils/misc", wmem_test_miscutls);
|
||||
g_test_add_func("/wmem/utils/strings", wmem_test_strutls);
|
||||
|
||||
g_test_add_func("/wmem/datastruct/slist", wmem_test_slist);
|
||||
|
|
Loading…
Reference in New Issue