idx.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* A type for indices and sizes.
  2. Copyright (C) 2020-2021 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _IDX_H
  16. #define _IDX_H
  17. /* Get ptrdiff_t. */
  18. #include <stddef.h>
  19. /* Get PTRDIFF_MAX. */
  20. #include <stdint.h>
  21. /* The type 'idx_t' holds an (array) index or an (object) size.
  22. Its implementation promotes to a signed integer type,
  23. which can hold the values
  24. 0..2^63-1 (on 64-bit platforms) or
  25. 0..2^31-1 (on 32-bit platforms).
  26. Why a signed integer type?
  27. * Security: Signed types can be checked for overflow via
  28. '-fsanitize=undefined', but unsigned types cannot.
  29. * Comparisons without surprises: ISO C99 § 6.3.1.8 specifies a few
  30. surprising results for comparisons, such as
  31. (int) -3 < (unsigned long) 7 => false
  32. (int) -3 < (unsigned int) 7 => false
  33. and on 32-bit machines:
  34. (long) -3 < (unsigned int) 7 => false
  35. This is surprising because the natural comparison order is by
  36. value in the realm of infinite-precision signed integers (ℤ).
  37. The best way to get rid of such surprises is to use signed types
  38. for numerical integer values, and use unsigned types only for
  39. bit masks and enums.
  40. Why not use 'size_t' directly?
  41. * Because 'size_t' is an unsigned type, and a signed type is better.
  42. See above.
  43. Why not use 'ptrdiff_t' directly?
  44. * Maintainability: When reading and modifying code, it helps to know that
  45. a certain variable cannot have negative values. For example, when you
  46. have a loop
  47. int n = ...;
  48. for (int i = 0; i < n; i++) ...
  49. or
  50. ptrdiff_t n = ...;
  51. for (ptrdiff_t i = 0; i < n; i++) ...
  52. you have to ask yourself "what if n < 0?". Whereas in
  53. idx_t n = ...;
  54. for (idx_t i = 0; i < n; i++) ...
  55. you know that this case cannot happen.
  56. Similarly, when a programmer writes
  57. idx_t = ptr2 - ptr1;
  58. there is an implied assertion that ptr1 and ptr2 point into the same
  59. object and that ptr1 <= ptr2.
  60. * Being future-proof: In the future, range types (integers which are
  61. constrained to a certain range of values) may be added to C compilers
  62. or to the C standard. Several programming languages (Ada, Haskell,
  63. Common Lisp, Pascal) already have range types. Such range types may
  64. help producing good code and good warnings. The type 'idx_t' could
  65. then be typedef'ed to a range type that is signed after promotion. */
  66. /* In the future, idx_t could be typedef'ed to a signed range type.
  67. The clang "extended integer types", supported in Clang 11 or newer
  68. <https://clang.llvm.org/docs/LanguageExtensions.html#extended-integer-types>,
  69. are a special case of range types. However, these types don't support binary
  70. operators with plain integer types (e.g. expressions such as x > 1).
  71. Therefore, they don't behave like signed types (and not like unsigned types
  72. either). So, we cannot use them here. */
  73. /* Use the signed type 'ptrdiff_t'. */
  74. /* Note: ISO C does not mandate that 'size_t' and 'ptrdiff_t' have the same
  75. size, but it is so on all platforms we have seen since 1990. */
  76. typedef ptrdiff_t idx_t;
  77. /* IDX_MAX is the maximum value of an idx_t. */
  78. #define IDX_MAX PTRDIFF_MAX
  79. /* So far no need has been found for an IDX_WIDTH macro.
  80. Perhaps there should be another macro IDX_VALUE_BITS that does not
  81. count the sign bit and is therefore one less than PTRDIFF_WIDTH. */
  82. #endif /* _IDX_H */