Skip to contentSkip to conversation

implementing qemu_reallocz

PATCH In review Stefano Stabellini 2 participants 2 messages Nov 21, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 implementing qemu_reallocz

Adding a qemu_reallocz implementation.

---

qemu-common.h +1 -0
diff -r aceb9c1e17b1 qemu-common.h
@@ -96,6 +96,7 @@
 
 void *qemu_malloc(size_t size);
 void *qemu_realloc(void *ptr, size_t size);
+void *qemu_reallocz(void *ptr, size_t size);
 void *qemu_mallocz(size_t size);
 void qemu_free(void *ptr);
 char *qemu_strdup(const char *str);
qemu-malloc.c +10 -0
diff -r aceb9c1e17b1 qemu-malloc.c
@@ -53,6 +53,16 @@
     return ptr;
 }
 
+void *qemu_reallocz(void *ptr, size_t size)
+{
+    void *res;
+    res = realloc(ptr, size);
+    if (!res)
+        return NULL;
+    memset(res, 0, size);
+    return res;
+}
+
 char *qemu_strdup(const char *str)
 {
     char *ptr;
#1 lore raw

qemu-malloc.c line 65

Quoted 13 lines · Stefano Stabellini

Adding a qemu_reallocz implementation.

qemu-malloc.c +10 -0
+void *qemu_reallocz(void *ptr, size_t size)
+{
+    void *res;
+    res = realloc(ptr, size);
+    if (!res)
+        return NULL;
+    memset(res, 0, size);
+    return res;
+}
+

I think this interface is pretty non-intuitive. My first expectation is that only the grown region would be zero'd, not the whole thing.

In general, if you don't care about the old memory, free()/malloc() would be a better combination. You're just doing extra work by using realloc() here.

Regards,

Anthony Liguori

Quoted 6 lines

char *qemu_strdup(const char *str)

 {
     char *ptr;