Skip to content

Commit

Permalink
qapi: More complex uses of QAPI_LIST_APPEND
Browse files Browse the repository at this point in the history
These cases require a bit more thought to review; in each case, the
code was appending to a list, but not with a FOOList **tail variable.

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20210113221013.390592-6-eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Flawed change to qmp_guest_network_get_interfaces() dropped]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
  • Loading branch information
ebblake authored and Markus Armbruster committed Jan 28, 2021
1 parent c3033fd commit 95b3a8c
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 305 deletions.
13 changes: 3 additions & 10 deletions block/gluster.c
Expand Up @@ -514,7 +514,7 @@ static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,
{
QemuOpts *opts;
SocketAddress *gsconf = NULL;
SocketAddressList *curr = NULL;
SocketAddressList **tail;
QDict *backing_options = NULL;
Error *local_err = NULL;
char *str = NULL;
Expand Down Expand Up @@ -547,6 +547,7 @@ static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,
}
gconf->path = g_strdup(ptr);
qemu_opts_del(opts);
tail = &gconf->server;

for (i = 0; i < num_servers; i++) {
str = g_strdup_printf(GLUSTER_OPT_SERVER_PATTERN"%d.", i);
Expand Down Expand Up @@ -655,15 +656,7 @@ static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,
qemu_opts_del(opts);
}

if (gconf->server == NULL) {
gconf->server = g_new0(SocketAddressList, 1);
gconf->server->value = gsconf;
curr = gconf->server;
} else {
curr->next = g_new0(SocketAddressList, 1);
curr->next->value = gsconf;
curr = curr->next;
}
QAPI_LIST_APPEND(tail, gsconf);
gsconf = NULL;

qobject_unref(backing_options);
Expand Down
14 changes: 2 additions & 12 deletions block/qapi.c
Expand Up @@ -198,7 +198,7 @@ int bdrv_query_snapshot_info_list(BlockDriverState *bs,
{
int i, sn_count;
QEMUSnapshotInfo *sn_tab = NULL;
SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
SnapshotInfoList *head = NULL, **tail = &head;
SnapshotInfo *info;

sn_count = bdrv_snapshot_list(bs, &sn_tab);
Expand Down Expand Up @@ -233,17 +233,7 @@ int bdrv_query_snapshot_info_list(BlockDriverState *bs,
info->icount = sn_tab[i].icount;
info->has_icount = sn_tab[i].icount != -1ULL;

info_list = g_new0(SnapshotInfoList, 1);
info_list->value = info;

/* XXX: waiting for the qapi to support qemu-queue.h types */
if (!cur_item) {
head = cur_item = info_list;
} else {
cur_item->next = info_list;
cur_item = info_list;
}

QAPI_LIST_APPEND(tail, info);
}

g_free(sn_tab);
Expand Down
22 changes: 6 additions & 16 deletions dump/dump.c
Expand Up @@ -2030,39 +2030,29 @@ void qmp_dump_guest_memory(bool paging, const char *file,

DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
{
DumpGuestMemoryFormatList *item;
DumpGuestMemoryCapability *cap =
g_malloc0(sizeof(DumpGuestMemoryCapability));
DumpGuestMemoryFormatList **tail = &cap->formats;

/* elf is always available */
item = g_malloc0(sizeof(DumpGuestMemoryFormatList));
cap->formats = item;
item->value = DUMP_GUEST_MEMORY_FORMAT_ELF;
QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_ELF);

/* kdump-zlib is always available */
item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
item = item->next;
item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB);

/* add new item if kdump-lzo is available */
#ifdef CONFIG_LZO
item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
item = item->next;
item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO);
#endif

/* add new item if kdump-snappy is available */
#ifdef CONFIG_SNAPPY
item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
item = item->next;
item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY);
#endif

/* Windows dump is available only if target is x86_64 */
#ifdef TARGET_X86_64
item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
item = item->next;
item->value = DUMP_GUEST_MEMORY_FORMAT_WIN_DMP;
QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP);
#endif

return cap;
Expand Down
93 changes: 40 additions & 53 deletions hw/core/machine-qmp-cmds.c
Expand Up @@ -28,11 +28,11 @@ CpuInfoList *qmp_query_cpus(Error **errp)
{
MachineState *ms = MACHINE(qdev_get_machine());
MachineClass *mc = MACHINE_GET_CLASS(ms);
CpuInfoList *head = NULL, *cur_item = NULL;
CpuInfoList *head = NULL, **tail = &head;
CPUState *cpu;

CPU_FOREACH(cpu) {
CpuInfoList *info;
CpuInfo *value;
#if defined(TARGET_I386)
X86CPU *x86_cpu = X86_CPU(cpu);
CPUX86State *env = &x86_cpu->env;
Expand All @@ -58,53 +58,46 @@ CpuInfoList *qmp_query_cpus(Error **errp)

cpu_synchronize_state(cpu);

info = g_malloc0(sizeof(*info));
info->value = g_malloc0(sizeof(*info->value));
info->value->CPU = cpu->cpu_index;
info->value->current = (cpu == first_cpu);
info->value->halted = cpu->halted;
info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
info->value->thread_id = cpu->thread_id;
value = g_malloc0(sizeof(*value));
value->CPU = cpu->cpu_index;
value->current = (cpu == first_cpu);
value->halted = cpu->halted;
value->qom_path = object_get_canonical_path(OBJECT(cpu));
value->thread_id = cpu->thread_id;
#if defined(TARGET_I386)
info->value->arch = CPU_INFO_ARCH_X86;
info->value->u.x86.pc = env->eip + env->segs[R_CS].base;
value->arch = CPU_INFO_ARCH_X86;
value->u.x86.pc = env->eip + env->segs[R_CS].base;
#elif defined(TARGET_PPC)
info->value->arch = CPU_INFO_ARCH_PPC;
info->value->u.ppc.nip = env->nip;
value->arch = CPU_INFO_ARCH_PPC;
value->u.ppc.nip = env->nip;
#elif defined(TARGET_SPARC)
info->value->arch = CPU_INFO_ARCH_SPARC;
info->value->u.q_sparc.pc = env->pc;
info->value->u.q_sparc.npc = env->npc;
value->arch = CPU_INFO_ARCH_SPARC;
value->u.q_sparc.pc = env->pc;
value->u.q_sparc.npc = env->npc;
#elif defined(TARGET_MIPS)
info->value->arch = CPU_INFO_ARCH_MIPS;
info->value->u.q_mips.PC = env->active_tc.PC;
value->arch = CPU_INFO_ARCH_MIPS;
value->u.q_mips.PC = env->active_tc.PC;
#elif defined(TARGET_TRICORE)
info->value->arch = CPU_INFO_ARCH_TRICORE;
info->value->u.tricore.PC = env->PC;
value->arch = CPU_INFO_ARCH_TRICORE;
value->u.tricore.PC = env->PC;
#elif defined(TARGET_S390X)
info->value->arch = CPU_INFO_ARCH_S390;
info->value->u.s390.cpu_state = env->cpu_state;
value->arch = CPU_INFO_ARCH_S390;
value->u.s390.cpu_state = env->cpu_state;
#elif defined(TARGET_RISCV)
info->value->arch = CPU_INFO_ARCH_RISCV;
info->value->u.riscv.pc = env->pc;
value->arch = CPU_INFO_ARCH_RISCV;
value->u.riscv.pc = env->pc;
#else
info->value->arch = CPU_INFO_ARCH_OTHER;
value->arch = CPU_INFO_ARCH_OTHER;
#endif
info->value->has_props = !!mc->cpu_index_to_instance_props;
if (info->value->has_props) {
value->has_props = !!mc->cpu_index_to_instance_props;
if (value->has_props) {
CpuInstanceProperties *props;
props = g_malloc0(sizeof(*props));
*props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
info->value->props = props;
value->props = props;
}

/* XXX: waiting for the qapi to support GSList */
if (!cur_item) {
head = cur_item = info;
} else {
cur_item->next = info;
cur_item = info;
}
QAPI_LIST_APPEND(tail, value);
}

return head;
Expand Down Expand Up @@ -170,39 +163,33 @@ CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
{
MachineState *ms = MACHINE(qdev_get_machine());
MachineClass *mc = MACHINE_GET_CLASS(ms);
CpuInfoFastList *head = NULL, *cur_item = NULL;
CpuInfoFastList *head = NULL, **tail = &head;
SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME,
-1, &error_abort);
CPUState *cpu;

CPU_FOREACH(cpu) {
CpuInfoFastList *info = g_malloc0(sizeof(*info));
info->value = g_malloc0(sizeof(*info->value));
CpuInfoFast *value = g_malloc0(sizeof(*value));

info->value->cpu_index = cpu->cpu_index;
info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
info->value->thread_id = cpu->thread_id;
value->cpu_index = cpu->cpu_index;
value->qom_path = object_get_canonical_path(OBJECT(cpu));
value->thread_id = cpu->thread_id;

info->value->has_props = !!mc->cpu_index_to_instance_props;
if (info->value->has_props) {
value->has_props = !!mc->cpu_index_to_instance_props;
if (value->has_props) {
CpuInstanceProperties *props;
props = g_malloc0(sizeof(*props));
*props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
info->value->props = props;
value->props = props;
}

info->value->arch = sysemu_target_to_cpuinfo_arch(target);
info->value->target = target;
value->arch = sysemu_target_to_cpuinfo_arch(target);
value->target = target;
if (target == SYS_EMU_TARGET_S390X) {
cpustate_to_cpuinfo_s390(&info->value->u.s390x, cpu);
cpustate_to_cpuinfo_s390(&value->u.s390x, cpu);
}

if (!cur_item) {
head = cur_item = info;
} else {
cur_item->next = info;
cur_item = info;
}
QAPI_LIST_APPEND(tail, value);
}

return head;
Expand Down
12 changes: 2 additions & 10 deletions hw/mem/memory-device.c
Expand Up @@ -199,27 +199,19 @@ static uint64_t memory_device_get_free_addr(MachineState *ms,
MemoryDeviceInfoList *qmp_memory_device_list(void)
{
GSList *devices = NULL, *item;
MemoryDeviceInfoList *list = NULL, *prev = NULL;
MemoryDeviceInfoList *list = NULL, **tail = &list;

object_child_foreach(qdev_get_machine(), memory_device_build_list,
&devices);

for (item = devices; item; item = g_slist_next(item)) {
const MemoryDeviceState *md = MEMORY_DEVICE(item->data);
const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(item->data);
MemoryDeviceInfoList *elem = g_new0(MemoryDeviceInfoList, 1);
MemoryDeviceInfo *info = g_new0(MemoryDeviceInfo, 1);

mdc->fill_device_info(md, info);

elem->value = info;
elem->next = NULL;
if (prev) {
prev->next = elem;
} else {
list = elem;
}
prev = elem;
QAPI_LIST_APPEND(tail, info);
}

g_slist_free(devices);
Expand Down
60 changes: 18 additions & 42 deletions hw/pci/pci.c
Expand Up @@ -1683,41 +1683,34 @@ static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num);

static PciMemoryRegionList *qmp_query_pci_regions(const PCIDevice *dev)
{
PciMemoryRegionList *head = NULL, *cur_item = NULL;
PciMemoryRegionList *head = NULL, **tail = &head;
int i;

for (i = 0; i < PCI_NUM_REGIONS; i++) {
const PCIIORegion *r = &dev->io_regions[i];
PciMemoryRegionList *region;
PciMemoryRegion *region;

if (!r->size) {
continue;
}

region = g_malloc0(sizeof(*region));
region->value = g_malloc0(sizeof(*region->value));

if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
region->value->type = g_strdup("io");
region->type = g_strdup("io");
} else {
region->value->type = g_strdup("memory");
region->value->has_prefetch = true;
region->value->prefetch = !!(r->type & PCI_BASE_ADDRESS_MEM_PREFETCH);
region->value->has_mem_type_64 = true;
region->value->mem_type_64 = !!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64);
region->type = g_strdup("memory");
region->has_prefetch = true;
region->prefetch = !!(r->type & PCI_BASE_ADDRESS_MEM_PREFETCH);
region->has_mem_type_64 = true;
region->mem_type_64 = !!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64);
}

region->value->bar = i;
region->value->address = r->addr;
region->value->size = r->size;
region->bar = i;
region->address = r->addr;
region->size = r->size;

/* XXX: waiting for the qapi to support GSList */
if (!cur_item) {
head = cur_item = region;
} else {
cur_item->next = region;
cur_item = region;
}
QAPI_LIST_APPEND(tail, region);
}

return head;
Expand Down Expand Up @@ -1814,23 +1807,14 @@ static PciDeviceInfo *qmp_query_pci_device(PCIDevice *dev, PCIBus *bus,

static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num)
{
PciDeviceInfoList *info, *head = NULL, *cur_item = NULL;
PciDeviceInfoList *head = NULL, **tail = &head;
PCIDevice *dev;
int devfn;

for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
dev = bus->devices[devfn];
if (dev) {
info = g_malloc0(sizeof(*info));
info->value = qmp_query_pci_device(dev, bus, bus_num);

/* XXX: waiting for the qapi to support GSList */
if (!cur_item) {
head = cur_item = info;
} else {
cur_item->next = info;
cur_item = info;
}
QAPI_LIST_APPEND(tail, qmp_query_pci_device(dev, bus, bus_num));
}
}

Expand All @@ -1853,21 +1837,13 @@ static PciInfo *qmp_query_pci_bus(PCIBus *bus, int bus_num)

PciInfoList *qmp_query_pci(Error **errp)
{
PciInfoList *info, *head = NULL, *cur_item = NULL;
PciInfoList *head = NULL, **tail = &head;
PCIHostState *host_bridge;

QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
info = g_malloc0(sizeof(*info));
info->value = qmp_query_pci_bus(host_bridge->bus,
pci_bus_num(host_bridge->bus));

/* XXX: waiting for the qapi to support GSList */
if (!cur_item) {
head = cur_item = info;
} else {
cur_item->next = info;
cur_item = info;
}
QAPI_LIST_APPEND(tail,
qmp_query_pci_bus(host_bridge->bus,
pci_bus_num(host_bridge->bus)));
}

return head;
Expand Down

0 comments on commit 95b3a8c

Please sign in to comment.