Skip to content

Commit

Permalink
core/lib: Add macros/utils.h header
Browse files Browse the repository at this point in the history
The macros CONCAT(), MIN(), and MAX() are defined over and over again in
RIOT's code base. This de-duplicates the code by moving the macros to a
common place.
  • Loading branch information
Marian Buschsieweke committed Jan 7, 2023
1 parent ed3e5de commit 86fdbd7
Show file tree
Hide file tree
Showing 24 changed files with 124 additions and 103 deletions.
71 changes: 71 additions & 0 deletions core/lib/include/macros/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (C) 2023 Otto-von-Guericke-Universität Magdeburg
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup core_macros
* @{
*
* @file
* @brief Various helper macros
*
* @author Marian Buschsieweke <marian.buschsieweke@ovgu.de>
*/

#ifndef MACROS_UTILS_H
#define MACROS_UTILS_H

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Concatenate the tokens @p a and @p b
*/
#define CONCAT(a, b) a ## b

/**
* @brief Concatenate the tokens @p a , @p b , and @p c
*/
#define CONCAT3(a, b, c) a ## b ## c

/**
* @brief Concatenate the tokens @p a , @p b , @p c , and @p d
*/
#define CONCAT4(a, b, c, d) a ## b ## c ## d

/* For compatibility with vendor headers, only provide MAX() and MIN() if not
* provided. (The alternative approach of using #undef has the downside that
* vendor header files may provide a smarter version of MAX() / MIN() that does
* not evaluate the argument twice and rely on this).
*/
#ifndef MAX
/**
* @brief Get the maximum of the two parameters
*
* @note This is the trivial implementation that does evaluate the arguments
* more than once
*/
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif

#ifndef MIN
/**
* @brief Get the minimum of the two parameters
*
* @note This is the trivial implementation that does evaluate the arguments
* more than once
*/
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif

#ifdef __cplusplus
}
#endif

#endif /* MACROS_UTILS_H */
/** @} */
6 changes: 1 addition & 5 deletions cpu/atmega_common/periph/rtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "byteorder.h"
#include "cpu.h"
#include "irq.h"
#include "macros/utils.h"
#include "periph/rtt.h"
#include "periph_conf.h"

Expand Down Expand Up @@ -91,11 +92,6 @@ static inline void reg32_write(volatile uint8_t *reg_ll, uint32_t _val)
irq_restore(state);
}

/* To build proper register names */
#ifndef CONCAT
#define CONCAT(a, b) (a##b)
#endif

/* To read the whole 32-bit register */
#define RG_READ32(reg) (reg32_read(&CONCAT(reg, LL)))

Expand Down
11 changes: 1 addition & 10 deletions cpu/esp_common/include/esp_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern "C" {

#include "log.h"
#include "esp_common_log.h"
#include "macros/utils.h"
#include "macros/xtstr.h"

#if !defined(ICACHE_FLASH)
Expand Down Expand Up @@ -93,16 +94,6 @@ extern "C" {

#endif /* ENABLE_DEBUG */

/** gives the minimum of a and b */
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif

/** gives the maximum of a and b */
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif

/**
* @brief function name mappings for source code compatibility with ESP8266 port
* @{
Expand Down
8 changes: 3 additions & 5 deletions cpu/native/mtd/mtd_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@
* @author Vincent Dupont <vincent@otakeys.com>
*/

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

#include "macros/utils.h"
#include "mtd.h"
#include "mtd_native.h"

#include "native_internal.h"

#define ENABLE_DEBUG 0
#include "debug.h"

#define MIN(a, b) ((a) > (b) ? (b) : (a))

static int _init(mtd_dev_t *dev)
{
mtd_native_dev_t *_dev = (mtd_native_dev_t*) dev;
Expand Down
3 changes: 1 addition & 2 deletions cpu/sam0_common/periph/flashpage.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@
#include <string.h>

#include "cpu.h"
#include "macros/utils.h"
#include "periph/flashpage.h"
#include "unaligned.h"

#define ENABLE_DEBUG 0
#include "debug.h"

#define MIN(x, y) (((x) < (y)) ? (x) : (y))

/* Write Quad Word is the only allowed operation on AUX pages */
#if defined(NVMCTRL_CTRLB_CMD_WQW)
#define AUX_CHUNK_SIZE (4 * sizeof(uint32_t))
Expand Down
5 changes: 2 additions & 3 deletions cpu/stm32/periph/eth.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
#include "bitarithm.h"
#include "board.h"
#include "iolist.h"
#include "macros/utils.h"
#include "mii.h"
#include "mutex.h"
#include "net/ethernet.h"
#include "net/eui_provider.h"
#include "net/netdev/eth.h"
#include "periph/gpio.h"
#include "periph/gpio_ll.h"
#include "timex.h"
#include "time_units.h"

#define ENABLE_DEBUG 0
#define ENABLE_DEBUG_VERBOSE 0
Expand Down Expand Up @@ -91,8 +92,6 @@ static ztimer_t _link_status_timer;
#warning "Total RX buffers lower than MTU, you won't receive huge frames!"
#endif

#define MIN(a, b) (((a) <= (b)) ? (a) : (b))

/**
* @name GPIOs to use for tracing STM32 Ethernet state via module
* `stm32_eth_tracing`
Expand Down
5 changes: 1 addition & 4 deletions drivers/at24cxxx/at24cxxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <string.h>

#include "assert.h"
#include "macros/utils.h"
#include "xtimer.h"

#include "at24cxxx_defines.h"
Expand All @@ -30,10 +31,6 @@
#define ENABLE_DEBUG 0
#include "debug.h"

#ifndef MIN
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#endif

/**
* @brief Calculate x mod y, if y is a power of 2
*/
Expand Down
3 changes: 1 addition & 2 deletions drivers/lsm6dsl/lsm6dsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@
#include <assert.h>

#include "ztimer.h"
#include "macros/utils.h"

#include "lsm6dsl.h"
#include "lsm6dsl_internal.h"

#define ENABLE_DEBUG 0
#include "debug.h"

#define MAX(a, b) ((a) > (b) ? (a) : (b))

#define BUS (dev->params.i2c)
#define ADDR (dev->params.addr)

Expand Down
5 changes: 2 additions & 3 deletions drivers/mtd_sdcard/mtd_sdcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@
*/
#define ENABLE_DEBUG 0
#include "debug.h"
#include "kernel_defines.h"
#include "macros/utils.h"
#include "mtd.h"
#include "mtd_sdcard.h"
#include "sdcard_spi.h"
#include "sdcard_spi_internal.h"
#include "kernel_defines.h"

#include <inttypes.h>
#include <errno.h>
#include <string.h>

#define MIN(a, b) ((a) > (b) ? (b) : (a))

static int mtd_sdcard_init(mtd_dev_t *dev)
{
DEBUG("mtd_sdcard_init\n");
Expand Down
10 changes: 5 additions & 5 deletions drivers/mtd_spi_nor/mtd_spi_nor.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@
#include <string.h>
#include <errno.h>

#include "byteorder.h"
#include "kernel_defines.h"
#include "macros/utils.h"
#include "mtd.h"
#include "mtd_spi_nor.h"
#include "thread.h"

#if IS_USED(MODULE_ZTIMER_USEC)
#include "ztimer.h"
#elif IS_USED(MODULE_XTIMER)
#include "xtimer.h"
#endif
#include "thread.h"
#include "byteorder.h"
#include "mtd_spi_nor.h"

#define ENABLE_DEBUG 0
#include "debug.h"
Expand All @@ -61,8 +63,6 @@

#define MBIT_AS_BYTES ((1024 * 1024) / 8)

#define MIN(a, b) ((a) > (b) ? (b) : (a))

/**
* @brief JEDEC memory manufacturer ID codes.
*
Expand Down
2 changes: 1 addition & 1 deletion pkg/lua/contrib/lua_run.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <setjmp.h>

#include "kernel_defines.h"
#include "macros/utils.h"
#include "tlsf.h"

#include "lua.h"
Expand Down Expand Up @@ -272,7 +273,6 @@ LUALIB_API int lua_riot_do_buffer(const uint8_t *buf, size_t buflen, void *memor
}

#define MAX_ERR_STRING (ARRAY_SIZE(lua_riot_str_errors) - 1)
#define MIN(x, y) (((x) < (y)) ? (x) : (y))

LUALIB_API const char *lua_riot_strerror(int errn)
{
Expand Down
12 changes: 5 additions & 7 deletions sys/can/isotp/isotp.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
#include <errno.h>
#include <string.h>

#include "net/gnrc/pktbuf.h"

#include "can/isotp.h"
#include "can/common.h"
#include "can/isotp.h"
#include "can/raw.h"
#include "can/router.h"
#include "thread.h"
#include "macros/utils.h"
#include "mutex.h"
#include "net/gnrc/pktbuf.h"
#include "thread.h"
#include "timex.h"
#include "ztimer.h"
#include "utlist.h"
#include "ztimer.h"

#define ENABLE_DEBUG 0
#include "debug.h"
Expand Down Expand Up @@ -88,8 +88,6 @@ static kernel_pid_t isotp_pid = KERNEL_PID_UNDEF;
static struct isotp *isotp_list = NULL;
static mutex_t lock = MUTEX_INIT;

#define MIN(a, b) (((a) < (b)) ? (a) : (b))

static void _rx_timeout(void *arg);
static int _isotp_send_fc(struct isotp *isotp, int ae, uint8_t status);
static int _isotp_tx_send(struct isotp *isotp, struct can_frame *frame);
Expand Down
3 changes: 1 addition & 2 deletions sys/hashes/aes128_cmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@

#include "crypto/ciphers.h"
#include "hashes/aes128_cmac.h"

#define MIN(a, b) a < b ? a : b
#include "macros/utils.h"

static void _xor128(uint8_t *x, uint8_t *y)
{
Expand Down
8 changes: 4 additions & 4 deletions sys/hashes/sha3.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@
================================================================
*/

#include <hashes/sha3.h>
#include <stdint.h>
#include <string.h>

#include "hashes/sha3.h"
#include "macros/utils.h"

/**
* Function to compute the Keccak[r, c] sponge function over a given input.
Expand Down Expand Up @@ -330,9 +333,6 @@ static void KeccakF1600_StatePermute(void *state)
================================================================
*/

#include <string.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b))

static void Keccak(unsigned int rate, unsigned int capacity, const unsigned char *input,
unsigned long long int inputByteLen, unsigned char delimitedSuffix,
unsigned char *output, unsigned long long int outputByteLen)
Expand Down
11 changes: 1 addition & 10 deletions sys/include/atomic_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
#include <stdint.h>

#include "irq.h"
#include "macros/utils.h"
#include "sched.h"

#include "atomic_utils_arch.h"
Expand Down Expand Up @@ -849,16 +850,6 @@ static inline uint64_t semi_atomic_fetch_and_u64(volatile uint64_t *dest,

/* Fallback implementations of atomic utility functions: */

/**
* @brief Concatenate two tokens
*/
#define CONCAT(a, b) a ## b

/**
* @brief Concatenate four tokens
*/
#define CONCAT4(a, b, c, d) a ## b ## c ## d

/**
* @brief Generates a static inline function implementing
* `atomic_load_u<width>()`
Expand Down
8 changes: 3 additions & 5 deletions sys/net/gnrc/link_layer/lorawan/gnrc_lorawan_region.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@
* @file
* @author José Ignacio Alamos <jose.alamos@haw-hamburg.de>
*/
#include "kernel_defines.h"
#include "bitarithm.h"
#include "random.h"
#include "kernel_defines.h"
#include "macros/utils.h"
#include "net/gnrc/lorawan/region.h"
#include "random.h"

#define ENABLE_DEBUG 0
#include "debug.h"

#define GNRC_LORAWAN_DATARATES_NUMOF (6U)

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))

static uint8_t dr_sf[GNRC_LORAWAN_DATARATES_NUMOF] =
{ LORA_SF12, LORA_SF11, LORA_SF10, LORA_SF9, LORA_SF8, LORA_SF7 };
static uint8_t dr_bw[GNRC_LORAWAN_DATARATES_NUMOF] =
Expand Down
Loading

0 comments on commit 86fdbd7

Please sign in to comment.