Use g_new() & friends where that makes obvious sense

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 <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Cédric Le Goater <clg@kaod.org>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Message-Id: <20220315144156.1595462-4-armbru@redhat.com>
Reviewed-by: Pavel Dovgalyuk <Pavel.Dovgalyuk@ispras.ru>
This commit is contained in:
Markus Armbruster 2022-03-15 15:41:56 +01:00
parent 1366244ab6
commit b21e238037
102 changed files with 195 additions and 200 deletions

View file

@ -113,7 +113,7 @@ static void hbitmap_test_truncate_impl(TestHBitmapData *data,
n = hbitmap_test_array_size(size);
m = hbitmap_test_array_size(data->old_size);
data->bits = g_realloc(data->bits, sizeof(unsigned long) * n);
data->bits = g_renew(unsigned long, data->bits, n);
if (n > m) {
memset(&data->bits[m], 0x00, sizeof(unsigned long) * (n - m));
}

View file

@ -82,8 +82,8 @@ UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a,
Error **errp)
{
UserDefTwo *ret;
UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne));
UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne));
UserDefOne *ud1c = g_new0(UserDefOne, 1);
UserDefOne *ud1d = g_new0(UserDefOne, 1);
ud1c->string = strdup(ud1a->string);
ud1c->integer = ud1a->integer;
@ -344,23 +344,23 @@ static void test_dealloc_types(void)
UserDefOne *ud1test, *ud1a, *ud1b;
UserDefOneList *ud1list;
ud1test = g_malloc0(sizeof(UserDefOne));
ud1test = g_new0(UserDefOne, 1);
ud1test->integer = 42;
ud1test->string = g_strdup("hi there 42");
qapi_free_UserDefOne(ud1test);
ud1a = g_malloc0(sizeof(UserDefOne));
ud1a = g_new0(UserDefOne, 1);
ud1a->integer = 43;
ud1a->string = g_strdup("hi there 43");
ud1b = g_malloc0(sizeof(UserDefOne));
ud1b = g_new0(UserDefOne, 1);
ud1b->integer = 44;
ud1b->string = g_strdup("hi there 44");
ud1list = g_malloc0(sizeof(UserDefOneList));
ud1list = g_new0(UserDefOneList, 1);
ud1list->value = ud1a;
ud1list->next = g_malloc0(sizeof(UserDefOneList));
ud1list->next = g_new0(UserDefOneList, 1);
ud1list->next->value = ud1b;
qapi_free_UserDefOneList(ud1list);

View file

@ -338,7 +338,7 @@ static void test_visitor_out_union_flat(TestOutputVisitorData *data,
{
QDict *qdict;
UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion));
UserDefFlatUnion *tmp = g_new0(UserDefFlatUnion, 1);
tmp->enum1 = ENUM_ONE_VALUE1;
tmp->string = g_strdup("str");
tmp->integer = 41;

View file

@ -1002,22 +1002,22 @@ static TestGTreeDomain *create_first_domain(void)
TestGTreeMapping *map_a, *map_b;
TestGTreeInterval *a, *b;
domain = g_malloc0(sizeof(TestGTreeDomain));
domain = g_new0(TestGTreeDomain, 1);
domain->id = 6;
a = g_malloc0(sizeof(TestGTreeInterval));
a = g_new0(TestGTreeInterval, 1);
a->low = 0x1000;
a->high = 0x1FFF;
b = g_malloc0(sizeof(TestGTreeInterval));
b = g_new0(TestGTreeInterval, 1);
b->low = 0x4000;
b->high = 0x4FFF;
map_a = g_malloc0(sizeof(TestGTreeMapping));
map_a = g_new0(TestGTreeMapping, 1);
map_a->phys_addr = 0xa000;
map_a->flags = 1;
map_b = g_malloc0(sizeof(TestGTreeMapping));
map_b = g_new0(TestGTreeMapping, 1);
map_b->phys_addr = 0xe0000;
map_b->flags = 2;
@ -1120,7 +1120,7 @@ static void diff_iommu(TestGTreeIOMMU *iommu1, TestGTreeIOMMU *iommu2)
static void test_gtree_load_domain(void)
{
TestGTreeDomain *dest_domain = g_malloc0(sizeof(TestGTreeDomain));
TestGTreeDomain *dest_domain = g_new0(TestGTreeDomain, 1);
TestGTreeDomain *orig_domain = create_first_domain();
QEMUFile *fload, *fsave;
char eof;
@ -1185,7 +1185,7 @@ uint8_t iommu_dump[] = {
static TestGTreeIOMMU *create_iommu(void)
{
TestGTreeIOMMU *iommu = g_malloc0(sizeof(TestGTreeIOMMU));
TestGTreeIOMMU *iommu = g_new0(TestGTreeIOMMU, 1);
TestGTreeDomain *first_domain = create_first_domain();
TestGTreeDomain *second_domain;
TestGTreeMapping *map_c;
@ -1196,7 +1196,7 @@ static TestGTreeIOMMU *create_iommu(void)
NULL,
destroy_domain);
second_domain = g_malloc0(sizeof(TestGTreeDomain));
second_domain = g_new0(TestGTreeDomain, 1);
second_domain->id = 5;
second_domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp,
NULL,
@ -1206,11 +1206,11 @@ static TestGTreeIOMMU *create_iommu(void)
g_tree_insert(iommu->domains, GUINT_TO_POINTER(6), first_domain);
g_tree_insert(iommu->domains, (gpointer)0x0000000000000005, second_domain);
c = g_malloc0(sizeof(TestGTreeInterval));
c = g_new0(TestGTreeInterval, 1);
c->low = 0x1000000;
c->high = 0x1FFFFFF;
map_c = g_malloc0(sizeof(TestGTreeMapping));
map_c = g_new0(TestGTreeMapping, 1);
map_c->phys_addr = 0xF000000;
map_c->flags = 0x3;
@ -1235,7 +1235,7 @@ static void test_gtree_save_iommu(void)
static void test_gtree_load_iommu(void)
{
TestGTreeIOMMU *dest_iommu = g_malloc0(sizeof(TestGTreeIOMMU));
TestGTreeIOMMU *dest_iommu = g_new0(TestGTreeIOMMU, 1);
TestGTreeIOMMU *orig_iommu = create_iommu();
QEMUFile *fsave, *fload;
char eof;
@ -1274,11 +1274,11 @@ static uint8_t qlist_dump[] = {
static TestQListContainer *alloc_container(void)
{
TestQListElement *a = g_malloc(sizeof(TestQListElement));
TestQListElement *b = g_malloc(sizeof(TestQListElement));
TestQListElement *c = g_malloc(sizeof(TestQListElement));
TestQListElement *d = g_malloc(sizeof(TestQListElement));
TestQListContainer *container = g_malloc(sizeof(TestQListContainer));
TestQListElement *a = g_new(TestQListElement, 1);
TestQListElement *b = g_new(TestQListElement, 1);
TestQListElement *c = g_new(TestQListElement, 1);
TestQListElement *d = g_new(TestQListElement, 1);
TestQListContainer *container = g_new(TestQListContainer, 1);
a->id = 0x0a;
b->id = 0x0b00;
@ -1332,11 +1332,11 @@ static void manipulate_container(TestQListContainer *c)
TestQListElement *prev = NULL, *iter = QLIST_FIRST(&c->list);
TestQListElement *elem;
elem = g_malloc(sizeof(TestQListElement));
elem = g_new(TestQListElement, 1);
elem->id = 0x12;
QLIST_INSERT_AFTER(iter, elem, next);
elem = g_malloc(sizeof(TestQListElement));
elem = g_new(TestQListElement, 1);
elem->id = 0x13;
QLIST_INSERT_HEAD(&c->list, elem, next);
@ -1345,11 +1345,11 @@ static void manipulate_container(TestQListContainer *c)
iter = QLIST_NEXT(iter, next);
}
elem = g_malloc(sizeof(TestQListElement));
elem = g_new(TestQListElement, 1);
elem->id = 0x14;
QLIST_INSERT_BEFORE(prev, elem, next);
elem = g_malloc(sizeof(TestQListElement));
elem = g_new(TestQListElement, 1);
elem->id = 0x15;
QLIST_INSERT_AFTER(prev, elem, next);
@ -1370,7 +1370,7 @@ static void test_load_qlist(void)
{
QEMUFile *fsave, *fload;
TestQListContainer *orig_container = alloc_container();
TestQListContainer *dest_container = g_malloc0(sizeof(TestQListContainer));
TestQListContainer *dest_container = g_new0(TestQListContainer, 1);
char eof;
QLIST_INIT(&dest_container->list);