Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230524212835.GC4163@brightrain.aerifal.cx>
Date: Wed, 24 May 2023 17:28:35 -0400
From: Rich Felker <dalias@...c.org>
To: Jens Gustedt <Jens.Gustedt@...ia.fr>
Cc: musl@...ts.openwall.com
Subject: Re: [C23 new stdlib 2/4] C23: add the memalignment function

On Wed, May 24, 2023 at 09:38:52PM +0200, Jens Gustedt wrote:
> The name is reserved, so we don't need to protect it via a feature
> macro or a weak symbol.
> ---
>  include/stdlib.h          | 3 +++
>  src/stdlib/memalignment.c | 6 ++++++
>  2 files changed, 9 insertions(+)
>  create mode 100644 src/stdlib/memalignment.c
> 
> diff --git a/include/stdlib.h b/include/stdlib.h
> index 7800074d..68993c04 100644
> --- a/include/stdlib.h
> +++ b/include/stdlib.h
> @@ -178,6 +178,9 @@ long double strtold_l(const char *__restrict, char **__restrict, struct __locale
>  typedef int once_flag;
>  void call_once(once_flag *, void (*)(void));
>  
> +size_t (memalignment)(const void *);
> +#define memalignment(P) (((size_t)1)<<__builtin_ctzll((unsigned long long)P))
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/src/stdlib/memalignment.c b/src/stdlib/memalignment.c
> new file mode 100644
> index 00000000..58b68954
> --- /dev/null
> +++ b/src/stdlib/memalignment.c
> @@ -0,0 +1,6 @@
> +#include <stdlib.h>
> +
> +size_t (memalignment)(const void *p)
> +{
> +	return memalignment(p);
> +}
> -- 
> 2.34.1

There's a more efficient implementation of this function which does
not depend on __builtin_ctzll (which we don't): p^(p-1)&p

I'm not clear that there's a really good motivation for having it
implemented in the header. It kinda falls under the "maybe do it
because it's easy" case, but once you switch to the non-GNUC-specific
version, it needs a static inline function to avoid multiple
evaluation, and then it gets to the point of "this is really too much
code to have in a header" (which is more an issue of avoiding
"creative" content in headers than anything else).

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.