From: Ben Pfaff Date: Tue, 11 Mar 2014 06:50:54 +0000 (-0700) Subject: util: New macro PAD_SIZE. X-Git-Tag: sliver-openvswitch-2.2.90-1~6^2~118 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=f6e984d7949a1d5e2b82683e2ecf6deb4b144e93;p=sliver-openvswitch.git util: New macro PAD_SIZE. PAD_SIZE(x,y) is a little shorter and may have a more obvious meaning than ROUND_UP(x,y) - x. I intend to add more users in an upcoming comment. Signed-off-by: Ben Pfaff Acked-by: Andy Zhou --- diff --git a/lib/netlink.c b/lib/netlink.c index ada429e9f..3f479bef8 100644 --- a/lib/netlink.c +++ b/lib/netlink.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc. + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -174,7 +174,7 @@ nl_msg_put(struct ofpbuf *msg, const void *data, size_t size) void * nl_msg_put_uninit(struct ofpbuf *msg, size_t size) { - size_t pad = NLMSG_ALIGN(size) - size; + size_t pad = PAD_SIZE(size, NLMSG_ALIGNTO); char *p = ofpbuf_put_uninit(msg, size + pad); if (pad) { memset(p + size, 0, pad); @@ -197,7 +197,7 @@ nl_msg_push(struct ofpbuf *msg, const void *data, size_t size) void * nl_msg_push_uninit(struct ofpbuf *msg, size_t size) { - size_t pad = NLMSG_ALIGN(size) - size; + size_t pad = PAD_SIZE(size, NLMSG_ALIGNTO); char *p = ofpbuf_push_uninit(msg, size + pad); if (pad) { memset(p + size, 0, pad); diff --git a/lib/nx-match.c b/lib/nx-match.c index 437e85b2a..de790098a 100644 --- a/lib/nx-match.c +++ b/lib/nx-match.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc. + * Copyright (c) 2010, 2011, 2012, 2013, 2014 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -730,7 +730,7 @@ nx_put_match(struct ofpbuf *b, const struct match *match, { int match_len = nx_put_raw(b, false, match, cookie, cookie_mask); - ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len); + ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8)); return match_len; } @@ -753,7 +753,7 @@ oxm_put_match(struct ofpbuf *b, const struct match *match) ofpbuf_put_uninit(b, sizeof *omh); match_len = nx_put_raw(b, true, match, cookie, cookie_mask) + sizeof *omh; - ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len); + ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8)); omh = ofpbuf_at(b, start_len, sizeof *omh); omh->type = htons(OFPMT_OXM); @@ -994,7 +994,7 @@ int nx_match_from_string(const char *s, struct ofpbuf *b) { int match_len = nx_match_from_string_raw(s, b); - ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len); + ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8)); return match_len; } @@ -1007,7 +1007,7 @@ oxm_match_from_string(const char *s, struct ofpbuf *b) ofpbuf_put_uninit(b, sizeof *omh); match_len = nx_match_from_string_raw(s, b) + sizeof *omh; - ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len); + ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8)); omh = ofpbuf_at(b, start_len, sizeof *omh); omh->type = htons(OFPMT_OXM); diff --git a/lib/ofp-actions.c b/lib/ofp-actions.c index 3cd1e8b40..273ceae57 100644 --- a/lib/ofp-actions.c +++ b/lib/ofp-actions.c @@ -3627,8 +3627,8 @@ ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact) void ofpact_pad(struct ofpbuf *ofpacts) { - unsigned int rem = ofpacts->size % OFPACT_ALIGNTO; - if (rem) { - ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem); + unsigned int pad = PAD_SIZE(ofpacts->size, OFPACT_ALIGNTO); + if (pad) { + ofpbuf_put_zeros(ofpacts, pad); } } diff --git a/lib/ofp-actions.h b/lib/ofp-actions.h index 5fd318575..9dee74af4 100644 --- a/lib/ofp-actions.h +++ b/lib/ofp-actions.h @@ -436,7 +436,7 @@ struct ofpact_meter { * Used for OFPIT11_WRITE_ACTIONS. */ struct ofpact_nest { struct ofpact ofpact; - uint8_t pad[OFPACT_ALIGN(sizeof(struct ofpact)) - sizeof(struct ofpact)]; + uint8_t pad[PAD_SIZE(sizeof(struct ofpact), OFPACT_ALIGNTO)]; struct ofpact actions[]; }; BUILD_ASSERT_DECL(offsetof(struct ofpact_nest, actions) % OFPACT_ALIGNTO == 0); diff --git a/lib/util.h b/lib/util.h index 53039da41..8c4d9d6a5 100644 --- a/lib/util.h +++ b/lib/util.h @@ -111,6 +111,9 @@ extern const char *program_name; /* Returns X rounded up to the nearest multiple of Y. */ #define ROUND_UP(X, Y) (DIV_ROUND_UP(X, Y) * (Y)) +/* Returns the least number that, when added to X, yields a multiple of Y. */ +#define PAD_SIZE(X, Y) (ROUND_UP(X, Y) - (X)) + /* Returns X rounded down to the nearest multiple of Y. */ #define ROUND_DOWN(X, Y) ((X) / (Y) * (Y))