]>
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/>.
29 size_t bitmaps_allocated
;
32 /* Bitmaps are only meant to store relatively small numbers
33 * (corresponding to, say, an enum), so it is ok to limit
34 * the max entry. 64k should be plenty. */
35 #define BITMAPS_MAX_ENTRY 0xffff
37 /* This indicates that we reached the end of the bitmap */
38 #define BITMAP_END ((unsigned) -1)
40 #define BITMAP_NUM_TO_OFFSET(n) ((n) / (sizeof(uint64_t) * 8))
41 #define BITMAP_NUM_TO_REM(n) ((n) % (sizeof(uint64_t) * 8))
42 #define BITMAP_OFFSET_TO_NUM(offset, rem) ((offset) * sizeof(uint64_t) * 8 + (rem))
44 Bitmap
*bitmap_new(void) {
45 return new0(Bitmap
, 1);
48 void bitmap_free(Bitmap
*b
) {
56 int bitmap_ensure_allocated(Bitmap
**b
) {
73 int bitmap_set(Bitmap
*b
, unsigned n
) {
79 /* we refuse to allocate huge bitmaps */
80 if (n
> BITMAPS_MAX_ENTRY
)
83 offset
= BITMAP_NUM_TO_OFFSET(n
);
85 if (offset
>= b
->n_bitmaps
) {
86 if (!GREEDY_REALLOC0(b
->bitmaps
, b
->bitmaps_allocated
, offset
+ 1))
89 b
->n_bitmaps
= offset
+ 1;
92 bitmask
= UINT64_C(1) << BITMAP_NUM_TO_REM(n
);
94 b
->bitmaps
[offset
] |= bitmask
;
99 void bitmap_unset(Bitmap
*b
, unsigned n
) {
106 offset
= BITMAP_NUM_TO_OFFSET(n
);
108 if (offset
>= b
->n_bitmaps
)
111 bitmask
= UINT64_C(1) << BITMAP_NUM_TO_REM(n
);
113 b
->bitmaps
[offset
] &= ~bitmask
;
116 bool bitmap_isset(Bitmap
*b
, unsigned n
) {
123 offset
= BITMAP_NUM_TO_OFFSET(n
);
125 if (offset
>= b
->n_bitmaps
)
128 bitmask
= UINT64_C(1) << BITMAP_NUM_TO_REM(n
);
130 return !!(b
->bitmaps
[offset
] & bitmask
);
133 bool bitmap_isclear(Bitmap
*b
) {
138 for (i
= 0; i
< b
->n_bitmaps
; i
++)
139 if (b
->bitmaps
[i
] != 0)
145 void bitmap_clear(Bitmap
*b
) {
151 b
->bitmaps_allocated
= 0;
154 bool bitmap_iterate(Bitmap
*b
, Iterator
*i
, unsigned *n
) {
156 unsigned offset
, rem
;
161 if (!b
|| i
->idx
== BITMAP_END
)
164 offset
= BITMAP_NUM_TO_OFFSET(i
->idx
);
165 rem
= BITMAP_NUM_TO_REM(i
->idx
);
166 bitmask
= UINT64_C(1) << rem
;
168 for (; offset
< b
->n_bitmaps
; offset
++) {
169 if (b
->bitmaps
[offset
]) {
170 for (; bitmask
; bitmask
<<= 1, rem
++) {
171 if (b
->bitmaps
[offset
] & bitmask
) {
172 *n
= BITMAP_OFFSET_TO_NUM(offset
, rem
);
189 bool bitmap_equal(Bitmap
*a
, Bitmap
*b
) {
190 size_t common_n_bitmaps
;
200 common_n_bitmaps
= MIN(a
->n_bitmaps
, b
->n_bitmaps
);
201 if (memcmp(a
->bitmaps
, b
->bitmaps
, sizeof(uint64_t) * common_n_bitmaps
) != 0)
204 c
= a
->n_bitmaps
> b
->n_bitmaps
? a
: b
;
205 for (i
= common_n_bitmaps
; i
< c
->n_bitmaps
; i
++)
206 if (c
->bitmaps
[i
] != 0)