Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 3 Sep 2014 20:45:33 -0700
From:      Garrett Cooper <yaneurabeya@gmail.com>
To:        freebsd-arch@freebsd.org
Cc:        Julio Merino <jmmv@freebsd.org>, "rpaulo@freebsd.org" <rpaulo@freebsd.org>
Subject:   [RFC] Add __arraycount from NetBSD to sys/cdefs.h
Message-ID:  <CAGHfRMBMPra5YXDn0e83dpVxwnarg3DL8o31xr7DhWv%2BVXskTg@mail.gmail.com>

index | next in thread | raw e-mail

[-- Attachment #1 --]
Hi all,
    In order to ease porting code and reduce divergence with NetBSD
when importing code (a large chunk of which for me are tests), I would
like to move nitems to sys/cdefs.h and alias __arraycount to nitems.
    Here's the __arraycount #define in lib/libnetbsd/sys/cdefs.h:

44 /*
45  * Return the number of elements in a statically-allocated array,
46  * __x.
47  */
48 #define __arraycount(__x)       (sizeof(__x) / sizeof(__x[0]))

    Here's the nitems #define in sys/sys/param.h:

277 #define nitems(x)       (sizeof((x)) / sizeof((x)[0]))

    sys/cdefs.h gets pulled in automatically with sys/param.h, so
anything using nitems will continue to function like before (see below
for more details). I've attached a patch which addresses all hardcoded
definitions in the tree added by FreeBSD developers.
    If there aren't any major concerns with my proposed change, I'll
put it up for review on Phabricator.
Thank you!
-Garrett

$ cat cdefs_pound_define.c
#include <sys/param.h>

#ifdef _SYS_CDEFS_H_
#warning "sys/cdefs.h has been included"
#endif
$ cc -c cdefs_pound_define.c
cdefs_pound_define.c:4:2: warning: "sys/cdefs.h has been included" [-W#warnings]
#warning "sys/cdefs.h has been included"
 ^
1 warning generated.
$ cc -D_KERNEL -c cdefs_pound_define.c
cdefs_pound_define.c:4:2: warning: "sys/cdefs.h has been included" [-W#warnings]
#warning "sys/cdefs.h has been included"
 ^
1 warning generated.
$ gcc -c cdefs_pound_define.c
cdefs_pound_define.c:4:2: warning: #warning "sys/cdefs.h has been included"
$ gcc -D_KERNEL -c cdefs_pound_define.c
cdefs_pound_define.c:4:2: warning: #warning "sys/cdefs.h has been included"

[-- Attachment #2 --]
From 6f91e006496118af4b18f02aed202a1c850ea3d0 Mon Sep 17 00:00:00 2001
From: Garrett Cooper <yanegomi@gmail.com>
Date: Thu, 4 Sep 2014 02:51:39 -0700
Subject: [PATCH] Make __arraycount macros from NetBSD to ease porting from
 NetBSD

1. Move nitems from sys/param.h to sys/cdefs.h
2. Alias __arraycount to nitems
3. Garbage collect all ad hoc definitions in the tree that aren't
   protected by #ifndef __arraycount not provided by a third-party source

Sponsored by: EMC / Isilon Storage Division
---
 contrib/libc-vis/unvis.c           | 6 ------
 lib/libnetbsd/sys/cdefs.h          | 6 ------
 sys/kern/stack_protector.c         | 1 -
 sys/sys/cdefs.h                    | 3 +++
 sys/sys/param.h                    | 1 -
 usr.sbin/bluetooth/btpand/btpand.h | 4 ----
 6 files changed, 3 insertions(+), 18 deletions(-)

diff --git a/contrib/libc-vis/unvis.c b/contrib/libc-vis/unvis.c
index 9cf112c..83ff2a0 100644
--- a/contrib/libc-vis/unvis.c
+++ b/contrib/libc-vis/unvis.c
@@ -51,12 +51,6 @@ __FBSDID("$FreeBSD$");
 
 #define	_DIAGASSERT(x)	assert(x)
 
-/*
- * Return the number of elements in a statically-allocated array,
- * __x.
- */
-#define	__arraycount(__x)	(sizeof(__x) / sizeof(__x[0]))
-
 #ifdef __weak_alias
 __weak_alias(strnunvisx,_strnunvisx)
 #endif
diff --git a/lib/libnetbsd/sys/cdefs.h b/lib/libnetbsd/sys/cdefs.h
index b3d52c9..fb5367f 100644
--- a/lib/libnetbsd/sys/cdefs.h
+++ b/lib/libnetbsd/sys/cdefs.h
@@ -41,12 +41,6 @@
 #define __dead
 #endif
 
-/*
- * Return the number of elements in a statically-allocated array,
- * __x.
- */
-#define	__arraycount(__x)	(sizeof(__x) / sizeof(__x[0]))
-
 #define	___STRING(x)	__STRING(x)
 #define	__STRING(x)	#x
 
diff --git a/sys/kern/stack_protector.c b/sys/kern/stack_protector.c
index b5f9973..63acb90 100644
--- a/sys/kern/stack_protector.c
+++ b/sys/kern/stack_protector.c
@@ -17,7 +17,6 @@ __stack_chk_fail(void)
 	panic("stack overflow detected; backtrace may be corrupted");
 }
 
-#define __arraycount(__x)	(sizeof(__x) / sizeof(__x[0]))
 static void
 __stack_chk_init(void *dummy __unused)
 {
diff --git a/sys/sys/cdefs.h b/sys/sys/cdefs.h
index 4c4c2af..befc59d 100644
--- a/sys/sys/cdefs.h
+++ b/sys/sys/cdefs.h
@@ -739,4 +739,7 @@
 #define __NO_TLS 1
 #endif
 
+#define	nitems(x)	(sizeof((x)) / sizeof((x)[0]))
+#define	__arraycount(x)	nitems(x)
+
 #endif /* !_SYS_CDEFS_H_ */
diff --git a/sys/sys/param.h b/sys/sys/param.h
index 264a38a..7eca74b 100644
--- a/sys/sys/param.h
+++ b/sys/sys/param.h
@@ -274,7 +274,6 @@
 #ifndef howmany
 #define	howmany(x, y)	(((x)+((y)-1))/(y))
 #endif
-#define	nitems(x)	(sizeof((x)) / sizeof((x)[0]))
 #define	rounddown(x, y)	(((x)/(y))*(y))
 #define	rounddown2(x, y) ((x)&(~((y)-1)))          /* if y is power of two */
 #define	roundup(x, y)	((((x)+((y)-1))/(y))*(y))  /* to any y */
diff --git a/usr.sbin/bluetooth/btpand/btpand.h b/usr.sbin/bluetooth/btpand/btpand.h
index 440cca1..b20c6c5 100644
--- a/usr.sbin/bluetooth/btpand/btpand.h
+++ b/usr.sbin/bluetooth/btpand/btpand.h
@@ -43,10 +43,6 @@
 
 #include "event.h"
 
-#ifndef __arraycount
-#define __arraycount(__x)	(sizeof(__x) / sizeof(__x[0]))
-#endif
-
 #ifndef	L2CAP_PSM_INVALID
 #define	L2CAP_PSM_INVALID(psm)	(((psm) & 0x0101) != 0x0001)
 #endif
-- 
2.0.2

help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAGHfRMBMPra5YXDn0e83dpVxwnarg3DL8o31xr7DhWv%2BVXskTg>