]>
Commit | Line | Data |
---|---|---|
50e24903 DS |
1 | /* Bitfields |
2 | * Copyright (C) 2016 Cumulus Networks, Inc. | |
3 | * | |
4 | * This file is part of Quagga. | |
5 | * | |
6 | * Quagga is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * Quagga is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
50e24903 | 19 | */ |
c3c0ac83 DS |
20 | /** |
21 | * A simple bit array implementation to allocate and free IDs. An example | |
22 | * of its usage is in allocating link state IDs for OSPFv3 as OSPFv3 has | |
23 | * removed all address semantics from LS ID. Another usage can be in | |
24 | * allocating IDs for BGP neighbors (and dynamic update groups) for | |
25 | * efficient storage of adj-rib-out. | |
26 | * | |
27 | * An example: | |
28 | * #include "bitfield.h" | |
29 | * | |
30 | * bitfield_t bitfield; | |
31 | * | |
32 | * bf_init(bitfield, 32); | |
33 | * ... | |
34 | * bf_assign_index(bitfield, id1); | |
35 | * bf_assign_index(bitfield, id2); | |
36 | * ... | |
37 | * bf_release_index(bitfield, id1); | |
38 | */ | |
39 | ||
40 | #ifndef _BITFIELD_H | |
41 | #define _BITFIELD_H | |
42 | ||
43 | #include <stdio.h> | |
44 | #include <string.h> | |
45 | #include <stdlib.h> | |
46 | ||
47 | typedef unsigned int word_t; | |
48 | #define WORD_MAX 0xFFFFFFFF | |
49 | #define WORD_SIZE (sizeof(word_t) * 8) | |
50 | ||
51 | /** | |
52 | * The bitfield structure. | |
53 | * @data: the bits to manage. | |
54 | * @n: The current word number that is being used. | |
55 | * @m: total number of words in 'data' | |
56 | */ | |
57 | #define bitfield_t struct { word_t *data; size_t n, m; } | |
58 | ||
59 | /** | |
60 | * Initialize the bits. | |
61 | * @v: an instance of bitfield_t struct. | |
62 | * @N: number of bits to start with, which equates to how many | |
63 | * IDs can be allocated. | |
64 | */ | |
d62a17ae | 65 | #define bf_init(v, N) \ |
66 | do { \ | |
67 | (v).n = 0; \ | |
68 | (v).m = ((N) / WORD_SIZE + 1); \ | |
69 | (v).data = calloc(1, ((v).m * sizeof(word_t))); \ | |
70 | } while (0) | |
c3c0ac83 DS |
71 | |
72 | /** | |
73 | * allocate and assign an id from bitfield v. | |
74 | */ | |
d62a17ae | 75 | #define bf_assign_index(v, id) \ |
76 | do { \ | |
77 | bf_find_bit(v, id); \ | |
78 | bf_set_bit(v, id); \ | |
79 | } while (0) | |
c3c0ac83 | 80 | |
4e9da201 | 81 | /* |
82 | * allocate and assign 0th bit in the bitfiled. | |
83 | */ | |
d62a17ae | 84 | #define bf_assign_zero_index(v) \ |
85 | do { \ | |
86 | int id = 0; \ | |
87 | bf_assign_index(v, id); \ | |
88 | } while (0) | |
4e9da201 | 89 | |
90 | /* | |
c3c0ac83 DS |
91 | * return an id to bitfield v |
92 | */ | |
d62a17ae | 93 | #define bf_release_index(v, id) \ |
94 | (v).data[bf_index(id)] &= ~(1 << (bf_offset(id))) | |
c3c0ac83 | 95 | |
4e9da201 | 96 | /* |
97 | * return 0th index back to bitfield | |
98 | */ | |
d62a17ae | 99 | #define bf_release_zero_index(v) bf_release_index(v, 0) |
4e9da201 | 100 | |
c3c0ac83 DS |
101 | #define bf_index(b) ((b) / WORD_SIZE) |
102 | #define bf_offset(b) ((b) % WORD_SIZE) | |
103 | ||
104 | /** | |
105 | * Set a bit in the array. If it fills up that word and we are | |
106 | * out of words, extend it by one more word. | |
107 | */ | |
d62a17ae | 108 | #define bf_set_bit(v, b) \ |
109 | do { \ | |
110 | size_t w = bf_index(b); \ | |
111 | (v).data[w] |= 1 << (bf_offset(b)); \ | |
112 | (v).n += ((v).data[w] == WORD_MAX); \ | |
113 | if ((v).n == (v).m) { \ | |
114 | (v).m = (v).m + 1; \ | |
115 | (v).data = realloc((v).data, (v).m * sizeof(word_t)); \ | |
116 | } \ | |
117 | } while (0) | |
c3c0ac83 DS |
118 | |
119 | /* Find a clear bit in v and assign it to b. */ | |
d62a17ae | 120 | #define bf_find_bit(v, b) \ |
121 | do { \ | |
122 | word_t word = 0; \ | |
123 | unsigned int w, sh; \ | |
124 | for (w = 0; w <= (v).n; w++) { \ | |
125 | if ((word = (v).data[w]) != WORD_MAX) \ | |
126 | break; \ | |
127 | } \ | |
128 | (b) = ((word & 0xFFFF) == 0xFFFF) << 4; \ | |
129 | word >>= (b); \ | |
130 | sh = ((word & 0xFF) == 0xFF) << 3; \ | |
131 | word >>= sh; \ | |
132 | (b) |= sh; \ | |
133 | sh = ((word & 0xF) == 0xF) << 2; \ | |
134 | word >>= sh; \ | |
135 | (b) |= sh; \ | |
136 | sh = ((word & 0x3) == 0x3) << 1; \ | |
137 | word >>= sh; \ | |
138 | (b) |= sh; \ | |
139 | sh = ((word & 0x1) == 0x1) << 0; \ | |
140 | word >>= sh; \ | |
141 | (b) |= sh; \ | |
142 | (b) += (w * WORD_SIZE); \ | |
143 | } while (0) | |
c3c0ac83 | 144 | |
4e9da201 | 145 | /* |
146 | * Free the allocated memory for data | |
147 | * @v: an instance of bitfield_t struct. | |
148 | */ | |
d62a17ae | 149 | #define bf_free(v) \ |
150 | do { \ | |
151 | if ((v).data) { \ | |
152 | free((v).data); \ | |
153 | } \ | |
154 | } while (0) | |
4e9da201 | 155 | |
c3c0ac83 | 156 | #endif |