* [PATCH] Use g_new() & friends where that makes obvious sense
@ 2022-09-23 8:42 Markus Armbruster
2022-09-23 10:41 ` Michael S. Tsirkin
2022-09-29 19:30 ` Laurent Vivier
0 siblings, 2 replies; 3+ messages in thread
From: Markus Armbruster @ 2022-09-23 8:42 UTC (permalink / raw)
To: qemu-devel
Cc: elena.ufimtseva, jag.raman, john.g.johnson, pizhenwei,
arei.gonglei, mst, huangy81, quintela, dgilbert, qemu-trivial
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...
The previous iteration was commit a95942b50c.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
hw/remote/iommu.c | 2 +-
hw/virtio/virtio-crypto.c | 2 +-
migration/dirtyrate.c | 4 ++--
softmmu/dirtylimit.c | 4 ++--
4 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/hw/remote/iommu.c b/hw/remote/iommu.c
index fd723d91f3..1391dd712c 100644
--- a/hw/remote/iommu.c
+++ b/hw/remote/iommu.c
@@ -47,7 +47,7 @@ static AddressSpace *remote_iommu_find_add_as(PCIBus *pci_bus,
elem = g_hash_table_lookup(iommu->elem_by_devfn, INT2VOIDP(devfn));
if (!elem) {
- elem = g_malloc0(sizeof(RemoteIommuElem));
+ elem = g_new0(RemoteIommuElem, 1);
g_hash_table_insert(iommu->elem_by_devfn, INT2VOIDP(devfn), elem);
}
diff --git a/hw/virtio/virtio-crypto.c b/hw/virtio/virtio-crypto.c
index c1243c3f93..df4bde210b 100644
--- a/hw/virtio/virtio-crypto.c
+++ b/hw/virtio/virtio-crypto.c
@@ -710,7 +710,7 @@ virtio_crypto_handle_asym_req(VirtIOCrypto *vcrypto,
uint8_t *src = NULL;
uint8_t *dst = NULL;
- asym_op_info = g_malloc0(sizeof(CryptoDevBackendAsymOpInfo));
+ asym_op_info = g_new0(CryptoDevBackendAsymOpInfo, 1);
src_len = ldl_le_p(&req->para.src_data_len);
dst_len = ldl_le_p(&req->para.dst_data_len);
diff --git a/migration/dirtyrate.c b/migration/dirtyrate.c
index 795fab5c37..d6f1e01a70 100644
--- a/migration/dirtyrate.c
+++ b/migration/dirtyrate.c
@@ -119,9 +119,9 @@ static DirtyPageRecord *vcpu_dirty_stat_alloc(VcpuStat *stat)
}
stat->nvcpu = nvcpu;
- stat->rates = g_malloc0(sizeof(DirtyRateVcpu) * nvcpu);
+ stat->rates = g_new0(DirtyRateVcpu, nvcpu);
- records = g_malloc0(sizeof(DirtyPageRecord) * nvcpu);
+ records = g_new0(DirtyPageRecord, nvcpu);
return records;
}
diff --git a/softmmu/dirtylimit.c b/softmmu/dirtylimit.c
index 8d98cb7f2c..12668555f2 100644
--- a/softmmu/dirtylimit.c
+++ b/softmmu/dirtylimit.c
@@ -154,7 +154,7 @@ void vcpu_dirty_rate_stat_initialize(void)
vcpu_dirty_rate_stat->stat.nvcpu = max_cpus;
vcpu_dirty_rate_stat->stat.rates =
- g_malloc0(sizeof(DirtyRateVcpu) * max_cpus);
+ g_new0(DirtyRateVcpu, max_cpus);
vcpu_dirty_rate_stat->running = false;
}
@@ -198,7 +198,7 @@ void dirtylimit_state_initialize(void)
dirtylimit_state = g_malloc0(sizeof(*dirtylimit_state));
dirtylimit_state->states =
- g_malloc0(sizeof(VcpuDirtyLimitState) * max_cpus);
+ g_new0(VcpuDirtyLimitState, max_cpus);
for (i = 0; i < max_cpus; i++) {
dirtylimit_state->states[i].cpu_index = i;
--
2.37.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] Use g_new() & friends where that makes obvious sense
2022-09-23 8:42 [PATCH] Use g_new() & friends where that makes obvious sense Markus Armbruster
@ 2022-09-23 10:41 ` Michael S. Tsirkin
2022-09-29 19:30 ` Laurent Vivier
1 sibling, 0 replies; 3+ messages in thread
From: Michael S. Tsirkin @ 2022-09-23 10:41 UTC (permalink / raw)
To: Markus Armbruster
Cc: qemu-devel, elena.ufimtseva, jag.raman, john.g.johnson, pizhenwei,
arei.gonglei, huangy81, quintela, dgilbert, qemu-trivial
On Fri, Sep 23, 2022 at 10:42:54AM +0200, Markus Armbruster wrote:
> 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...
>
> The previous iteration was commit a95942b50c.
>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
> ---
> hw/remote/iommu.c | 2 +-
> hw/virtio/virtio-crypto.c | 2 +-
> migration/dirtyrate.c | 4 ++--
> softmmu/dirtylimit.c | 4 ++--
> 4 files changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/hw/remote/iommu.c b/hw/remote/iommu.c
> index fd723d91f3..1391dd712c 100644
> --- a/hw/remote/iommu.c
> +++ b/hw/remote/iommu.c
> @@ -47,7 +47,7 @@ static AddressSpace *remote_iommu_find_add_as(PCIBus *pci_bus,
> elem = g_hash_table_lookup(iommu->elem_by_devfn, INT2VOIDP(devfn));
>
> if (!elem) {
> - elem = g_malloc0(sizeof(RemoteIommuElem));
> + elem = g_new0(RemoteIommuElem, 1);
> g_hash_table_insert(iommu->elem_by_devfn, INT2VOIDP(devfn), elem);
> }
>
> diff --git a/hw/virtio/virtio-crypto.c b/hw/virtio/virtio-crypto.c
> index c1243c3f93..df4bde210b 100644
> --- a/hw/virtio/virtio-crypto.c
> +++ b/hw/virtio/virtio-crypto.c
> @@ -710,7 +710,7 @@ virtio_crypto_handle_asym_req(VirtIOCrypto *vcrypto,
> uint8_t *src = NULL;
> uint8_t *dst = NULL;
>
> - asym_op_info = g_malloc0(sizeof(CryptoDevBackendAsymOpInfo));
> + asym_op_info = g_new0(CryptoDevBackendAsymOpInfo, 1);
> src_len = ldl_le_p(&req->para.src_data_len);
> dst_len = ldl_le_p(&req->para.dst_data_len);
>
> diff --git a/migration/dirtyrate.c b/migration/dirtyrate.c
> index 795fab5c37..d6f1e01a70 100644
> --- a/migration/dirtyrate.c
> +++ b/migration/dirtyrate.c
> @@ -119,9 +119,9 @@ static DirtyPageRecord *vcpu_dirty_stat_alloc(VcpuStat *stat)
> }
>
> stat->nvcpu = nvcpu;
> - stat->rates = g_malloc0(sizeof(DirtyRateVcpu) * nvcpu);
> + stat->rates = g_new0(DirtyRateVcpu, nvcpu);
>
> - records = g_malloc0(sizeof(DirtyPageRecord) * nvcpu);
> + records = g_new0(DirtyPageRecord, nvcpu);
>
> return records;
> }
> diff --git a/softmmu/dirtylimit.c b/softmmu/dirtylimit.c
> index 8d98cb7f2c..12668555f2 100644
> --- a/softmmu/dirtylimit.c
> +++ b/softmmu/dirtylimit.c
> @@ -154,7 +154,7 @@ void vcpu_dirty_rate_stat_initialize(void)
>
> vcpu_dirty_rate_stat->stat.nvcpu = max_cpus;
> vcpu_dirty_rate_stat->stat.rates =
> - g_malloc0(sizeof(DirtyRateVcpu) * max_cpus);
> + g_new0(DirtyRateVcpu, max_cpus);
>
> vcpu_dirty_rate_stat->running = false;
> }
> @@ -198,7 +198,7 @@ void dirtylimit_state_initialize(void)
> dirtylimit_state = g_malloc0(sizeof(*dirtylimit_state));
>
> dirtylimit_state->states =
> - g_malloc0(sizeof(VcpuDirtyLimitState) * max_cpus);
> + g_new0(VcpuDirtyLimitState, max_cpus);
>
> for (i = 0; i < max_cpus; i++) {
> dirtylimit_state->states[i].cpu_index = i;
> --
> 2.37.2
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] Use g_new() & friends where that makes obvious sense
2022-09-23 8:42 [PATCH] Use g_new() & friends where that makes obvious sense Markus Armbruster
2022-09-23 10:41 ` Michael S. Tsirkin
@ 2022-09-29 19:30 ` Laurent Vivier
1 sibling, 0 replies; 3+ messages in thread
From: Laurent Vivier @ 2022-09-29 19:30 UTC (permalink / raw)
To: Markus Armbruster, qemu-devel
Cc: elena.ufimtseva, jag.raman, john.g.johnson, pizhenwei,
arei.gonglei, mst, huangy81, quintela, dgilbert, qemu-trivial
Le 23/09/2022 à 10:42, Markus Armbruster a écrit :
> 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...
>
> The previous iteration was commit a95942b50c.
>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
> hw/remote/iommu.c | 2 +-
> hw/virtio/virtio-crypto.c | 2 +-
> migration/dirtyrate.c | 4 ++--
> softmmu/dirtylimit.c | 4 ++--
> 4 files changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/hw/remote/iommu.c b/hw/remote/iommu.c
> index fd723d91f3..1391dd712c 100644
> --- a/hw/remote/iommu.c
> +++ b/hw/remote/iommu.c
> @@ -47,7 +47,7 @@ static AddressSpace *remote_iommu_find_add_as(PCIBus *pci_bus,
> elem = g_hash_table_lookup(iommu->elem_by_devfn, INT2VOIDP(devfn));
>
> if (!elem) {
> - elem = g_malloc0(sizeof(RemoteIommuElem));
> + elem = g_new0(RemoteIommuElem, 1);
> g_hash_table_insert(iommu->elem_by_devfn, INT2VOIDP(devfn), elem);
> }
>
> diff --git a/hw/virtio/virtio-crypto.c b/hw/virtio/virtio-crypto.c
> index c1243c3f93..df4bde210b 100644
> --- a/hw/virtio/virtio-crypto.c
> +++ b/hw/virtio/virtio-crypto.c
> @@ -710,7 +710,7 @@ virtio_crypto_handle_asym_req(VirtIOCrypto *vcrypto,
> uint8_t *src = NULL;
> uint8_t *dst = NULL;
>
> - asym_op_info = g_malloc0(sizeof(CryptoDevBackendAsymOpInfo));
> + asym_op_info = g_new0(CryptoDevBackendAsymOpInfo, 1);
> src_len = ldl_le_p(&req->para.src_data_len);
> dst_len = ldl_le_p(&req->para.dst_data_len);
>
> diff --git a/migration/dirtyrate.c b/migration/dirtyrate.c
> index 795fab5c37..d6f1e01a70 100644
> --- a/migration/dirtyrate.c
> +++ b/migration/dirtyrate.c
> @@ -119,9 +119,9 @@ static DirtyPageRecord *vcpu_dirty_stat_alloc(VcpuStat *stat)
> }
>
> stat->nvcpu = nvcpu;
> - stat->rates = g_malloc0(sizeof(DirtyRateVcpu) * nvcpu);
> + stat->rates = g_new0(DirtyRateVcpu, nvcpu);
>
> - records = g_malloc0(sizeof(DirtyPageRecord) * nvcpu);
> + records = g_new0(DirtyPageRecord, nvcpu);
>
> return records;
> }
> diff --git a/softmmu/dirtylimit.c b/softmmu/dirtylimit.c
> index 8d98cb7f2c..12668555f2 100644
> --- a/softmmu/dirtylimit.c
> +++ b/softmmu/dirtylimit.c
> @@ -154,7 +154,7 @@ void vcpu_dirty_rate_stat_initialize(void)
>
> vcpu_dirty_rate_stat->stat.nvcpu = max_cpus;
> vcpu_dirty_rate_stat->stat.rates =
> - g_malloc0(sizeof(DirtyRateVcpu) * max_cpus);
> + g_new0(DirtyRateVcpu, max_cpus);
>
> vcpu_dirty_rate_stat->running = false;
> }
> @@ -198,7 +198,7 @@ void dirtylimit_state_initialize(void)
> dirtylimit_state = g_malloc0(sizeof(*dirtylimit_state));
>
> dirtylimit_state->states =
> - g_malloc0(sizeof(VcpuDirtyLimitState) * max_cpus);
> + g_new0(VcpuDirtyLimitState, max_cpus);
>
> for (i = 0; i < max_cpus; i++) {
> dirtylimit_state->states[i].cpu_index = i;
Applied to my trivial-patches branch.
Thanks,
Laurent
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-09-29 19:33 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-23 8:42 [PATCH] Use g_new() & friends where that makes obvious sense Markus Armbruster
2022-09-23 10:41 ` Michael S. Tsirkin
2022-09-29 19:30 ` Laurent Vivier
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).