Skip to contentSkip to conversation

target: hax: replace g_malloc with g_malloc0

PATCH Applied Li Qiang 5 participants 7 messages Nov 15, 2018 – Nov 22, 2018

lore
Share & tools
lore

Li Qiang

author
#0 lore raw

PATCH 1/1 target: hax: replace g_malloc with g_malloc0

And also the g_malloc doesn't need check return value, remove it.

---
 target/i386/hax-all.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
target/i386/hax-all.c +3 -12
index d2e512856b..a460c605af 100644
@@ -154,13 +154,7 @@ int hax_vcpu_create(int id)
         return 0;
     }
 
-    vcpu = g_malloc(sizeof(struct hax_vcpu_state));
-    if (!vcpu) {
-        fprintf(stderr, "Failed to alloc vcpu state\n");
-        return -ENOMEM;
-    }
-
-    memset(vcpu, 0, sizeof(struct hax_vcpu_state));
+    vcpu = g_malloc0(sizeof(struct hax_vcpu_state));
 
     ret = hax_host_create_vcpu(hax_global.vm->fd, id);
     if (ret) {
@@ -250,11 +244,8 @@ struct hax_vm *hax_vm_create(struct hax_state *hax)
         return hax->vm;
     }
 
-    vm = g_malloc(sizeof(struct hax_vm));
-    if (!vm) {
-        return NULL;
-    }
-    memset(vm, 0, sizeof(struct hax_vm));
+    vm = g_malloc0(sizeof(struct hax_vm));
+
     ret = hax_host_create_vm(hax, &vm_id);
     if (ret) {
         fprintf(stderr, "Failed to create vm %x\n", ret);
Signature · 1 line
2.11.0
Reviewed-by on v1 1/1
#2 lore raw
In reply to Li Qiang
Quoted 6 lines · Li Qiang

And also the g_malloc doesn't need check return value, remove it.

Quoted 27 lines
---
 target/i386/hax-all.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
target/i386/hax-all.c +1 -7
index d2e512856b..a460c605af 100644
@@ -154,13 +154,7 @@ int hax_vcpu_create(int id)
         return 0;
     }
 
-    vcpu = g_malloc(sizeof(struct hax_vcpu_state));
-    if (!vcpu) {
-        fprintf(stderr, "Failed to alloc vcpu state\n");
-        return -ENOMEM;
-    }
-
-    memset(vcpu, 0, sizeof(struct hax_vcpu_state));
+    vcpu = g_malloc0(sizeof(struct hax_vcpu_state));
 
     ret = hax_host_create_vcpu(hax_global.vm->fd, id);
     if (ret) {
@@ -250,11 +244,8 @@ struct hax_vm *hax_vm_create(struct hax_state *hax)
         return hax->vm;
     }
 

target/i386/hax-all.c struct hax_vm *hax_vm_create(struct hax_state *hax) · line 251

Quoted 10 lines
target/i386/hax-all.c +2 -5
-    vm = g_malloc(sizeof(struct hax_vm));
-    if (!vm) {
-        return NULL;
-    }
-    memset(vm, 0, sizeof(struct hax_vm));
+    vm = g_malloc0(sizeof(struct hax_vm));
+
     ret = hax_host_create_vm(hax, &vm_id);
     if (ret) {
         fprintf(stderr, "Failed to create vm %x\n", ret);

--
Alex Bennée

Reviewed-by on v1 1/1
#3 lore raw
In reply to Li Qiang
Quoted 16 lines · Li Qiang

And also the g_malloc doesn't need check return value, remove it.

---
  target/i386/hax-all.c | 15 +++------------
  1 file changed, 3 insertions(+), 12 deletions(-)
target/i386/hax-all.c +0 -0
index d2e512856b..a460c605af 100644
@@ -154,13 +154,7 @@ int hax_vcpu_create(int id)
          return 0;

target/i386/hax-all.c int hax_vcpu_create(int id) · line 157

Quoted 10 lines
target/i386/hax-all.c +1 -7
      }
 
-    vcpu = g_malloc(sizeof(struct hax_vcpu_state));
-    if (!vcpu) {
-        fprintf(stderr, "Failed to alloc vcpu state\n");
-        return -ENOMEM;
-    }
-
-    memset(vcpu, 0, sizeof(struct hax_vcpu_state));
+    vcpu = g_malloc0(sizeof(struct hax_vcpu_state));

Can you use the g_new form?

       vcpu = g_new0(struct hax_vcpu_state, 1);

target/i386/hax-all.c struct hax_vm *hax_vm_create(struct hax_state *hax) · line 247

Quoted 13 lines
target/i386/hax-all.c +1 -5
      ret = hax_host_create_vcpu(hax_global.vm->fd, id);
      if (ret) {
@@ -250,11 +244,8 @@ struct hax_vm *hax_vm_create(struct hax_state *hax)
          return hax->vm;
      }
 
-    vm = g_malloc(sizeof(struct hax_vm));
-    if (!vm) {
-        return NULL;
-    }
-    memset(vm, 0, sizeof(struct hax_vm));
+    vm = g_malloc0(sizeof(struct hax_vm));

Ditto.

target/i386/hax-all.c struct hax_vm *hax_vm_create(struct hax_state *hax) · line 251

Quoted 5 lines
target/i386/hax-all.c +1 -0
+
      ret = hax_host_create_vm(hax, &vm_id);
      if (ret) {
          fprintf(stderr, "Failed to create vm %x\n", ret);

This patch as it or using g_new0:

#4 lore raw
In reply to Li Qiang
Quoted 34 lines · Li Qiang

And also the g_malloc doesn't need check return value, remove it.

---
 target/i386/hax-all.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
target/i386/hax-all.c +1 -7
index d2e512856b..a460c605af 100644
@@ -154,13 +154,7 @@ int hax_vcpu_create(int id)
         return 0;
     }
 
-    vcpu = g_malloc(sizeof(struct hax_vcpu_state));
-    if (!vcpu) {
-        fprintf(stderr, "Failed to alloc vcpu state\n");
-        return -ENOMEM;
-    }
-
-    memset(vcpu, 0, sizeof(struct hax_vcpu_state));
+    vcpu = g_malloc0(sizeof(struct hax_vcpu_state));
 
     ret = hax_host_create_vcpu(hax_global.vm->fd, id);
     if (ret) {
@@ -250,11 +244,8 @@ struct hax_vm *hax_vm_create(struct hax_state *hax)
         return hax->vm;
     }
 

target/i386/hax-all.c struct hax_vm *hax_vm_create(struct hax_state *hax) · line 251

Quoted 10 lines
target/i386/hax-all.c +2 -5
-    vm = g_malloc(sizeof(struct hax_vm));
-    if (!vm) {
-        return NULL;
-    }
-    memset(vm, 0, sizeof(struct hax_vm));
+    vm = g_malloc0(sizeof(struct hax_vm));
+
     ret = hax_host_create_vm(hax, &vm_id);
     if (ret) {
         fprintf(stderr, "Failed to create vm %x\n", ret);

Queued, thanks.

Paolo

#5 lore raw
Quoted 34 lines · Paolo Bonzini
Quoted 33 lines · Li Qiang

And also the g_malloc doesn't need check return value, remove it.

---
 target/i386/hax-all.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
target/i386/hax-all.c +1 -7
index d2e512856b..a460c605af 100644
@@ -154,13 +154,7 @@ int hax_vcpu_create(int id)
         return 0;
     }
 
-    vcpu = g_malloc(sizeof(struct hax_vcpu_state));
-    if (!vcpu) {
-        fprintf(stderr, "Failed to alloc vcpu state\n");
-        return -ENOMEM;
-    }
-
-    memset(vcpu, 0, sizeof(struct hax_vcpu_state));
+    vcpu = g_malloc0(sizeof(struct hax_vcpu_state));
 
     ret = hax_host_create_vcpu(hax_global.vm->fd, id);
     if (ret) {
@@ -250,11 +244,8 @@ struct hax_vm *hax_vm_create(struct hax_state *hax)
         return hax->vm;
     }
 
Quoted 13 lines

target/i386/hax-all.c struct hax_vm *hax_vm_create(struct hax_state *hax) · line 251

Quoted 11 lines
target/i386/hax-all.c +2 -5
-    vm = g_malloc(sizeof(struct hax_vm));
-    if (!vm) {
-        return NULL;
-    }
-    memset(vm, 0, sizeof(struct hax_vm));
+    vm = g_malloc0(sizeof(struct hax_vm));
+
     ret = hax_host_create_vm(hax, &vm_id);
     if (ret) {
         fprintf(stderr, "Failed to create vm %x\n", ret);

Queued, thanks.

Markus and Philippe have asked to use g_new0() rather than g_malloc0().

Thanks,
Laurent

#6 lore raw
Quoted 49 lines · Laurent Vivier
Quoted 34 lines · Paolo Bonzini
Quoted 33 lines · Li Qiang

And also the g_malloc doesn't need check return value, remove it.

---
 target/i386/hax-all.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
target/i386/hax-all.c +1 -7
index d2e512856b..a460c605af 100644
@@ -154,13 +154,7 @@ int hax_vcpu_create(int id)
         return 0;
     }
 
-    vcpu = g_malloc(sizeof(struct hax_vcpu_state));
-    if (!vcpu) {
-        fprintf(stderr, "Failed to alloc vcpu state\n");
-        return -ENOMEM;
-    }
-
-    memset(vcpu, 0, sizeof(struct hax_vcpu_state));
+    vcpu = g_malloc0(sizeof(struct hax_vcpu_state));
 
     ret = hax_host_create_vcpu(hax_global.vm->fd, id);
     if (ret) {
@@ -250,11 +244,8 @@ struct hax_vm *hax_vm_create(struct hax_state *hax)
         return hax->vm;
     }
 
Quoted 13 lines

target/i386/hax-all.c struct hax_vm *hax_vm_create(struct hax_state *hax) · line 251

Quoted 11 lines
target/i386/hax-all.c +2 -5
-    vm = g_malloc(sizeof(struct hax_vm));
-    if (!vm) {
-        return NULL;
-    }
-    memset(vm, 0, sizeof(struct hax_vm));
+    vm = g_malloc0(sizeof(struct hax_vm));
+
     ret = hax_host_create_vm(hax, &vm_id);
     if (ret) {
         fprintf(stderr, "Failed to create vm %x\n", ret);

Queued, thanks.

Quoted 1 line

Markus and Philippe have asked to use g_new0() rather than g_malloc0().

Sorry, not sure why I didn't notice the replies. Also, it's sent to qemu-trivial so I'll ignore v2...

Paolo