VPP-189 Clean up more coverity warnings
[vpp.git] / vppinfra / vppinfra / bitmap.h
index d8fd154..35de1b4 100644 (file)
@@ -45,6 +45,8 @@
 #include <vppinfra/error.h>
 #include <vppinfra/bitops.h>   /* for count_set_bits */
 
+typedef uword clib_bitmap_t;
+
 /* Returns 1 if the entire bitmap is zero, 0 otherwise */
 always_inline uword
 clib_bitmap_is_zero (uword * ai)
@@ -338,6 +340,26 @@ always_inline uword clib_bitmap_first_set (uword * ai)
   return ~0;
 }
 
+/* Return highest numbered set bit in bitmap.
+
+    Return infinity (~0) if bitmap is zero. */
+always_inline uword clib_bitmap_last_set (uword * ai)
+{
+  uword i;
+
+  for (i = vec_len (ai); i > 0 ; i--)
+    {
+      uword x = ai[i - 1];
+      if (x != 0)
+       {
+         uword first_bit;
+         count_leading_zeros (first_bit, x);
+         return (i) * BITS (ai[0]) - first_bit - 1;
+       }
+    }
+  return ~0;
+}
+
 /* Return lowest numbered clear bit in bitmap. */
 always_inline uword
 clib_bitmap_first_clear (uword * ai)
@@ -542,7 +564,7 @@ unformat_bitmap_list(unformat_input_t * input, va_list * va)
       else
         goto error;
 
-      if ((b < a) || (b > 63))
+      if (b < a)
         goto error;
 
       for (i = a; i <= b; i++)
@@ -555,4 +577,27 @@ error:
   return 0;
 }
 
+static inline u8 *
+format_bitmap_hex(u8 * s, va_list * args)
+{
+  uword * bitmap = va_arg (*args, uword *);
+  int i, is_trailing_zero = 1;
+
+  if (!bitmap)
+    return format(s, "0");
+
+  i = vec_bytes (bitmap) * 2;
+
+  while (i > 0)
+    {
+      u8 x = clib_bitmap_get_multiple(bitmap, --i * 4, 4);
+
+      if (x && is_trailing_zero)
+        is_trailing_zero = 0;
+
+      if (x || !is_trailing_zero)
+          s = format(s, "%x", x);
+    }
+  return s;
+}
 #endif /* included_clib_bitmap_h */