From 66b00e2966d348a298ebcb4881c01b538058a2ef Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Thu, 22 Feb 2024 10:13:51 -0400 Subject: [PATCH] util/ralloc: add memdup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Alyssa Rosenzweig Reviewed-by: Mike Blumenkrantz Reviewed-by: Marek Olšák Part-of: --- src/util/ralloc.c | 12 ++++++++++++ src/util/ralloc.h | 5 +++++ 2 files changed, 17 insertions(+) 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.