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@xxxxxxxxxx> --- 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 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html