chiark / gitweb /
macro: make sure ALIGN() can be calculated constant by the compiler
authorLennart Poettering <lennart@poettering.net>
Thu, 11 Apr 2013 00:07:14 +0000 (02:07 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 11 Apr 2013 21:10:40 +0000 (23:10 +0200)
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
src/shared/macro.h

index 3081664091af23e6629793efe057f1a00c644ff1..4d4f3b5260b2b115f04c9bfd6045720fcbd49e1a 100644 (file)
@@ -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;
 
index f884bf653fe41fd24ffd0b1e75f43b96ab54fdca..84a453a8f27319778eca17e5bd3e26330860dea4 100644 (file)
 #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));
 }