diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-10-18 05:59:31 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-10-19 11:31:44 +0200 |
commit | 2977904cad8e2da69d03747b88bcbb5a824921e1 (patch) | |
tree | fa978839ecaa648f9588bf6736b17fbbda054c53 /src/fundamental | |
parent | macro: paranoia about overflow (diff) | |
download | systemd-2977904cad8e2da69d03747b88bcbb5a824921e1.tar.xz systemd-2977904cad8e2da69d03747b88bcbb5a824921e1.zip |
macro: introduce several helper functions for alignment
Some of them are not used in this commit, but will be used later.
Diffstat (limited to 'src/fundamental')
-rw-r--r-- | src/fundamental/macro-fundamental.h | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h index a311b01e30..797330dd97 100644 --- a/src/fundamental/macro-fundamental.h +++ b/src/fundamental/macro-fundamental.h @@ -379,6 +379,39 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) { return ((l + (ali - 1)) & ~(ali - 1)); } +static inline uint64_t ALIGN_TO_U64(uint64_t l, uint64_t ali) { + assert(ISPOWEROF2(ali)); + + if (l > UINT64_MAX - (ali - 1)) + return UINT64_MAX; /* indicate overflow */ + + return ((l + (ali - 1)) & ~(ali - 1)); +} + +static inline size_t ALIGN_DOWN(size_t l, size_t ali) { + assert(ISPOWEROF2(ali)); + + return l & ~(ali - 1); +} + +static inline uint64_t ALIGN_DOWN_U64(uint64_t l, uint64_t ali) { + assert(ISPOWEROF2(ali)); + + return l & ~(ali - 1); +} + +static inline size_t ALIGN_OFFSET(size_t l, size_t ali) { + assert(ISPOWEROF2(ali)); + + return l & (ali - 1); +} + +static inline uint64_t ALIGN_OFFSET_U64(uint64_t l, uint64_t ali) { + assert(ISPOWEROF2(ali)); + + return l & (ali - 1); +} + #define ALIGN2(l) ALIGN_TO(l, 2) #define ALIGN4(l) ALIGN_TO(l, 4) #define ALIGN8(l) ALIGN_TO(l, 8) |