Skip to content

Commit

Permalink
qapi: Use QAPI_LIST_PREPEND() where possible
Browse files Browse the repository at this point in the history
Anywhere we create a list of just one item or by prepending items
(typically because order doesn't matter), we can use
QAPI_LIST_PREPEND().  But places where we must keep the list in order
by appending remain open-coded until later patches.

Note that as a side effect, this also performs a cleanup of two minor
issues in qga/commands-posix.c: the old code was performing
 new = g_malloc0(sizeof(*ret));
which 1) is confusing because you have to verify whether 'new' and
'ret' are variables with the same type, and 2) would conflict with C++
compilation (not an actual problem for this file, but makes
copy-and-paste harder).

Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <20201113011340.463563-5-eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
[Straightforward conflicts due to commit a8aa94b "qga: update
schema for guest-get-disks 'dependents' field" and commit a10b453
"target/mips: Move mips_cpu_add_definition() from helper.c to cpu.c"
resolved.  Commit message tweaked.]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
  • Loading branch information
ebblake authored and Markus Armbruster committed Dec 19, 2020
1 parent eaedde5 commit 54aa3de
Show file tree
Hide file tree
Showing 32 changed files with 161 additions and 424 deletions.
4 changes: 2 additions & 2 deletions block/gluster.c
Expand Up @@ -359,8 +359,8 @@ static int qemu_gluster_parse_uri(BlockdevOptionsGluster *gconf,
return -EINVAL;
}

gconf->server = g_new0(SocketAddressList, 1);
gconf->server->value = gsconf = g_new0(SocketAddress, 1);
gsconf = g_new0(SocketAddress, 1);
QAPI_LIST_PREPEND(gconf->server, gsconf);

/* transport */
if (!uri->scheme || !strcmp(uri->scheme, "gluster")) {
Expand Down
7 changes: 2 additions & 5 deletions block/qapi.c
Expand Up @@ -486,12 +486,7 @@ static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
ds->account_failed = stats->account_failed;

while ((ts = block_acct_interval_next(stats, ts))) {
BlockDeviceTimedStatsList *timed_stats =
g_malloc0(sizeof(*timed_stats));
BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
timed_stats->next = ds->timed_stats;
timed_stats->value = dev_stats;
ds->timed_stats = timed_stats;

TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
Expand All @@ -515,6 +510,8 @@ static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
block_acct_queue_depth(ts, BLOCK_ACCT_READ);
dev_stats->avg_wr_queue_depth =
block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);

QAPI_LIST_PREPEND(ds->timed_stats, dev_stats);
}

bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_READ],
Expand Down
20 changes: 8 additions & 12 deletions chardev/char.c
Expand Up @@ -776,15 +776,13 @@ static int qmp_query_chardev_foreach(Object *obj, void *data)
{
Chardev *chr = CHARDEV(obj);
ChardevInfoList **list = data;
ChardevInfoList *info = g_malloc0(sizeof(*info));
ChardevInfo *value = g_malloc0(sizeof(*value));

info->value = g_malloc0(sizeof(*info->value));
info->value->label = g_strdup(chr->label);
info->value->filename = g_strdup(chr->filename);
info->value->frontend_open = chr->be && chr->be->fe_open;
value->label = g_strdup(chr->label);
value->filename = g_strdup(chr->filename);
value->frontend_open = chr->be && chr->be->fe_open;

info->next = *list;
*list = info;
QAPI_LIST_PREPEND(*list, value);

return 0;
}
Expand All @@ -803,12 +801,10 @@ static void
qmp_prepend_backend(const char *name, void *opaque)
{
ChardevBackendInfoList **list = opaque;
ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
ChardevBackendInfo *value = g_new0(ChardevBackendInfo, 1);

info->value = g_malloc0(sizeof(*info->value));
info->value->name = g_strdup(name);
info->next = *list;
*list = info;
value->name = g_strdup(name);
QAPI_LIST_PREPEND(*list, value);
}

ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
Expand Down
12 changes: 4 additions & 8 deletions docs/devel/writing-qmp-commands.txt
Expand Up @@ -531,15 +531,11 @@ TimerAlarmMethodList *qmp_query_alarm_methods(Error **errp)
bool current = true;

for (p = alarm_timers; p->name; p++) {
TimerAlarmMethodList *info = g_malloc0(sizeof(*info));
info->value = g_malloc0(sizeof(*info->value));
info->value->method_name = g_strdup(p->name);
info->value->current = current;

TimerAlarmMethod *value = g_malloc0(*value);
value->method_name = g_strdup(p->name);
value->current = current;
QAPI_LIST_PREPEND(method_list, value);
current = false;

info->next = method_list;
method_list = info;
}

return method_list;
Expand Down
6 changes: 1 addition & 5 deletions hw/core/machine-qmp-cmds.c
Expand Up @@ -215,7 +215,6 @@ MachineInfoList *qmp_query_machines(Error **errp)

for (el = machines; el; el = el->next) {
MachineClass *mc = el->data;
MachineInfoList *entry;
MachineInfo *info;

info = g_malloc0(sizeof(*info));
Expand Down Expand Up @@ -243,10 +242,7 @@ MachineInfoList *qmp_query_machines(Error **errp)
info->has_default_ram_id = true;
}

entry = g_malloc0(sizeof(*entry));
entry->value = info;
entry->next = mach_list;
mach_list = entry;
QAPI_LIST_PREPEND(mach_list, info);
}

g_slist_free(machines);
Expand Down
11 changes: 2 additions & 9 deletions hw/core/machine.c
Expand Up @@ -504,11 +504,7 @@ static void machine_set_nvdimm_persistence(Object *obj, const char *value,

void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
{
strList *item = g_new0(strList, 1);

item->value = g_strdup(type);
item->next = mc->allowed_dynamic_sysbus_devices;
mc->allowed_dynamic_sysbus_devices = item;
QAPI_LIST_PREPEND(mc->allowed_dynamic_sysbus_devices, g_strdup(type));
}

static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
Expand Down Expand Up @@ -569,7 +565,6 @@ HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)

for (i = 0; i < machine->possible_cpus->len; i++) {
Object *cpu;
HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);

cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
Expand All @@ -582,9 +577,7 @@ HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
cpu_item->has_qom_path = true;
cpu_item->qom_path = object_get_canonical_path(cpu);
}
list_item->value = cpu_item;
list_item->next = head;
head = list_item;
QAPI_LIST_PREPEND(head, cpu_item);
}
return head;
}
Expand Down
20 changes: 5 additions & 15 deletions hw/net/rocker/rocker_of_dpa.c
Expand Up @@ -2296,7 +2296,6 @@ static void of_dpa_flow_fill(void *cookie, void *value, void *user_data)
struct of_dpa_flow_key *key = &flow->key;
struct of_dpa_flow_key *mask = &flow->mask;
struct of_dpa_flow_fill_context *flow_context = user_data;
RockerOfDpaFlowList *new;
RockerOfDpaFlow *nflow;
RockerOfDpaFlowKey *nkey;
RockerOfDpaFlowMask *nmask;
Expand All @@ -2307,8 +2306,7 @@ static void of_dpa_flow_fill(void *cookie, void *value, void *user_data)
return;
}

new = g_malloc0(sizeof(*new));
nflow = new->value = g_malloc0(sizeof(*nflow));
nflow = g_malloc0(sizeof(*nflow));
nkey = nflow->key = g_malloc0(sizeof(*nkey));
nmask = nflow->mask = g_malloc0(sizeof(*nmask));
naction = nflow->action = g_malloc0(sizeof(*naction));
Expand Down Expand Up @@ -2424,8 +2422,7 @@ static void of_dpa_flow_fill(void *cookie, void *value, void *user_data)
naction->new_vlan_id = flow->action.apply.new_vlan_id;
}

new->next = flow_context->list;
flow_context->list = new;
QAPI_LIST_PREPEND(flow_context->list, nflow);
}

RockerOfDpaFlowList *qmp_query_rocker_of_dpa_flows(const char *name,
Expand Down Expand Up @@ -2469,18 +2466,15 @@ static void of_dpa_group_fill(void *key, void *value, void *user_data)
{
struct of_dpa_group *group = value;
struct of_dpa_group_fill_context *flow_context = user_data;
RockerOfDpaGroupList *new;
RockerOfDpaGroup *ngroup;
struct uint32List *id;
int i;

if (flow_context->type != 9 &&
flow_context->type != ROCKER_GROUP_TYPE_GET(group->id)) {
return;
}

new = g_malloc0(sizeof(*new));
ngroup = new->value = g_malloc0(sizeof(*ngroup));
ngroup = g_malloc0(sizeof(*ngroup));

ngroup->id = group->id;

Expand Down Expand Up @@ -2525,10 +2519,7 @@ static void of_dpa_group_fill(void *key, void *value, void *user_data)
ngroup->index = ROCKER_GROUP_INDEX_GET(group->id);
for (i = 0; i < group->l2_flood.group_count; i++) {
ngroup->has_group_ids = true;
id = g_malloc0(sizeof(*id));
id->value = group->l2_flood.group_ids[i];
id->next = ngroup->group_ids;
ngroup->group_ids = id;
QAPI_LIST_PREPEND(ngroup->group_ids, group->l2_flood.group_ids[i]);
}
break;
case ROCKER_OF_DPA_GROUP_TYPE_L3_UCAST:
Expand Down Expand Up @@ -2557,8 +2548,7 @@ static void of_dpa_group_fill(void *key, void *value, void *user_data)
break;
}

new->next = flow_context->list;
flow_context->list = new;
QAPI_LIST_PREPEND(flow_context->list, ngroup);
}

RockerOfDpaGroupList *qmp_query_rocker_of_dpa_groups(const char *name,
Expand Down
21 changes: 7 additions & 14 deletions hw/net/virtio-net.c
Expand Up @@ -437,17 +437,14 @@ static void rxfilter_notify(NetClientState *nc)

static intList *get_vlan_table(VirtIONet *n)
{
intList *list, *entry;
intList *list;
int i, j;

list = NULL;
for (i = 0; i < MAX_VLAN >> 5; i++) {
for (j = 0; n->vlans[i] && j <= 0x1f; j++) {
if (n->vlans[i] & (1U << j)) {
entry = g_malloc0(sizeof(*entry));
entry->value = (i << 5) + j;
entry->next = list;
list = entry;
QAPI_LIST_PREPEND(list, (i << 5) + j);
}
}
}
Expand All @@ -460,7 +457,7 @@ static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
VirtIONet *n = qemu_get_nic_opaque(nc);
VirtIODevice *vdev = VIRTIO_DEVICE(n);
RxFilterInfo *info;
strList *str_list, *entry;
strList *str_list;
int i;

info = g_malloc0(sizeof(*info));
Expand Down Expand Up @@ -491,19 +488,15 @@ static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)

str_list = NULL;
for (i = 0; i < n->mac_table.first_multi; i++) {
entry = g_malloc0(sizeof(*entry));
entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
entry->next = str_list;
str_list = entry;
QAPI_LIST_PREPEND(str_list,
qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN));
}
info->unicast_table = str_list;

str_list = NULL;
for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
entry = g_malloc0(sizeof(*entry));
entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
entry->next = str_list;
str_list = entry;
QAPI_LIST_PREPEND(str_list,
qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN));
}
info->multicast_table = str_list;
info->vlan_table = get_vlan_table(n);
Expand Down
7 changes: 2 additions & 5 deletions migration/migration.c
Expand Up @@ -406,12 +406,9 @@ int migration_incoming_enable_colo(void)
void migrate_add_address(SocketAddress *address)
{
MigrationIncomingState *mis = migration_incoming_get_current();
SocketAddressList *addrs;

addrs = g_new0(SocketAddressList, 1);
addrs->next = mis->socket_address_list;
mis->socket_address_list = addrs;
addrs->value = QAPI_CLONE(SocketAddress, address);
QAPI_LIST_PREPEND(mis->socket_address_list,
QAPI_CLONE(SocketAddress, address));
}

static void qemu_start_incoming_migration(const char *uri, Error **errp)
Expand Down
7 changes: 2 additions & 5 deletions migration/postcopy-ram.c
Expand Up @@ -145,14 +145,11 @@ static struct PostcopyBlocktimeContext *blocktime_context_new(void)
static uint32List *get_vcpu_blocktime_list(PostcopyBlocktimeContext *ctx)
{
MachineState *ms = MACHINE(qdev_get_machine());
uint32List *list = NULL, *entry = NULL;
uint32List *list = NULL;
int i;

for (i = ms->smp.cpus - 1; i >= 0; i--) {
entry = g_new0(uint32List, 1);
entry->value = ctx->vcpu_blocktime[i];
entry->next = list;
list = entry;
QAPI_LIST_PREPEND(list, ctx->vcpu_blocktime[i]);
}

return list;
Expand Down
13 changes: 7 additions & 6 deletions monitor/hmp-cmds.c
Expand Up @@ -1255,22 +1255,23 @@ void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
const char *cap = qdict_get_str(qdict, "capability");
bool state = qdict_get_bool(qdict, "state");
Error *err = NULL;
MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
MigrationCapabilityStatusList *caps = NULL;
MigrationCapabilityStatus *value;
int val;

val = qapi_enum_parse(&MigrationCapability_lookup, cap, -1, &err);
if (val < 0) {
goto end;
}

caps->value = g_malloc0(sizeof(*caps->value));
caps->value->capability = val;
caps->value->state = state;
caps->next = NULL;
value = g_malloc0(sizeof(*value));
value->capability = val;
value->state = state;
QAPI_LIST_PREPEND(caps, value);
qmp_migrate_set_capabilities(caps, &err);
qapi_free_MigrationCapabilityStatusList(caps);

end:
qapi_free_MigrationCapabilityStatusList(caps);
hmp_handle_error(mon, err);
}

Expand Down
25 changes: 9 additions & 16 deletions monitor/misc.c
Expand Up @@ -1430,33 +1430,26 @@ FdsetInfoList *qmp_query_fdsets(Error **errp)

QEMU_LOCK_GUARD(&mon_fdsets_lock);
QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
FdsetFdInfoList *fdsetfd_list = NULL;
FdsetInfo *fdset_info = g_malloc0(sizeof(*fdset_info));

fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
fdset_info->value->fdset_id = mon_fdset->id;
fdset_info->fdset_id = mon_fdset->id;

QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
FdsetFdInfoList *fdsetfd_info;
FdsetFdInfo *fdsetfd_info;

fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
fdsetfd_info->value->fd = mon_fdset_fd->fd;
fdsetfd_info->fd = mon_fdset_fd->fd;
if (mon_fdset_fd->opaque) {
fdsetfd_info->value->has_opaque = true;
fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
fdsetfd_info->has_opaque = true;
fdsetfd_info->opaque = g_strdup(mon_fdset_fd->opaque);
} else {
fdsetfd_info->value->has_opaque = false;
fdsetfd_info->has_opaque = false;
}

fdsetfd_info->next = fdsetfd_list;
fdsetfd_list = fdsetfd_info;
QAPI_LIST_PREPEND(fdset_info->fds, fdsetfd_info);
}

fdset_info->value->fds = fdsetfd_list;

fdset_info->next = fdset_list;
fdset_list = fdset_info;
QAPI_LIST_PREPEND(fdset_list, fdset_info);
}

return fdset_list;
Expand Down
10 changes: 5 additions & 5 deletions monitor/qmp-cmds-control.c
Expand Up @@ -138,18 +138,18 @@ EventInfoList *qmp_query_events(Error **errp)
* QAPIEvent_str() and QAPIEvent_lookup[]. When the command goes,
* they should go, too.
*/
EventInfoList *info, *ev_list = NULL;
EventInfoList *ev_list = NULL;
QAPIEvent e;

for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
const char *event_name = QAPIEvent_str(e);
EventInfo *info;

assert(event_name != NULL);
info = g_malloc0(sizeof(*info));
info->value = g_malloc0(sizeof(*info->value));
info->value->name = g_strdup(event_name);
info->name = g_strdup(event_name);

info->next = ev_list;
ev_list = info;
QAPI_LIST_PREPEND(ev_list, info);
}

return ev_list;
Expand Down

0 comments on commit 54aa3de

Please sign in to comment.