Patchwork [1/7] add qemu_memdup

login
register
mail settings
Submitter Paolo Bonzini
Date Oct. 17, 2009, 7:55 a.m.
Message ID <1255766136-3028-2-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/36290/
State New
Headers show

Comments

Paolo Bonzini - Oct. 17, 2009, 7:55 a.m.
A nice-to-have utility function, it helps avoiding useless strlen+strdup
pairs.

Cc: Luiz Capitulino <lcapitulino@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 qemu-common.h |    1 +
 qemu-malloc.c |    8 ++++++++
 2 files changed, 9 insertions(+), 0 deletions(-)

Patch

diff --git a/qemu-common.h b/qemu-common.h
index 820dd37..351f1ac 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -141,6 +141,7 @@  void *qemu_realloc(void *ptr, size_t size);
 void *qemu_mallocz(size_t size);
 void qemu_free(void *ptr);
 char *qemu_strdup(const char *str);
+void *qemu_memdup(const void *str, size_t size);
 char *qemu_strndup(const char *str, size_t size);
 
 void *get_mmap_addr(unsigned long size);
diff --git a/qemu-malloc.c b/qemu-malloc.c
index 295d185..9e11e4b 100644
--- a/qemu-malloc.c
+++ b/qemu-malloc.c
@@ -70,6 +70,14 @@  void *qemu_mallocz(size_t size)
     return ptr;
 }
 
+void *qemu_memdup(const void *str, size_t n)
+{
+    char *ptr;
+    ptr = qemu_malloc(n);
+    memcpy(ptr, str, n);
+    return ptr;
+}
+
 char *qemu_strdup(const char *str)
 {
     char *ptr;