Skip to content

Commit

Permalink
Use g_new() & friends where that makes obvious sense
Browse files Browse the repository at this point in the history
g_new(T, n) is neater than g_malloc(sizeof(T) * n).  It's also safer,
for two reasons.  One, it catches multiplication overflowing size_t.
Two, it returns T * rather than void *, which lets the compiler catch
more type errors.

This commit only touches allocations with size arguments of the form
sizeof(T).

Patch created mechanically with:

    $ spatch --in-place --sp-file scripts/coccinelle/use-g_new-etc.cocci \
	     --macro-file scripts/cocci-macro-file.h FILES...

Signed-off-by: Markus Armbruster <[email protected]>
Reviewed-by: Philippe Mathieu-Daudé <[email protected]>
Reviewed-by: Cédric Le Goater <[email protected]>
Reviewed-by: Alex Bennée <[email protected]>
Acked-by: Dr. David Alan Gilbert <[email protected]>
Message-Id: <[email protected]>
Reviewed-by: Pavel Dovgalyuk <[email protected]>
  • Loading branch information
Markus Armbruster committed Mar 21, 2022
1 parent 1366244 commit b21e238
Show file tree
Hide file tree
Showing 102 changed files with 195 additions and 200 deletions.
6 changes: 3 additions & 3 deletions accel/kvm/kvm-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -1646,7 +1646,7 @@ void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml,
{
int i;

kml->slots = g_malloc0(s->nr_slots * sizeof(KVMSlot));
kml->slots = g_new0(KVMSlot, s->nr_slots);
kml->as_id = as_id;

for (i = 0; i < s->nr_slots; i++) {
Expand Down Expand Up @@ -1941,7 +1941,7 @@ int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
return virq;
}

route = g_malloc0(sizeof(KVMMSIRoute));
route = g_new0(KVMMSIRoute, 1);
route->kroute.gsi = virq;
route->kroute.type = KVM_IRQ_ROUTING_MSI;
route->kroute.flags = 0;
Expand Down Expand Up @@ -3244,7 +3244,7 @@ int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr,
return 0;
}

bp = g_malloc(sizeof(struct kvm_sw_breakpoint));
bp = g_new(struct kvm_sw_breakpoint, 1);
bp->pc = addr;
bp->use_count = 1;
err = kvm_arch_insert_sw_breakpoint(cpu, bp);
Expand Down
2 changes: 1 addition & 1 deletion accel/tcg/tcg-accel-ops-mttcg.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void mttcg_start_vcpu_thread(CPUState *cpu)
g_assert(tcg_enabled());
tcg_cpu_init_cflags(cpu, current_machine->smp.max_cpus > 1);

cpu->thread = g_malloc0(sizeof(QemuThread));
cpu->thread = g_new0(QemuThread, 1);
cpu->halt_cond = g_malloc0(sizeof(QemuCond));
qemu_cond_init(cpu->halt_cond);

Expand Down
4 changes: 2 additions & 2 deletions accel/tcg/tcg-accel-ops-rr.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ void rr_start_vcpu_thread(CPUState *cpu)
tcg_cpu_init_cflags(cpu, false);

if (!single_tcg_cpu_thread) {
cpu->thread = g_malloc0(sizeof(QemuThread));
cpu->halt_cond = g_malloc0(sizeof(QemuCond));
cpu->thread = g_new0(QemuThread, 1);
cpu->halt_cond = g_new0(QemuCond, 1);
qemu_cond_init(cpu->halt_cond);

/* share a single thread for all cpus with TCG */
Expand Down
4 changes: 2 additions & 2 deletions audio/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1733,7 +1733,7 @@ static AudioState *audio_init(Audiodev *dev, const char *name)
audio_validate_opts(dev, &error_abort);
}

s = g_malloc0(sizeof(AudioState));
s = g_new0(AudioState, 1);
s->dev = dev;

QLIST_INIT (&s->hw_head_out);
Expand Down Expand Up @@ -2108,7 +2108,7 @@ void audio_parse_option(const char *opt)

audio_validate_opts(dev, &error_fatal);

e = g_malloc0(sizeof(AudiodevListEntry));
e = g_new0(AudiodevListEntry, 1);
e->dev = dev;
QSIMPLEQ_INSERT_TAIL(&audiodevs, e, next);
}
Expand Down
6 changes: 3 additions & 3 deletions audio/audio_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ static void handle_per_direction(

static AudiodevListEntry *legacy_opt(const char *drvname)
{
AudiodevListEntry *e = g_malloc0(sizeof(AudiodevListEntry));
e->dev = g_malloc0(sizeof(Audiodev));
AudiodevListEntry *e = g_new0(AudiodevListEntry, 1);
e->dev = g_new0(Audiodev, 1);
e->dev->id = g_strdup(drvname);
e->dev->driver = qapi_enum_parse(
&AudiodevDriver_lookup, drvname, -1, &error_abort);
Expand Down Expand Up @@ -508,7 +508,7 @@ static void lv_free(Visitor *v)

static Visitor *legacy_visitor_new(void)
{
LegacyPrintVisitor *lv = g_malloc0(sizeof(LegacyPrintVisitor));
LegacyPrintVisitor *lv = g_new0(LegacyPrintVisitor, 1);

lv->visitor.start_struct = lv_start_struct;
lv->visitor.end_struct = lv_end_struct;
Expand Down
2 changes: 1 addition & 1 deletion audio/dsoundaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ static void *dsound_audio_init(Audiodev *dev)
{
int err;
HRESULT hr;
dsound *s = g_malloc0(sizeof(dsound));
dsound *s = g_new0(dsound, 1);
AudiodevDsoundOptions *dso;

assert(dev->driver == AUDIODEV_DRIVER_DSOUND);
Expand Down
6 changes: 3 additions & 3 deletions audio/jackaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ static void qjack_buffer_create(QJackBuffer *buffer, int channels, int frames)
buffer->used = 0;
buffer->rptr = 0;
buffer->wptr = 0;
buffer->data = g_malloc(channels * sizeof(float *));
buffer->data = g_new(float *, channels);
for (int i = 0; i < channels; ++i) {
buffer->data[i] = g_malloc(frames * sizeof(float));
buffer->data[i] = g_new(float, frames);
}
}

Expand Down Expand Up @@ -453,7 +453,7 @@ static int qjack_client_init(QJackClient *c)
jack_on_shutdown(c->client, qjack_shutdown, c);

/* allocate and register the ports */
c->port = g_malloc(sizeof(jack_port_t *) * c->nchannels);
c->port = g_new(jack_port_t *, c->nchannels);
for (int i = 0; i < c->nchannels; ++i) {

char port_name[16];
Expand Down
4 changes: 2 additions & 2 deletions audio/paaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ static int qpa_validate_per_direction_opts(Audiodev *dev,
/* common */
static void *qpa_conn_init(const char *server)
{
PAConnection *c = g_malloc0(sizeof(PAConnection));
PAConnection *c = g_new0(PAConnection, 1);
QTAILQ_INSERT_TAIL(&pa_conns, c, list);

c->mainloop = pa_threaded_mainloop_new();
Expand Down Expand Up @@ -849,7 +849,7 @@ static void *qpa_audio_init(Audiodev *dev)
return NULL;
}

g = g_malloc0(sizeof(paaudio));
g = g_new0(paaudio, 1);
server = popts->has_server ? popts->server : NULL;

g->dev = dev;
Expand Down
2 changes: 1 addition & 1 deletion backends/cryptodev.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ cryptodev_backend_new_client(const char *model,
{
CryptoDevBackendClient *cc;

cc = g_malloc0(sizeof(CryptoDevBackendClient));
cc = g_new0(CryptoDevBackendClient, 1);
cc->model = g_strdup(model);
if (name) {
cc->name = g_strdup(name);
Expand Down
2 changes: 1 addition & 1 deletion contrib/vhost-user-gpu/vhost-user-gpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ vg_create_mapping_iov(VuGpu *g,
return -1;
}

*iov = g_malloc0(sizeof(struct iovec) * ab->nr_entries);
*iov = g_new0(struct iovec, ab->nr_entries);
for (i = 0; i < ab->nr_entries; i++) {
uint64_t len = ents[i].length;
(*iov)[i].iov_len = ents[i].length;
Expand Down
4 changes: 2 additions & 2 deletions cpus-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
{
struct qemu_work_item *wi;

wi = g_malloc0(sizeof(struct qemu_work_item));
wi = g_new0(struct qemu_work_item, 1);
wi->func = func;
wi->data = data;
wi->free = true;
Expand Down Expand Up @@ -305,7 +305,7 @@ void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func,
{
struct qemu_work_item *wi;

wi = g_malloc0(sizeof(struct qemu_work_item));
wi = g_new0(struct qemu_work_item, 1);
wi->func = func;
wi->data = data;
wi->free = true;
Expand Down
2 changes: 1 addition & 1 deletion dump/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -2041,7 +2041,7 @@ void qmp_dump_guest_memory(bool paging, const char *file,
DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
{
DumpGuestMemoryCapability *cap =
g_malloc0(sizeof(DumpGuestMemoryCapability));
g_new0(DumpGuestMemoryCapability, 1);
DumpGuestMemoryFormatList **tail = &cap->formats;

/* elf is always available */
Expand Down
2 changes: 1 addition & 1 deletion hw/acpi/hmat.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ static void build_hmat_lb(GArray *table_data, HMAT_LB_Info *hmat_lb,
}

/* Latency or Bandwidth Entries */
entry_list = g_malloc0(num_initiator * num_target * sizeof(uint16_t));
entry_list = g_new0(uint16_t, num_initiator * num_target);
for (i = 0; i < hmat_lb->list->len; i++) {
lb_data = &g_array_index(hmat_lb->list, HMAT_LB_Data, i);
index = lb_data->initiator * num_target + lb_data->target;
Expand Down
2 changes: 1 addition & 1 deletion hw/audio/intel-hda.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ static void intel_hda_parse_bdl(IntelHDAState *d, IntelHDAStream *st)
addr = intel_hda_addr(st->bdlp_lbase, st->bdlp_ubase);
st->bentries = st->lvi +1;
g_free(st->bpl);
st->bpl = g_malloc(sizeof(bpl) * st->bentries);
st->bpl = g_new(bpl, st->bentries);
for (i = 0; i < st->bentries; i++, addr += 16) {
pci_dma_read(&d->pci, addr, buf, 16);
st->bpl[i].addr = le64_to_cpu(*(uint64_t *)buf);
Expand Down
2 changes: 1 addition & 1 deletion hw/char/parallel.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ bool parallel_mm_init(MemoryRegion *address_space,
{
ParallelState *s;

s = g_malloc0(sizeof(ParallelState));
s = g_new0(ParallelState, 1);
s->irq = irq;
qemu_chr_fe_init(&s->chr, chr, &error_abort);
s->it_shift = it_shift;
Expand Down
2 changes: 1 addition & 1 deletion hw/char/riscv_htif.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ HTIFState *htif_mm_init(MemoryRegion *address_space, MemoryRegion *main_mem,
tohost_offset = tohost_addr - base;
fromhost_offset = fromhost_addr - base;

HTIFState *s = g_malloc0(sizeof(HTIFState));
HTIFState *s = g_new0(HTIFState, 1);
s->address_space = address_space;
s->main_mem = main_mem;
s->main_mem_ram_ptr = memory_region_get_ram_ptr(main_mem);
Expand Down
6 changes: 2 additions & 4 deletions hw/char/virtio-serial-bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -1055,10 +1055,8 @@ static void virtio_serial_device_realize(DeviceState *dev, Error **errp)
QTAILQ_INIT(&vser->ports);

vser->bus.max_nr_ports = vser->serial.max_virtserial_ports;
vser->ivqs = g_malloc(vser->serial.max_virtserial_ports
* sizeof(VirtQueue *));
vser->ovqs = g_malloc(vser->serial.max_virtserial_ports
* sizeof(VirtQueue *));
vser->ivqs = g_new(VirtQueue *, vser->serial.max_virtserial_ports);
vser->ovqs = g_new(VirtQueue *, vser->serial.max_virtserial_ports);

/* Add a queue for host to guest transfers for port 0 (backward compat) */
vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
Expand Down
2 changes: 1 addition & 1 deletion hw/core/irq.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static void qemu_splitirq(void *opaque, int line, int level)

qemu_irq qemu_irq_split(qemu_irq irq1, qemu_irq irq2)
{
qemu_irq *s = g_malloc0(2 * sizeof(qemu_irq));
qemu_irq *s = g_new0(qemu_irq, 2);
s[0] = irq1;
s[1] = irq2;
return qemu_allocate_irq(qemu_splitirq, s, 0);
Expand Down
2 changes: 1 addition & 1 deletion hw/core/reset.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static QTAILQ_HEAD(, QEMUResetEntry) reset_handlers =

void qemu_register_reset(QEMUResetHandler *func, void *opaque)
{
QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
QEMUResetEntry *re = g_new0(QEMUResetEntry, 1);

re->func = func;
re->opaque = opaque;
Expand Down
2 changes: 1 addition & 1 deletion hw/display/pxa2xx_lcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ PXA2xxLCDState *pxa2xx_lcdc_init(MemoryRegion *sysmem,
{
PXA2xxLCDState *s;

s = (PXA2xxLCDState *) g_malloc0(sizeof(PXA2xxLCDState));
s = g_new0(PXA2xxLCDState, 1);
s->invalidated = 1;
s->irq = irq;
s->sysmem = sysmem;
Expand Down
2 changes: 1 addition & 1 deletion hw/display/tc6393xb.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ TC6393xbState *tc6393xb_init(MemoryRegion *sysmem, uint32_t base, qemu_irq irq)
},
};

s = (TC6393xbState *) g_malloc0(sizeof(TC6393xbState));
s = g_new0(TC6393xbState, 1);
s->irq = irq;
s->gpio_in = qemu_allocate_irqs(tc6393xb_gpio_set, s, TC6393XB_GPIOS);

Expand Down
4 changes: 2 additions & 2 deletions hw/display/virtio-gpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -831,9 +831,9 @@ int virtio_gpu_create_mapping_iov(VirtIOGPU *g,
}

if (!(v % 16)) {
*iov = g_realloc(*iov, sizeof(struct iovec) * (v + 16));
*iov = g_renew(struct iovec, *iov, v + 16);
if (addr) {
*addr = g_realloc(*addr, sizeof(uint64_t) * (v + 16));
*addr = g_renew(uint64_t, *addr, v + 16);
}
}
(*iov)[v].iov_base = map;
Expand Down
4 changes: 2 additions & 2 deletions hw/display/xenfb.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,8 @@ static int xenfb_map_fb(struct XenFB *xenfb)
n_fbdirs = xenfb->fbpages * mode / 8;
n_fbdirs = DIV_ROUND_UP(n_fbdirs, XC_PAGE_SIZE);

pgmfns = g_malloc0(sizeof(xen_pfn_t) * n_fbdirs);
fbmfns = g_malloc0(sizeof(xen_pfn_t) * xenfb->fbpages);
pgmfns = g_new0(xen_pfn_t, n_fbdirs);
fbmfns = g_new0(xen_pfn_t, xenfb->fbpages);

xenfb_copy_mfns(mode, n_fbdirs, pgmfns, pd);
map = xenforeignmemory_map(xen_fmem, xenfb->c.xendev.dom,
Expand Down
4 changes: 2 additions & 2 deletions hw/dma/rc4030.c
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,8 @@ static rc4030_dma *rc4030_allocate_dmas(void *opaque, int n)
struct rc4030DMAState *p;
int i;

s = (rc4030_dma *)g_new0(rc4030_dma, n);
p = (struct rc4030DMAState *)g_new0(struct rc4030DMAState, n);
s = g_new0(rc4030_dma, n);
p = g_new0(struct rc4030DMAState, n);
for (i = 0; i < n; i++) {
p->opaque = opaque;
p->n = i;
Expand Down
4 changes: 2 additions & 2 deletions hw/i2c/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ static int i2c_slave_post_load(void *opaque, int version_id)
bus = I2C_BUS(qdev_get_parent_bus(DEVICE(dev)));
if ((bus->saved_address == dev->address) ||
(bus->saved_address == I2C_BROADCAST)) {
node = g_malloc(sizeof(struct I2CNode));
node = g_new(struct I2CNode, 1);
node->elt = dev;
QLIST_INSERT_HEAD(&bus->current_devs, node, next);
}
Expand Down Expand Up @@ -319,7 +319,7 @@ static bool i2c_slave_match(I2CSlave *candidate, uint8_t address,
bool broadcast, I2CNodeList *current_devs)
{
if ((candidate->address == address) || (broadcast)) {
I2CNode *node = g_malloc(sizeof(struct I2CNode));
I2CNode *node = g_new(struct I2CNode, 1);
node->elt = candidate;
QLIST_INSERT_HEAD(current_devs, node, next);
return true;
Expand Down
2 changes: 1 addition & 1 deletion hw/i2c/i2c_mux_pca954x.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static bool pca954x_match(I2CSlave *candidate, uint8_t address,

/* They are talking to the mux itself (or all devices enabled). */
if ((candidate->address == address) || broadcast) {
I2CNode *node = g_malloc(sizeof(struct I2CNode));
I2CNode *node = g_new(struct I2CNode, 1);
node->elt = candidate;
QLIST_INSERT_HEAD(current_devs, node, next);
if (!broadcast) {
Expand Down
4 changes: 2 additions & 2 deletions hw/i386/amd_iommu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1405,15 +1405,15 @@ static AddressSpace *amdvi_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)

/* allocate memory during the first run */
if (!iommu_as) {
iommu_as = g_malloc0(sizeof(AMDVIAddressSpace *) * PCI_DEVFN_MAX);
iommu_as = g_new0(AMDVIAddressSpace *, PCI_DEVFN_MAX);
s->address_spaces[bus_num] = iommu_as;
}

/* set up AMD-Vi region */
if (!iommu_as[devfn]) {
snprintf(name, sizeof(name), "amd_iommu_devfn_%d", devfn);

iommu_as[devfn] = g_malloc0(sizeof(AMDVIAddressSpace));
iommu_as[devfn] = g_new0(AMDVIAddressSpace, 1);
iommu_as[devfn]->bus_num = (uint8_t)bus_num;
iommu_as[devfn]->devfn = (uint8_t)devfn;
iommu_as[devfn]->iommu_state = s;
Expand Down
2 changes: 1 addition & 1 deletion hw/i386/intel_iommu.c
Original file line number Diff line number Diff line change
Expand Up @@ -3416,7 +3416,7 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)
if (!vtd_dev_as) {
snprintf(name, sizeof(name), "vtd-%02x.%x", PCI_SLOT(devfn),
PCI_FUNC(devfn));
vtd_bus->dev_as[devfn] = vtd_dev_as = g_malloc0(sizeof(VTDAddressSpace));
vtd_bus->dev_as[devfn] = vtd_dev_as = g_new0(VTDAddressSpace, 1);

vtd_dev_as->bus = bus;
vtd_dev_as->devfn = (uint8_t)devfn;
Expand Down
10 changes: 5 additions & 5 deletions hw/i386/xen/xen-hvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ static int xen_add_to_physmap(XenIOState *state,

mr_name = memory_region_name(mr);

physmap = g_malloc(sizeof(XenPhysmap));
physmap = g_new(XenPhysmap, 1);

physmap->start_addr = start_addr;
physmap->size = size;
Expand Down Expand Up @@ -1281,7 +1281,7 @@ static void xen_read_physmap(XenIOState *state)
return;

for (i = 0; i < num; i++) {
physmap = g_malloc(sizeof (XenPhysmap));
physmap = g_new(XenPhysmap, 1);
physmap->phys_offset = strtoull(entries[i], NULL, 16);
snprintf(path, sizeof(path),
"/local/domain/0/device-model/%d/physmap/%s/start_addr",
Expand Down Expand Up @@ -1410,7 +1410,7 @@ void xen_hvm_init_pc(PCMachineState *pcms, MemoryRegion **ram_memory)
xen_pfn_t ioreq_pfn;
XenIOState *state;

state = g_malloc0(sizeof (XenIOState));
state = g_new0(XenIOState, 1);

state->xce_handle = xenevtchn_open(NULL, 0);
if (state->xce_handle == NULL) {
Expand Down Expand Up @@ -1463,7 +1463,7 @@ void xen_hvm_init_pc(PCMachineState *pcms, MemoryRegion **ram_memory)
}

/* Note: cpus is empty at this point in init */
state->cpu_by_vcpu_id = g_malloc0(max_cpus * sizeof(CPUState *));
state->cpu_by_vcpu_id = g_new0(CPUState *, max_cpus);

rc = xen_set_ioreq_server_state(xen_domid, state->ioservid, true);
if (rc < 0) {
Expand All @@ -1472,7 +1472,7 @@ void xen_hvm_init_pc(PCMachineState *pcms, MemoryRegion **ram_memory)
goto err;
}

state->ioreq_local_port = g_malloc0(max_cpus * sizeof (evtchn_port_t));
state->ioreq_local_port = g_new0(evtchn_port_t, max_cpus);

/* FIXME: how about if we overflow the page here? */
for (i = 0; i < max_cpus; i++) {
Expand Down
Loading

0 comments on commit b21e238

Please sign in to comment.