Date: Tue, 9 Sep 2014 11:33:33 +0300 From: Konstantin Belousov <kostikbel@gmail.com> To: yaneurabeya@gmail.com Cc: "freebsd-arch@FreeBSD.org Arch" <freebsd-arch@freebsd.org> Subject: Re: [RFC] Add __arraycount from NetBSD to sys/cdefs.h Message-ID: <20140909083333.GA2737@kib.kiev.ua> In-Reply-To: <146C3E96-7461-4D30-8B7F-5E20F72CF061@gmail.com> References: <CAGHfRMBMPra5YXDn0e83dpVxwnarg3DL8o31xr7DhWv%2BVXskTg@mail.gmail.com> <8D279BDC-7D40-4750-8DA7-A4535DD2E458@bsdimp.com> <146C3E96-7461-4D30-8B7F-5E20F72CF061@gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
--RJWZ9TQ7tP81YmMW Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Sep 08, 2014 at 09:29:54AM -0700, yaneurabeya@gmail.com wrote: > On Sep 3, 2014, at 21:17, Warner Losh <imp@bsdimp.com> wrote: >=20 > > On Sep 3, 2014, at 9:45 PM, Garrett Cooper <yaneurabeya@gmail.com> wrot= e: > >=20 > >> 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: > >>=20 > >> 44 /* > >> 45 * Return the number of elements in a statically-allocated array, > >> 46 * __x. > >> 47 */ > >> 48 #define __arraycount(__x) (sizeof(__x) / sizeof(__x[0])) > >>=20 > >> Here's the nitems #define in sys/sys/param.h: > >>=20 > >> 277 #define nitems(x) (sizeof((x)) / sizeof((x)[0])) > >>=20 > >> 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 > >>=20 > >> $ cat cdefs_pound_define.c > >> #include <sys/param.h> > >>=20 > >> #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 incl= uded" > >> $ gcc -D_KERNEL -c cdefs_pound_define.c > >> cdefs_pound_define.c:4:2: warning: #warning "sys/cdefs.h has been incl= uded? > >=20 > > I wouldn?t bother changing the nitems #define. There?s no need, really,= to do that. >=20 > Rethinking my proposal, I agree. I had lofty hopes for unifying the macro= s, but the functional duplication (1 line) is harmless. >=20 > > I?d also be more inclined to believe the test if you tested what the th= ing does rather than test for an artificial, implementation defined side ef= fect. >=20 > Sure. I provided a lazy proof instead of a full proof :). >=20 > > But honestly the amount of duplication saved here is rather tiny? >=20 > Indeed! Thank you for the input :) ? I?ve attached a new patch which does= n?t disturb nitems or sys/param.h. Since this is seriously discussed on arch@, I think it is worth look at the following: git grep -e 'sizeof.*/sizeof' -- | grep -v '^contrib' | grep '#.*define' | = wc -l 118 These are only defines, not uses, not counting direct use of sizeof(x)/sizeof(x[0]) in the code. Not all of them are for equiv macros, but significant part repeats ntimes() in variations. At least Linuxish ARRAY_SIZE/DRM_ARRAY_SIZE is much more popular than __arraysize() which appears three (!) times. I do not see any reason to pollute central header cdefs.h with sparcely used macro for some compat code. --RJWZ9TQ7tP81YmMW Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBAgAGBQJUDrtdAAoJEJDCuSvBvK1BN4cQAJHrDO928nZq4p0WUfrGjj/N cCekP+TnQxuaBn8sm6fEuoff9ZF/eh48s/0/i+ULNwrY085OZfmhvczeTP9Xj+kh Lm2MDKY3uLeQRLPtG+2KLd7ROH8M4oGM0Md7kuBgsr+uQ5kV7zEkHXyiBcbVqgDy Sj2Fe+j88BiHwfGM8iWSNoQQze9zNYjpqADg/TlPUlCkxNlZp3YR1CWTajePYhiY faf5QO7JPLulQOzabezfLKi5SYh6l0lVhDD6/+pkXibCpX8hemeeRR5zB3ccuSMs Ba0ZPOLG9gysZFGHYUSOw4KnzJp8Un1um9HVhr98NqZ299LLgG6AwjSAXRi1/Ui3 ZwMqHkmorxQ/jE1+4zDISGfH28JEXSvxIWVVMPCnJ9iFvoQImjouunPCRsogR3e4 4vBX03MifrxszprVRsPfhCTX45htX+ip23iietx9ojuYcK1bxUjRtYW00rlkXDl+ w87/thxkQHw2LsGCXtxwPsMP0TPaqWOHEF5qnqqGohcRsLcngcakyQosoUXBIeks 7DiXW0jLIWZRikFWQFDEOSRsh2nLCgKK5KIbv/W/q4IrgDC7oOd6zEg9X3ZDOfRr j3r4VbLmmxrACe1iQ9azAP+hgiyPjv3j5Uh6ghkdyk8XhDdLSNuBCys4CxKZHXCX Alg91A8xEkmPWHpnj1Gu =lFuI -----END PGP SIGNATURE----- --RJWZ9TQ7tP81YmMW--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20140909083333.GA2737>