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

yaneurabeya at gmail.com yaneurabeya at gmail.com
Mon Sep 8 16:29:58 UTC 2014


On Sep 3, 2014, at 21:17, Warner Losh <imp at bsdimp.com> wrote:

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

Rethinking my proposal, I agree. I had lofty hopes for unifying the macros, but the functional duplication (1 line) is harmless.

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

Sure. I provided a lazy proof instead of a full proof :).

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

Indeed! Thank you for the input :) — I’ve attached a new patch which doesn’t disturb nitems or sys/param.h.

Thanks!
-Garrett

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Add-__arraycount-macro-from-NetBSD-to-ease-porting.patch
Type: application/octet-stream
Size: 3147 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-arch/attachments/20140908/0dbbb1ef/attachment.obj>
-------------- next part --------------

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


More information about the freebsd-arch mailing list