From 5f3259b6207aeda9b7f58b83c403e15609c3dbdf Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 13 Oct 2022 10:48:05 +0200 Subject: [PATCH] std-aux: add NM_ALIGN*() macros Taken from systemd's ALIGN(), ALIGN_TO(), etc. --- src/libnm-std-aux/nm-std-aux.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/libnm-std-aux/nm-std-aux.h b/src/libnm-std-aux/nm-std-aux.h index c11be14422..96e379926f 100644 --- a/src/libnm-std-aux/nm-std-aux.h +++ b/src/libnm-std-aux/nm-std-aux.h @@ -410,6 +410,24 @@ nm_mult_clamped_u(unsigned a, unsigned b) /*****************************************************************************/ +static inline size_t +NM_ALIGN_TO(size_t l, size_t ali) +{ + nm_assert(nm_utils_is_power_of_two(ali)); + + if (l > SIZE_MAX - (ali - 1)) + return SIZE_MAX; /* indicate overflow */ + + return ((l + ali - 1) & ~(ali - 1)); +} + +#define NM_ALIGN4(l) NM_ALIGN_TO(l, 4) +#define NM_ALIGN8(l) NM_ALIGN_TO(l, 8) +#define NM_ALIGN(l) NM_ALIGN_TO(l, sizeof(void *)) +#define NM_ALIGN_PTR(p) ((void *) NM_ALIGN((uintptr_t) (p))) + +/*****************************************************************************/ + #define NM_SWAP(p_a, p_b) \ do { \ typeof(*(p_a)) *const _p_a = (p_a); \