From owner-freebsd-arch@FreeBSD.ORG Mon Sep 8 16:29:58 2014 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id B623249F; Mon, 8 Sep 2014 16:29:58 +0000 (UTC) Received: from mail-pd0-x230.google.com (mail-pd0-x230.google.com [IPv6:2607:f8b0:400e:c02::230]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 753A91535; Mon, 8 Sep 2014 16:29:58 +0000 (UTC) Received: by mail-pd0-f176.google.com with SMTP id y13so3363123pdi.35 for ; Mon, 08 Sep 2014 09:29:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :message-id:references:to; bh=xy98zS2EBXKB+95tKZQalslzwxGaZJfM2OfOD+vWUss=; b=Sgvwo6pzih+InVcM96KUcchB4k5ieFqd8aXZVHB01c+hPlQ1/R7QX3i6xpUe4rFpGm FHjiouSrWMvbvuLHJ8lMtUsddxNAQWjYR+2qqsN8D7Ul6Hpvi9nn/GUBOonL1zSqnkl8 UwVQ5VUVWZUKJCwl+hxl+uyZ6qxr6MNFTFaS4lhHMNI/eAlgqzcq2/1WH0ELD+BWML1V TtSI2Gk8BCeTGNsBjaMjSN94VMVYc6dC9CcC7LQR16bm1zhQ2WG03gxoJGaw5e4iVLtq pveuVhm+MGEjnLyLVvS0qOC1o5wotDurbp1G4ufOLN2wT86nO4rmcqp0F3BVQI0nx0dK yPEA== X-Received: by 10.68.189.137 with SMTP id gi9mr21854229pbc.87.1410193797794; Mon, 08 Sep 2014 09:29:57 -0700 (PDT) Received: from ?IPv6:2601:8:ab80:7d6:9024:45ea:9d5:4ede? ([2601:8:ab80:7d6:9024:45ea:9d5:4ede]) by mx.google.com with ESMTPSA id bn4sm9304385pbc.40.2014.09.08.09.29.56 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 08 Sep 2014 09:29:57 -0700 (PDT) Content-Type: multipart/signed; boundary="Apple-Mail=_DCF9E948-C3D1-40D6-B4C1-306E23171DF3"; protocol="application/pgp-signature"; micalg=pgp-sha512 Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) Subject: Re: [RFC] Add __arraycount from NetBSD to sys/cdefs.h From: yaneurabeya@gmail.com In-Reply-To: <8D279BDC-7D40-4750-8DA7-A4535DD2E458@bsdimp.com> Date: Mon, 8 Sep 2014 09:29:54 -0700 Message-Id: <146C3E96-7461-4D30-8B7F-5E20F72CF061@gmail.com> References: <8D279BDC-7D40-4750-8DA7-A4535DD2E458@bsdimp.com> To: Warner Losh X-Mailer: Apple Mail (2.1878.6) Cc: Julio Merino , "rpaulo@freebsd.org" , "freebsd-arch@FreeBSD.org Arch" X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 08 Sep 2014 16:29:58 -0000 --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 wrote: > On Sep 3, 2014, at 9:45 PM, Garrett Cooper = 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 >>=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=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=0A=20=20#include=20=0A=20= =20#include=20=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--