]> git.proxmox.com Git - mirror_frr.git/blobdiff - lib/bitfield.h
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[mirror_frr.git] / lib / bitfield.h
index 7062796a9950f2f6425f699c747719c3680c64cd..0e031ccc497f69f1b1b435748248ece40ca4a07f 100644 (file)
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with Quagga; see the file COPYING.  If not, write to the Free
- * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- * 02111-1307, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; see the file COPYING; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  */
 /**
  * A simple bit array implementation to allocate and free IDs. An example
@@ -63,27 +62,41 @@ typedef unsigned int word_t;
  * @N: number of bits to start with, which equates to how many
  *     IDs can be allocated.
  */
-#define bf_init(v, N)                              \
-  do {                                             \
-    (v).n = 0;                                     \
-    (v).m = ((N) / WORD_SIZE + 1);                 \
-    (v).data = calloc(1, ((v).m * sizeof(word_t))); \
-  } while (0)
+#define bf_init(v, N)                                                          \
+       do {                                                                   \
+               (v).n = 0;                                                     \
+               (v).m = ((N) / WORD_SIZE + 1);                                 \
+               (v).data = calloc(1, ((v).m * sizeof(word_t)));                \
+       } while (0)
 
 /**
  * allocate and assign an id from bitfield v.
  */
-#define bf_assign_index(v, id)                         \
-  do {                                                 \
-    bf_find_bit(v, id);                                        \
-    bf_set_bit(v, id);                                 \
-  } while (0)
+#define bf_assign_index(v, id)                                                 \
+       do {                                                                   \
+               bf_find_bit(v, id);                                            \
+               bf_set_bit(v, id);                                             \
+       } while (0)
 
-/**
+/*
+ * allocate and assign 0th bit in the bitfiled.
+ */
+#define bf_assign_zero_index(v)                                                \
+       do {                                                                   \
+               int id = 0;                                                    \
+               bf_assign_index(v, id);                                        \
+       } while (0)
+
+/*
  * return an id to bitfield v
  */
-#define bf_release_index(v, id)                        \
-  (v).data[bf_index(id)] &= ~(1 << (bf_offset(id)))
+#define bf_release_index(v, id)                                                \
+       (v).data[bf_index(id)] &= ~(1 << (bf_offset(id)))
+
+/*
+ * return 0th index back to bitfield
+ */
+#define bf_release_zero_index(v) bf_release_index(v, 0)
 
 #define bf_index(b) ((b) / WORD_SIZE)
 #define bf_offset(b) ((b) % WORD_SIZE)
@@ -92,31 +105,52 @@ typedef unsigned int word_t;
  * Set a bit in the array. If it fills up that word and we are
  * out of words, extend it by one more word.
  */
-#define bf_set_bit(v, b)                                       \
-  do {                                                         \
-    size_t w = bf_index(b);                                    \
-    (v).data[w] |= 1 << (bf_offset(b));                                \
-    (v).n += ((v).data[w] == WORD_MAX);                                \
-    if ((v).n == (v).m) {                                      \
-      (v).m = (v).m + 1;                                       \
-      (v).data = realloc((v).data, (v).m * sizeof(word_t));    \
-    }                                                          \
-  } while (0)
+#define bf_set_bit(v, b)                                                       \
+       do {                                                                   \
+               size_t w = bf_index(b);                                        \
+               (v).data[w] |= 1 << (bf_offset(b));                            \
+               (v).n += ((v).data[w] == WORD_MAX);                            \
+               if ((v).n == (v).m) {                                          \
+                       (v).m = (v).m + 1;                                     \
+                       (v).data = realloc((v).data, (v).m * sizeof(word_t));  \
+               }                                                              \
+       } while (0)
 
 /* Find a clear bit in v and assign it to b. */
-#define bf_find_bit(v, b)                                      \
-  do {                                                         \
-    word_t word = 0;                                           \
-    unsigned int w, sh;                                                \
-    for (w = 0; w <= (v).n; w++) {                             \
-      if ((word = (v).data[w]) != WORD_MAX) break;             \
-    }                                                          \
-    (b) = ((word & 0xFFFF) == 0xFFFF) << 4; word >>= (b);      \
-    sh = ((word & 0xFF) == 0xFF) << 3; word >>= sh; (b) |= sh; \
-    sh = ((word & 0xF) == 0xF) << 2; word >>= sh; (b) |= sh;   \
-    sh = ((word & 0x3) == 0x3) << 1; word >>= sh; (b) |= sh;   \
-    sh = ((word & 0x1) == 0x1) << 0; word >>= sh; (b) |= sh;   \
-    (b) += (w * WORD_SIZE);                                    \
-  } while (0)
+#define bf_find_bit(v, b)                                                      \
+       do {                                                                   \
+               word_t word = 0;                                               \
+               unsigned int w, sh;                                            \
+               for (w = 0; w <= (v).n; w++) {                                 \
+                       if ((word = (v).data[w]) != WORD_MAX)                  \
+                               break;                                         \
+               }                                                              \
+               (b) = ((word & 0xFFFF) == 0xFFFF) << 4;                        \
+               word >>= (b);                                                  \
+               sh = ((word & 0xFF) == 0xFF) << 3;                             \
+               word >>= sh;                                                   \
+               (b) |= sh;                                                     \
+               sh = ((word & 0xF) == 0xF) << 2;                               \
+               word >>= sh;                                                   \
+               (b) |= sh;                                                     \
+               sh = ((word & 0x3) == 0x3) << 1;                               \
+               word >>= sh;                                                   \
+               (b) |= sh;                                                     \
+               sh = ((word & 0x1) == 0x1) << 0;                               \
+               word >>= sh;                                                   \
+               (b) |= sh;                                                     \
+               (b) += (w * WORD_SIZE);                                        \
+       } while (0)
+
+/*
+ * Free the allocated memory for data
+ * @v: an instance of bitfield_t struct.
+ */
+#define bf_free(v)                                                             \
+       do {                                                                   \
+               if ((v).data) {                                                \
+                       free((v).data);                                        \
+               }                                                              \
+       } while (0)
 
 #endif