]> git.proxmox.com Git - mirror_frr.git/blame - lib/bitfield.h
*: auto-convert to SPDX License IDs
[mirror_frr.git] / lib / bitfield.h
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
50e24903
DS
2/* Bitfields
3 * Copyright (C) 2016 Cumulus Networks, Inc.
50e24903 4 */
c3c0ac83
DS
5/**
6 * A simple bit array implementation to allocate and free IDs. An example
7 * of its usage is in allocating link state IDs for OSPFv3 as OSPFv3 has
8 * removed all address semantics from LS ID. Another usage can be in
9 * allocating IDs for BGP neighbors (and dynamic update groups) for
10 * efficient storage of adj-rib-out.
11 *
12 * An example:
13 * #include "bitfield.h"
14 *
15 * bitfield_t bitfield;
16 *
17 * bf_init(bitfield, 32);
18 * ...
19 * bf_assign_index(bitfield, id1);
20 * bf_assign_index(bitfield, id2);
21 * ...
22 * bf_release_index(bitfield, id1);
23 */
24
25#ifndef _BITFIELD_H
26#define _BITFIELD_H
27
28#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31
5e244469
RW
32#ifdef __cplusplus
33extern "C" {
34#endif
35
c3c0ac83
DS
36typedef unsigned int word_t;
37#define WORD_MAX 0xFFFFFFFF
38#define WORD_SIZE (sizeof(word_t) * 8)
39
40/**
41 * The bitfield structure.
42 * @data: the bits to manage.
43 * @n: The current word number that is being used.
44 * @m: total number of words in 'data'
45 */
89fbf168 46typedef struct {word_t *data; size_t n, m; } bitfield_t;
c3c0ac83 47
0e2deb58
PR
48DECLARE_MTYPE(BITFIELD);
49
c3c0ac83
DS
50/**
51 * Initialize the bits.
52 * @v: an instance of bitfield_t struct.
53 * @N: number of bits to start with, which equates to how many
54 * IDs can be allocated.
55 */
d62a17ae 56#define bf_init(v, N) \
57 do { \
58 (v).n = 0; \
59 (v).m = ((N) / WORD_SIZE + 1); \
0e2deb58 60 (v).data = XCALLOC(MTYPE_BITFIELD, ((v).m * sizeof(word_t))); \
d62a17ae 61 } while (0)
c3c0ac83
DS
62
63/**
64 * allocate and assign an id from bitfield v.
65 */
d62a17ae 66#define bf_assign_index(v, id) \
67 do { \
68 bf_find_bit(v, id); \
69 bf_set_bit(v, id); \
70 } while (0)
c3c0ac83 71
4e9da201 72/*
73 * allocate and assign 0th bit in the bitfiled.
74 */
d62a17ae 75#define bf_assign_zero_index(v) \
76 do { \
77 int id = 0; \
78 bf_assign_index(v, id); \
79 } while (0)
4e9da201 80
81/*
c3c0ac83
DS
82 * return an id to bitfield v
83 */
d62a17ae 84#define bf_release_index(v, id) \
85 (v).data[bf_index(id)] &= ~(1 << (bf_offset(id)))
c3c0ac83 86
89fbf168
AK
87/* check if an id is in use */
88#define bf_test_index(v, id) \
89 ((v).data[bf_index(id)] & (1 << (bf_offset(id))))
90
91/* check if the bit field has been setup */
92#define bf_is_inited(v) ((v).data)
93
94/* compare two bitmaps of the same length */
95#define bf_cmp(v1, v2) (memcmp((v1).data, (v2).data, ((v1).m * sizeof(word_t))))
96
4e9da201 97/*
98 * return 0th index back to bitfield
99 */
d62a17ae 100#define bf_release_zero_index(v) bf_release_index(v, 0)
4e9da201 101
c3c0ac83
DS
102#define bf_index(b) ((b) / WORD_SIZE)
103#define bf_offset(b) ((b) % WORD_SIZE)
104
105/**
106 * Set a bit in the array. If it fills up that word and we are
107 * out of words, extend it by one more word.
108 */
d62a17ae 109#define bf_set_bit(v, b) \
110 do { \
111 size_t w = bf_index(b); \
112 (v).data[w] |= 1 << (bf_offset(b)); \
113 (v).n += ((v).data[w] == WORD_MAX); \
114 if ((v).n == (v).m) { \
115 (v).m = (v).m + 1; \
116 (v).data = realloc((v).data, (v).m * sizeof(word_t)); \
117 } \
118 } while (0)
c3c0ac83
DS
119
120/* Find a clear bit in v and assign it to b. */
d62a17ae 121#define bf_find_bit(v, b) \
122 do { \
123 word_t word = 0; \
124 unsigned int w, sh; \
125 for (w = 0; w <= (v).n; w++) { \
126 if ((word = (v).data[w]) != WORD_MAX) \
127 break; \
128 } \
129 (b) = ((word & 0xFFFF) == 0xFFFF) << 4; \
130 word >>= (b); \
131 sh = ((word & 0xFF) == 0xFF) << 3; \
132 word >>= sh; \
133 (b) |= sh; \
134 sh = ((word & 0xF) == 0xF) << 2; \
135 word >>= sh; \
136 (b) |= sh; \
137 sh = ((word & 0x3) == 0x3) << 1; \
138 word >>= sh; \
139 (b) |= sh; \
140 sh = ((word & 0x1) == 0x1) << 0; \
141 word >>= sh; \
142 (b) |= sh; \
143 (b) += (w * WORD_SIZE); \
144 } while (0)
c3c0ac83 145
80853c2e
PZ
146/*
147 * Find a clear bit in v and return it
148 * Start looking in the word containing bit position start_index.
149 * If necessary, wrap around after bit position max_index.
150 */
151static inline unsigned int
152bf_find_next_clear_bit_wrap(bitfield_t *v, word_t start_index, word_t max_index)
153{
154 int start_bit;
155 unsigned long i, offset, scanbits, wordcount_max, index_max;
156
157 if (start_index > max_index)
158 start_index = 0;
159
160 start_bit = start_index & (WORD_SIZE - 1);
161 wordcount_max = bf_index(max_index) + 1;
162
163 scanbits = WORD_SIZE;
164 for (i = bf_index(start_index); i < v->m; ++i) {
165 if (v->data[i] == WORD_MAX) {
166 /* if the whole word is full move to the next */
167 start_bit = 0;
168 continue;
169 }
170 /* scan one word for clear bits */
171 if ((i == v->m - 1) && (v->m >= wordcount_max))
172 /* max index could be only part of word */
173 scanbits = (max_index % WORD_SIZE) + 1;
174 for (offset = start_bit; offset < scanbits; ++offset) {
175 if (!((v->data[i] >> offset) & 1))
176 return ((i * WORD_SIZE) + offset);
177 }
178 /* move to the next word */
179 start_bit = 0;
180 }
181
182 if (v->m < wordcount_max) {
183 /*
184 * We can expand bitfield, so no need to wrap.
185 * Return the index of the first bit of the next word.
186 * Assumption is that caller will call bf_set_bit which
187 * will allocate additional space.
188 */
189 v->m += 1;
190 v->data = (word_t *)realloc(v->data, v->m * sizeof(word_t));
191 v->data[v->m - 1] = 0;
192 return v->m * WORD_SIZE;
193 }
194
195 /*
196 * start looking for a clear bit at the start of the bitfield and
197 * stop when we reach start_index
198 */
199 scanbits = WORD_SIZE;
200 index_max = bf_index(start_index - 1);
201 for (i = 0; i <= index_max; ++i) {
202 if (i == index_max)
203 scanbits = ((start_index - 1) % WORD_SIZE) + 1;
204 for (offset = start_bit; offset < scanbits; ++offset) {
205 if (!((v->data[i] >> offset) & 1))
206 return ((i * WORD_SIZE) + offset);
207 }
208 /* move to the next word */
209 start_bit = 0;
210 }
211
212 return WORD_MAX;
213}
214
89fbf168
AK
215static inline unsigned int bf_find_next_set_bit(bitfield_t v,
216 word_t start_index)
217{
218 int start_bit;
219 unsigned long i, offset;
220
221 start_bit = start_index & (WORD_SIZE - 1);
222
223 for (i = bf_index(start_index); i < v.m; ++i) {
224 if (v.data[i] == 0) {
225 /* if the whole word is empty move to the next */
226 start_bit = 0;
227 continue;
228 }
229 /* scan one word for set bits */
230 for (offset = start_bit; offset < WORD_SIZE; ++offset) {
231 if ((v.data[i] >> offset) & 1)
232 return ((i * WORD_SIZE) + offset);
233 }
234 /* move to the next word */
235 start_bit = 0;
236 }
237 return WORD_MAX;
238}
239
240/* iterate through all the set bits */
241#define bf_for_each_set_bit(v, b, max) \
242 for ((b) = bf_find_next_set_bit((v), 0); \
243 (b) < max; \
244 (b) = bf_find_next_set_bit((v), (b) + 1))
245
4e9da201 246/*
247 * Free the allocated memory for data
248 * @v: an instance of bitfield_t struct.
249 */
d62a17ae 250#define bf_free(v) \
251 do { \
0e2deb58 252 XFREE(MTYPE_BITFIELD, (v).data); \
1ac10b15 253 (v).data = NULL; \
d62a17ae 254 } while (0)
4e9da201 255
5e244469
RW
256#ifdef __cplusplus
257}
258#endif
259
c3c0ac83 260#endif