From 9be9c7cff600018279a0c3fa5fbe719bd1c0b8ad Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 11 Apr 2013 02:07:14 +0200 Subject: macro: make sure ALIGN() can be calculated constant by the compiler If we pass a constant value to ALIGN() gcc should have the chance to calculate the value during compilation rather than runtime, so let's avoid a static inline call if we can. --- src/libsystemd-bus/bus-message.c | 4 ++-- src/shared/macro.h | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c index 3081664091..4d4f3b5260 100644 --- a/src/libsystemd-bus/bus-message.c +++ b/src/libsystemd-bus/bus-message.c @@ -265,7 +265,7 @@ int bus_message_from_malloc( } else return -EBADMSG; - total = sizeof(struct bus_header) + ALIGN_TO(fs, 8) + bs; + total = sizeof(struct bus_header) + ALIGN8(fs) + bs; if (length != total) return -EBADMSG; @@ -283,7 +283,7 @@ int bus_message_from_malloc( m->sealed = true; m->header = h; m->fields = (uint8_t*) buffer + sizeof(struct bus_header); - m->body = (uint8_t*) buffer + sizeof(struct bus_header) + ALIGN_TO(fs, 8); + m->body = (uint8_t*) buffer + sizeof(struct bus_header) + ALIGN8(fs); m->fds = fds; m->n_fds = n_fds; diff --git a/src/shared/macro.h b/src/shared/macro.h index f884bf653f..84a453a8f2 100644 --- a/src/shared/macro.h +++ b/src/shared/macro.h @@ -53,7 +53,18 @@ #define STRINGIFY(x) XSTRINGIFY(x) /* Rounds up */ -#define ALIGN(l) ALIGN_TO((l), sizeof(void*)) + +#define ALIGN4(l) (((l) + 3) & ~3) +#define ALIGN8(l) (((l) + 7) & ~7) + +#if __SIZEOF_POINTER__ == 8 +#define ALIGN(l) ALIGN8(l) +#elif __SIZEOF_POINTER__ == 4 +#define ALIGN(l) ALIGN4(l) +#else +#error "Wut? Pointers are neither 4 nor 8 bytes long?" +#endif + static inline size_t ALIGN_TO(size_t l, size_t ali) { return ((l + ali - 1) & ~(ali - 1)); } -- cgit v1.2.3-54-g00ecf