Re: alignas (C11)

Peter Eisentraut <peter@eisentraut.org>

From: Peter Eisentraut <peter@eisentraut.org>
To: Chao Li <li.evan.chao@gmail.com>
Cc: pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2025-11-18T13:35:59Z
Lists: pgsql-hackers
On 12.11.25 15:27, Chao Li wrote:
> 0002 simplifies several structures/unions by using alignas, I have a couple of minor comment:
> 
> 1 - 0002
> ```
> -typedef union PGAlignedBlock
> +typedef struct PGAlignedBlock
>   {
> -	char		data[BLCKSZ];
> -	double		force_align_d;
> -	int64		force_align_i64;
> +	alignas(MAXIMUM_ALIGNOF) char data[BLCKSZ];
>   } PGAlignedBlock;
> ```
> 
> As we changes PGAlignedBlock from union to structure, I think we can explicitly mention in the commit message something like “PGAlignedBlock has the same alignment and contiguous array data, thus no ABI change”.

We don't care about ABI changes in major releases.

> 2 - 0002
> ```
> -	/* page_buffer must be adequately aligned, so use a union */
> -	union
> -	{
> -		char		buf[QUEUE_PAGESIZE];
> -		AsyncQueueEntry align;
> -	}			page_buffer;
> +	/* page_buffer must be adequately aligned */
> +	alignas(AsyncQueueEntry) char page_buffer[QUEUE_PAGESIZE];
> ```
> 
> To make readers easier to understand the statement, maybe we can explicitly use alignof:
> 
> alignas(alignof(AsyncQueueEntry)) char page_buffer[QUEUE_PAGESIZE];

I don't know.  alignas(type) is standard C and seems pretty intuitive to 
me.  Writing it the long way would be potentially more confusing IMO.




Commits

  1. Further fix extended alignment for older g++.

  2. Disable extended alignment uses on older g++

  3. Work around buggy alignas in older g++

  4. Use C11 alignas in pg_atomic_uint64 definitions

  5. C11 alignas instead of unions -- extended alignments

  6. C11 alignas instead of unions

  7. Add <stdalign.h> to c.h