From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_REPLYTO_END_DIGIT, MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id B4DFD1F66F for ; Tue, 17 Nov 2020 22:23:39 +0000 (UTC) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4F83B3985410; Tue, 17 Nov 2020 22:23:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4F83B3985410 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1605651806; bh=BU/2+3u+luBOSXVp26vtF5n5fKDVDjUKSzy/0F4KkWs=; h=Subject:To:References:Date:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=HtafYmvAcIh6LKE+AZSKw2QDV+4ccw5cxdXQ0/TO57XqhZqPZvKrySg/6DH1d7Lv5 R5SKESpQsJin92mxJ/K5lJkYRwI2KAkn+Rt1rHvu2LKqftFLgu3avdqsVdxOIhoJbk mmpLFtJyHrbE+I+hQiNatG6NERjsFIWbPs67cp00= Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by sourceware.org (Postfix) with ESMTPS id 075053858006 for ; Tue, 17 Nov 2020 22:23:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 075053858006 Received: by mail-wm1-x344.google.com with SMTP id p22so234979wmg.3 for ; Tue, 17 Nov 2020 14:23:21 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=BU/2+3u+luBOSXVp26vtF5n5fKDVDjUKSzy/0F4KkWs=; b=I08Fqz4cA/VVjqg20qK840EZSIOZ6xImMJ7tyLijIp2g+aciFwvrSpYJUyiZpo2h5/ 1tR4sTqHUbuVIK8JYuEtVLjbTl6t3J9+NkiOW2d8raTSsGX50sF3TngyFkasVcqnpnXr DdgcRoPcYGiqnhPDZib0gPNdNzxJiYcGemHhyEgNNeZ/HI7Z3iXTFFuAo8UQRk3KfUpP o+qXdzejfMDiX5HYh7cnUHp2peBqROXXhcj5u4z4bPAm7aR0dIWh+cTWiZ5k3rDKOeZ6 35EerRzJ/wYSLcESUBFW2KdOS6S8Sdgqu2JjnP5WFuLqM5xg/wajhsehfFhzfrB2M2rL x0Qg== X-Gm-Message-State: AOAM532v4rLMEDjERAMiJ74t65fFA676Aw4dZdjhQamkOYeSwkEkxOR0 03d8/go5q0lCFE/Y+RtwtsYBIePyGmDYJg== X-Google-Smtp-Source: ABdhPJwq5jr42ekRr2apUJ9sQM02jL4V/btNO/erGaRO2KrnB0jVHlqKui51RV/CU3w8xnrIBGbWHg== X-Received: by 2002:a1c:a70b:: with SMTP id q11mr1251771wme.90.1605651800944; Tue, 17 Nov 2020 14:23:20 -0800 (PST) Received: from [192.168.1.143] ([170.253.51.130]) by smtp.gmail.com with ESMTPSA id u203sm176958wme.32.2020.11.17.14.23.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 17 Nov 2020 14:23:20 -0800 (PST) Subject: Ping(3): [PATCH v4] : Add nitems() To: libc-alpha@sourceware.org, libc-coord@lists.openwall.com References: <20200928191237.32063-1-colomar.6.4.3@gmail.com> Message-ID: Date: Tue, 17 Nov 2020 23:23:19 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <20200928191237.32063-1-colomar.6.4.3@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Alejandro Colomar via Libc-alpha Reply-To: Alejandro Colomar Cc: fweimer@redhat.com, gcc@gcc.gnu.org, ville.voutilainen@gmail.com, linux-man@vger.kernel.org, rusty@rustcorp.com.au, linux-kernel@vger.kernel.org, libstdc++@gcc.gnu.org, jwakely@redhat.com, enh@google.com Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Hi, Do you think the patch is ready, or is there anything else I should do before merging it? Thanks, Alex On 9/28/20 9:12 PM, Alejandro Colomar wrote: > 'nitems()' calculates the length of an array in number of items. > It is safe: if a pointer is passed to the macro (or function, in C++), > the compilation is broken due to: > - In >= C11: _Static_assert() > - In C89, C99: Negative anonymous bitfield > - In C++: The template requires an array > > Some BSDs already provide a macro nitems() in , > although it usually doesn't provide safety against pointers. > > This patch uses the same name for compatibility reasons, > and to be the least disruptive with existing code. > > This patch also adds some other macros, which are required by 'nitems()': > > __is_same_type(a, b): > Returns non-zero if the two input arguments are of the same type. > > __is_array(arr): > Returns non-zero if the input argument is of an array type. > > __must_be(expr, msg): > Allows using _Static_assert() everywhere an expression can be used. > It evaluates '(int)0' or breaks the compilation. > > __must_be_array(arr): > It evaluates to '(int)0' if the argument is of an array type. > Else, it breaks compilation. > > __nitems(arr): > It implements the basic sizeof division needed to calculate the array length. > > > P.S.: I'd like to put this patch in the public domain. > > Signed-off-by: Alejandro Colomar > --- > > A few changes since v3: > > - Macros don't need reserved names in their parameters, > so I simplified those names. > > - I fixed some wrong indentation levels. > > - Renamed __array_len() to __nitems() for consistency. > > > misc/sys/param.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 47 insertions(+) > > diff --git a/misc/sys/param.h b/misc/sys/param.h > index d7c319b157..08d4093961 100644 > --- a/misc/sys/param.h > +++ b/misc/sys/param.h > @@ -102,5 +102,52 @@ > #define MIN(a,b) (((a)<(b))?(a):(b)) > #define MAX(a,b) (((a)>(b))?(a):(b)) > > +/* Macros related to the types of variables */ > +#define __is_same_type(a, b) \ > + __builtin_types_compatible_p(__typeof__(a), __typeof__(b)) > +#define __is_array(arr) (!__is_same_type((arr), &(arr)[0])) > + > +/* Macros for embedding _Static_assert() in expressions */ > +#if __STDC_VERSION__ >= 201112L > +# define __must_be(expr, msg) ( \ > + 0 * (int)sizeof( \ > + struct { \ > + _Static_assert((expr), msg); \ > + char _ISO_C_forbids_a_struct_with_no_members; \ > + } \ > + ) \ > +) > +#else > +# define __must_be(expr, msg) ( \ > + 0 * (int)sizeof( \ > + struct { \ > + int : (-!(expr)); \ > + char _ISO_C_forbids_a_struct_with_no_members; \ > + } \ > + ) \ > +) > +#endif > +#define __must_be_array(arr) __must_be(__is_array(arr), "Must be an array!") > + > +/* Macros for array sizes */ > +#if defined(__cplusplus) > +# if __cplusplus >= 201103L > +template > + constexpr inline std::size_t > + nitems(const _Tp(&)[_Len]) __THROW > + { > + return _Len; > + } > +# else /* __cplusplus < 201103L */ > +template > + char > + (&__nitems_chararr(const _Tp(&)[_Len]))[_Len]; > +# define nitems(arr) (sizeof(__nitems_chararr(arr))) > +# endif /* __cplusplus < 201103L */ > +#else /* !defined(__cplusplus) */ > +# define __nitems(arr) (sizeof((arr)) / sizeof((arr)[0])) > +# define nitems(arr) (__nitems(arr) + __must_be_array(arr)) > +#endif /* !defined(__cplusplus) */ > + > > #endif /* sys/param.h */ >