Use G_N_ELEMENTS() more

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>
This commit is contained in:
Michal Privoznik 2023-03-13 10:43:50 +01:00
parent b07a1f40c7
commit 7db8373e08
6 changed files with 14 additions and 10 deletions

View File

@ -7,6 +7,8 @@
#include <libvirt/libvirt.h> #include <libvirt/libvirt.h>
#include <libvirt/virterror.h> #include <libvirt/virterror.h>
#define G_N_ELEMENTS(Array) (sizeof(Array) / sizeof(*(Array)))
static void static void
showError(virConnectPtr conn) showError(virConnectPtr conn)
{ {
@ -205,7 +207,7 @@ static int credTypes[] = {
/* The auth struct that will be passed to virConnectOpenAuth */ /* The auth struct that will be passed to virConnectOpenAuth */
static virConnectAuth auth = { static virConnectAuth auth = {
credTypes, credTypes,
sizeof(credTypes) / sizeof(int), G_N_ELEMENTS(credTypes),
authCallback, authCallback,
NULL, /* cbdata will be initialized in main */ NULL, /* cbdata will be initialized in main */
}; };

View File

@ -192,7 +192,7 @@ static int virConnectCredTypeDefault[] = {
static virConnectAuth virConnectAuthDefault = { static virConnectAuth virConnectAuthDefault = {
virConnectCredTypeDefault, virConnectCredTypeDefault,
sizeof(virConnectCredTypeDefault)/sizeof(int), G_N_ELEMENTS(virConnectCredTypeDefault),
virConnectAuthCallbackDefault, virConnectAuthCallbackDefault,
NULL, NULL,
}; };

View File

@ -271,7 +271,7 @@ test4c(const void *data G_GNUC_UNUSED)
if (virBitmapNextSetBit(bitmap, i) != -1) if (virBitmapNextSetBit(bitmap, i) != -1)
return -1; return -1;
j = sizeof(bitsPos)/sizeof(int) - 1; j = G_N_ELEMENTS(bitsPos) - 1;
if (virBitmapLastSetBit(bitmap) != bitsPos[j]) if (virBitmapLastSetBit(bitmap) != bitsPos[j])
return -1; return -1;
@ -328,7 +328,7 @@ test5(const void *v G_GNUC_UNUSED)
i = 0; i = 0;
j = -1; j = -1;
while (i < sizeof(bits)/sizeof(int) && while (i < G_N_ELEMENTS(bits) &&
(j = virBitmapNextSetBit(bitmap, j)) >= 0) { (j = virBitmapNextSetBit(bitmap, j)) >= 0) {
if (j != bits[i++]) if (j != bits[i++])
return -1; return -1;

View 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 }, .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, rc = virNetDevSetVfMac(testCases[i].ifname, testCases[i].vf_num,
&testCases[i].macaddr, &testCases[i].allow_retry); &testCases[i].macaddr, &testCases[i].allow_retry);
if (rc != testCases[i].rc) { if (rc != testCases[i].rc) {
@ -252,14 +252,14 @@ testVirNetDevSetVfVlan(const void *opaque G_GNUC_UNUSED)
{ .ifname = "fakeiface-ok", .vf_num = 1, .rc = 0 }, { .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); rc = virNetDevSetVfVlan(testCases[i].ifname, testCases[i].vf_num, &testCases[i].vlan_id);
if (rc != testCases[i].rc) { if (rc != testCases[i].rc) {
return -1; 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); rc = virNetDevSetVfVlan(nullVLANTestCases[i].ifname, nullVLANTestCases[i].vf_num, NULL);
if (rc != nullVLANTestCases[i].rc) { if (rc != nullVLANTestCases[i].rc) {
return -1; return -1;
@ -292,7 +292,7 @@ testVirNetDevSetVfConfig(const void *opaque G_GNUC_UNUSED)
{ .ifname = "fakeiface-nomacerror-novlanerror", .rc = 0 }, { .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); rc = virNetDevSetVfConfig(testCases[i].ifname, vfNum, &mac, &vlanid, allowRetry);
if (rc != testCases[i].rc) { if (rc != testCases[i].rc) {
return -1; return -1;

View File

@ -53,6 +53,7 @@
#define LIBVIRT_ALIGN(x) (((x) + __SIZEOF_POINTER__ - 1) & ~(__SIZEOF_POINTER__ - 1)) #define LIBVIRT_ALIGN(x) (((x) + __SIZEOF_POINTER__ - 1) & ~(__SIZEOF_POINTER__ - 1))
#define FAMILY_ADDRESS_SIZE(family) ((family) == AF_INET6 ? 16 : 4) #define FAMILY_ADDRESS_SIZE(family) ((family) == AF_INET6 ? 16 : 4)
#define G_N_ELEMENTS(Array) (sizeof(Array) / sizeof(*(Array)))
static int static int
leaseAddressSorter(const void *a, leaseAddressSorter(const void *a,
@ -595,7 +596,7 @@ nss_module_register(const char *name __attribute__((unused)),
unsigned int *size, unsigned int *size,
nss_module_unregister_fn *unregister) nss_module_unregister_fn *unregister)
{ {
*size = sizeof(methods) / sizeof(methods[0]); *size = G_N_ELEMENTS(methods);
*unregister = NULL; *unregister = NULL;
return methods; return methods;
} }

View File

@ -30,6 +30,7 @@
#include "configmake.h" #include "configmake.h"
#define G_N_ELEMENTS(Array) (sizeof(Array) / sizeof(*(Array)))
#define VIR_INT64_STR_BUFLEN 21 #define VIR_INT64_STR_BUFLEN 21
int main(int argc, char **argv) { int main(int argc, char **argv) {
@ -69,7 +70,7 @@ int main(int argc, char **argv) {
newargv[nargs++] = gidstr; newargv[nargs++] = gidstr;
newargv[nargs++] = NULL; newargv[nargs++] = NULL;
assert(nargs <= (sizeof(newargv)/sizeof(newargv[0]))); assert(nargs <= G_N_ELEMENTS(newargv));
if (term && if (term &&
asprintf(&(newenv[0]), "TERM=%s", term) < 0) { asprintf(&(newenv[0]), "TERM=%s", term) < 0) {