Skip to content

Commit c3033fd

Browse files
ebblakeMarkus Armbruster
authored and
Markus Armbruster
committed
qapi: Use QAPI_LIST_APPEND in trivial cases
The easiest spots to use QAPI_LIST_APPEND are where we already have an obvious pointer to the tail of a list. While at it, consistently use the variable name 'tail' for that purpose. Signed-off-by: Eric Blake <[email protected]> Reviewed-by: Vladimir Sementsov-Ogievskiy <[email protected]> Reviewed-by: Markus Armbruster <[email protected]> Message-Id: <[email protected]> Signed-off-by: Markus Armbruster <[email protected]>
1 parent dc13f40 commit c3033fd

20 files changed

+96
-222
lines changed

backends/hostmem.c

+3-7
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,23 @@ host_memory_backend_get_host_nodes(Object *obj, Visitor *v, const char *name,
8080
{
8181
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
8282
uint16List *host_nodes = NULL;
83-
uint16List **node = &host_nodes;
83+
uint16List **tail = &host_nodes;
8484
unsigned long value;
8585

8686
value = find_first_bit(backend->host_nodes, MAX_NODES);
8787
if (value == MAX_NODES) {
8888
goto ret;
8989
}
9090

91-
*node = g_malloc0(sizeof(**node));
92-
(*node)->value = value;
93-
node = &(*node)->next;
91+
QAPI_LIST_APPEND(tail, value);
9492

9593
do {
9694
value = find_next_bit(backend->host_nodes, MAX_NODES, value + 1);
9795
if (value == MAX_NODES) {
9896
break;
9997
}
10098

101-
*node = g_malloc0(sizeof(**node));
102-
(*node)->value = value;
103-
node = &(*node)->next;
99+
QAPI_LIST_APPEND(tail, value);
104100
} while (true);
105101

106102
ret:

block/dirty-bitmap.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -572,12 +572,12 @@ BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
572572
{
573573
BdrvDirtyBitmap *bm;
574574
BlockDirtyInfoList *list = NULL;
575-
BlockDirtyInfoList **plist = &list;
575+
BlockDirtyInfoList **tail = &list;
576576

577577
bdrv_dirty_bitmaps_lock(bs);
578578
QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
579579
BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
580-
BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
580+
581581
info->count = bdrv_get_dirty_count(bm);
582582
info->granularity = bdrv_dirty_bitmap_granularity(bm);
583583
info->has_name = !!bm->name;
@@ -588,9 +588,7 @@ BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
588588
info->persistent = bm->persistent;
589589
info->has_inconsistent = bm->inconsistent;
590590
info->inconsistent = bm->inconsistent;
591-
entry->value = info;
592-
*plist = entry;
593-
plist = &entry->next;
591+
QAPI_LIST_APPEND(tail, info);
594592
}
595593
bdrv_dirty_bitmaps_unlock(bs);
596594

block/export/export.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,10 @@ void qmp_block_export_del(const char *id,
342342

343343
BlockExportInfoList *qmp_query_block_exports(Error **errp)
344344
{
345-
BlockExportInfoList *head = NULL, **p_next = &head;
345+
BlockExportInfoList *head = NULL, **tail = &head;
346346
BlockExport *exp;
347347

348348
QLIST_FOREACH(exp, &block_exports, next) {
349-
BlockExportInfoList *entry = g_new0(BlockExportInfoList, 1);
350349
BlockExportInfo *info = g_new(BlockExportInfo, 1);
351350
*info = (BlockExportInfo) {
352351
.id = g_strdup(exp->id),
@@ -355,9 +354,7 @@ BlockExportInfoList *qmp_query_block_exports(Error **errp)
355354
.shutting_down = !exp->user_owned,
356355
};
357356

358-
entry->value = info;
359-
*p_next = entry;
360-
p_next = &entry->next;
357+
QAPI_LIST_APPEND(tail, info);
361358
}
362359

363360
return head;

block/qapi.c

+5-18
Original file line numberDiff line numberDiff line change
@@ -418,17 +418,12 @@ static uint64List *uint64_list(uint64_t *list, int size)
418418
{
419419
int i;
420420
uint64List *out_list = NULL;
421-
uint64List **pout_list = &out_list;
421+
uint64List **tail = &out_list;
422422

423423
for (i = 0; i < size; i++) {
424-
uint64List *entry = g_new(uint64List, 1);
425-
entry->value = list[i];
426-
*pout_list = entry;
427-
pout_list = &entry->next;
424+
QAPI_LIST_APPEND(tail, list[i]);
428425
}
429426

430-
*pout_list = NULL;
431-
432427
return out_list;
433428
}
434429

@@ -636,26 +631,21 @@ BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
636631
bool query_nodes,
637632
Error **errp)
638633
{
639-
BlockStatsList *head = NULL, **p_next = &head;
634+
BlockStatsList *head = NULL, **tail = &head;
640635
BlockBackend *blk;
641636
BlockDriverState *bs;
642637

643638
/* Just to be safe if query_nodes is not always initialized */
644639
if (has_query_nodes && query_nodes) {
645640
for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
646-
BlockStatsList *info = g_malloc0(sizeof(*info));
647641
AioContext *ctx = bdrv_get_aio_context(bs);
648642

649643
aio_context_acquire(ctx);
650-
info->value = bdrv_query_bds_stats(bs, false);
644+
QAPI_LIST_APPEND(tail, bdrv_query_bds_stats(bs, false));
651645
aio_context_release(ctx);
652-
653-
*p_next = info;
654-
p_next = &info->next;
655646
}
656647
} else {
657648
for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
658-
BlockStatsList *info;
659649
AioContext *ctx = blk_get_aio_context(blk);
660650
BlockStats *s;
661651
char *qdev;
@@ -680,10 +670,7 @@ BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
680670
bdrv_query_blk_stats(s->stats, blk);
681671
aio_context_release(ctx);
682672

683-
info = g_malloc0(sizeof(*info));
684-
info->value = s;
685-
*p_next = info;
686-
p_next = &info->next;
673+
QAPI_LIST_APPEND(tail, s);
687674
}
688675
}
689676

block/qcow2-bitmap.c

+4-11
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ bool qcow2_load_dirty_bitmaps(BlockDriverState *bs, Error **errp)
10611061
static Qcow2BitmapInfoFlagsList *get_bitmap_info_flags(uint32_t flags)
10621062
{
10631063
Qcow2BitmapInfoFlagsList *list = NULL;
1064-
Qcow2BitmapInfoFlagsList **plist = &list;
1064+
Qcow2BitmapInfoFlagsList **tail = &list;
10651065
int i;
10661066

10671067
static const struct {
@@ -1076,11 +1076,7 @@ static Qcow2BitmapInfoFlagsList *get_bitmap_info_flags(uint32_t flags)
10761076

10771077
for (i = 0; i < map_size; ++i) {
10781078
if (flags & map[i].bme) {
1079-
Qcow2BitmapInfoFlagsList *entry =
1080-
g_new0(Qcow2BitmapInfoFlagsList, 1);
1081-
entry->value = map[i].info;
1082-
*plist = entry;
1083-
plist = &entry->next;
1079+
QAPI_LIST_APPEND(tail, map[i].info);
10841080
flags &= ~map[i].bme;
10851081
}
10861082
}
@@ -1105,7 +1101,7 @@ Qcow2BitmapInfoList *qcow2_get_bitmap_info_list(BlockDriverState *bs,
11051101
Qcow2BitmapList *bm_list;
11061102
Qcow2Bitmap *bm;
11071103
Qcow2BitmapInfoList *list = NULL;
1108-
Qcow2BitmapInfoList **plist = &list;
1104+
Qcow2BitmapInfoList **tail = &list;
11091105

11101106
if (s->nb_bitmaps == 0) {
11111107
return NULL;
@@ -1119,13 +1115,10 @@ Qcow2BitmapInfoList *qcow2_get_bitmap_info_list(BlockDriverState *bs,
11191115

11201116
QSIMPLEQ_FOREACH(bm, bm_list, entry) {
11211117
Qcow2BitmapInfo *info = g_new0(Qcow2BitmapInfo, 1);
1122-
Qcow2BitmapInfoList *obj = g_new0(Qcow2BitmapInfoList, 1);
11231118
info->granularity = 1U << bm->granularity_bits;
11241119
info->name = g_strdup(bm->name);
11251120
info->flags = get_bitmap_info_flags(bm->flags & ~BME_RESERVED_FLAGS);
1126-
obj->value = info;
1127-
*plist = obj;
1128-
plist = &obj->next;
1121+
QAPI_LIST_APPEND(tail, info);
11291122
}
11301123

11311124
bitmap_list_free(bm_list);

block/vmdk.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -2928,7 +2928,7 @@ static ImageInfoSpecific *vmdk_get_specific_info(BlockDriverState *bs,
29282928
int i;
29292929
BDRVVmdkState *s = bs->opaque;
29302930
ImageInfoSpecific *spec_info = g_new0(ImageInfoSpecific, 1);
2931-
ImageInfoList **next;
2931+
ImageInfoList **tail;
29322932

29332933
*spec_info = (ImageInfoSpecific){
29342934
.type = IMAGE_INFO_SPECIFIC_KIND_VMDK,
@@ -2943,12 +2943,9 @@ static ImageInfoSpecific *vmdk_get_specific_info(BlockDriverState *bs,
29432943
.parent_cid = s->parent_cid,
29442944
};
29452945

2946-
next = &spec_info->u.vmdk.data->extents;
2946+
tail = &spec_info->u.vmdk.data->extents;
29472947
for (i = 0; i < s->num_extents; i++) {
2948-
*next = g_new0(ImageInfoList, 1);
2949-
(*next)->value = vmdk_get_extent_info(&s->extents[i]);
2950-
(*next)->next = NULL;
2951-
next = &(*next)->next;
2948+
QAPI_LIST_APPEND(tail, vmdk_get_extent_info(&s->extents[i]));
29522949
}
29532950

29542951
return spec_info;

blockdev.c

+5-8
Original file line numberDiff line numberDiff line change
@@ -3725,28 +3725,25 @@ void qmp_x_blockdev_change(const char *parent, bool has_child,
37253725

37263726
BlockJobInfoList *qmp_query_block_jobs(Error **errp)
37273727
{
3728-
BlockJobInfoList *head = NULL, **p_next = &head;
3728+
BlockJobInfoList *head = NULL, **tail = &head;
37293729
BlockJob *job;
37303730

37313731
for (job = block_job_next(NULL); job; job = block_job_next(job)) {
3732-
BlockJobInfoList *elem;
3732+
BlockJobInfo *value;
37333733
AioContext *aio_context;
37343734

37353735
if (block_job_is_internal(job)) {
37363736
continue;
37373737
}
3738-
elem = g_new0(BlockJobInfoList, 1);
37393738
aio_context = blk_get_aio_context(job->blk);
37403739
aio_context_acquire(aio_context);
3741-
elem->value = block_job_query(job, errp);
3740+
value = block_job_query(job, errp);
37423741
aio_context_release(aio_context);
3743-
if (!elem->value) {
3744-
g_free(elem);
3742+
if (!value) {
37453743
qapi_free_BlockJobInfoList(head);
37463744
return NULL;
37473745
}
3748-
*p_next = elem;
3749-
p_next = &elem->next;
3746+
QAPI_LIST_APPEND(tail, value);
37503747
}
37513748

37523749
return head;

crypto/block-luks.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -1885,7 +1885,7 @@ static int qcrypto_block_luks_get_info(QCryptoBlock *block,
18851885
{
18861886
QCryptoBlockLUKS *luks = block->opaque;
18871887
QCryptoBlockInfoLUKSSlot *slot;
1888-
QCryptoBlockInfoLUKSSlotList *slots = NULL, **prev = &info->u.luks.slots;
1888+
QCryptoBlockInfoLUKSSlotList **tail = &info->u.luks.slots;
18891889
size_t i;
18901890

18911891
info->u.luks.cipher_alg = luks->cipher_alg;
@@ -1902,10 +1902,7 @@ static int qcrypto_block_luks_get_info(QCryptoBlock *block,
19021902
sizeof(luks->header.uuid));
19031903

19041904
for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) {
1905-
slots = g_new0(QCryptoBlockInfoLUKSSlotList, 1);
1906-
*prev = slots;
1907-
1908-
slots->value = slot = g_new0(QCryptoBlockInfoLUKSSlot, 1);
1905+
slot = g_new0(QCryptoBlockInfoLUKSSlot, 1);
19091906
slot->active = luks->header.key_slots[i].active ==
19101907
QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED;
19111908
slot->key_offset = luks->header.key_slots[i].key_offset_sector
@@ -1917,7 +1914,7 @@ static int qcrypto_block_luks_get_info(QCryptoBlock *block,
19171914
slot->stripes = luks->header.key_slots[i].stripes;
19181915
}
19191916

1920-
prev = &slots->next;
1917+
QAPI_LIST_APPEND(tail, slot);
19211918
}
19221919

19231920
return 0;

hw/acpi/cpu.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,11 @@ static ACPIOSTInfo *acpi_cpu_device_status(int idx, AcpiCpuStatus *cdev)
4444

4545
void acpi_cpu_ospm_status(CPUHotplugState *cpu_st, ACPIOSTInfoList ***list)
4646
{
47+
ACPIOSTInfoList ***tail = list;
4748
int i;
4849

4950
for (i = 0; i < cpu_st->dev_count; i++) {
50-
ACPIOSTInfoList *elem = g_new0(ACPIOSTInfoList, 1);
51-
elem->value = acpi_cpu_device_status(i, &cpu_st->devs[i]);
52-
elem->next = NULL;
53-
**list = elem;
54-
*list = &elem->next;
51+
QAPI_LIST_APPEND(*tail, acpi_cpu_device_status(i, &cpu_st->devs[i]));
5552
}
5653
}
5754

hw/acpi/memory_hotplug.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,12 @@ static ACPIOSTInfo *acpi_memory_device_status(int slot, MemStatus *mdev)
5353

5454
void acpi_memory_ospm_status(MemHotplugState *mem_st, ACPIOSTInfoList ***list)
5555
{
56+
ACPIOSTInfoList ***tail = list;
5657
int i;
5758

5859
for (i = 0; i < mem_st->dev_count; i++) {
59-
ACPIOSTInfoList *elem = g_new0(ACPIOSTInfoList, 1);
60-
elem->value = acpi_memory_device_status(i, &mem_st->devs[i]);
61-
elem->next = NULL;
62-
**list = elem;
63-
*list = &elem->next;
60+
QAPI_LIST_APPEND(*tail,
61+
acpi_memory_device_status(i, &mem_st->devs[i]));
6462
}
6563
}
6664

iothread.c

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Event loop thread
33
*
4-
* Copyright Red Hat Inc., 2013
4+
* Copyright Red Hat Inc., 2013, 2020
55
*
66
* Authors:
77
* Stefan Hajnoczi <[email protected]>
@@ -310,8 +310,7 @@ AioContext *iothread_get_aio_context(IOThread *iothread)
310310

311311
static int query_one_iothread(Object *object, void *opaque)
312312
{
313-
IOThreadInfoList ***prev = opaque;
314-
IOThreadInfoList *elem;
313+
IOThreadInfoList ***tail = opaque;
315314
IOThreadInfo *info;
316315
IOThread *iothread;
317316

@@ -327,12 +326,7 @@ static int query_one_iothread(Object *object, void *opaque)
327326
info->poll_grow = iothread->poll_grow;
328327
info->poll_shrink = iothread->poll_shrink;
329328

330-
elem = g_new0(IOThreadInfoList, 1);
331-
elem->value = info;
332-
elem->next = NULL;
333-
334-
**prev = elem;
335-
*prev = &elem->next;
329+
QAPI_LIST_APPEND(*tail, info);
336330
return 0;
337331
}
338332

job-qmp.c

+5-8
Original file line numberDiff line numberDiff line change
@@ -164,28 +164,25 @@ static JobInfo *job_query_single(Job *job, Error **errp)
164164

165165
JobInfoList *qmp_query_jobs(Error **errp)
166166
{
167-
JobInfoList *head = NULL, **p_next = &head;
167+
JobInfoList *head = NULL, **tail = &head;
168168
Job *job;
169169

170170
for (job = job_next(NULL); job; job = job_next(job)) {
171-
JobInfoList *elem;
171+
JobInfo *value;
172172
AioContext *aio_context;
173173

174174
if (job_is_internal(job)) {
175175
continue;
176176
}
177-
elem = g_new0(JobInfoList, 1);
178177
aio_context = job->aio_context;
179178
aio_context_acquire(aio_context);
180-
elem->value = job_query_single(job, errp);
179+
value = job_query_single(job, errp);
181180
aio_context_release(aio_context);
182-
if (!elem->value) {
183-
g_free(elem);
181+
if (!value) {
184182
qapi_free_JobInfoList(head);
185183
return NULL;
186184
}
187-
*p_next = elem;
188-
p_next = &elem->next;
185+
QAPI_LIST_APPEND(tail, value);
189186
}
190187

191188
return head;

0 commit comments

Comments
 (0)