This makes it easy for callers to choose all-ones or all-zeros based on
a parameter instead of choice of function.

Signed-off-by: Ben Pfaff <[email protected]>
---
 lib/util.c | 25 ++++++++++++-------------
 lib/util.h |  1 +
 2 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/lib/util.c b/lib/util.c
index b832ac143249..36e373120af8 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -1230,34 +1230,33 @@ const uint8_t count_1bits_8[256] = {
 };
 #endif
 
-/* Returns true if the 'n' bytes starting at 'p' are zeros. */
+/* Returns true if the 'n' bytes starting at 'p' are 'byte'. */
 bool
-is_all_zeros(const void *p_, size_t n)
+is_all_byte(const void *p_, size_t n, uint8_t byte)
 {
     const uint8_t *p = p_;
     size_t i;
 
     for (i = 0; i < n; i++) {
-        if (p[i] != 0x00) {
+        if (p[i] != byte) {
             return false;
         }
     }
     return true;
 }
 
-/* Returns true if the 'n' bytes starting at 'p' are 0xff. */
+/* Returns true if the 'n' bytes starting at 'p' are zeros. */
 bool
-is_all_ones(const void *p_, size_t n)
+is_all_zeros(const void *p, size_t n)
 {
-    const uint8_t *p = p_;
-    size_t i;
+    return is_all_byte(p, n, 0);
+}
 
-    for (i = 0; i < n; i++) {
-        if (p[i] != 0xff) {
-            return false;
-        }
-    }
-    return true;
+/* Returns true if the 'n' bytes starting at 'p' are 0xff. */
+bool
+is_all_ones(const void *p, size_t n)
+{
+    return is_all_byte(p, n, 0xff);
 }
 
 /* Copies 'n_bits' bits starting from bit 'src_ofs' in 'src' to the 'n_bits'
diff --git a/lib/util.h b/lib/util.h
index 4706c99fe2cb..c45e6ebdb413 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -411,6 +411,7 @@ static inline ovs_be32 be32_prefix_mask(int plen)
 
 bool is_all_zeros(const void *, size_t);
 bool is_all_ones(const void *, size_t);
+bool is_all_byte(const void *, size_t, uint8_t byte);
 void bitwise_copy(const void *src, unsigned int src_len, unsigned int src_ofs,
                   void *dst, unsigned int dst_len, unsigned int dst_ofs,
                   unsigned int n_bits);
-- 
2.10.2

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to