[ovs-dev] [urcu v2 03/15] util: New macro PAD_SIZE.

Ben Pfaff blp at nicira.com
Tue Mar 11 20:56:05 UTC 2014


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 at nicira.com>
---
 lib/netlink.c     |    6 +++---
 lib/nx-match.c    |   10 +++++-----
 lib/ofp-actions.c |    6 +++---
 lib/ofp-actions.h |    2 +-
 lib/util.h        |    3 +++
 5 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/lib/netlink.c b/lib/netlink.c
index ada429e..3f479be 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 437e85b..de79009 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 3cd1e8b..273ceae 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 0f6bf70..0a1b46d 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);
diff --git a/lib/util.h b/lib/util.h
index 9afe10e..41fd51d 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))
 
-- 
1.7.10.4




More information about the dev mailing list