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=-3.9 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 475EC1F4B4 for ; Mon, 28 Dec 2020 14:00:14 +0000 (UTC) Received: from localhost ([::1]:48514 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ktt4G-0001k4-RB for normalperson@yhbt.net; Mon, 28 Dec 2020 09:00:12 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34158) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ktt3x-0001jo-J6 for bug-gnulib@gnu.org; Mon, 28 Dec 2020 08:59:53 -0500 Received: from mail-qt1-x82b.google.com ([2607:f8b0:4864:20::82b]:39650) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ktt3u-0003A8-Ko for bug-gnulib@gnu.org; Mon, 28 Dec 2020 08:59:53 -0500 Received: by mail-qt1-x82b.google.com with SMTP id a6so6902032qtw.6 for ; Mon, 28 Dec 2020 05:59:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=i7NvhQAm6Ui57UistCytNO9Wa9rzZEg6bbu2hOKCEBc=; b=HOct8NVMNtM+gGaKy4hxzAMbZsmcU19HBboQSu1N9sWioMW/1UEM8MoDYBADxoxuXK a3bb/BVUYuzxFACKsuLD8O6zpVZgqk3P0RHau3qnD1I12oKi6eShoRfGkFaW6DVryt2E vs7hyvfT3xjfOEkkHVzu2dPz6X8/7CjSyPXpruwBnHB0IKkWgZVBveQYEU2whLtIyRz8 5YS4t8IcQEjPDU8NXU/KCdRqfB6sqGfWWabUOT9qWUsDdGEqwh9gz8DQBEEiIQpWHYxF i0kPYz6zCD6L8jD3g1PXGWCLoEnqDes86/GuLzazuE/EU891cslv0Xngdc0/drt3rV+0 X2UA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=i7NvhQAm6Ui57UistCytNO9Wa9rzZEg6bbu2hOKCEBc=; b=drp4OTP/3IjPCXQBNwlXb/fjUekCYIW+80DHEW34hjKxjHbnY95czk6F06YzSzHgHD hSvvS3U+zKNjAGnwAY+7bMa868qLgeTVmWnliyQCldICX4oEFj9nUZUUPvIVlYBc//vy iG+ZtI2LbkgyOZlfGhsxpzGOBnZy4gobvLx8RPdibalzEyCn+kZ8Zna5QtG/rb4UkX9z 9jItZIyPUhOrsyBCWBKt8dESj+YuUKdrVbPemsEIXIZjx23BtFTsW2HDk6TLcpMDxpBs /VmrhFOhn4Ay5kqvWSeU+H7a9S45d9nHMGITSrW4etd/uBUVjuYtQzWeXeMOJ3yH4zne u8Hw== X-Gm-Message-State: AOAM530yPmvWOABEL6V80UEMuhPw4r/oxdqEtwTgKQSCY2xd7S9vrT24 4p24A7bPq7MVMWNYNs5JAFAuPQ== X-Google-Smtp-Source: ABdhPJz/iYhMoOjBzi38aiqhgHidOWroO1sLIYroxE0/POaDGgAmunQ6K6N44++DK9ssJjZqe+Hu4g== X-Received: by 2002:ac8:5359:: with SMTP id d25mr45412577qto.132.1609163989598; Mon, 28 Dec 2020 05:59:49 -0800 (PST) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id i17sm24243744qtg.77.2020.12.28.05.59.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Dec 2020 05:59:49 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v2 1/6] Import idx.h from gnulib Date: Mon, 28 Dec 2020 10:59:39 -0300 Message-Id: <20201228135944.1018303-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::82b; envelope-from=adhemerval.zanella@linaro.org; helo=mail-qt1-x82b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: bug-gnulib@gnu.org Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" Changes from previous version: - Change idx.h comment to LGPLv2.1+. -- And use to simplify stdlib/canonicalize.c implementation. --- include/idx.h | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 include/idx.h diff --git a/include/idx.h b/include/idx.h new file mode 100644 index 0000000000..024b44ae98 --- /dev/null +++ b/include/idx.h @@ -0,0 +1,114 @@ +/* A type for indices and sizes. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _IDX_H +#define _IDX_H + +/* Get ptrdiff_t. */ +#include + +/* Get PTRDIFF_MAX. */ +#include + +/* The type 'idx_t' holds an (array) index or an (object) size. + Its implementation promotes to a signed integer type, + which can hold the values + 0..2^63-1 (on 64-bit platforms) or + 0..2^31-1 (on 32-bit platforms). + + Why a signed integer type? + + * Security: Signed types can be checked for overflow via + '-fsanitize=undefined', but unsigned types cannot. + + * Comparisons without surprises: ISO C99 § 6.3.1.8 specifies a few + surprising results for comparisons, such as + + (int) -3 < (unsigned long) 7 => false + (int) -3 < (unsigned int) 7 => false + and on 32-bit machines: + (long) -3 < (unsigned int) 7 => false + + This is surprising because the natural comparison order is by + value in the realm of infinite-precision signed integers (ℤ). + + The best way to get rid of such surprises is to use signed types + for numerical integer values, and use unsigned types only for + bit masks and enums. + + Why not use 'size_t' directly? + + * Because 'size_t' is an unsigned type, and a signed type is better. + See above. + + Why not use 'ptrdiff_t' directly? + + * Maintainability: When reading and modifying code, it helps to know that + a certain variable cannot have negative values. For example, when you + have a loop + + int n = ...; + for (int i = 0; i < n; i++) ... + + or + + ptrdiff_t n = ...; + for (ptrdiff_t i = 0; i < n; i++) ... + + you have to ask yourself "what if n < 0?". Whereas in + + idx_t n = ...; + for (idx_t i = 0; i < n; i++) ... + + you know that this case cannot happen. + + Similarly, when a programmer writes + + idx_t = ptr2 - ptr1; + + there is an implied assertion that ptr1 and ptr2 point into the same + object and that ptr1 <= ptr2. + + * Being future-proof: In the future, range types (integers which are + constrained to a certain range of values) may be added to C compilers + or to the C standard. Several programming languages (Ada, Haskell, + Common Lisp, Pascal) already have range types. Such range types may + help producing good code and good warnings. The type 'idx_t' could + then be typedef'ed to a range type that is signed after promotion. */ + +/* In the future, idx_t could be typedef'ed to a signed range type. + The clang "extended integer types", supported in Clang 11 or newer + , + are a special case of range types. However, these types don't support binary + operators with plain integer types (e.g. expressions such as x > 1). + Therefore, they don't behave like signed types (and not like unsigned types + either). So, we cannot use them here. */ + +/* Use the signed type 'ptrdiff_t'. */ +/* Note: ISO C does not mandate that 'size_t' and 'ptrdiff_t' have the same + size, but it is so on all platforms we have seen since 1990. */ +typedef ptrdiff_t idx_t; + +/* IDX_MAX is the maximum value of an idx_t. */ +#define IDX_MAX PTRDIFF_MAX + +/* So far no need has been found for an IDX_WIDTH macro. + Perhaps there should be another macro IDX_VALUE_BITS that does not + count the sign bit and is therefore one less than PTRDIFF_WIDTH. */ + +#endif /* _IDX_H */ -- 2.25.1