diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2013-08-02 15:10:20 +0200 |
---|---|---|
committer | Tobias Klauser <tklauser@distanz.ch> | 2013-08-02 15:12:29 +0200 |
commit | 98c15cbb658f226d52108ea2cb5b3bc27bc6a977 (patch) | |
tree | 74a1e2b730676a26ebf6ff9d6487e6dfc5ef012b | |
parent | c95c515fa9075443900734039cfd7f92b5ba832f (diff) |
built_in: Add min_t() and max_t() macros and use them
Introduce non-typechecking versions of min_t() and max_t() and use them
where a cast would be needed.
The macros were taken from the Linux Kernel, release under GPL v2.
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
-rw-r--r-- | built_in.h | 18 | ||||
-rw-r--r-- | geoip.c | 6 | ||||
-rw-r--r-- | mac80211.c | 2 | ||||
-rw-r--r-- | proto_ipv4.c | 4 |
4 files changed, 24 insertions, 6 deletions
@@ -109,6 +109,15 @@ typedef uint8_t u8; }) #endif /* max */ +#ifndef max_t +# define max_t(type, a, b) \ + ({ \ + type ___max1 = (a); \ + type ___max2 = (b); \ + ___max1 > ___max2 ? ___max1 : ___max2; \ + }) +#endif /* max_t */ + #ifndef min # define min(a, b) \ ({ \ @@ -118,6 +127,15 @@ typedef uint8_t u8; }) #endif /* min */ +#ifndef min_t +# define min_t(type, a, b) \ + ({ \ + type ___min1 = (a); \ + type ___min2 = (b); \ + ___min1 < ___min2 ? ___min1 : ___min2; \ + }) +#endif /* min_t */ + #ifndef ispow2 # define ispow2(x) ({ !!((x) && !((x) & ((x) - 1))); }) #endif @@ -198,15 +198,15 @@ again: raw[sizeof(raw) - 1] = 0; for (i = 0; i < ret; i++) { - if (!strncmp(raw + i, "Content-Length: ", min((size_t)(ret - i), lenl))) { + if (!strncmp(raw + i, "Content-Length: ", min_t(size_t, ret - i, lenl))) { ptr = raw + i + lenl; rtotlen = strtoul(ptr, NULL, 10); } - if (!strncmp(raw + i, "HTTP/1.1 200 OK", min((size_t)(ret - i), lent))) + if (!strncmp(raw + i, "HTTP/1.1 200 OK", min_t(size_t, ret - i, lent))) good = 1; - if (!strncmp(raw + i, "\r\n\r\n", min((size_t)(ret - i), lenc))) { + if (!strncmp(raw + i, "\r\n\r\n", min_t(size_t, ret - i, lenc))) { ptr = raw + i + lenc; len = ret - i - lenc; found = 1; @@ -56,7 +56,7 @@ static void get_mac80211_phydev(const char *device, char *phydev_path, } xfree(pathstr); - phydev_path[min(num, phydev_len - 1)] = 0; + phydev_path[min_t(size_t, num, phydev_len - 1)] = 0; } static inline struct nl_msg *nl80211_nlmsg_xalloc(void) diff --git a/proto_ipv4.c b/proto_ipv4.c index e11098f..303319e 100644 --- a/proto_ipv4.c +++ b/proto_ipv4.c @@ -121,7 +121,7 @@ static void ipv4(struct pkt_buff *pkt) tprintf(") ]\n"); } - opts_len = max((uint8_t) ip->h_ihl, sizeof(*ip) / sizeof(uint32_t)) * + opts_len = max_t(uint8_t, ip->h_ihl, sizeof(*ip) / sizeof(uint32_t)) * sizeof(uint32_t) - sizeof(*ip); for (opt = pkt_pull(pkt, opts_len); opt && opts_len > 0; opt++) { @@ -184,7 +184,7 @@ static void ipv4_less(struct pkt_buff *pkt) ntohs(ip->h_tot_len)); /* cut off IP options and everything that is not part of IPv4 payload */ - pkt_pull(pkt, max((uint8_t) ip->h_ihl, sizeof(*ip) / sizeof(uint32_t)) + pkt_pull(pkt, max_t(uint8_t, ip->h_ihl, sizeof(*ip) / sizeof(uint32_t)) * sizeof(uint32_t) - sizeof(*ip)); /* XXX there coul still be an Ethernet trailer included or others */ #if 0 |