util: New macro PAD_SIZE.
authorBen Pfaff <blp@nicira.com>
Tue, 11 Mar 2014 06:50:54 +0000 (23:50 -0700)
committerBen Pfaff <blp@nicira.com>
Thu, 13 Mar 2014 19:45:12 +0000 (12:45 -0700)
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 <blp@nicira.com>
Acked-by: Andy Zhou <azhou@nicira.com>
lib/netlink.c
lib/nx-match.c
lib/ofp-actions.c
lib/ofp-actions.h
lib/util.h

index ada429e..3f479be 100644 (file)
@@ -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);
index 437e85b..de79009 100644 (file)
@@ -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);
index 3cd1e8b..273ceae 100644 (file)
@@ -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);
     }
 }
index 5fd3185..9dee74a 100644 (file)
@@ -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);
index 53039da..8c4d9d6 100644 (file)
@@ -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))