diff --git a/src/util/ralloc.c b/src/util/ralloc.c index aaa1f28094e..04bb6cc1d71 100644 --- a/src/util/ralloc.c +++ b/src/util/ralloc.c @@ -365,6 +365,18 @@ ralloc_set_destructor(const void *ptr, void(*destructor)(void *)) info->destructor = destructor; } +void * +ralloc_memdup(const void *ctx, const void *mem, size_t n) +{ + void *ptr = ralloc_size(ctx, n); + + if (unlikely(ptr == NULL)) + return NULL; + + memcpy(ptr, mem, n); + return ptr; +} + char * ralloc_strdup(const void *ctx, const char *str) { diff --git a/src/util/ralloc.h b/src/util/ralloc.h index 1af4da8c824..f12606381f9 100644 --- a/src/util/ralloc.h +++ b/src/util/ralloc.h @@ -313,6 +313,11 @@ void *ralloc_parent(const void *ptr); */ void ralloc_set_destructor(const void *ptr, void(*destructor)(void *)); +/** + * Duplicate memory, allocating the memory from the given context. + */ +void *ralloc_memdup(const void *ctx, const void *mem, size_t n) MALLOCLIKE; + /// \defgroup array String Functions @{ /** * Duplicate a string, allocating the memory from the given context.