diff options
author | Eric Blake <eblake@redhat.com> | 2020-11-12 19:13:37 -0600 |
---|---|---|
committer | Markus Armbruster <armbru@redhat.com> | 2020-12-19 10:20:14 +0100 |
commit | 54aa3de72ea2aaa2e903e7e879a4f3dda515a00e (patch) | |
tree | 5f1041c336d7a5064f338179022901ab56d8b5d1 /tests | |
parent | eaedde5255842e8add96bec127567e2a8b3be9a8 (diff) | |
download | qemu-54aa3de72ea2aaa2e903e7e879a4f3dda515a00e.zip qemu-54aa3de72ea2aaa2e903e7e879a4f3dda515a00e.tar.gz qemu-54aa3de72ea2aaa2e903e7e879a4f3dda515a00e.tar.bz2 |
qapi: Use QAPI_LIST_PREPEND() where possible
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 a8aa94b5f8 "qga: update
schema for guest-get-disks 'dependents' field" and commit a10b453a52
"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>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test-clone-visitor.c | 7 | ||||
-rw-r--r-- | tests/test-qobject-output-visitor.c | 48 | ||||
-rw-r--r-- | tests/test-visitor-serialization.c | 113 |
3 files changed, 38 insertions, 130 deletions
diff --git a/tests/test-clone-visitor.c b/tests/test-clone-visitor.c index 5e1e8b2..4944b3d 100644 --- a/tests/test-clone-visitor.c +++ b/tests/test-clone-visitor.c @@ -65,16 +65,13 @@ static void test_clone_alternate(void) static void test_clone_list_union(void) { - uint8List *src, *dst; + uint8List *src = NULL, *dst; uint8List *tmp = NULL; int i; /* Build list in reverse */ for (i = 10; i; i--) { - src = g_new0(uint8List, 1); - src->next = tmp; - src->value = i; - tmp = src; + QAPI_LIST_PREPEND(src, i); } dst = QAPI_CLONE(uint8List, src); diff --git a/tests/test-qobject-output-visitor.c b/tests/test-qobject-output-visitor.c index 1c856d9..b20ab8b 100644 --- a/tests/test-qobject-output-visitor.c +++ b/tests/test-qobject-output-visitor.c @@ -223,7 +223,8 @@ static void test_visitor_out_list(TestOutputVisitorData *data, const void *unused) { const char *value_str = "list value"; - TestStructList *p, *head = NULL; + TestStruct *value; + TestStructList *head = NULL; const int max_items = 10; bool value_bool = true; int value_int = 10; @@ -233,14 +234,12 @@ static void test_visitor_out_list(TestOutputVisitorData *data, /* Build the list in reverse order... */ for (i = 0; i < max_items; i++) { - p = g_malloc0(sizeof(*p)); - p->value = g_malloc0(sizeof(*p->value)); - p->value->integer = value_int + (max_items - i - 1); - p->value->boolean = value_bool; - p->value->string = g_strdup(value_str); - - p->next = head; - head = p; + value = g_malloc0(sizeof(*value)); + value->integer = value_int + (max_items - i - 1); + value->boolean = value_bool; + value->string = g_strdup(value_str); + + QAPI_LIST_PREPEND(head, value); } visit_type_TestStructList(data->ov, NULL, &head, &error_abort); @@ -270,26 +269,25 @@ static void test_visitor_out_list(TestOutputVisitorData *data, static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data, const void *unused) { - UserDefTwoList *p, *head = NULL; + UserDefTwo *value; + UserDefTwoList *head = NULL; const char string[] = "foo bar"; int i, max_count = 1024; for (i = 0; i < max_count; i++) { - p = g_malloc0(sizeof(*p)); - p->value = g_malloc0(sizeof(*p->value)); - - p->value->string0 = g_strdup(string); - p->value->dict1 = g_new0(UserDefTwoDict, 1); - p->value->dict1->string1 = g_strdup(string); - p->value->dict1->dict2 = g_new0(UserDefTwoDictDict, 1); - p->value->dict1->dict2->userdef = g_new0(UserDefOne, 1); - p->value->dict1->dict2->userdef->string = g_strdup(string); - p->value->dict1->dict2->userdef->integer = 42; - p->value->dict1->dict2->string = g_strdup(string); - p->value->dict1->has_dict3 = false; - - p->next = head; - head = p; + value = g_malloc0(sizeof(*value)); + + value->string0 = g_strdup(string); + value->dict1 = g_new0(UserDefTwoDict, 1); + value->dict1->string1 = g_strdup(string); + value->dict1->dict2 = g_new0(UserDefTwoDictDict, 1); + value->dict1->dict2->userdef = g_new0(UserDefOne, 1); + value->dict1->dict2->userdef->string = g_strdup(string); + value->dict1->dict2->userdef->integer = 42; + value->dict1->dict2->string = g_strdup(string); + value->dict1->has_dict3 = false; + + QAPI_LIST_PREPEND(head, value); } qapi_free_UserDefTwoList(head); diff --git a/tests/test-visitor-serialization.c b/tests/test-visitor-serialization.c index 1c5a8b9..12275e5 100644 --- a/tests/test-visitor-serialization.c +++ b/tests/test-visitor-serialization.c @@ -351,135 +351,51 @@ static void test_primitive_lists(gconstpointer opaque) for (i = 0; i < 32; i++) { switch (pl.type) { case PTYPE_STRING: { - strList *tmp = g_new0(strList, 1); - tmp->value = g_strdup(pt->value.string); - if (pl.value.strings == NULL) { - pl.value.strings = tmp; - } else { - tmp->next = pl.value.strings; - pl.value.strings = tmp; - } + QAPI_LIST_PREPEND(pl.value.strings, g_strdup(pt->value.string)); break; } case PTYPE_INTEGER: { - intList *tmp = g_new0(intList, 1); - tmp->value = pt->value.integer; - if (pl.value.integers == NULL) { - pl.value.integers = tmp; - } else { - tmp->next = pl.value.integers; - pl.value.integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.integers, pt->value.integer); break; } case PTYPE_S8: { - int8List *tmp = g_new0(int8List, 1); - tmp->value = pt->value.s8; - if (pl.value.s8_integers == NULL) { - pl.value.s8_integers = tmp; - } else { - tmp->next = pl.value.s8_integers; - pl.value.s8_integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.s8_integers, pt->value.s8); break; } case PTYPE_S16: { - int16List *tmp = g_new0(int16List, 1); - tmp->value = pt->value.s16; - if (pl.value.s16_integers == NULL) { - pl.value.s16_integers = tmp; - } else { - tmp->next = pl.value.s16_integers; - pl.value.s16_integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.s16_integers, pt->value.s16); break; } case PTYPE_S32: { - int32List *tmp = g_new0(int32List, 1); - tmp->value = pt->value.s32; - if (pl.value.s32_integers == NULL) { - pl.value.s32_integers = tmp; - } else { - tmp->next = pl.value.s32_integers; - pl.value.s32_integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.s32_integers, pt->value.s32); break; } case PTYPE_S64: { - int64List *tmp = g_new0(int64List, 1); - tmp->value = pt->value.s64; - if (pl.value.s64_integers == NULL) { - pl.value.s64_integers = tmp; - } else { - tmp->next = pl.value.s64_integers; - pl.value.s64_integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.s64_integers, pt->value.s64); break; } case PTYPE_U8: { - uint8List *tmp = g_new0(uint8List, 1); - tmp->value = pt->value.u8; - if (pl.value.u8_integers == NULL) { - pl.value.u8_integers = tmp; - } else { - tmp->next = pl.value.u8_integers; - pl.value.u8_integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.u8_integers, pt->value.u8); break; } case PTYPE_U16: { - uint16List *tmp = g_new0(uint16List, 1); - tmp->value = pt->value.u16; - if (pl.value.u16_integers == NULL) { - pl.value.u16_integers = tmp; - } else { - tmp->next = pl.value.u16_integers; - pl.value.u16_integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.u16_integers, pt->value.u16); break; } case PTYPE_U32: { - uint32List *tmp = g_new0(uint32List, 1); - tmp->value = pt->value.u32; - if (pl.value.u32_integers == NULL) { - pl.value.u32_integers = tmp; - } else { - tmp->next = pl.value.u32_integers; - pl.value.u32_integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.u32_integers, pt->value.u32); break; } case PTYPE_U64: { - uint64List *tmp = g_new0(uint64List, 1); - tmp->value = pt->value.u64; - if (pl.value.u64_integers == NULL) { - pl.value.u64_integers = tmp; - } else { - tmp->next = pl.value.u64_integers; - pl.value.u64_integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.u64_integers, pt->value.u64); break; } case PTYPE_NUMBER: { - numberList *tmp = g_new0(numberList, 1); - tmp->value = pt->value.number; - if (pl.value.numbers == NULL) { - pl.value.numbers = tmp; - } else { - tmp->next = pl.value.numbers; - pl.value.numbers = tmp; - } + QAPI_LIST_PREPEND(pl.value.numbers, pt->value.number); break; } case PTYPE_BOOLEAN: { - boolList *tmp = g_new0(boolList, 1); - tmp->value = pt->value.boolean; - if (pl.value.booleans == NULL) { - pl.value.booleans = tmp; - } else { - tmp->next = pl.value.booleans; - pl.value.booleans = tmp; - } + QAPI_LIST_PREPEND(pl.value.booleans, pt->value.boolean); break; } default: @@ -704,10 +620,7 @@ static void test_nested_struct_list(gconstpointer opaque) int i = 0; for (i = 0; i < 8; i++) { - tmp = g_new0(UserDefTwoList, 1); - tmp->value = nested_struct_create(); - tmp->next = listp; - listp = tmp; + QAPI_LIST_PREPEND(listp, nested_struct_create()); } ops->serialize(listp, &serialize_data, visit_nested_struct_list, |