]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
lib: Use G_N_ELEMENTS instead of sizeof()/sizeof()
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 2 Nov 2021 16:32:43 +0000 (17:32 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 3 Nov 2021 13:46:54 +0000 (14:46 +0100)
For statically declared arrays one can use G_N_ELEMENTS() instead
of explicit sizeof(array) / sizeof(item). I've noticed couple of
places where the latter was used.

I am not fixing every occurrence because we have some places
which do not use glib (examples and NSS module).

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Tim Wiederhake <twiederh@redhat.com>
src/libxl/libxl_capabilities.c
tests/virpcimock.c
tests/virpcivpdtest.c

index e03b6fd3c3d6e85959b8275f0b3896eb0411bcd9..6263b5c8b58f759b7cdba4619d4b1f6f648b13e5 100644 (file)
@@ -380,7 +380,7 @@ libxlCapsInitGuests(libxl_ctx *ctx, virCaps *caps)
      * we "own" the buffer.  Parse out the features from each token.
      */
     for (str = ver_info->capabilities, nr_guest_archs = 0;
-         nr_guest_archs < sizeof(guest_archs) / sizeof(guest_archs[0])
+         nr_guest_archs < G_N_ELEMENTS(guest_archs)
                  && (token = strtok_r(str, " ", &saveptr)) != NULL;
          str = NULL) {
         if (g_regex_match(regex, token, 0, &info)) {
index f65ae7c0c51e48a9c6b39e953f97ed7a489c95b9..77d46f0952280eaf1279caafdbd68f3dea6bea45 100644 (file)
@@ -965,7 +965,7 @@ init_env(void)
     };
     struct pciVPD exampleVPD = {
         .data = fullVPDExampleData,
-        .vpd_len = sizeof(fullVPDExampleData) / sizeof(fullVPDExampleData[0]),
+        .vpd_len = G_N_ELEMENTS(fullVPDExampleData),
     };
 
     if (!(fakerootdir = getenv("LIBVIRT_FAKE_ROOT_DIR")))
index add1c74c046158c8766db7f0c845a8853c58bc7e..284350fe2904d37fc43d21444cbcca2a587ad9bb 100644 (file)
@@ -76,9 +76,9 @@ testPCIVPDResourceBasic(const void *data G_GNUC_UNUSED)
         {.keyword = "CP", .value = "42", .actual = NULL},
         {.keyword = "EX", .value = "42", .actual = NULL},
     };
-    size_t numROCases = sizeof(readOnlyCases) / sizeof(TestPCIVPDKeywordValue);
-    size_t numRWCases = sizeof(readWriteCases) / sizeof(TestPCIVPDKeywordValue);
-    size_t numUnsupportedCases = sizeof(unsupportedFieldCases) / sizeof(TestPCIVPDKeywordValue);
+    size_t numROCases = G_N_ELEMENTS(readOnlyCases);
+    size_t numRWCases = G_N_ELEMENTS(readWriteCases);
+    size_t numUnsupportedCases = G_N_ELEMENTS(unsupportedFieldCases);
     g_autoptr(virPCIVPDResource) res = g_new0(virPCIVPDResource, 1);
     virPCIVPDResourceCustom *custom = NULL;
 
@@ -328,7 +328,7 @@ testPCIVPDIsValidTextValue(const void *data G_GNUC_UNUSED)
         /* The first and last code points are outside ASCII (multi-byte in UTF-8). */
         {"гbl🐧", false},
     };
-    for (i = 0; i < sizeof(textValueCases) / sizeof(textValueCases[0]); ++i) {
+    for (i = 0; i < G_N_ELEMENTS(textValueCases); ++i) {
         if (virPCIVPDResourceIsValidTextValue(textValueCases[i].keyword) !=
             textValueCases[i].expected)
             return -1;
@@ -385,7 +385,7 @@ testPCIVPDGetFieldValueFormat(const void *data G_GNUC_UNUSED)
         /* Many letters. */
         {"EXAMPLE", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
     };
-    for (i = 0; i < sizeof(valueFormatCases) / sizeof(valueFormatCases[0]); ++i) {
+    for (i = 0; i < G_N_ELEMENTS(valueFormatCases); ++i) {
         if (virPCIVPDResourceGetFieldValueFormat(valueFormatCases[i].keyword) !=
             valueFormatCases[i].expected)
             return -1;
@@ -442,7 +442,7 @@ testVirPCIVPDReadVPDBytes(const void *opaque G_GNUC_UNUSED)
         VPD_R_FIELDS_EXAMPLE_HEADER, VPD_R_FIELDS_EXAMPLE_DATA,
         PCI_VPD_RESOURCE_END_VAL
     };
-    dataLen = sizeof(fullVPDExample) / sizeof(uint8_t) - 2;
+    dataLen = G_N_ELEMENTS(fullVPDExample) - 2;
     buf = g_malloc0(dataLen);
 
     fd = virCreateAnonymousFile(fullVPDExample, dataLen);
@@ -480,7 +480,7 @@ testVirPCIVPDParseVPDStringResource(const void *opaque G_GNUC_UNUSED)
         VPD_STRING_RESOURCE_EXAMPLE_DATA
     };
 
-    dataLen = sizeof(stringResExample) / sizeof(uint8_t);
+    dataLen = G_N_ELEMENTS(stringResExample);
     fd = virCreateAnonymousFile(stringResExample, dataLen);
     result = virPCIVPDParseVPDLargeResourceString(fd, 0, dataLen, &csum, res);
     VIR_FORCE_CLOSE(fd);
@@ -550,7 +550,7 @@ testVirPCIVPDParseFullVPD(const void *opaque G_GNUC_UNUSED)
         PCI_VPD_RESOURCE_END_VAL
     };
 
-    dataLen = sizeof(fullVPDExample) / sizeof(uint8_t);
+    dataLen = G_N_ELEMENTS(fullVPDExample);
     fd = virCreateAnonymousFile(fullVPDExample, dataLen);
     res = virPCIVPDParse(fd);
     VIR_FORCE_CLOSE(fd);
@@ -618,7 +618,7 @@ testVirPCIVPDParseZeroLengthRW(const void *opaque G_GNUC_UNUSED)
         PCI_VPD_RESOURCE_END_VAL
     };
 
-    dataLen = sizeof(fullVPDExample) / sizeof(uint8_t);
+    dataLen = G_N_ELEMENTS(fullVPDExample);
     fd = virCreateAnonymousFile(fullVPDExample, dataLen);
     res = virPCIVPDParse(fd);
     VIR_FORCE_CLOSE(fd);
@@ -668,7 +668,7 @@ testVirPCIVPDParseNoRW(const void *opaque G_GNUC_UNUSED)
         PCI_VPD_RESOURCE_END_VAL
     };
 
-    dataLen = sizeof(fullVPDExample) / sizeof(uint8_t);
+    dataLen = G_N_ELEMENTS(fullVPDExample);
     fd = virCreateAnonymousFile(fullVPDExample, dataLen);
     res = virPCIVPDParse(fd);
     VIR_FORCE_CLOSE(fd);
@@ -721,7 +721,7 @@ testVirPCIVPDParseFullVPDSkipInvalidKeywords(const void *opaque G_GNUC_UNUSED)
         PCI_VPD_RESOURCE_END_VAL
     };
 
-    dataLen = sizeof(fullVPDExample) / sizeof(uint8_t);
+    dataLen = G_N_ELEMENTS(fullVPDExample);
     fd = virCreateAnonymousFile(fullVPDExample, dataLen);
     res = virPCIVPDParse(fd);
     VIR_FORCE_CLOSE(fd);
@@ -774,7 +774,7 @@ testVirPCIVPDParseFullVPDSkipInvalidValues(const void *opaque G_GNUC_UNUSED)
         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 'R', 'W', 0x00, 0x78,
     };
 
-    dataLen = sizeof(fullVPDExample) / sizeof(uint8_t);
+    dataLen = G_N_ELEMENTS(fullVPDExample);
     fd = virCreateAnonymousFile(fullVPDExample, dataLen);
     res = virPCIVPDParse(fd);
     VIR_FORCE_CLOSE(fd);
@@ -950,7 +950,7 @@ testVirPCIVPDParseFullVPDInvalid(const void *opaque G_GNUC_UNUSED)
     do { \
         g_autoptr(virPCIVPDResource) res = NULL; \
         const uint8_t testCase[] = { invalidVPD }; \
-        dataLen = sizeof(testCase) / sizeof(uint8_t); \
+        dataLen = G_N_ELEMENTS(testCase); \
         fd = virCreateAnonymousFile(testCase, dataLen); \
         if ((res = virPCIVPDParse(fd))) { \
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \