]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/bitmap.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2015 Tom Gundersen
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 long long unsigned *bitmaps
;
29 size_t bitmaps_allocated
;
33 /* Bitmaps are only meant to store relatively small numbers
34 * (corresponding to, say, an enum), so it is ok to limit
35 * the max entry. 64k should be plenty. */
36 #define BITMAPS_MAX_ENTRY 0xffff
38 /* This indicates that we reached the end of the bitmap */
39 #define BITMAP_END ((unsigned) -1)
41 #define BITMAP_NUM_TO_OFFSET(n) ((n) / (sizeof(long long unsigned) * 8))
42 #define BITMAP_NUM_TO_REM(n) ((n) % (sizeof(long long unsigned) * 8))
43 #define BITMAP_OFFSET_TO_NUM(offset, rem) ((offset) * sizeof(long long unsigned) * 8 + (rem))
45 Bitmap
*bitmap_new(void) {
46 return new0(Bitmap
, 1);
49 void bitmap_free(Bitmap
*b
) {
57 int bitmap_ensure_allocated(Bitmap
**b
) {
72 int bitmap_set(Bitmap
*b
, unsigned n
) {
78 /* we refuse to allocate huge bitmaps */
79 if (n
> BITMAPS_MAX_ENTRY
)
82 offset
= BITMAP_NUM_TO_OFFSET(n
);
84 if (offset
>= b
->n_bitmaps
) {
85 if (!GREEDY_REALLOC0(b
->bitmaps
, b
->bitmaps_allocated
, offset
+ 1))
88 b
->n_bitmaps
= offset
+ 1;
91 bitmask
= 1 << BITMAP_NUM_TO_REM(n
);
93 b
->bitmaps
[offset
] |= bitmask
;
98 void bitmap_unset(Bitmap
*b
, unsigned n
) {
104 offset
= BITMAP_NUM_TO_OFFSET(n
);
106 if (offset
>= b
->n_bitmaps
)
109 bitmask
= 1 << BITMAP_NUM_TO_REM(n
);
111 b
->bitmaps
[offset
] &= ~bitmask
;
114 bool bitmap_isset(Bitmap
*b
, unsigned n
) {
118 if (!b
|| !b
->bitmaps
)
121 offset
= BITMAP_NUM_TO_OFFSET(n
);
123 if (offset
>= b
->n_bitmaps
)
126 bitmask
= 1 << BITMAP_NUM_TO_REM(n
);
128 return !!(b
->bitmaps
[offset
] & bitmask
);
131 bool bitmap_isclear(Bitmap
*b
) {
136 for (i
= 0; i
< b
->n_bitmaps
; i
++)
143 void bitmap_clear(Bitmap
*b
) {
148 for (i
= 0; i
< b
->n_bitmaps
; i
++)
152 void bitmap_rewind(Bitmap
*b
) {
159 bool bitmap_next(Bitmap
*b
, unsigned *n
) {
161 unsigned offset
, rem
;
163 if (!b
&& b
->next_entry
== BITMAP_END
)
166 offset
= BITMAP_NUM_TO_OFFSET(b
->next_entry
);
167 rem
= BITMAP_NUM_TO_REM(b
->next_entry
);
170 for (; offset
< b
->n_bitmaps
; offset
++) {
171 if (b
->bitmaps
[offset
]) {
172 for (; bitmask
; bitmask
<<= 1, rem
++) {
173 if (b
->bitmaps
[offset
] & bitmask
) {
174 *n
= BITMAP_OFFSET_TO_NUM(offset
, rem
);
175 b
->next_entry
= *n
+ 1;
186 b
->next_entry
= BITMAP_END
;
191 bool bitmap_equal(Bitmap
*a
, Bitmap
*b
) {
200 if (a
->n_bitmaps
!= b
->n_bitmaps
)
203 for (i
= 0; i
< a
->n_bitmaps
; i
++)
204 if (a
->bitmaps
[i
] != b
->bitmaps
[i
])