]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Use G_N_ELEMENTS() more
authorMichal Privoznik <mprivozn@redhat.com>
Mon, 13 Mar 2023 09:43:50 +0000 (10:43 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 13 Mar 2023 12:29:07 +0000 (13:29 +0100)
In a few places we still use the good old:

  sizeof(var) / sizeof(var[0])
  sizeof(var) / sizeof(int)

The G_N_ELEMENTS() macro is preferred though. In a few places we
don't link with glib, so provide the macro definition.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Kristina Hanicova <khanicov@redhat.com>
examples/c/misc/openauth.c
src/libvirt.c
tests/virbitmaptest.c
tests/virnetdevtest.c
tools/nss/libvirt_nss.c
tools/virt-login-shell.c

index 4c9dd34be3fa112c52ad395e103bcf6185e9c002..9c6a1323c66d76a3b2164c04509b29434edf5b3b 100644 (file)
@@ -7,6 +7,8 @@
 #include <libvirt/libvirt.h>
 #include <libvirt/virterror.h>
 
+#define G_N_ELEMENTS(Array) (sizeof(Array) / sizeof(*(Array)))
+
 static void
 showError(virConnectPtr conn)
 {
@@ -205,7 +207,7 @@ static int credTypes[] = {
 /* The auth struct that will be passed to virConnectOpenAuth */
 static virConnectAuth auth = {
     credTypes,
-    sizeof(credTypes) / sizeof(int),
+    G_N_ELEMENTS(credTypes),
     authCallback,
     NULL, /* cbdata will be initialized in main */
 };
index 748f2d8ba0e830bdc169ea97017308a950a92de0..fac074b73a622391a5103ccbd029f84fb395541b 100644 (file)
@@ -192,7 +192,7 @@ static int virConnectCredTypeDefault[] = {
 
 static virConnectAuth virConnectAuthDefault = {
     virConnectCredTypeDefault,
-    sizeof(virConnectCredTypeDefault)/sizeof(int),
+    G_N_ELEMENTS(virConnectCredTypeDefault),
     virConnectAuthCallbackDefault,
     NULL,
 };
index 89dc702da24278e305621a34dadb78fac7bfab1c..f4fadb7c8acbd154cda91fb901b7658ae490088e 100644 (file)
@@ -271,7 +271,7 @@ test4c(const void *data G_GNUC_UNUSED)
     if (virBitmapNextSetBit(bitmap, i) != -1)
         return -1;
 
-    j = sizeof(bitsPos)/sizeof(int) - 1;
+    j = G_N_ELEMENTS(bitsPos) - 1;
 
     if (virBitmapLastSetBit(bitmap) != bitsPos[j])
         return -1;
@@ -328,7 +328,7 @@ test5(const void *v G_GNUC_UNUSED)
 
     i = 0;
     j = -1;
-    while (i < sizeof(bits)/sizeof(int) &&
+    while (i < G_N_ELEMENTS(bits) &&
            (j = virBitmapNextSetBit(bitmap, j)) >= 0) {
         if (j != bits[i++])
             return -1;
index aec84c6c35f1f8d560083fe1ce5b9b3cd1d4c751..42f1a74ee9ad66f40150920670a4f71518c9d610 100644 (file)
@@ -186,7 +186,7 @@ testVirNetDevSetVfMac(const void *opaque G_GNUC_UNUSED)
           .macaddr = { .addr = { 0, 0, 0, 7, 7, 7 } }, .allow_retry = true, .rc = -EINVAL },
     };
 
-    for (i = 0; i < sizeof(testCases) / sizeof(struct testCase); ++i) {
+    for (i = 0; i < G_N_ELEMENTS(testCases); ++i) {
        rc = virNetDevSetVfMac(testCases[i].ifname, testCases[i].vf_num,
                               &testCases[i].macaddr, &testCases[i].allow_retry);
        if (rc != testCases[i].rc) {
@@ -252,14 +252,14 @@ testVirNetDevSetVfVlan(const void *opaque G_GNUC_UNUSED)
         { .ifname = "fakeiface-ok", .vf_num = 1, .rc = 0 },
     };
 
-    for (i = 0; i < sizeof(testCases) / sizeof(struct testCase); ++i) {
+    for (i = 0; i < G_N_ELEMENTS(testCases); ++i) {
        rc = virNetDevSetVfVlan(testCases[i].ifname, testCases[i].vf_num, &testCases[i].vlan_id);
        if (rc != testCases[i].rc) {
            return -1;
        }
     }
 
-    for (i = 0; i < sizeof(nullVLANTestCases) / sizeof(struct nullVlanTestCase); ++i) {
+    for (i = 0; i < G_N_ELEMENTS(nullVLANTestCases); ++i) {
        rc = virNetDevSetVfVlan(nullVLANTestCases[i].ifname, nullVLANTestCases[i].vf_num, NULL);
        if (rc != nullVLANTestCases[i].rc) {
            return -1;
@@ -292,7 +292,7 @@ testVirNetDevSetVfConfig(const void *opaque G_GNUC_UNUSED)
         { .ifname = "fakeiface-nomacerror-novlanerror", .rc = 0 },
     };
 
-    for (i = 0; i < sizeof(testCases) / sizeof(struct testCase); ++i) {
+    for (i = 0; i < G_N_ELEMENTS(testCases); ++i) {
        rc = virNetDevSetVfConfig(testCases[i].ifname, vfNum, &mac, &vlanid, allowRetry);
        if (rc != testCases[i].rc) {
            return -1;
index a6e8e4b12b48222d0e426338b0b554667e77d502..ec7763224b76829c9b09c30dd1fabe43b982bf4d 100644 (file)
@@ -53,6 +53,7 @@
 
 #define LIBVIRT_ALIGN(x) (((x) + __SIZEOF_POINTER__ - 1) & ~(__SIZEOF_POINTER__ - 1))
 #define FAMILY_ADDRESS_SIZE(family) ((family) == AF_INET6 ? 16 : 4)
+#define G_N_ELEMENTS(Array) (sizeof(Array) / sizeof(*(Array)))
 
 static int
 leaseAddressSorter(const void *a,
@@ -595,7 +596,7 @@ nss_module_register(const char *name __attribute__((unused)),
                     unsigned int *size,
                     nss_module_unregister_fn *unregister)
 {
-    *size = sizeof(methods) / sizeof(methods[0]);
+    *size = G_N_ELEMENTS(methods);
     *unregister = NULL;
     return methods;
 }
index 25449f972dea52dc24fae94cd89c43a24b64ba8e..8a67e2223c104e7c69b4240d039c100d83af7200 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "configmake.h"
 
+#define G_N_ELEMENTS(Array) (sizeof(Array) / sizeof(*(Array)))
 #define VIR_INT64_STR_BUFLEN 21
 
 int main(int argc, char **argv) {
@@ -69,7 +70,7 @@ int main(int argc, char **argv) {
     newargv[nargs++] = gidstr;
     newargv[nargs++] = NULL;
 
-    assert(nargs <= (sizeof(newargv)/sizeof(newargv[0])));
+    assert(nargs <= G_N_ELEMENTS(newargv));
 
     if (term &&
         asprintf(&(newenv[0]), "TERM=%s", term) < 0) {