From owner-freebsd-arch@FreeBSD.ORG Thu Sep 4 05:53:46 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 A1C0E2B7; Thu, 4 Sep 2014 05:53:46 +0000 (UTC) Received: from mail109.syd.optusnet.com.au (mail109.syd.optusnet.com.au [211.29.132.80]) by mx1.freebsd.org (Postfix) with ESMTP id 63D851F53; Thu, 4 Sep 2014 05:53:45 +0000 (UTC) Received: from c122-106-147-133.carlnfd1.nsw.optusnet.com.au (c122-106-147-133.carlnfd1.nsw.optusnet.com.au [122.106.147.133]) by mail109.syd.optusnet.com.au (Postfix) with ESMTPS id 1E9A0D62B58; Thu, 4 Sep 2014 15:53:41 +1000 (EST) Date: Thu, 4 Sep 2014 15:53:24 +1000 (EST) From: Bruce Evans X-X-Sender: bde@besplex.bde.org To: Garrett Cooper Subject: Re: [RFC] Add __arraycount from NetBSD to sys/cdefs.h In-Reply-To: Message-ID: <20140904151838.X1355@besplex.bde.org> References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.1 cv=BdjhjNd2 c=1 sm=1 tr=0 a=7NqvjVvQucbO2RlWB8PEog==:117 a=PO7r1zJSAAAA:8 a=ho85ICRMCckA:10 a=XXALs8e33l8A:10 a=kj9zAlcOel0A:10 a=JzwRw_2MAAAA:8 a=M3jX3SRTu_DTOVG5eQwA:9 a=CjuIK1q_8ugA:10 Cc: Julio Merino , "rpaulo@freebsd.org" , freebsd-arch@freebsd.org 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: Thu, 04 Sep 2014 05:53:46 -0000 On Wed, 3 Sep 2014, Garrett Cooper 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])) The NetBSD version is less namespace-polluting. The underscores in the name of its __x parameter are bogus (plain x would be in the macro's namespace). The version in the patch is vastly more namespace-polluting. It adds nitems() to and thus defeats the careful underscoring of all (?) other public names there. nitems() in was bad enough. It is of course not documented in any man page. has lots of historical pollution. It is still used a lot. Adding nitems() to it broke any application that uses this name for almost anything except possibly as a plain identifier (nitems followed by a left parentheses gives the macro, but nitems not followed by a left parenthese might not be broken). Since nitems is a very reasonable variable name, its pollution is more likely to break applications than most. I don't like depending on either nitems() or __arraycount() being in a system header. Any use of these is unportable at best. Most uses of reserved identifiers are undefined (C99 7.1.3p2). Use of __arraycount is no exception, since it is of course not documented in any man page. In practice, the undefined behaviour is usually just unportability. The only portable way to use these macros is to write your own version and spell it without leading underscores. Even this is not portable, due to bugs like the undocumented nitems() in . Use of is also unportable, but it should be possible to know what is in it by reading its documentation and not necessary to reread it often to check for new pollution in it. I usually spell my version of this macro it more like arraycount() than nitems(). nitems() is too generic even for local use. arraycount() is too verbose without being completely precise about what is being counted. Bruce