Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/alloc.h: Reimplement [X]REALLOC[F]() macros with _Generic(3) #988

Merged
merged 1 commit into from May 15, 2024

Conversation

alejandro-colomar
Copy link
Collaborator

@alejandro-colomar alejandro-colomar commented May 9, 2024

Instead of GNU builtins and extensions, these macros can be implemented with C11's _Generic(3), and the result is much simpler (and safer, since it's now an error, not just a warning).

I don't remember who showed me that trick; it was some months ago, in the gcc@ or libc-alpha@ mailing lists (maybe it was @uecker). I reimplemented these macros from scratch for neomutt(1), and did it with _Generic(3), and it was much nicer than all those GNU extensions. Let's incorporate those improvements here. :-)


Revisions:

v2

v2 changes:

  • Update copyright
$ git range-diff shadow/master gh/alloc alloc 
1:  18ec0d85 ! 1:  84517ec8 lib/alloc.h: Reimplement [X]REALLOC[F]() macros with _Generic(3)
    @@ Commit message
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
     
      ## lib/alloc.h ##
    +@@
    +-/*
    +- * SPDX-FileCopyrightText:  2023, Alejandro Colomar <alx@kernel.org>
    +- *
    +- * SPDX-License-Identifier:  BSD-3-Clause
    +- */
    ++// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
    ++// SPDX-License-Identifier: BSD-3-Clause
    + 
    + 
    + #ifndef SHADOW_INCLUDE_LIB_MALLOC_H_
     @@
      #define XMALLOC(n, type)  ((type *) xmallocarray(n, sizeof(type)))
      
v2b
  • Whitespace
$ git range-diff gh/master gh/alloc alloc 
1:  84517ec8 ! 1:  bff08b61 lib/alloc.h: Reimplement [X]REALLOC[F]() macros with _Generic(3)
    @@ lib/alloc.h
     -  (type *) reallocarray(p_, n, sizeof(type));                           \
     -})
     +(                                                                             \
    -+  _Generic(ptr, type *: (type *) reallocarray(ptr, n, sizeof(type)))    \
    ++  _Generic(ptr, type *:  (type *) reallocarray(ptr, n, sizeof(type)))   \
     +)
      
      #define REALLOCF(ptr, n, type)                                                \
    @@ lib/alloc.h
     -  (type *) reallocarrayf(p_, n, sizeof(type));                          \
     -})
     +(                                                                             \
    -+  _Generic(ptr, type *: (type *) reallocarrayf(ptr, n, sizeof(type)))   \
    ++  _Generic(ptr, type *:  (type *) reallocarrayf(ptr, n, sizeof(type)))  \
     +)
      
      #define XREALLOC(ptr, n, type)                                                \
    @@ lib/alloc.h
     -  (type *) xreallocarray(p_, n, sizeof(type));                          \
     -})
     +(                                                                             \
    -+  _Generic(ptr, type *: (type *) xreallocarray(ptr, n, sizeof(type)))   \
    ++  _Generic(ptr, type *:  (type *) xreallocarray(ptr, n, sizeof(type)))  \
     +)
      
      
v2c
  • Rebase
$ git range-diff gh/master..gh/alloc master..alloc 
1:  bff08b61 = 1:  51a03214 lib/alloc.h: Reimplement [X]REALLOC[F]() macros with _Generic(3)

@alejandro-colomar
Copy link
Collaborator Author

alejandro-colomar commented May 9, 2024

The trick is that _Generic(ptr, type *: expr) guarantees that typeof(ptr) is exactly type * (or it wouldn't compile at all), since there's no default: or alternate type in that generic expression.

@alejandro-colomar alejandro-colomar marked this pull request as ready for review May 9, 2024 23:18
Instead of GNU builtins and extensions, these macros can be implemented
with C11's _Generic(3), and the result is much simpler (and safer, since
it's now an error, not just a warning).

Signed-off-by: Alejandro Colomar <alx@kernel.org>
Copy link
Collaborator

@ikerexxe ikerexxe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Merging.

@ikerexxe ikerexxe merged commit 26c9dd3 into shadow-maint:master May 15, 2024
9 checks passed
@alejandro-colomar
Copy link
Collaborator Author

alejandro-colomar commented May 15, 2024

Thanks! I've come up with some similar ideas to simplify and improve other magic macros. I'll send another patch set soon. :-)

@alejandro-colomar alejandro-colomar deleted the alloc branch May 15, 2024 12:23
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.

None yet

2 participants