]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/bitmap.c
2 This file is part of systemd.
4 Copyright 2015 Tom Gundersen
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "alloc-util.h"
34 size_t bitmaps_allocated
;
37 /* Bitmaps are only meant to store relatively small numbers
38 * (corresponding to, say, an enum), so it is ok to limit
39 * the max entry. 64k should be plenty. */
40 #define BITMAPS_MAX_ENTRY 0xffff
42 /* This indicates that we reached the end of the bitmap */
43 #define BITMAP_END ((unsigned) -1)
45 #define BITMAP_NUM_TO_OFFSET(n) ((n) / (sizeof(uint64_t) * 8))
46 #define BITMAP_NUM_TO_REM(n) ((n) % (sizeof(uint64_t) * 8))
47 #define BITMAP_OFFSET_TO_NUM(offset, rem) ((offset) * sizeof(uint64_t) * 8 + (rem))
49 Bitmap
*bitmap_new(void) {
50 return new0(Bitmap
, 1);
53 Bitmap
*bitmap_copy(Bitmap
*b
) {
60 ret
->bitmaps
= newdup(uint64_t, b
->bitmaps
, b
->n_bitmaps
);
66 ret
->n_bitmaps
= ret
->bitmaps_allocated
= b
->n_bitmaps
;
70 void bitmap_free(Bitmap
*b
) {
78 int bitmap_ensure_allocated(Bitmap
**b
) {
95 int bitmap_set(Bitmap
*b
, unsigned n
) {
101 /* we refuse to allocate huge bitmaps */
102 if (n
> BITMAPS_MAX_ENTRY
)
105 offset
= BITMAP_NUM_TO_OFFSET(n
);
107 if (offset
>= b
->n_bitmaps
) {
108 if (!GREEDY_REALLOC0(b
->bitmaps
, b
->bitmaps_allocated
, offset
+ 1))
111 b
->n_bitmaps
= offset
+ 1;
114 bitmask
= UINT64_C(1) << BITMAP_NUM_TO_REM(n
);
116 b
->bitmaps
[offset
] |= bitmask
;
121 void bitmap_unset(Bitmap
*b
, unsigned n
) {
128 offset
= BITMAP_NUM_TO_OFFSET(n
);
130 if (offset
>= b
->n_bitmaps
)
133 bitmask
= UINT64_C(1) << BITMAP_NUM_TO_REM(n
);
135 b
->bitmaps
[offset
] &= ~bitmask
;
138 bool bitmap_isset(Bitmap
*b
, unsigned n
) {
145 offset
= BITMAP_NUM_TO_OFFSET(n
);
147 if (offset
>= b
->n_bitmaps
)
150 bitmask
= UINT64_C(1) << BITMAP_NUM_TO_REM(n
);
152 return !!(b
->bitmaps
[offset
] & bitmask
);
155 bool bitmap_isclear(Bitmap
*b
) {
161 for (i
= 0; i
< b
->n_bitmaps
; i
++)
162 if (b
->bitmaps
[i
] != 0)
168 void bitmap_clear(Bitmap
*b
) {
173 b
->bitmaps
= mfree(b
->bitmaps
);
175 b
->bitmaps_allocated
= 0;
178 bool bitmap_iterate(Bitmap
*b
, Iterator
*i
, unsigned *n
) {
180 unsigned offset
, rem
;
185 if (!b
|| i
->idx
== BITMAP_END
)
188 offset
= BITMAP_NUM_TO_OFFSET(i
->idx
);
189 rem
= BITMAP_NUM_TO_REM(i
->idx
);
190 bitmask
= UINT64_C(1) << rem
;
192 for (; offset
< b
->n_bitmaps
; offset
++) {
193 if (b
->bitmaps
[offset
]) {
194 for (; bitmask
; bitmask
<<= 1, rem
++) {
195 if (b
->bitmaps
[offset
] & bitmask
) {
196 *n
= BITMAP_OFFSET_TO_NUM(offset
, rem
);
213 bool bitmap_equal(Bitmap
*a
, Bitmap
*b
) {
214 size_t common_n_bitmaps
;
227 common_n_bitmaps
= MIN(a
->n_bitmaps
, b
->n_bitmaps
);
228 if (memcmp(a
->bitmaps
, b
->bitmaps
, sizeof(uint64_t) * common_n_bitmaps
) != 0)
231 c
= a
->n_bitmaps
> b
->n_bitmaps
? a
: b
;
232 for (i
= common_n_bitmaps
; i
< c
->n_bitmaps
; i
++)
233 if (c
->bitmaps
[i
] != 0)