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

Garrett Cooper yaneurabeya at gmail.com
Thu Sep 4 03:45:34 UTC 2014


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"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Make-__arraycount-macros-from-NetBSD-to-ease-porting.patch
Type: application/octet-stream
Size: 3347 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-arch/attachments/20140903/1a0a62c3/attachment.obj>


More information about the freebsd-arch mailing list