Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 8 Sep 2014 09:29:54 -0700
From:      yaneurabeya@gmail.com
To:        Warner Losh <imp@bsdimp.com>
Cc:        Julio Merino <jmmv@freebsd.org>, "rpaulo@freebsd.org" <rpaulo@freebsd.org>, "freebsd-arch@FreeBSD.org Arch" <freebsd-arch@freebsd.org>
Subject:   Re: [RFC] Add __arraycount from NetBSD to sys/cdefs.h
Message-ID:  <146C3E96-7461-4D30-8B7F-5E20F72CF061@gmail.com>
In-Reply-To: <8D279BDC-7D40-4750-8DA7-A4535DD2E458@bsdimp.com>
References:  <CAGHfRMBMPra5YXDn0e83dpVxwnarg3DL8o31xr7DhWv%2BVXskTg@mail.gmail.com> <8D279BDC-7D40-4750-8DA7-A4535DD2E458@bsdimp.com>

next in thread | previous in thread | raw e-mail | index | archive | help

--Apple-Mail=_DCF9E948-C3D1-40D6-B4C1-306E23171DF3
Content-Type: multipart/mixed;
	boundary="Apple-Mail=_F8F39904-E01A-40C8-9E24-7132A67CC7A4"


--Apple-Mail=_F8F39904-E01A-40C8-9E24-7132A67CC7A4
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=windows-1252

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

> On Sep 3, 2014, at 9:45 PM, Garrett Cooper <yaneurabeya@gmail.com> =
wrote:
>=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 =
included"
>> $ gcc -D_KERNEL -c cdefs_pound_define.c
>> cdefs_pound_define.c:4:2: warning: #warning "sys/cdefs.h has been =
included=94
>=20
> I wouldn=92t bother changing the nitems #define. There=92s 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=92d 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=85

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

Thanks!
-Garrett


--Apple-Mail=_F8F39904-E01A-40C8-9E24-7132A67CC7A4
Content-Disposition: attachment;
	filename=0001-Add-__arraycount-macro-from-NetBSD-to-ease-porting.patch
Content-Type: application/octet-stream;
	name="0001-Add-__arraycount-macro-from-NetBSD-to-ease-porting.patch"
Content-Transfer-Encoding: quoted-printable

=46rom=20ba220f306a1849948ecb198c6f2424ed6cfec6bc=20Mon=20Sep=2017=20=
00:00:00=202001=0AFrom:=20Garrett=20Cooper=20<yanegomi@gmail.com>=0A=
Date:=20Thu,=204=20Sep=202014=2013:30:19=20-0700=0ASubject:=20[PATCH]=20=
Add=20__arraycount=20macro=20from=20NetBSD=20to=20ease=20porting=0A=0A=
__arraycount=20is=20used=20for=20counting=20the=20number=20of=20items=20=
in=20a=20static=0Aarray,=20so=20you=20could=20do=20something=20like=20=
so:=0A=0A=20=20#include=20<sys/cdefs.h>=0A=20=20#include=20<stdio.h>=0A=20=
=20#include=20<string.h>=0A=0A=20=20int=0A=20=20main(void)=0A=20=20{=0A=20=
=20=20=20char=20arr[20];=0A=20=20=20=20unsigned=20int=20i;=0A=0A=20=20=20=
=20memset(arr,=20'a',=20__arraycount(arr));=0A=0A=20=20=20=20for=20(i=20=
=3D=200;=20i=20<=20__arraycount(arr);=20i++)=0A=20=20=20=20=20=20=
putchar(arr[i]);=0A=20=20=20=20return=20(0);=0A=20=20}=0A=0A__arraycount=20=
is=20functionally=20equivalent=20to=20the=20nitems=20macro=20in=20=
sys/param.h=0A=0AGarbage=20collect=20all=20ad=20hoc=20definitions=20in=20=
the=20tree=20that=20aren't=20protected=0Aby=20#ifndef=20__arraycount=20=
not=20provided=20by=20a=20third-party=20source=0A=0ASponsored=20by:=20=
EMC=20/=20Isilon=20Storage=20Division=0A---=0A=20=
contrib/libc-vis/unvis.c=20=20=20=20=20=20=20=20=20=20=20|=206=20------=0A=
=20lib/libnetbsd/sys/cdefs.h=20=20=20=20=20=20=20=20=20=20|=206=20------=0A=
=20sys/kern/stack_protector.c=20=20=20=20=20=20=20=20=20|=201=20-=0A=20=
sys/sys/cdefs.h=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
|=206=20++++++=0A=20usr.sbin/bluetooth/btpand/btpand.h=20|=204=20----=0A=20=
5=20files=20changed,=206=20insertions(+),=2017=20deletions(-)=0A=0Adiff=20=
--git=20a/contrib/libc-vis/unvis.c=20b/contrib/libc-vis/unvis.c=0Aindex=20=
9cf112c..83ff2a0=20100644=0A---=20a/contrib/libc-vis/unvis.c=0A+++=20=
b/contrib/libc-vis/unvis.c=0A@@=20-51,12=20+51,6=20@@=20=
__FBSDID("$FreeBSD$");=0A=20=0A=20#define=09_DIAGASSERT(x)=09assert(x)=0A=
=20=0A-/*=0A-=20*=20Return=20the=20number=20of=20elements=20in=20a=20=
statically-allocated=20array,=0A-=20*=20__x.=0A-=20*/=0A-#define=09=
__arraycount(__x)=09(sizeof(__x)=20/=20sizeof(__x[0]))=0A-=0A=20#ifdef=20=
__weak_alias=0A=20__weak_alias(strnunvisx,_strnunvisx)=0A=20#endif=0A=
diff=20--git=20a/lib/libnetbsd/sys/cdefs.h=20b/lib/libnetbsd/sys/cdefs.h=0A=
index=2009a7ca4..616141b=20100644=0A---=20a/lib/libnetbsd/sys/cdefs.h=0A=
+++=20b/lib/libnetbsd/sys/cdefs.h=0A@@=20-41,10=20+41,4=20@@=0A=20=
#define=20__dead=0A=20#endif=0A=20=0A-/*=0A-=20*=20Return=20the=20number=20=
of=20elements=20in=20a=20statically-allocated=20array,=0A-=20*=20__x.=0A=
-=20*/=0A-#define=09__arraycount(__x)=09(sizeof(__x)=20/=20=
sizeof(__x[0]))=0A-=0A=20#endif=20/*=20_LIBNETBSD_SYS_CDEFS_H_=20*/=0A=
diff=20--git=20a/sys/kern/stack_protector.c=20=
b/sys/kern/stack_protector.c=0Aindex=20b5f9973..63acb90=20100644=0A---=20=
a/sys/kern/stack_protector.c=0A+++=20b/sys/kern/stack_protector.c=0A@@=20=
-17,7=20+17,6=20@@=20__stack_chk_fail(void)=0A=20=09panic("stack=20=
overflow=20detected;=20backtrace=20may=20be=20corrupted");=0A=20}=0A=20=0A=
-#define=20__arraycount(__x)=09(sizeof(__x)=20/=20sizeof(__x[0]))=0A=20=
static=20void=0A=20__stack_chk_init(void=20*dummy=20__unused)=0A=20{=0A=
diff=20--git=20a/sys/sys/cdefs.h=20b/sys/sys/cdefs.h=0Aindex=20=
4c4c2af..731bcf8=20100644=0A---=20a/sys/sys/cdefs.h=0A+++=20=
b/sys/sys/cdefs.h=0A@@=20-739,4=20+739,10=20@@=0A=20#define=20__NO_TLS=20=
1=0A=20#endif=0A=20=0A+/*=0A+=20*=20Return=20the=20number=20of=20=
elements=20in=20a=20statically-allocated=20array,=0A+=20*=20__x.=0A+=20=
*/=0A+#define=09__arraycount(__x)=09(sizeof(__x)=20/=20sizeof(__x[0]))=0A=
+=0A=20#endif=20/*=20!_SYS_CDEFS_H_=20*/=0Adiff=20--git=20=
a/usr.sbin/bluetooth/btpand/btpand.h=20=
b/usr.sbin/bluetooth/btpand/btpand.h=0Aindex=20c5f7204..b20c6c5=20100644=0A=
---=20a/usr.sbin/bluetooth/btpand/btpand.h=0A+++=20=
b/usr.sbin/bluetooth/btpand/btpand.h=0A@@=20-43,10=20+43,6=20@@=0A=20=0A=20=
#include=20"event.h"=0A=20=0A-#ifndef=20__arraycount=0A-#define=20=
__arraycount(__x)=09(int)(sizeof((__x))=20/=20sizeof((__x)[0]))=0A=
-#endif=0A-=0A=20#ifndef=09L2CAP_PSM_INVALID=0A=20#define=09=
L2CAP_PSM_INVALID(psm)=09(((psm)=20&=200x0101)=20!=3D=200x0001)=0A=20=
#endif=0A--=20=0A2.0.0=0A=0A=

--Apple-Mail=_F8F39904-E01A-40C8-9E24-7132A67CC7A4
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=windows-1252



--Apple-Mail=_F8F39904-E01A-40C8-9E24-7132A67CC7A4--

--Apple-Mail=_DCF9E948-C3D1-40D6-B4C1-306E23171DF3
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename=signature.asc
Content-Type: application/pgp-signature;
	name=signature.asc
Content-Description: Message signed with OpenPGP using GPGMail

-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - https://gpgtools.org

iQEcBAEBCgAGBQJUDdmDAAoJEMZr5QU6S73eGRUH/j5TGlNsx3OmPnbXZZhYX7WM
v10KOXbrhgW6uVFkOIgX16vuHPwQuI3QpyYIAL7qzsM92AEK9tE5GRYkgJvsqu2x
1V7oVQ612JcCRc5hvYgQVBFcZ20TzBiac03aNkWwwSTBhSeSABysanajVo+HQL8i
5WObB404W/TMzigYjO8yubMC7ovLUMCN4VV4+EGnr2M4l5sWYgC1FQYYu6YEV73f
UnaoeNiNmRq6JEGa3kJjBGS/zVAF+41z0AvTg4WY+XqZnDgiQ2p1qb0bSLI+s8lo
J3XU6hODhPUPtikAXwaZJJ/I0XFXmpT/FNFQnRTMOul6fl727n62O+Jyu7BHBAI=
=2sXE
-----END PGP SIGNATURE-----

--Apple-Mail=_DCF9E948-C3D1-40D6-B4C1-306E23171DF3--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?146C3E96-7461-4D30-8B7F-5E20F72CF061>