[RFC] Add __arraycount from NetBSD to sys/cdefs.h

Warner Losh imp at bsdimp.com
Thu Sep 4 04:17:47 UTC 2014


On Sep 3, 2014, at 9:45 PM, Garrett Cooper <yaneurabeya at gmail.com> wrote:

> 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”

I wouldn’t bother changing the nitems #define. There’s no need, really, to do that.

I’d also be more inclined to believe the test if you tested what the thing does rather than test for an artificial, implementation defined side effect.

But honestly the amount of duplication saved here is rather tiny…

Warner
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 842 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://lists.freebsd.org/pipermail/freebsd-arch/attachments/20140903/fc92b366/attachment.sig>


More information about the freebsd-arch mailing list