[systemd-commits] 3 commits - src/resolve src/shared src/test

David Herrmann dvdhrm at kemper.freedesktop.org
Fri Aug 15 07:59:59 PDT 2014


 src/resolve/resolved-dns-stream.c |    2 -
 src/resolve/resolved-manager.c    |    2 -
 src/shared/macro.h                |   61 +++++++++++++++++++++-----------------
 src/test/test-util.c              |   23 ++++++++++++++
 4 files changed, 60 insertions(+), 28 deletions(-)

New commits:
commit bf32f99f65ec42539af65736b057c9e09d3987b9
Author: David Herrmann <dh.herrmann at gmail.com>
Date:   Fri Aug 15 16:58:16 2014 +0200

    resolve: fix compilation on LLVM+clang
    
    LLVM+clang does not allow statement-expressions inside of
    type-declarations (file-scope). Use CONST_MAX() to avoid this.

diff --git a/src/resolve/resolved-dns-stream.c b/src/resolve/resolved-dns-stream.c
index eb78587..8b3a3ce 100644
--- a/src/resolve/resolved-dns-stream.c
+++ b/src/resolve/resolved-dns-stream.c
@@ -64,7 +64,7 @@ static int dns_stream_complete(DnsStream *s, int error) {
 static int dns_stream_identify(DnsStream *s) {
         union {
                 struct cmsghdr header; /* For alignment */
-                uint8_t buffer[CMSG_SPACE(MAX(sizeof(struct in_pktinfo), sizeof(struct in6_pktinfo)))
+                uint8_t buffer[CMSG_SPACE(CONST_MAX(sizeof(struct in_pktinfo), sizeof(struct in6_pktinfo)))
                                + EXTRA_CMSG_SPACE /* kernel appears to require extra space */];
         } control;
         struct msghdr mh = {};
diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c
index bfbdc7d..04ee204 100644
--- a/src/resolve/resolved-manager.c
+++ b/src/resolve/resolved-manager.c
@@ -841,7 +841,7 @@ int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) {
         _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
         union {
                 struct cmsghdr header; /* For alignment */
-                uint8_t buffer[CMSG_SPACE(MAX(sizeof(struct in_pktinfo), sizeof(struct in6_pktinfo)))
+                uint8_t buffer[CMSG_SPACE(CONST_MAX(sizeof(struct in_pktinfo), sizeof(struct in6_pktinfo)))
                                + CMSG_SPACE(int) /* ttl/hoplimit */
                                + EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */];
         } control;

commit 7242d7420b04132f93f1426ec713f9b09bdeba54
Author: David Herrmann <dh.herrmann at gmail.com>
Date:   Fri Aug 15 16:54:52 2014 +0200

    macro: add CONST_MAX() macro
    
    The CONST_MAX() macro is similar to MAX(), but verifies that both
    arguments have the same type and are constant expressions. Furthermore,
    the result of CONST_MAX() is again a constant-expression.
    
    CONST_MAX() avoids any statement-expressions and other non-trivial
    expression-types. This avoids rather arbitrary restrictions in both GCC
    and LLVM, which both either fail with statement-expressions inside
    type-declarations or statement-expressions inside static-const
    initializations.
    
    If anybody knows how to circumvent this, please feel free to unify
    CONST_MAX() and MAX().

diff --git a/src/shared/macro.h b/src/shared/macro.h
index 11bd8b3..179b24c 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -140,6 +140,15 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
                         _a > _b ? _a : _b;              \
                 })
 
+/* evaluates to (void) if _A or _B are not constant or of different types */
+#define CONST_MAX(_A, _B) \
+        __extension__ (__builtin_choose_expr(                           \
+                __builtin_constant_p(_A) &&                             \
+                __builtin_constant_p(_B) &&                             \
+                __builtin_types_compatible_p(typeof(_A), typeof(_B)),   \
+                ((_A) > (_B)) ? (_A) : (_B),                            \
+                (void)0))
+
 #define MAX3(x,y,z)                                     \
         __extension__ ({                                \
                         const typeof(x) _c = MAX(x,y);  \
diff --git a/src/test/test-util.c b/src/test/test-util.c
index 7d81b0b..a8fa48a 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -70,6 +70,28 @@ static void test_align_power2(void) {
         }
 }
 
+static void test_max(void) {
+        static const struct {
+                int a;
+                int b[CONST_MAX(10, 100)];
+        } val1 = {
+                .a = CONST_MAX(10, 100),
+        };
+        int d = 0;
+
+        assert_cc(sizeof(val1.b) == sizeof(int) * 100);
+
+        /* CONST_MAX returns (void) instead of a value if the passed arguments
+         * are not of the same type or not constant expressions. */
+        assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 10)), int));
+        assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(d, 10)), void));
+        assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 1U)), void));
+
+        assert_se(val1.a == 100);
+        assert_se(MAX(++d, 0) == 1);
+        assert_se(d == 1);
+}
+
 static void test_first_word(void) {
         assert_se(first_word("Hello", ""));
         assert_se(first_word("Hello", "Hello"));
@@ -927,6 +949,7 @@ int main(int argc, char *argv[]) {
 
         test_streq_ptr();
         test_align_power2();
+        test_max();
         test_first_word();
         test_close_many();
         test_parse_boolean();

commit fdcba430aeae442ab0ea12a08d96cfc3d13f57ef
Author: David Herrmann <dh.herrmann at gmail.com>
Date:   Fri Aug 15 16:16:30 2014 +0200

    macro: const'ify MIN/MAX/... macros
    
    We must add 'const' to local variables in statement-expressions to
    guarantee that the macros can produce constant-expressions if given such.
    GCC seems to ignore this, but LLVM/clang requires it (understandably).

diff --git a/src/shared/macro.h b/src/shared/macro.h
index 5619c32..11bd8b3 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -133,46 +133,46 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
                 })
 
 #undef MAX
-#define MAX(a,b)                                 \
-        __extension__ ({                         \
-                        typeof(a) _a = (a);      \
-                        typeof(b) _b = (b);      \
-                        _a > _b ? _a : _b;       \
+#define MAX(a,b)                                        \
+        __extension__ ({                                \
+                        const typeof(a) _a = (a);       \
+                        const typeof(b) _b = (b);       \
+                        _a > _b ? _a : _b;              \
                 })
 
-#define MAX3(x,y,z)                              \
-        __extension__ ({                         \
-                        typeof(x) _c = MAX(x,y); \
-                        MAX(_c, z);              \
+#define MAX3(x,y,z)                                     \
+        __extension__ ({                                \
+                        const typeof(x) _c = MAX(x,y);  \
+                        MAX(_c, z);                     \
                 })
 
 #undef MIN
-#define MIN(a,b)                                \
-        __extension__ ({                        \
-                        typeof(a) _a = (a);     \
-                        typeof(b) _b = (b);     \
-                        _a < _b ? _a : _b;      \
+#define MIN(a,b)                                        \
+        __extension__ ({                                \
+                        const typeof(a) _a = (a);       \
+                        const typeof(b) _b = (b);       \
+                        _a < _b ? _a : _b;              \
                 })
 
-#define MIN3(x,y,z)                              \
-        __extension__ ({                         \
-                        typeof(x) _c = MIN(x,y); \
-                        MIN(_c, z);              \
+#define MIN3(x,y,z)                                     \
+        __extension__ ({                                \
+                        const typeof(x) _c = MIN(x,y);  \
+                        MIN(_c, z);                     \
                 })
 
-#define LESS_BY(A,B)                            \
-        __extension__ ({                        \
-                        typeof(A) _A = (A);     \
-                        typeof(B) _B = (B);     \
-                        _A > _B ? _A - _B : 0;  \
+#define LESS_BY(A,B)                                    \
+        __extension__ ({                                \
+                        const typeof(A) _A = (A);       \
+                        const typeof(B) _B = (B);       \
+                        _A > _B ? _A - _B : 0;          \
                 })
 
 #ifndef CLAMP
 #define CLAMP(x, low, high)                                             \
         __extension__ ({                                                \
-                        typeof(x) _x = (x);                             \
-                        typeof(low) _low = (low);                       \
-                        typeof(high) _high = (high);                    \
+                        const typeof(x) _x = (x);                       \
+                        const typeof(low) _low = (low);                 \
+                        const typeof(high) _high = (high);              \
                         ((_x > _high) ? _high : ((_x < _low) ? _low : _x)); \
                 })
 #endif



More information about the systemd-commits mailing list