ryantimwilson / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
Zbigniew Jędrzejewski-Szmek 62fe94
From 40a1eebde6be7ac3f1885147fc24e06ad1da260c Mon Sep 17 00:00:00 2001
Zbigniew Jędrzejewski-Szmek 62fe94
From: David Herrmann <dh.herrmann@gmail.com>
Zbigniew Jędrzejewski-Szmek 62fe94
Date: Fri, 22 Aug 2014 13:55:57 +0200
Zbigniew Jędrzejewski-Szmek 62fe94
Subject: [PATCH] shared: add MAXSIZE() and use it in resolved
Zbigniew Jędrzejewski-Szmek 62fe94
Zbigniew Jędrzejewski-Szmek 62fe94
The MAXSIZE() macro takes two types and returns the size of the larger
Zbigniew Jędrzejewski-Szmek 62fe94
one. It is much simpler to use than MAX(sizeof(A), sizeof(B)) and also
Zbigniew Jędrzejewski-Szmek 62fe94
avoids any compiler-extensions, unlike CONST_MAX() and MAX() (which are
Zbigniew Jędrzejewski-Szmek 62fe94
needed to avoid evaluating arguments more than once). This was suggested
Zbigniew Jędrzejewski-Szmek 62fe94
by Daniele Nicolodi <daniele@grinta.net>.
Zbigniew Jędrzejewski-Szmek 62fe94
Zbigniew Jędrzejewski-Szmek 62fe94
Also make resolved use this macro instead of CONST_MAX(). This enhances
Zbigniew Jędrzejewski-Szmek 62fe94
readability quite a bit.
Zbigniew Jędrzejewski-Szmek 62fe94
---
Zbigniew Jędrzejewski-Szmek 62fe94
 src/resolve/resolved-dns-stream.c | 2 +-
Zbigniew Jędrzejewski-Szmek 62fe94
 src/resolve/resolved-manager.c    | 2 +-
Zbigniew Jędrzejewski-Szmek 62fe94
 src/shared/macro.h                | 3 +++
Zbigniew Jędrzejewski-Szmek 62fe94
 src/test/test-util.c              | 4 ++++
Zbigniew Jędrzejewski-Szmek 62fe94
 4 files changed, 9 insertions(+), 2 deletions(-)
Zbigniew Jędrzejewski-Szmek 62fe94
Zbigniew Jędrzejewski-Szmek 62fe94
diff --git a/src/resolve/resolved-dns-stream.c b/src/resolve/resolved-dns-stream.c
Zbigniew Jędrzejewski-Szmek 62fe94
index 8b3a3ced4b..8aad5e4df1 100644
Zbigniew Jędrzejewski-Szmek 62fe94
--- a/src/resolve/resolved-dns-stream.c
Zbigniew Jędrzejewski-Szmek 62fe94
+++ b/src/resolve/resolved-dns-stream.c
Zbigniew Jędrzejewski-Szmek 62fe94
@@ -64,7 +64,7 @@ static int dns_stream_complete(DnsStream *s, int error) {
Zbigniew Jędrzejewski-Szmek 62fe94
 static int dns_stream_identify(DnsStream *s) {
Zbigniew Jędrzejewski-Szmek 62fe94
         union {
Zbigniew Jędrzejewski-Szmek 62fe94
                 struct cmsghdr header; /* For alignment */
Zbigniew Jędrzejewski-Szmek 62fe94
-                uint8_t buffer[CMSG_SPACE(CONST_MAX(sizeof(struct in_pktinfo), sizeof(struct in6_pktinfo)))
Zbigniew Jędrzejewski-Szmek 62fe94
+                uint8_t buffer[CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo))
Zbigniew Jędrzejewski-Szmek 62fe94
                                + EXTRA_CMSG_SPACE /* kernel appears to require extra space */];
Zbigniew Jędrzejewski-Szmek 62fe94
         } control;
Zbigniew Jędrzejewski-Szmek 62fe94
         struct msghdr mh = {};
Zbigniew Jędrzejewski-Szmek 62fe94
diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c
Zbigniew Jędrzejewski-Szmek 62fe94
index 56baf8730d..659b1dacc8 100644
Zbigniew Jędrzejewski-Szmek 62fe94
--- a/src/resolve/resolved-manager.c
Zbigniew Jędrzejewski-Szmek 62fe94
+++ b/src/resolve/resolved-manager.c
Zbigniew Jędrzejewski-Szmek 62fe94
@@ -841,7 +841,7 @@ int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) {
Zbigniew Jędrzejewski-Szmek 62fe94
         _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
Zbigniew Jędrzejewski-Szmek 62fe94
         union {
Zbigniew Jędrzejewski-Szmek 62fe94
                 struct cmsghdr header; /* For alignment */
Zbigniew Jędrzejewski-Szmek 62fe94
-                uint8_t buffer[CMSG_SPACE(CONST_MAX(sizeof(struct in_pktinfo), sizeof(struct in6_pktinfo)))
Zbigniew Jędrzejewski-Szmek 62fe94
+                uint8_t buffer[CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo))
Zbigniew Jędrzejewski-Szmek 62fe94
                                + CMSG_SPACE(int) /* ttl/hoplimit */
Zbigniew Jędrzejewski-Szmek 62fe94
                                + EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */];
Zbigniew Jędrzejewski-Szmek 62fe94
         } control;
Zbigniew Jędrzejewski-Szmek 62fe94
diff --git a/src/shared/macro.h b/src/shared/macro.h
Zbigniew Jędrzejewski-Szmek 62fe94
index 179b24c983..43fa3e556f 100644
Zbigniew Jędrzejewski-Szmek 62fe94
--- a/src/shared/macro.h
Zbigniew Jędrzejewski-Szmek 62fe94
+++ b/src/shared/macro.h
Zbigniew Jędrzejewski-Szmek 62fe94
@@ -149,6 +149,9 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
Zbigniew Jędrzejewski-Szmek 62fe94
                 ((_A) > (_B)) ? (_A) : (_B),                            \
Zbigniew Jędrzejewski-Szmek 62fe94
                 (void)0))
Zbigniew Jędrzejewski-Szmek 62fe94
 
Zbigniew Jędrzejewski-Szmek 62fe94
+/* takes two types and returns the size of the larger one */
Zbigniew Jędrzejewski-Szmek 62fe94
+#define MAXSIZE(A, B) (sizeof(union _packed_ { typeof(A) a; typeof(B) b; }))
Zbigniew Jędrzejewski-Szmek 62fe94
+
Zbigniew Jędrzejewski-Szmek 62fe94
 #define MAX3(x,y,z)                                     \
Zbigniew Jędrzejewski-Szmek 62fe94
         __extension__ ({                                \
Zbigniew Jędrzejewski-Szmek 62fe94
                         const typeof(x) _c = MAX(x,y);  \
Zbigniew Jędrzejewski-Szmek 62fe94
diff --git a/src/test/test-util.c b/src/test/test-util.c
Zbigniew Jędrzejewski-Szmek 62fe94
index ac1afce86b..34d5f2ed7d 100644
Zbigniew Jędrzejewski-Szmek 62fe94
--- a/src/test/test-util.c
Zbigniew Jędrzejewski-Szmek 62fe94
+++ b/src/test/test-util.c
Zbigniew Jędrzejewski-Szmek 62fe94
@@ -90,6 +90,10 @@ static void test_max(void) {
Zbigniew Jędrzejewski-Szmek 62fe94
         assert_se(val1.a == 100);
Zbigniew Jędrzejewski-Szmek 62fe94
         assert_se(MAX(++d, 0) == 1);
Zbigniew Jędrzejewski-Szmek 62fe94
         assert_se(d == 1);
Zbigniew Jędrzejewski-Szmek 62fe94
+
Zbigniew Jędrzejewski-Szmek 62fe94
+        assert_cc(MAXSIZE(char[3], uint16_t) == 3);
Zbigniew Jędrzejewski-Szmek 62fe94
+        assert_cc(MAXSIZE(char[3], uint32_t) == 4);
Zbigniew Jędrzejewski-Szmek 62fe94
+        assert_cc(MAXSIZE(char, long) == sizeof(long));
Zbigniew Jędrzejewski-Szmek 62fe94
 }
Zbigniew Jędrzejewski-Szmek 62fe94
 
Zbigniew Jędrzejewski-Szmek 62fe94
 static void test_first_word(void) {