Skip to content

Commit

Permalink
qapi: Use QAPI_LIST_APPEND in trivial cases
Browse files Browse the repository at this point in the history
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 <eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20210113221013.390592-5-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
  • Loading branch information
ebblake authored and Markus Armbruster committed Jan 28, 2021
1 parent dc13f40 commit c3033fd
Show file tree
Hide file tree
Showing 20 changed files with 96 additions and 222 deletions.
10 changes: 3 additions & 7 deletions backends/hostmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,23 @@ host_memory_backend_get_host_nodes(Object *obj, Visitor *v, const char *name,
{
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
uint16List *host_nodes = NULL;
uint16List **node = &host_nodes;
uint16List **tail = &host_nodes;
unsigned long value;

value = find_first_bit(backend->host_nodes, MAX_NODES);
if (value == MAX_NODES) {
goto ret;
}

*node = g_malloc0(sizeof(**node));
(*node)->value = value;
node = &(*node)->next;
QAPI_LIST_APPEND(tail, value);

do {
value = find_next_bit(backend->host_nodes, MAX_NODES, value + 1);
if (value == MAX_NODES) {
break;
}

*node = g_malloc0(sizeof(**node));
(*node)->value = value;
node = &(*node)->next;
QAPI_LIST_APPEND(tail, value);
} while (true);

ret:
Expand Down
8 changes: 3 additions & 5 deletions block/dirty-bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,12 +572,12 @@ BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
{
BdrvDirtyBitmap *bm;
BlockDirtyInfoList *list = NULL;
BlockDirtyInfoList **plist = &list;
BlockDirtyInfoList **tail = &list;

bdrv_dirty_bitmaps_lock(bs);
QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);

info->count = bdrv_get_dirty_count(bm);
info->granularity = bdrv_dirty_bitmap_granularity(bm);
info->has_name = !!bm->name;
Expand All @@ -588,9 +588,7 @@ BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
info->persistent = bm->persistent;
info->has_inconsistent = bm->inconsistent;
info->inconsistent = bm->inconsistent;
entry->value = info;
*plist = entry;
plist = &entry->next;
QAPI_LIST_APPEND(tail, info);
}
bdrv_dirty_bitmaps_unlock(bs);

Expand Down
7 changes: 2 additions & 5 deletions block/export/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,10 @@ void qmp_block_export_del(const char *id,

BlockExportInfoList *qmp_query_block_exports(Error **errp)
{
BlockExportInfoList *head = NULL, **p_next = &head;
BlockExportInfoList *head = NULL, **tail = &head;
BlockExport *exp;

QLIST_FOREACH(exp, &block_exports, next) {
BlockExportInfoList *entry = g_new0(BlockExportInfoList, 1);
BlockExportInfo *info = g_new(BlockExportInfo, 1);
*info = (BlockExportInfo) {
.id = g_strdup(exp->id),
Expand All @@ -355,9 +354,7 @@ BlockExportInfoList *qmp_query_block_exports(Error **errp)
.shutting_down = !exp->user_owned,
};

entry->value = info;
*p_next = entry;
p_next = &entry->next;
QAPI_LIST_APPEND(tail, info);
}

return head;
Expand Down
23 changes: 5 additions & 18 deletions block/qapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,17 +418,12 @@ static uint64List *uint64_list(uint64_t *list, int size)
{
int i;
uint64List *out_list = NULL;
uint64List **pout_list = &out_list;
uint64List **tail = &out_list;

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

*pout_list = NULL;

return out_list;
}

Expand Down Expand Up @@ -636,26 +631,21 @@ BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
bool query_nodes,
Error **errp)
{
BlockStatsList *head = NULL, **p_next = &head;
BlockStatsList *head = NULL, **tail = &head;
BlockBackend *blk;
BlockDriverState *bs;

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

aio_context_acquire(ctx);
info->value = bdrv_query_bds_stats(bs, false);
QAPI_LIST_APPEND(tail, bdrv_query_bds_stats(bs, false));
aio_context_release(ctx);

*p_next = info;
p_next = &info->next;
}
} else {
for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
BlockStatsList *info;
AioContext *ctx = blk_get_aio_context(blk);
BlockStats *s;
char *qdev;
Expand All @@ -680,10 +670,7 @@ BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
bdrv_query_blk_stats(s->stats, blk);
aio_context_release(ctx);

info = g_malloc0(sizeof(*info));
info->value = s;
*p_next = info;
p_next = &info->next;
QAPI_LIST_APPEND(tail, s);
}
}

Expand Down
15 changes: 4 additions & 11 deletions block/qcow2-bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ bool qcow2_load_dirty_bitmaps(BlockDriverState *bs, Error **errp)
static Qcow2BitmapInfoFlagsList *get_bitmap_info_flags(uint32_t flags)
{
Qcow2BitmapInfoFlagsList *list = NULL;
Qcow2BitmapInfoFlagsList **plist = &list;
Qcow2BitmapInfoFlagsList **tail = &list;
int i;

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

for (i = 0; i < map_size; ++i) {
if (flags & map[i].bme) {
Qcow2BitmapInfoFlagsList *entry =
g_new0(Qcow2BitmapInfoFlagsList, 1);
entry->value = map[i].info;
*plist = entry;
plist = &entry->next;
QAPI_LIST_APPEND(tail, map[i].info);
flags &= ~map[i].bme;
}
}
Expand All @@ -1105,7 +1101,7 @@ Qcow2BitmapInfoList *qcow2_get_bitmap_info_list(BlockDriverState *bs,
Qcow2BitmapList *bm_list;
Qcow2Bitmap *bm;
Qcow2BitmapInfoList *list = NULL;
Qcow2BitmapInfoList **plist = &list;
Qcow2BitmapInfoList **tail = &list;

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

QSIMPLEQ_FOREACH(bm, bm_list, entry) {
Qcow2BitmapInfo *info = g_new0(Qcow2BitmapInfo, 1);
Qcow2BitmapInfoList *obj = g_new0(Qcow2BitmapInfoList, 1);
info->granularity = 1U << bm->granularity_bits;
info->name = g_strdup(bm->name);
info->flags = get_bitmap_info_flags(bm->flags & ~BME_RESERVED_FLAGS);
obj->value = info;
*plist = obj;
plist = &obj->next;
QAPI_LIST_APPEND(tail, info);
}

bitmap_list_free(bm_list);
Expand Down
9 changes: 3 additions & 6 deletions block/vmdk.c
Original file line number Diff line number Diff line change
Expand Up @@ -2928,7 +2928,7 @@ static ImageInfoSpecific *vmdk_get_specific_info(BlockDriverState *bs,
int i;
BDRVVmdkState *s = bs->opaque;
ImageInfoSpecific *spec_info = g_new0(ImageInfoSpecific, 1);
ImageInfoList **next;
ImageInfoList **tail;

*spec_info = (ImageInfoSpecific){
.type = IMAGE_INFO_SPECIFIC_KIND_VMDK,
Expand All @@ -2943,12 +2943,9 @@ static ImageInfoSpecific *vmdk_get_specific_info(BlockDriverState *bs,
.parent_cid = s->parent_cid,
};

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

return spec_info;
Expand Down
13 changes: 5 additions & 8 deletions blockdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -3725,28 +3725,25 @@ void qmp_x_blockdev_change(const char *parent, bool has_child,

BlockJobInfoList *qmp_query_block_jobs(Error **errp)
{
BlockJobInfoList *head = NULL, **p_next = &head;
BlockJobInfoList *head = NULL, **tail = &head;
BlockJob *job;

for (job = block_job_next(NULL); job; job = block_job_next(job)) {
BlockJobInfoList *elem;
BlockJobInfo *value;
AioContext *aio_context;

if (block_job_is_internal(job)) {
continue;
}
elem = g_new0(BlockJobInfoList, 1);
aio_context = blk_get_aio_context(job->blk);
aio_context_acquire(aio_context);
elem->value = block_job_query(job, errp);
value = block_job_query(job, errp);
aio_context_release(aio_context);
if (!elem->value) {
g_free(elem);
if (!value) {
qapi_free_BlockJobInfoList(head);
return NULL;
}
*p_next = elem;
p_next = &elem->next;
QAPI_LIST_APPEND(tail, value);
}

return head;
Expand Down
9 changes: 3 additions & 6 deletions crypto/block-luks.c
Original file line number Diff line number Diff line change
Expand Up @@ -1885,7 +1885,7 @@ static int qcrypto_block_luks_get_info(QCryptoBlock *block,
{
QCryptoBlockLUKS *luks = block->opaque;
QCryptoBlockInfoLUKSSlot *slot;
QCryptoBlockInfoLUKSSlotList *slots = NULL, **prev = &info->u.luks.slots;
QCryptoBlockInfoLUKSSlotList **tail = &info->u.luks.slots;
size_t i;

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

for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) {
slots = g_new0(QCryptoBlockInfoLUKSSlotList, 1);
*prev = slots;

slots->value = slot = g_new0(QCryptoBlockInfoLUKSSlot, 1);
slot = g_new0(QCryptoBlockInfoLUKSSlot, 1);
slot->active = luks->header.key_slots[i].active ==
QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED;
slot->key_offset = luks->header.key_slots[i].key_offset_sector
Expand All @@ -1917,7 +1914,7 @@ static int qcrypto_block_luks_get_info(QCryptoBlock *block,
slot->stripes = luks->header.key_slots[i].stripes;
}

prev = &slots->next;
QAPI_LIST_APPEND(tail, slot);
}

return 0;
Expand Down
7 changes: 2 additions & 5 deletions hw/acpi/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,11 @@ static ACPIOSTInfo *acpi_cpu_device_status(int idx, AcpiCpuStatus *cdev)

void acpi_cpu_ospm_status(CPUHotplugState *cpu_st, ACPIOSTInfoList ***list)
{
ACPIOSTInfoList ***tail = list;
int i;

for (i = 0; i < cpu_st->dev_count; i++) {
ACPIOSTInfoList *elem = g_new0(ACPIOSTInfoList, 1);
elem->value = acpi_cpu_device_status(i, &cpu_st->devs[i]);
elem->next = NULL;
**list = elem;
*list = &elem->next;
QAPI_LIST_APPEND(*tail, acpi_cpu_device_status(i, &cpu_st->devs[i]));
}
}

Expand Down
8 changes: 3 additions & 5 deletions hw/acpi/memory_hotplug.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,12 @@ static ACPIOSTInfo *acpi_memory_device_status(int slot, MemStatus *mdev)

void acpi_memory_ospm_status(MemHotplugState *mem_st, ACPIOSTInfoList ***list)
{
ACPIOSTInfoList ***tail = list;
int i;

for (i = 0; i < mem_st->dev_count; i++) {
ACPIOSTInfoList *elem = g_new0(ACPIOSTInfoList, 1);
elem->value = acpi_memory_device_status(i, &mem_st->devs[i]);
elem->next = NULL;
**list = elem;
*list = &elem->next;
QAPI_LIST_APPEND(*tail,
acpi_memory_device_status(i, &mem_st->devs[i]));
}
}

Expand Down
12 changes: 3 additions & 9 deletions iothread.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Event loop thread
*
* Copyright Red Hat Inc., 2013
* Copyright Red Hat Inc., 2013, 2020
*
* Authors:
* Stefan Hajnoczi <stefanha@redhat.com>
Expand Down Expand Up @@ -310,8 +310,7 @@ AioContext *iothread_get_aio_context(IOThread *iothread)

static int query_one_iothread(Object *object, void *opaque)
{
IOThreadInfoList ***prev = opaque;
IOThreadInfoList *elem;
IOThreadInfoList ***tail = opaque;
IOThreadInfo *info;
IOThread *iothread;

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

elem = g_new0(IOThreadInfoList, 1);
elem->value = info;
elem->next = NULL;

**prev = elem;
*prev = &elem->next;
QAPI_LIST_APPEND(*tail, info);
return 0;
}

Expand Down
13 changes: 5 additions & 8 deletions job-qmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,28 +164,25 @@ static JobInfo *job_query_single(Job *job, Error **errp)

JobInfoList *qmp_query_jobs(Error **errp)
{
JobInfoList *head = NULL, **p_next = &head;
JobInfoList *head = NULL, **tail = &head;
Job *job;

for (job = job_next(NULL); job; job = job_next(job)) {
JobInfoList *elem;
JobInfo *value;
AioContext *aio_context;

if (job_is_internal(job)) {
continue;
}
elem = g_new0(JobInfoList, 1);
aio_context = job->aio_context;
aio_context_acquire(aio_context);
elem->value = job_query_single(job, errp);
value = job_query_single(job, errp);
aio_context_release(aio_context);
if (!elem->value) {
g_free(elem);
if (!value) {
qapi_free_JobInfoList(head);
return NULL;
}
*p_next = elem;
p_next = &elem->next;
QAPI_LIST_APPEND(tail, value);
}

return head;
Expand Down
Loading

0 comments on commit c3033fd

Please sign in to comment.