Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 29 Apr 2024 04:27:11 GMT
From:      Warner Losh <imp@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: fdbf7cab91ae - main - bluetooth: Use nitems(foo) instead of sizeof(foo)/sizeof(foo[0])
Message-ID:  <202404290427.43T4RBcE044442@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=fdbf7cab91ae9ae7ca87bd47acb7400813bd7160

commit fdbf7cab91ae9ae7ca87bd47acb7400813bd7160
Author:     Elyes Haouas <ehaouas@noos.fr>
AuthorDate: 2024-04-29 02:55:30 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-04-29 03:24:46 +0000

    bluetooth: Use nitems(foo) instead of sizeof(foo)/sizeof(foo[0])
    
    Pull Request: https://github.com/freebsd/freebsd-src/pull/888
    Signed-off-by: Elyes Haouas <ehaouas@noos.fr>
---
 usr.sbin/bluetooth/bthidcontrol/sdp.c    | 6 +++---
 usr.sbin/bluetooth/bthidd/kbd.c          | 5 +++--
 usr.sbin/bluetooth/hccontrol/hccontrol.c | 5 +++--
 usr.sbin/bluetooth/hccontrol/node.c      | 7 ++++---
 usr.sbin/bluetooth/sdpcontrol/search.c   | 5 +++--
 usr.sbin/bluetooth/sdpd/profile.c        | 5 +++--
 usr.sbin/bluetooth/sdpd/sar.c            | 3 ++-
 usr.sbin/bluetooth/sdpd/srr.c            | 3 ++-
 usr.sbin/bluetooth/sdpd/ssr.c            | 3 ++-
 9 files changed, 25 insertions(+), 17 deletions(-)

diff --git a/usr.sbin/bluetooth/bthidcontrol/sdp.c b/usr.sbin/bluetooth/bthidcontrol/sdp.c
index dc83229f080c..4754744a866a 100644
--- a/usr.sbin/bluetooth/bthidcontrol/sdp.c
+++ b/usr.sbin/bluetooth/bthidcontrol/sdp.c
@@ -30,7 +30,7 @@
  * $Id: sdp.c,v 1.3 2004/02/17 22:14:57 max Exp $
  */
 
-#include <sys/types.h>
+#include <sys/param.h>
 #include <sys/queue.h>
 #include <sys/sysctl.h>
 #define L2CAP_SOCKET_CHECKED
@@ -79,10 +79,10 @@ SDP_ATTR_RANGE(	0x0209,		/* HIDBatteryPower */
 SDP_ATTR_RANGE(	0x020d,		/* HIDNormallyConnectable */
 		0x020d)
 	};
-#define	nattrs	(sizeof(attrs)/sizeof(attrs[0]))
+#define	nattrs	nitems(attrs)
 
 static sdp_attr_t	values[8];
-#define	nvalues	(sizeof(values)/sizeof(values[0]))
+#define	nvalues	nitems(values)
 
 static uint8_t		buffer[nvalues][512];
 
diff --git a/usr.sbin/bluetooth/bthidd/kbd.c b/usr.sbin/bluetooth/bthidd/kbd.c
index c1616c7e4bd3..a807a9b27dec 100644
--- a/usr.sbin/bluetooth/bthidd/kbd.c
+++ b/usr.sbin/bluetooth/bthidd/kbd.c
@@ -35,6 +35,7 @@
 #include <sys/consio.h>
 #include <sys/ioctl.h>
 #include <sys/kbio.h>
+#include <sys/param.h>
 #include <sys/queue.h>
 #include <sys/wait.h>
 #include <assert.h>
@@ -319,7 +320,7 @@ static int32_t const	x[] =
 /* Right GUI                    E7 */ E0PREFIX|0x5C  /* E0 DC */
 };
 
-#define xsize	((int32_t)(sizeof(x)/sizeof(x[0])))
+#define xsize	(int32_t)nitems(x)
 
 /*
  * Get a max HID keycode (aligned)
@@ -437,7 +438,7 @@ kbd_write(bitstr_t *m, int32_t fb, int32_t make, int32_t fd)
 	int32_t	i, *b, *eob, n, buf[64];
 
 	b = buf;
-	eob = b + sizeof(buf)/sizeof(buf[0]);
+	eob = b + nitems(buf);
 	i = fb;
 
 	while (i < xsize) {
diff --git a/usr.sbin/bluetooth/hccontrol/hccontrol.c b/usr.sbin/bluetooth/hccontrol/hccontrol.c
index c1ce016e6246..bd63c9aff6ec 100644
--- a/usr.sbin/bluetooth/hccontrol/hccontrol.c
+++ b/usr.sbin/bluetooth/hccontrol/hccontrol.c
@@ -33,6 +33,7 @@
 #define L2CAP_SOCKET_CHECKED
 #include <bluetooth.h>
 #include <sys/ioctl.h>
+#include <sys/param.h>
 #include <sys/sysctl.h>
 #include <assert.h>
 #include <err.h>
@@ -153,11 +154,11 @@ socket_open(char const *node)
 			(void * const) &filter, sizeof(filter)) < 0)
 		err(4, "Could not setsockopt()");
 
-	size = (sizeof(mib)/sizeof(mib[0]));
+	size = nitems(mib);
 	if (sysctlnametomib("net.bluetooth.hci.command_timeout",mib,&size) < 0)
 		err(5, "Could not sysctlnametomib()");
 
-	if (sysctl(mib, sizeof(mib)/sizeof(mib[0]),
+	if (sysctl(mib, nitems(mib),
 			(void *) &timeout, &size, NULL, 0) < 0)
 		err(6, "Could not sysctl()");
 
diff --git a/usr.sbin/bluetooth/hccontrol/node.c b/usr.sbin/bluetooth/hccontrol/node.c
index b100900f527d..61b60ba95db5 100644
--- a/usr.sbin/bluetooth/hccontrol/node.c
+++ b/usr.sbin/bluetooth/hccontrol/node.c
@@ -31,6 +31,7 @@
  */
 
 #include <sys/ioctl.h>
+#include <sys/param.h>
 #define L2CAP_SOCKET_CHECKED
 #include <bluetooth.h>
 #include <errno.h>
@@ -160,7 +161,7 @@ hci_read_node_features(int s, int argc, char **argv)
 		return (ERROR);
 
 	fprintf(stdout, "Features: ");
-	for (n = 0; n < sizeof(r.features)/sizeof(r.features[0]); n++)
+	for (n = 0; n < nitems(r.features); n++)
 		fprintf(stdout, "%#02x ", r.features[n]);
 	fprintf(stdout, "\n%s\n", hci_features2str(r.features, 
 		buffer, sizeof(buffer)));
@@ -243,8 +244,8 @@ hci_read_neighbor_cache(int s, int argc, char **argv)
 
 	for (n = 0; n < r.num_entries; n++) {
 	        uint8_t addrtype = r.entries[n].addrtype;
-		if(addrtype >= sizeof(addrtype2str)/sizeof(addrtype2str[0]))
-			addrtype = sizeof(addrtype2str)/sizeof(addrtype2str[0]) - 1;
+		if(addrtype >= nitems(addrtype2str))
+			addrtype = nitems(addrtype2str) - 1;
 		fprintf(stdout, 
 "%1s %-17.17s " \
 "%02x %02x %02x %02x %02x %02x %02x %02x " \
diff --git a/usr.sbin/bluetooth/sdpcontrol/search.c b/usr.sbin/bluetooth/sdpcontrol/search.c
index 94c34828a949..2a4b2468c7f7 100644
--- a/usr.sbin/bluetooth/sdpcontrol/search.c
+++ b/usr.sbin/bluetooth/sdpcontrol/search.c
@@ -30,6 +30,7 @@
  * $Id: search.c,v 1.2 2003/09/08 17:35:15 max Exp $
  */
 
+#include <sys/param.h>
 #include <netinet/in.h>
 #define L2CAP_SOCKET_CHECKED
 #include <bluetooth.h>
@@ -51,7 +52,7 @@ static uint32_t	attrs[] =
 	SDP_ATTR_RANGE(	SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST,
 			SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST)
 };
-#define attrs_len	(sizeof(attrs)/sizeof(attrs[0]))
+#define attrs_len	nitems(attrs)
 
 /* Buffer for the attributes */
 #define NRECS	25	/* request this much records from the SDP server */
@@ -60,7 +61,7 @@ static uint8_t		buffer[NRECS * attrs_len][BSIZE];
 
 /* SDP attributes */
 static sdp_attr_t	values[NRECS * attrs_len];
-#define values_len	(sizeof(values)/sizeof(values[0]))
+#define values_len	nitems(values)
 
 /*
  * Print Service Class ID List
diff --git a/usr.sbin/bluetooth/sdpd/profile.c b/usr.sbin/bluetooth/sdpd/profile.c
index 3c6ada6871c7..e6d81f8ef4b1 100644
--- a/usr.sbin/bluetooth/sdpd/profile.c
+++ b/usr.sbin/bluetooth/sdpd/profile.c
@@ -32,6 +32,7 @@
  * $Id: profile.c,v 1.6 2004/01/13 19:31:54 max Exp $
  */
 
+#include <sys/param.h>
 #include <sys/queue.h>
 #define L2CAP_SOCKET_CHECKED
 #include <bluetooth.h>
@@ -77,7 +78,7 @@ profile_get_descriptor(uint16_t uuid)
 
 	int32_t			i;
 
-	for (i = 0; i < sizeof(profiles)/sizeof(profiles[0]); i++)
+	for (i = 0; i < nitems(profiles); i++)
 		if (profiles[i]->uuid == uuid)
 			return (profiles[i]);
 
@@ -444,7 +445,7 @@ bnep_profile_create_protocol_descriptor_list(
 	};
 
 	uint16_t	 i, psm, version = 0x0100,
-			 nptypes = sizeof(ptype)/sizeof(ptype[0]),
+			 nptypes = nitems(ptype),
 			 nptypes_size = nptypes * 3;
 
 	if (datalen != 2 || 18 + nptypes_size > 255 ||
diff --git a/usr.sbin/bluetooth/sdpd/sar.c b/usr.sbin/bluetooth/sdpd/sar.c
index 0470109ebc07..68193b567fd3 100644
--- a/usr.sbin/bluetooth/sdpd/sar.c
+++ b/usr.sbin/bluetooth/sdpd/sar.c
@@ -30,6 +30,7 @@
  * $Id: sar.c,v 1.2 2004/01/08 23:46:51 max Exp $
  */
 
+#include <sys/param.h>
 #include <sys/queue.h>
 #include <sys/uio.h>
 #include <netinet/in.h>
@@ -304,7 +305,7 @@ server_send_service_attribute_response(server_p srv, int32_t fd)
 	iov[3].iov_len = 1 + cs[0];
 
 	do {
-		size = writev(fd, (struct iovec const *) &iov, sizeof(iov)/sizeof(iov[0]));
+		size = writev(fd, (struct iovec const *) &iov, nitems(iov));
 	} while (size < 0 && errno == EINTR);
 
 	/* Check if we have sent (or failed to sent) last response chunk */
diff --git a/usr.sbin/bluetooth/sdpd/srr.c b/usr.sbin/bluetooth/sdpd/srr.c
index 8e887a4841de..b16eb25c9ea4 100644
--- a/usr.sbin/bluetooth/sdpd/srr.c
+++ b/usr.sbin/bluetooth/sdpd/srr.c
@@ -30,6 +30,7 @@
  * $Id: srr.c,v 1.1 2004/01/13 01:54:39 max Exp $
  */
 
+#include <sys/param.h>
 #include <sys/queue.h>
 #include <sys/uio.h>
 #include <netinet/in.h>
@@ -129,7 +130,7 @@ server_send_service_register_response(server_p srv, int32_t fd)
 	iov[1].iov_len = srv->fdidx[fd].rsp_size;
 
 	do {
-		size = writev(fd, (struct iovec const *) &iov, sizeof(iov)/sizeof(iov[0]));
+		size = writev(fd, (struct iovec const *) &iov, nitems(iov));
 	} while (size < 0 && errno == EINTR);
 
 	srv->fdidx[fd].rsp_cs = 0;
diff --git a/usr.sbin/bluetooth/sdpd/ssr.c b/usr.sbin/bluetooth/sdpd/ssr.c
index c6b3a1f65fe1..1a29dde97ee5 100644
--- a/usr.sbin/bluetooth/sdpd/ssr.c
+++ b/usr.sbin/bluetooth/sdpd/ssr.c
@@ -30,6 +30,7 @@
  * $Id: ssr.c,v 1.5 2004/01/13 01:54:39 max Exp $
  */
 
+#include <sys/param.h>
 #include <sys/queue.h>
 #include <sys/uio.h>
 #include <netinet/in.h>
@@ -269,7 +270,7 @@ server_send_service_search_response(server_p srv, int32_t fd)
 	iov[3].iov_len = 1 + cs[0];
 
 	do {
-		size = writev(fd, (struct iovec const *) &iov, sizeof(iov)/sizeof(iov[0]));
+		size = writev(fd, (struct iovec const *) &iov, nitems(iov));
 	} while (size < 0 && errno == EINTR);
 
 	/* Check if we have sent (or failed to sent) last response chunk */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202404290427.43T4RBcE044442>