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(a)redhat.com>
---
examples/c/misc/openauth.c | 4 +++-
src/libvirt.c | 2 +-
tests/virbitmaptest.c | 4 ++--
tests/virnetdevtest.c | 8 ++++----
tools/nss/libvirt_nss.c | 3 ++-
tools/virt-login-shell.c | 3 ++-
6 files changed, 14 insertions(+), 10 deletions(-)
diff --git a/examples/c/misc/openauth.c b/examples/c/misc/openauth.c
index 4c9dd34be3..9c6a1323c6 100644
--- a/examples/c/misc/openauth.c
+++ b/examples/c/misc/openauth.c
@@ -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 */
};
diff --git a/src/libvirt.c b/src/libvirt.c
index 748f2d8ba0..fac074b73a 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -192,7 +192,7 @@ static int virConnectCredTypeDefault[] = {
static virConnectAuth virConnectAuthDefault = {
virConnectCredTypeDefault,
- sizeof(virConnectCredTypeDefault)/sizeof(int),
+ G_N_ELEMENTS(virConnectCredTypeDefault),
virConnectAuthCallbackDefault,
NULL,
};
diff --git a/tests/virbitmaptest.c b/tests/virbitmaptest.c
index 89dc702da2..f4fadb7c8a 100644
--- a/tests/virbitmaptest.c
+++ b/tests/virbitmaptest.c
@@ -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;
diff --git a/tests/virnetdevtest.c b/tests/virnetdevtest.c
index aec84c6c35..42f1a74ee9 100644
--- a/tests/virnetdevtest.c
+++ b/tests/virnetdevtest.c
@@ -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;
diff --git a/tools/nss/libvirt_nss.c b/tools/nss/libvirt_nss.c
index a6e8e4b12b..ec7763224b 100644
--- a/tools/nss/libvirt_nss.c
+++ b/tools/nss/libvirt_nss.c
@@ -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;
}
diff --git a/tools/virt-login-shell.c b/tools/virt-login-shell.c
index 25449f972d..8a67e2223c 100644
--- a/tools/virt-login-shell.c
+++ b/tools/virt-login-shell.c
@@ -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) {
--
2.39.2