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

tree-wide: use alignas() for alignment #21148

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
tree-wide: use alignas() for alignment
- Use of `__attribute__((aligned(<NUM>)))` is a compiler extension. We
  can just use `alignas()`, which is part of the C standard since C11
- Use of `_Alignas()` is a keyword since C11 and `alignas()` the alias
  provided by `<stdalign.h>`. But `_Alignas()` is deprecated since C23.
  Let's just go for `alignas()` instead.
  • Loading branch information
maribu committed Jan 21, 2025
commit 534a2d2ec8ab53e2a893b1571deccac5c71fc868
4 changes: 3 additions & 1 deletion boards/cc2650stk/doc.txt
Original file line number Diff line number Diff line change
@@ -127,7 +127,9 @@ advertising channels, as well as the corresponding variables in the RIOT
command.

```
typedef struct __attribute__ ((aligned(4))) {
#include <stdalign.h>

typedef struct alignas(4) {
radio_op_command_t ropCmd;
uint8_t channel;
struct {
6 changes: 4 additions & 2 deletions core/lib/include/xfa.h
Original file line number Diff line number Diff line change
@@ -26,6 +26,8 @@
#define XFA_H

#include <inttypes.h>
#include <stdalign.h>

#include "compiler_hints.h"

/*
@@ -45,7 +47,7 @@ _Pragma("GCC diagnostic ignored \"-Warray-bounds\"")
*/
#define _XFA(type, name, prio) \
NO_SANITIZE_ARRAY \
__attribute__((used, section(".xfa." #name "." #prio))) _Alignas(type) type
__attribute__((used, section(".xfa." #name "." #prio))) alignas(type) type

/**
* @brief helper macro for other XFA_* macros
@@ -54,7 +56,7 @@ _Pragma("GCC diagnostic ignored \"-Warray-bounds\"")
*/
#define _XFA_CONST(type, name, prio) \
NO_SANITIZE_ARRAY \
__attribute__((used, section(".roxfa." #name "." #prio))) _Alignas(type) type
__attribute__((used, section(".roxfa." #name "." #prio))) alignas(type) type

/**
* @brief Define a read-only cross-file array
14 changes: 8 additions & 6 deletions cpu/arm7_common/arm_cpu.c
Original file line number Diff line number Diff line change
@@ -16,8 +16,10 @@
* @}
*/

#include <stdalign.h>
#include <stdint.h>
#include <stdio.h>

#include "arm_cpu.h"
#include "irq.h"
#include "sched.h"
@@ -26,12 +28,12 @@
#define STACK_MARKER (0x77777777)
#define REGISTER_CNT (12)

__attribute__((used, section(".usr_stack"), aligned(4))) uint8_t usr_stack[USR_STACKSIZE];
__attribute__((used, section(".und_stack"), aligned(4))) uint8_t und_stack[UND_STACKSIZE];
__attribute__((used, section(".fiq_stack"), aligned(4))) uint8_t fiq_stack[FIQ_STACKSIZE];
__attribute__((used, section(".irq_stack"), aligned(4))) uint8_t irq_stack[ISR_STACKSIZE];
__attribute__((used, section(".abt_stack"), aligned(4))) uint8_t abt_stack[ABT_STACKSIZE];
__attribute__((used, section(".svc_stack"), aligned(4))) uint8_t svc_stack[ISR_STACKSIZE];
__attribute__((used, section(".usr_stack"))) alignas(4) uint8_t usr_stack[USR_STACKSIZE];
__attribute__((used, section(".und_stack"))) alignas(4) uint8_t und_stack[UND_STACKSIZE];
__attribute__((used, section(".fiq_stack"))) alignas(4) uint8_t fiq_stack[FIQ_STACKSIZE];
__attribute__((used, section(".irq_stack"))) alignas(4) uint8_t irq_stack[ISR_STACKSIZE];
__attribute__((used, section(".abt_stack"))) alignas(4) uint8_t abt_stack[ABT_STACKSIZE];
__attribute__((used, section(".svc_stack"))) alignas(4) uint8_t svc_stack[ISR_STACKSIZE];

#if (ISR_STACKSIZE % 4)
#error "ISR_STACKSIZE must be a multiple of 4"
@@ -57,7 +59,7 @@
/* make sure the stack is double word aligned (8 bytes) */
/* This is required in order to conform with Procedure Call Standard for the
* ARM® Architecture (AAPCS) */
/* https://web.archive.org/web/20150316114702/http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042e/IHI0042E_aapcs.pdf

Check warning on line 62 in cpu/arm7_common/arm_cpu.c

GitHub Actions / static-tests

line is longer than 100 characters
*/
if (((uint32_t) stk & 0x7) != 0) {
/* add a single word padding */
@@ -144,7 +146,7 @@
uint32_t *ptr = (uint32_t *)(uintptr_t)&irq_stack[0];
uint32_t *end = (uint32_t *)(uintptr_t)&irq_stack[ISR_STACKSIZE];

while(((*ptr) == STACK_CANARY_WORD) && (ptr < end)) {

Check warning on line 149 in cpu/arm7_common/arm_cpu.c

GitHub Actions / static-tests

keyword 'while' not followed by a single space
++ptr;
}

3 changes: 2 additions & 1 deletion cpu/esp32/include/periph_cpu.h
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
#ifndef PERIPH_CPU_H
#define PERIPH_CPU_H

#include <stdalign.h>
#include <stdbool.h>
#include <stdint.h>
#include "sdkconfig.h"
@@ -726,7 +727,7 @@ typedef struct {
/**
* @brief SDIO/SDMMC buffer instantiation requirement for SDHC
*/
#define SDMMC_CPU_DMA_REQUIREMENTS __attribute__((aligned(SDMMC_CPU_DMA_ALIGNMENT)))
#define SDMMC_CPU_DMA_REQUIREMENTS alignas(SDMMC_CPU_DMA_ALIGNMENT)

/**
* @brief SDIO/SDMMC buffer alignment for SDHC because of DMA/FIFO buffer restrictions
3 changes: 2 additions & 1 deletion cpu/native/periph/flashpage.c
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
*/

#include <assert.h>
#include <stdalign.h>
#include <string.h>

#include "cpu.h"
@@ -27,7 +28,7 @@
#define ENABLE_DEBUG 0
#include "debug.h"

__attribute__((aligned(FLASHPAGE_SIZE * FLASHPAGE_NUMOF)))
alignas(FLASHPAGE_SIZE * FLASHPAGE_NUMOF)
char _native_flash[FLASHPAGE_SIZE * FLASHPAGE_NUMOF];

void flashpage_erase(unsigned page)
2 changes: 1 addition & 1 deletion cpu/nrf5x_common/include/periph_cpu_common.h
Original file line number Diff line number Diff line change
@@ -417,7 +417,7 @@ typedef struct {
/**
* @brief USBDEV buffer instantiation requirement
*/
#define USBDEV_CPU_DMA_REQUIREMENTS __attribute__((aligned(USBDEV_CPU_DMA_ALIGNMENT)))
#define USBDEV_CPU_DMA_REQUIREMENTS alignas(USBDEV_CPU_DMA_ALIGNMENT)

#if !defined(CPU_FAM_NRF51) && !defined(DOXYGEN)
/**
5 changes: 3 additions & 2 deletions cpu/riscv_common/irq_arch.c
Original file line number Diff line number Diff line change
@@ -18,8 +18,9 @@
* @}
*/

#include <stdio.h>
#include <inttypes.h>
#include <stdalign.h>
#include <stdio.h>

#include "macros/xtstr.h"
#include "cpu.h"
@@ -160,7 +161,7 @@ static void handle_trap(uword_t mcause)
/* Marking this as interrupt to ensure an mret at the end, provided by the
* compiler. Aligned to 64-byte boundary as per RISC-V spec and required by some
* of the supported platforms (gd32)*/
__attribute((aligned(64)))
alignas(64)
static void __attribute__((interrupt)) trap_entry(void)
{
__asm__ volatile (
6 changes: 4 additions & 2 deletions cpu/sam0_common/include/periph_cpu_common.h
Original file line number Diff line number Diff line change
@@ -21,6 +21,8 @@
#ifndef PERIPH_CPU_COMMON_H
#define PERIPH_CPU_COMMON_H

#include <stdalign.h>

#include "cpu.h"
#include "exti_config.h"
#include "timer_config.h"
@@ -992,7 +994,7 @@ typedef struct {
/**
* @brief USBDEV buffer instantiation requirement
*/
#define USBDEV_CPU_DMA_REQUIREMENTS __attribute__((aligned(USBDEV_CPU_DMA_ALIGNMENT)))
#define USBDEV_CPU_DMA_REQUIREMENTS alignas(USBDEV_CPU_DMA_ALIGNMENT)

/**
* @brief USB peripheral parameters
@@ -1015,7 +1017,7 @@ typedef struct {
/**
* @brief SDIO/SDMMC buffer instantiation requirement for SDHC
*/
#define SDMMC_CPU_DMA_REQUIREMENTS __attribute__((aligned(SDMMC_CPU_DMA_ALIGNMENT)))
#define SDMMC_CPU_DMA_REQUIREMENTS alignas(SDMMC_CPU_DMA_ALIGNMENT)

/**
* @brief SDHC peripheral configuration
8 changes: 4 additions & 4 deletions cpu/sam0_common/periph/eth.c
Original file line number Diff line number Diff line change
@@ -78,8 +78,8 @@ struct eth_buf_desc {
/* GMAC buffer descriptors */
#define GMAC_DESC_ALIGNMENT 8
#define GMAC_BUF_ALIGNMENT 32
static struct eth_buf_desc rx_desc[ETH_RX_BUFFER_COUNT] __attribute__((aligned(GMAC_DESC_ALIGNMENT)));
static struct eth_buf_desc tx_desc[ETH_TX_BUFFER_COUNT] __attribute__((aligned(GMAC_DESC_ALIGNMENT)));
static alignas(GMAC_DESC_ALIGNMENT) struct eth_buf_desc rx_desc[ETH_RX_BUFFER_COUNT];
static alignas(GMAC_DESC_ALIGNMENT) struct eth_buf_desc tx_desc[ETH_TX_BUFFER_COUNT];

static struct eth_buf_desc *rx_curr;
static struct eth_buf_desc *tx_curr;
@@ -89,8 +89,8 @@ static struct eth_buf_desc *tx_curr;
static uint8_t tx_idx;
static uint8_t rx_idx;

static uint8_t rx_buf[ETH_RX_BUFFER_COUNT][ETH_RX_BUFFER_SIZE] __attribute__((aligned(GMAC_BUF_ALIGNMENT)));
static uint8_t tx_buf[ETH_TX_BUFFER_COUNT][ETH_TX_BUFFER_SIZE] __attribute__((aligned(GMAC_BUF_ALIGNMENT)));
static alignas(GMAC_BUF_ALIGNMENT) uint8_t rx_buf[ETH_RX_BUFFER_COUNT][ETH_RX_BUFFER_SIZE];
static alignas(GMAC_BUF_ALIGNMENT) uint8_t tx_buf[ETH_TX_BUFFER_COUNT][ETH_TX_BUFFER_SIZE];
extern sam0_eth_netdev_t _sam0_eth_dev;

static bool _is_sleeping;
3 changes: 2 additions & 1 deletion cpu/stm32/include/periph/cpu_sdmmc.h
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
#ifndef PERIPH_CPU_SDMMC_H
#define PERIPH_CPU_SDMMC_H

#include <stdalign.h>
#include <stdint.h>

#include "periph/cpu_dma.h"
@@ -45,7 +46,7 @@ extern "C" {
/**
* @brief SDIO/SDMMC buffer instantiation requirement for STM32
*/
#define SDMMC_CPU_DMA_REQUIREMENTS __attribute__((aligned(SDMMC_CPU_DMA_ALIGNMENT)))
#define SDMMC_CPU_DMA_REQUIREMENTS alignas(SDMMC_CPU_DMA_ALIGNMENT)

/**
* @brief SDIO/SDMMC pin structure for STM32
3 changes: 2 additions & 1 deletion cpu/stm32/include/periph/cpu_usbdev.h
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
#ifndef PERIPH_CPU_USBDEV_H
#define PERIPH_CPU_USBDEV_H

#include <stdalign.h>
#include <stdint.h>

#include "periph/cpu_gpio.h"
@@ -37,7 +38,7 @@ extern "C" {
/**
* @brief USBDEV buffer instantiation requirement
*/
#define USBDEV_CPU_DMA_REQUIREMENTS __attribute__((aligned(USBDEV_CPU_DMA_ALIGNMENT)))
#define USBDEV_CPU_DMA_REQUIREMENTS alignas(USBDEV_CPU_DMA_ALIGNMENT)

/**
* @brief stm32 USB device FS configuration
3 changes: 2 additions & 1 deletion drivers/include/periph/flashpage.h
Original file line number Diff line number Diff line change
@@ -48,6 +48,7 @@
#ifndef PERIPH_FLASHPAGE_H
#define PERIPH_FLASHPAGE_H

#include <stdalign.h>
#include <stddef.h>
#include <stdint.h>

@@ -171,7 +172,7 @@ enum {
* @param[in] size size of the array in unit of @ref FLASHPAGE_SIZE
*/
#define FLASH_WRITABLE_INIT(name, size) \
__attribute__((aligned(FLASHPAGE_SIZE))) \
alignas(FLASHPAGE_SIZE) \
__attribute__((section(".flash_writable." #name))) \
static const uint8_t name [size * FLASHPAGE_SIZE]
#endif
8 changes: 4 additions & 4 deletions drivers/mtd_flashpage/mtd_flashpage.c
Original file line number Diff line number Diff line change
@@ -21,9 +21,10 @@
* @}
*/

#include <string.h>
#include <errno.h>
#include <assert.h>
#include <errno.h>
#include <stdalign.h>
#include <string.h>

#include "architecture.h"
#include "cpu.h"
@@ -114,8 +115,7 @@ static int _write_page(mtd_dev_t *dev, const void *buf, uint32_t page, uint32_t

if ((addr % FLASHPAGE_WRITE_BLOCK_ALIGNMENT) || (size < FLASHPAGE_WRITE_BLOCK_SIZE) ||
((uintptr_t)buf % FLASHPAGE_WRITE_BLOCK_ALIGNMENT)) {
uint8_t tmp[FLASHPAGE_WRITE_BLOCK_SIZE]
__attribute__ ((aligned (FLASHPAGE_WRITE_BLOCK_ALIGNMENT)));
uint8_t alignas(FLASHPAGE_WRITE_BLOCK_ALIGNMENT) tmp[FLASHPAGE_WRITE_BLOCK_SIZE];

offset = addr % FLASHPAGE_WRITE_BLOCK_ALIGNMENT;
size = MIN(size, FLASHPAGE_WRITE_BLOCK_SIZE - offset);
3 changes: 2 additions & 1 deletion examples/lua_REPL/main.c
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
* @}
*/

#include <stdalign.h>
#include <stdio.h>
#include <string.h>

@@ -31,7 +32,7 @@
*/
#define MAIN_LUA_MEM_SIZE (40000)

static char lua_memory[MAIN_LUA_MEM_SIZE] __attribute__ ((aligned(__BIGGEST_ALIGNMENT__)));
static alignas(__BIGGEST_ALIGNMENT__) char lua_memory[MAIN_LUA_MEM_SIZE];

#define BARE_MINIMUM_MODS (LUAR_LOAD_BASE | LUAR_LOAD_IO | LUAR_LOAD_CORO | LUAR_LOAD_PACKAGE)

3 changes: 2 additions & 1 deletion examples/lua_basic/main.c
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
* @}
*/

#include <stdalign.h>
#include <stdio.h>
#include <errno.h>

@@ -32,7 +33,7 @@
#else
#define LUA_MEM_SIZE (11000)
#endif
static char lua_mem[LUA_MEM_SIZE] __attribute__ ((aligned(__BIGGEST_ALIGNMENT__)));
static alignas(__BIGGEST_ALIGNMENT__) char lua_mem[LUA_MEM_SIZE];

int lua_run_script(const uint8_t *buffer, size_t buffer_len)
{
5 changes: 3 additions & 2 deletions sys/include/architecture.h
Original file line number Diff line number Diff line change
@@ -24,9 +24,10 @@
#ifndef ARCHITECTURE_H
#define ARCHITECTURE_H

#include <stdint.h>
#include <inttypes.h>
#include <limits.h>
#include <stdalign.h>
#include <stdint.h>

#include "architecture_arch.h" /* IWYU pragma: export */

@@ -203,7 +204,7 @@ typedef uintptr_t uinttxtptr_t;
* char WORD_ALIGNED thread_stack[THREAD_STACKSIZE_DEFAULT];
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#define WORD_ALIGNED __attribute__((aligned(ARCHITECTURE_WORD_BYTES)))
#define WORD_ALIGNED alignas(ARCHITECTURE_WORD_BYTES)

/**
* @brief Check if @p addr is alignment to @p alignment
7 changes: 4 additions & 3 deletions sys/include/can/can.h
Original file line number Diff line number Diff line change
@@ -26,12 +26,13 @@
#ifndef CAN_CAN_H
#define CAN_CAN_H

#include <stdalign.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>

#if defined(__linux__)

#include <linux/can.h>
@@ -92,7 +93,7 @@ struct can_frame {
uint8_t __res0; /**< reserved / padding */
uint8_t __res1; /**< reserved / padding */
/** Frame data */
uint8_t data[CAN_MAX_DLEN] __attribute__((aligned(8)));
uint8_t alignas(8) data[CAN_MAX_DLEN];
};

/**
3 changes: 2 additions & 1 deletion sys/include/event.h
Original file line number Diff line number Diff line change
@@ -153,7 +153,8 @@ struct event {
/**
* @brief event queue structure
*/
typedef struct PTRTAG {
typedef struct {
PTRTAG
clist_node_t event_list; /**< list of queued events */
thread_t *waiter; /**< thread owning event queue */
} event_queue_t;
3 changes: 2 additions & 1 deletion sys/include/pm_layered.h
Original file line number Diff line number Diff line change
@@ -55,8 +55,9 @@ extern "C" {
* @brief Power Management mode blocker typedef
*/
typedef struct {
WORD_ALIGNED
uint8_t blockers[PM_NUM_MODES]; /**< number of blockers for the mode */
} WORD_ALIGNED pm_blocker_t;
} pm_blocker_t;

/**
* @brief Block a power mode
3 changes: 2 additions & 1 deletion sys/include/ptrtag.h
Original file line number Diff line number Diff line change
@@ -63,6 +63,7 @@

#include <assert.h>
#include <inttypes.h>
#include <stdalign.h>
#include <stdint.h>

#ifdef __cplusplus
@@ -74,7 +75,7 @@ extern "C" {
*
* @details This will ensure a minimum alignment of 4 bytes
*/
#define PTRTAG __attribute__((aligned(4)))
#define PTRTAG alignas(4)

/**
* @brief Create a tagged pointer
4 changes: 3 additions & 1 deletion sys/include/riotboot/flashwrite.h
Original file line number Diff line number Diff line change
@@ -59,6 +59,8 @@
#ifndef RIOTBOOT_FLASHWRITE_H
#define RIOTBOOT_FLASHWRITE_H

#include <stdalign.h>

#ifdef __cplusplus
extern "C" {
#endif
@@ -94,7 +96,7 @@ extern "C" {
* @brief Extra attributes required for the firmware intermediate buffer
*/
#define RIOTBOOT_FLASHPAGE_BUFFER_ATTRS \
__attribute__((aligned(FLASHPAGE_WRITE_BLOCK_ALIGNMENT)))
alignas(FLASHPAGE_WRITE_BLOCK_ALIGNMENT)

/**
* @brief firmware update state structure
Loading
Loading