All of lore.kernel.org
 help / color / mirror / Atom feed
From: Glauber Costa <glommer@redhat.com>
To: kvm@vger.kernel.org
Cc: avi@redhat.com
Subject: [PATCH 4/8] replace malloc with qemu_malloc
Date: Wed,  8 Jul 2009 09:08:58 -0400	[thread overview]
Message-ID: <1247058542-31211-5-git-send-email-glommer@redhat.com> (raw)
In-Reply-To: <1247058542-31211-4-git-send-email-glommer@redhat.com>

This patch replaces both malloc and malloc+memset sequences
with qemu_malloc and qemu_mallocz. Target is upstream integration

Signed-off-by: Glauber Costa <glommer@redhat.com>
---
 kvm-all.c         |   26 +++++---------------------
 target-i386/kvm.c |   31 ++++++++-----------------------
 2 files changed, 13 insertions(+), 44 deletions(-)

diff --git a/kvm-all.c b/kvm-all.c
index fd68f4c..034ae52 100644
--- a/kvm-all.c
+++ b/kvm-all.c
@@ -1469,10 +1469,7 @@ kvm_context_t kvm_init(void *opaque)
 	}
 	kvm_abi = r;
 	kvm_page_size = getpagesize();
-	kvm = malloc(sizeof(*kvm));
-	if (kvm == NULL)
-		goto out_close;
-	memset(kvm, 0, sizeof(*kvm));
+	kvm = qemu_mallocz(sizeof(*kvm));
 	kvm->fd = fd;
 	kvm->vm_fd = -1;
 	kvm->opaque = opaque;
@@ -1486,10 +1483,7 @@ kvm_context_t kvm_init(void *opaque)
 
 		/* Round up so we can search ints using ffs */
 		gsi_bits = ALIGN(gsi_count, 32);
-		kvm->used_gsi_bitmap = malloc(gsi_bits / 8);
-		if (!kvm->used_gsi_bitmap)
-			goto out_close;
-		memset(kvm->used_gsi_bitmap, 0, gsi_bits / 8);
+		kvm->used_gsi_bitmap = qemu_mallocz(gsi_bits / 8);
 		kvm->max_gsi = gsi_bits;
 
 		/* Mark any over-allocated bits as already in use */
@@ -1529,12 +1523,7 @@ kvm_vcpu_context_t kvm_create_vcpu(kvm_context_t kvm, int id)
 {
 	long mmap_size;
 	int r;
-	kvm_vcpu_context_t vcpu_ctx = malloc(sizeof(struct kvm_vcpu_context));
-
-	if (!vcpu_ctx) {
-		errno = ENOMEM;
-		return NULL;
-	}
+	kvm_vcpu_context_t vcpu_ctx = qemu_malloc(sizeof(struct kvm_vcpu_context));
 
 	vcpu_ctx->kvm = kvm;
 	vcpu_ctx->id = id;
@@ -1581,10 +1570,7 @@ int kvm_create_vm(kvm_context_t kvm)
 	int fd = kvm->fd;
 
 #ifdef KVM_CAP_IRQ_ROUTING
-	kvm->irq_routes = malloc(sizeof(*kvm->irq_routes));
-	if (!kvm->irq_routes)
-		return -ENOMEM;
-	memset(kvm->irq_routes, 0, sizeof(*kvm->irq_routes));
+	kvm->irq_routes = qemu_mallocz(sizeof(*kvm->irq_routes));
 	kvm->nr_allocated_irq_routes = 0;
 #endif
 
@@ -2189,9 +2175,7 @@ int kvm_set_signal_mask(kvm_vcpu_context_t vcpu, const sigset_t *sigset)
 			r = -errno;
 		return r;
 	}
-	sigmask = malloc(sizeof(*sigmask) + sizeof(*sigset));
-	if (!sigmask)
-		return -ENOMEM;
+	sigmask = qemu_malloc(sizeof(*sigmask) + sizeof(*sigset));
 
 	sigmask->len = 8;
 	memcpy(sigmask->sigset, sigset, sizeof(*sigset));
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
index 4c3948c..ab324f6 100644
--- a/target-i386/kvm.c
+++ b/target-i386/kvm.c
@@ -1303,12 +1303,9 @@ struct kvm_msr_list *kvm_get_msr_list(kvm_context_t kvm)
 		return NULL;
 	/* Old kernel modules had a bug and could write beyond the provided
 	   memory. Allocate at least a safe amount of 1K. */
-	msrs = malloc(MAX(1024, sizeof(*msrs) +
-				sizer.nmsrs * sizeof(*msrs->indices)));
-	if (!msrs) {
-		errno = ENOMEM;
-		return NULL;
-	}
+	msrs = qemu_malloc(MAX(1024, sizeof(*msrs) +
+				       sizer.nmsrs * sizeof(*msrs->indices)));
+
 	msrs->nmsrs = sizer.nmsrs;
 	r = ioctl(kvm->fd, KVM_GET_MSR_INDEX_LIST, msrs);
 	if (r == -1) {
@@ -1322,13 +1319,9 @@ struct kvm_msr_list *kvm_get_msr_list(kvm_context_t kvm)
 
 int kvm_get_msrs(kvm_vcpu_context_t vcpu, struct kvm_msr_entry *msrs, int n)
 {
-    struct kvm_msrs *kmsrs = malloc(sizeof *kmsrs + n * sizeof *msrs);
+    struct kvm_msrs *kmsrs = qemu_malloc(sizeof *kmsrs + n * sizeof *msrs);
     int r, e;
 
-    if (!kmsrs) {
-	errno = ENOMEM;
-	return -1;
-    }
     kmsrs->nmsrs = n;
     memcpy(kmsrs->entries, msrs, n * sizeof *msrs);
     r = ioctl(vcpu->fd, KVM_GET_MSRS, kmsrs);
@@ -1341,13 +1334,9 @@ int kvm_get_msrs(kvm_vcpu_context_t vcpu, struct kvm_msr_entry *msrs, int n)
 
 int kvm_set_msrs(kvm_vcpu_context_t vcpu, struct kvm_msr_entry *msrs, int n)
 {
-    struct kvm_msrs *kmsrs = malloc(sizeof *kmsrs + n * sizeof *msrs);
+    struct kvm_msrs *kmsrs = qemu_malloc(sizeof *kmsrs + n * sizeof *msrs);
     int r, e;
 
-    if (!kmsrs) {
-	errno = ENOMEM;
-	return -1;
-    }
     kmsrs->nmsrs = n;
     memcpy(kmsrs->entries, msrs, n * sizeof *msrs);
     r = ioctl(vcpu->fd, KVM_SET_MSRS, kmsrs);
@@ -1437,9 +1426,7 @@ int kvm_setup_cpuid(kvm_vcpu_context_t vcpu, int nent,
 	struct kvm_cpuid *cpuid;
 	int r;
 
-	cpuid = malloc(sizeof(*cpuid) + nent * sizeof(*entries));
-	if (!cpuid)
-		return -ENOMEM;
+	cpuid = qemu_malloc(sizeof(*cpuid) + nent * sizeof(*entries));
 
 	cpuid->nent = nent;
 	memcpy(cpuid->entries, entries, nent * sizeof(*entries));
@@ -1455,9 +1442,7 @@ int kvm_setup_cpuid2(kvm_vcpu_context_t vcpu, int nent,
 	struct kvm_cpuid2 *cpuid;
 	int r;
 
-	cpuid = malloc(sizeof(*cpuid) + nent * sizeof(*entries));
-	if (!cpuid)
-		return -ENOMEM;
+	cpuid = qemu_malloc(sizeof(*cpuid) + nent * sizeof(*entries));
 
 	cpuid->nent = nent;
 	memcpy(cpuid->entries, entries, nent * sizeof(*entries));
@@ -1545,7 +1530,7 @@ static struct kvm_cpuid2 *try_get_cpuid(kvm_context_t kvm, int max)
 	int r, size;
 
 	size = sizeof(*cpuid) + max * sizeof(*cpuid->entries);
-	cpuid = (struct kvm_cpuid2 *)malloc(size);
+	cpuid = qemu_malloc(size);
 	cpuid->nent = max;
 	r = ioctl(kvm->fd, KVM_GET_SUPPORTED_CPUID, cpuid);
 	if (r == -1)
-- 
1.6.2.2


  reply	other threads:[~2009-07-08 13:09 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-08 13:08 [PATCH 0/8] Move closer to upstream Glauber Costa
2009-07-08 13:08 ` [PATCH 1/8] Provide KVMState definition outside CONFIG_KVM Glauber Costa
2009-07-08 13:22   ` Avi Kivity
     [not found]   ` <1247058542-31211-3-git-send-email-glommer@redhat.com>
2009-07-08 13:08     ` [PATCH 3/8] put qemu-kvm-x86.c into kvm.c Glauber Costa
2009-07-08 13:08       ` Glauber Costa [this message]
2009-07-08 13:08         ` [PATCH 5/8] move kvm_context inside KVMState Glauber Costa
2009-07-08 13:09           ` [PATCH 6/8] provide env->kvm_fd Glauber Costa
2009-07-08 13:09             ` [PATCH 7/8] use kvm_upstream sw_breakpoints structure Glauber Costa
2009-07-08 13:09               ` [PATCH 8/8] use upstream code for breakpoint handling Glauber Costa
2009-07-08 13:27               ` [PATCH 7/8] use kvm_upstream sw_breakpoints structure Avi Kivity
2009-07-08 13:39                 ` Glauber Costa
2009-07-08 13:44                   ` Avi Kivity
2009-07-08 15:23                     ` Jan Kiszka
2009-07-08 19:44                       ` Jan Kiszka
2009-07-08 19:55                         ` Avi Kivity
2009-07-08 13:32             ` [PATCH 6/8] provide env->kvm_fd Gleb Natapov
2009-07-08 13:44               ` Glauber Costa
2009-07-08 13:38                 ` Gleb Natapov
2009-07-08 13:23       ` [PATCH 3/8] put qemu-kvm-x86.c into kvm.c Avi Kivity
2009-07-08 13:28       ` Gleb Natapov
2009-07-08 13:41         ` Glauber Costa
2009-07-08 13:23     ` [PATCH 2/8] move qemu-kvm.c to kvm-all.c Avi Kivity
2009-07-08 13:52       ` Glauber Costa
2009-07-08 13:27 ` [PATCH 0/8] Move closer to upstream Avi Kivity

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1247058542-31211-5-git-send-email-glommer@redhat.com \
    --to=glommer@redhat.com \
    --cc=avi@redhat.com \
    --cc=kvm@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.