Skip to content

Conversation

@alejandro-colomar
Copy link
Collaborator

@alejandro-colomar alejandro-colomar commented May 21, 2025

countof() is the name blessed by the C Committee for this macro in ISO C2y. Use it if available, and define it if not.

countof() will be provided by GCC 16 and Clang 21.

This is mostly a scripted change:

$ grep -rl NITEMS | xargs sed -i s/NITEMS/countof/;

Link: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3550.pdf#subsubsection.0.6.5.4.5
Link: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3550.pdf#section.0.7.21


Revisions:

v1b
  • Rebase
$ git rd 
1:  4713ce66 ! 1:  40627ab6 lib/, src/, tests/: Use the standard countof() instead of our NITEMS()
    @@ lib/string/strcpy/strtcpy.h
     
      ## lib/string/strdup/strndupa.h ##
     @@
    - // Similar to strndupa(3), but ensure that 's' is an array.
    - #define STRNDUPA(s)                                                           \
    - (                                                                             \
    --  STRNCAT(strcpy(alloca(strnlen(s, NITEMS(s)) + 1), ""), s)             \
    -+  STRNCAT(strcpy(alloca(strnlen(s, countof(s)) + 1), ""), s)            \
    - )
    + #endif
      
      
    +-#define STRNDUPA(s)  strndupa(s, NITEMS(s))
    ++#define STRNDUPA(s)  strndupa(s, countof(s))
    + 
    + 
    + #endif  // include guard
     
      ## lib/string/strdup/xstrndup.h ##
     @@
2:  52ee105d = 2:  0272ba8a lib/sizeof.h: Define SIZEOF_ARRAY() in terms of countof()
3:  ee4de260 = 3:  e30e207b autogen.sh, lib/: Replace must_be_array() by -Werror=sizeof-pointer-div
4:  2efe549e = 4:  e12ab0f3 lib/, src/: Move macros to "typetraits.h"
v2
  • Use countof() instead of its pattern.
$ git rd
1:  40627ab6 = 1:  40627ab6 lib/, src/, tests/: Use the standard countof() instead of our NITEMS()
2:  0272ba8a = 2:  0272ba8a lib/sizeof.h: Define SIZEOF_ARRAY() in terms of countof()
3:  e30e207b = 3:  e30e207b autogen.sh, lib/: Replace must_be_array() by -Werror=sizeof-pointer-div
4:  e12ab0f3 = 4:  e12ab0f3 lib/, src/: Move macros to "typetraits.h"
-:  -------- > 5:  48e5e851 lib/getdef.c: Use countof() instead of its pattern
v2b
  • Rebase
$ git rd
1:  40627ab6 = 1:  b137a0c2 lib/, src/, tests/: Use the standard countof() instead of our NITEMS()
2:  0272ba8a = 2:  6d8e0251 lib/sizeof.h: Define SIZEOF_ARRAY() in terms of countof()
3:  e30e207b = 3:  25dd20d8 autogen.sh, lib/: Replace must_be_array() by -Werror=sizeof-pointer-div
4:  e12ab0f3 = 4:  fc614d10 lib/, src/: Move macros to "typetraits.h"
5:  48e5e851 = 5:  33ff0383 lib/getdef.c: Use countof() instead of its pattern
v2c
  • Rebase
$ git rd
1:  b137a0c2 = 1:  b59ac227 lib/, src/, tests/: Use the standard countof() instead of our NITEMS()
2:  6d8e0251 = 2:  fa754b3c lib/sizeof.h: Define SIZEOF_ARRAY() in terms of countof()
3:  25dd20d8 = 3:  bc6210cc autogen.sh, lib/: Replace must_be_array() by -Werror=sizeof-pointer-div
4:  fc614d10 = 4:  444c3623 lib/, src/: Move macros to "typetraits.h"
5:  33ff0383 = 5:  658841f7 lib/getdef.c: Use countof() instead of its pattern
v2d
  • Rebase
$ git rd
1:  b59ac227 = 1:  45299b55 lib/, src/, tests/: Use the standard countof() instead of our NITEMS()
2:  fa754b3c = 2:  17c3d556 lib/sizeof.h: Define SIZEOF_ARRAY() in terms of countof()
3:  bc6210cc = 3:  56fa3975 autogen.sh, lib/: Replace must_be_array() by -Werror=sizeof-pointer-div
4:  444c3623 = 4:  db2946cc lib/, src/: Move macros to "typetraits.h"
5:  658841f7 = 5:  35c87a1f lib/getdef.c: Use countof() instead of its pattern
v2e
  • Rebase
$ git rd
1:  45299b55 = 1:  37e5c4fe lib/, src/, tests/: Use the standard countof() instead of our NITEMS()
2:  17c3d556 = 2:  eef531a4 lib/sizeof.h: Define SIZEOF_ARRAY() in terms of countof()
3:  56fa3975 = 3:  ceb70a65 autogen.sh, lib/: Replace must_be_array() by -Werror=sizeof-pointer-div
4:  db2946cc = 4:  93dbe0a5 lib/, src/: Move macros to "typetraits.h"
5:  35c87a1f = 5:  bf02a07f lib/getdef.c: Use countof() instead of its pattern
v2f
  • Rebase
$ git rd
1:  37e5c4fe = 1:  5612b0a4 lib/, src/, tests/: Use the standard countof() instead of our NITEMS()
2:  eef531a4 = 2:  20299e3e lib/sizeof.h: Define SIZEOF_ARRAY() in terms of countof()
3:  ceb70a65 = 3:  842ae501 autogen.sh, lib/: Replace must_be_array() by -Werror=sizeof-pointer-div
4:  93dbe0a5 = 4:  633661de lib/, src/: Move macros to "typetraits.h"
5:  bf02a07f = 5:  8cbd0e76 lib/getdef.c: Use countof() instead of its pattern

@alejandro-colomar alejandro-colomar changed the title lib/, src/, tests/: Rename NITEMS() to countof() lib/, src/, tests/: Use the standard countof() instead of out NITEMS() May 21, 2025
@alejandro-colomar alejandro-colomar changed the title lib/, src/, tests/: Use the standard countof() instead of out NITEMS() lib/, src/, tests/: Use the standard countof() instead of our NITEMS() May 21, 2025
@alejandro-colomar alejandro-colomar force-pushed the countof branch 11 times, most recently from 4d2bec3 to 2efe549 Compare May 26, 2025 16:35
@alejandro-colomar alejandro-colomar marked this pull request as ready for review May 26, 2025 16:43
@alejandro-colomar
Copy link
Collaborator Author

Note: GCC hasn't yet merged the patches implementing _Countof and <stdcountof.h>, but it's a matter of days or weeks. Clang has already merged that.

@alejandro-colomar
Copy link
Collaborator Author

alejandro-colomar commented May 27, 2025

Note: GCC hasn't yet merged the patches implementing _Countof and <stdcountof.h>, but it's a matter of days or weeks. Clang has already merged that.

Aaaaand, done! GCC has merged my patches implementing _Countof and <stdcountof.h> today: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117025#c3. :-)

@alejandro-colomar alejandro-colomar force-pushed the countof branch 4 times, most recently from 35c87a1 to bf02a07 Compare June 2, 2025 09:26
countof() is the name blessed by the C Committee for ISO C2y.
Use it if available, and define it if not.

countof() will be provided by GCC 16 and Clang 21.

This is mostly a scripted change:

	$ grep -rl NITEMS | xargs sed -i s/NITEMS/countof/;

Apart from the scripted changes, I've adjusted white-space alignment,
and of course the definition at "lib/sizeof.h".

Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3550.pdf#subsubsection.0.6.5.4.5>
Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3550.pdf#section.0.7.21>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This will allow us to eventually get rid of must_be_array(), once
we make sure countof() is safe.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
The error works as well as the magic macro, and we get cleaner code.
Plus, very soon we'll get the countof() operator from GCC 16 and
Clang 21, which doesn't even need this diagnostic to work safely.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
The macros that remain in "must_be.h" really belong in "typetraits.h".
Move them there, and remove "must_be.h".

Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
@hallyn hallyn merged commit 4e80ca8 into shadow-maint:master Jun 3, 2025
10 checks passed
@alejandro-colomar alejandro-colomar deleted the countof branch June 3, 2025 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants