FD.io VPP  v21.06
Vector Packet Processing
bitmap.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright(c) 2021 Cisco Systems, Inc.
3  */
4 
5 #include <vppinfra/vec.h>
6 #include <vppinfra/bitmap.h>
7 
8 /** unformat an any sized hexadecimal bitmask into a bitmap
9 
10  uword * bitmap;
11  rv = unformat ("%U", unformat_bitmap_mask, &bitmap);
12 
13  Standard unformat_function_t arguments
14 
15  @param input - pointer an unformat_input_t
16  @param va - varargs list comprising a single uword **
17  @returns 1 on success, 0 on failure
18 */
19 __clib_export uword
21 {
22  u8 *v = 0; /* hexadecimal vector */
23  uword **bitmap_return = va_arg (*va, uword **);
24  uword *bitmap = 0;
25 
26  if (unformat (input, "%U", unformat_hex_string, &v))
27  {
28  int i, s = vec_len (v) - 1; /* 's' for significance or shift */
29 
30  /* v[0] holds the most significant byte */
31  for (i = 0; s >= 0; i++, s--)
32  bitmap = clib_bitmap_set_multiple (bitmap, s * BITS (v[i]), v[i],
33  BITS (v[i]));
34 
35  vec_free (v);
36  *bitmap_return = bitmap;
37  return 1;
38  }
39 
40  return 0;
41 }
42 
43 /** unformat a list of bit ranges into a bitmap (eg "0-3,5-7,11" )
44 
45  uword * bitmap;
46  rv = unformat ("%U", unformat_bitmap_list, &bitmap);
47 
48  Standard unformat_function_t arguments
49 
50  @param input - pointer an unformat_input_t
51  @param va - varargs list comprising a single uword **
52  @returns 1 on success, 0 on failure
53 */
54 __clib_export uword
56 {
57  uword **bitmap_return = va_arg (*va, uword **);
58  uword *bitmap = 0;
59 
60  u32 a, b;
61 
63  {
64  int i;
65  if (unformat (input, "%u-%u,", &a, &b))
66  ;
67  else if (unformat (input, "%u,", &a))
68  b = a;
69  else if (unformat (input, "%u-%u", &a, &b))
70  ;
71  else if (unformat (input, "%u", &a))
72  b = a;
73  else if (bitmap)
74  {
75  unformat_put_input (input);
76  break;
77  }
78  else
79  goto error;
80 
81  if (b < a)
82  goto error;
83 
84  for (i = a; i <= b; i++)
85  bitmap = clib_bitmap_set (bitmap, i, 1);
86  }
87  *bitmap_return = bitmap;
88  return 1;
89 error:
90  clib_bitmap_free (bitmap);
91  return 0;
92 }
93 
94 /** Format a bitmap as a string of hex bytes
95 
96  uword * bitmap;
97  s = format ("%U", format_bitmap_hex, bitmap);
98 
99  Standard format_function_t arguments
100 
101  @param s - string under construction
102  @param args - varargs list comprising a single uword *
103  @returns string under construction
104 */
105 
106 __clib_export u8 *
107 format_bitmap_hex (u8 *s, va_list *args)
108 {
109  uword *bitmap = va_arg (*args, uword *);
110  int i, is_trailing_zero = 1;
111 
112  if (!bitmap)
113  return format (s, "0");
114 
115  i = vec_bytes (bitmap) * 2;
116 
117  while (i > 0)
118  {
119  u8 x = clib_bitmap_get_multiple (bitmap, --i * 4, 4);
120 
121  if (x && is_trailing_zero)
122  is_trailing_zero = 0;
123 
124  if (x || !is_trailing_zero)
125  s = format (s, "%x", x);
126  }
127  return s;
128 }
129 
130 /** Format a bitmap as a list
131 
132  uword * bitmap;
133  s = format ("%U", format_bitmap_list, bitmap);
134 
135  Standard format_function_t arguments
136 
137  @param s - string under construction
138  @param args - varargs list comprising a single uword *
139  @returns string under construction
140 */
141 
142 __clib_export u8 *
143 format_bitmap_list (u8 *s, va_list *args)
144 {
145  uword *bitmap = va_arg (*args, uword *);
146  uword fs, fc;
147 
148  if (!bitmap)
149  return s;
150 
151  fs = clib_bitmap_first_set (bitmap);
152  if (fs == ~0)
153  return s;
154 
155  while (1)
156  {
157  fc = clib_bitmap_next_clear (bitmap, fs + 1);
158  if (fc > fs + 1)
159  s = format (s, "%lu-%lu", fs, fc - 1);
160  else
161  s = format (s, "%lu", fs);
162 
163  if ((fs = clib_bitmap_next_set (bitmap, fc)) == ~0)
164  return s;
165  s = format (s, ", ");
166  }
167 }
a
Definition: bitmap.h:544
__clib_export uword unformat_bitmap_mask(unformat_input_t *input, va_list *va)
unformat an any sized hexadecimal bitmask into a bitmap
Definition: bitmap.c:20
unformat_function_t unformat_hex_string
Definition: format.h:281
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap. ...
Definition: bitmap.h:167
#define vec_bytes(v)
Number of data bytes in vector.
unsigned char u8
Definition: types.h:56
vlib_buffer_t ** b
unsigned int u32
Definition: types.h:88
description fragment has unexpected format
Definition: map.api:433
Definition: cJSON.c:88
static uword clib_bitmap_first_set(uword *ai)
Return the lowest numbered set bit in a bitmap.
Definition: bitmap.h:391
struct _unformat_input_t unformat_input_t
static void unformat_put_input(unformat_input_t *input)
Definition: format.h:197
__clib_export u8 * format_bitmap_hex(u8 *s, va_list *args)
Format a bitmap as a string of hex bytes.
Definition: bitmap.c:107
static uword clib_bitmap_get_multiple(uword *bitmap, uword i, uword n_bits)
Gets the ith through ith + n_bits bit values from a bitmap.
Definition: bitmap.h:235
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:395
__clib_export uword unformat_bitmap_list(unformat_input_t *input, va_list *va)
unformat a list of bit ranges into a bitmap (eg "0-3,5-7,11" )
Definition: bitmap.c:55
static uword * clib_bitmap_set_multiple(uword *bitmap, uword i, uword value, uword n_bits)
sets the ith through ith + n_bits bits in a bitmap
Definition: bitmap.h:275
__clib_export u8 * format_bitmap_list(u8 *s, va_list *args)
Format a bitmap as a list.
Definition: bitmap.c:143
Bitmaps built as vectors of machine words.
#define clib_bitmap_free(v)
Free a bitmap.
Definition: bitmap.h:92
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112
#define BITS(x)
Definition: clib.h:69
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163