]>
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
);
64 ret
->n_bitmaps
= ret
->bitmaps_allocated
= b
->n_bitmaps
;
68 void bitmap_free(Bitmap
*b
) {
76 int bitmap_ensure_allocated(Bitmap
**b
) {
93 int bitmap_set(Bitmap
*b
, unsigned n
) {
99 /* we refuse to allocate huge bitmaps */
100 if (n
> BITMAPS_MAX_ENTRY
)
103 offset
= BITMAP_NUM_TO_OFFSET(n
);
105 if (offset
>= b
->n_bitmaps
) {
106 if (!GREEDY_REALLOC0(b
->bitmaps
, b
->bitmaps_allocated
, offset
+ 1))
109 b
->n_bitmaps
= offset
+ 1;
112 bitmask
= UINT64_C(1) << BITMAP_NUM_TO_REM(n
);
114 b
->bitmaps
[offset
] |= bitmask
;
119 void bitmap_unset(Bitmap
*b
, unsigned n
) {
126 offset
= BITMAP_NUM_TO_OFFSET(n
);
128 if (offset
>= b
->n_bitmaps
)
131 bitmask
= UINT64_C(1) << BITMAP_NUM_TO_REM(n
);
133 b
->bitmaps
[offset
] &= ~bitmask
;
136 bool bitmap_isset(Bitmap
*b
, unsigned n
) {
143 offset
= BITMAP_NUM_TO_OFFSET(n
);
145 if (offset
>= b
->n_bitmaps
)
148 bitmask
= UINT64_C(1) << BITMAP_NUM_TO_REM(n
);
150 return !!(b
->bitmaps
[offset
] & bitmask
);
153 bool bitmap_isclear(Bitmap
*b
) {
159 for (i
= 0; i
< b
->n_bitmaps
; i
++)
160 if (b
->bitmaps
[i
] != 0)
166 void bitmap_clear(Bitmap
*b
) {
171 b
->bitmaps
= mfree(b
->bitmaps
);
173 b
->bitmaps_allocated
= 0;
176 bool bitmap_iterate(Bitmap
*b
, Iterator
*i
, unsigned *n
) {
178 unsigned offset
, rem
;
183 if (!b
|| i
->idx
== BITMAP_END
)
186 offset
= BITMAP_NUM_TO_OFFSET(i
->idx
);
187 rem
= BITMAP_NUM_TO_REM(i
->idx
);
188 bitmask
= UINT64_C(1) << rem
;
190 for (; offset
< b
->n_bitmaps
; offset
++) {
191 if (b
->bitmaps
[offset
]) {
192 for (; bitmask
; bitmask
<<= 1, rem
++) {
193 if (b
->bitmaps
[offset
] & bitmask
) {
194 *n
= BITMAP_OFFSET_TO_NUM(offset
, rem
);
211 bool bitmap_equal(Bitmap
*a
, Bitmap
*b
) {
212 size_t common_n_bitmaps
;
225 common_n_bitmaps
= MIN(a
->n_bitmaps
, b
->n_bitmaps
);
226 if (memcmp(a
->bitmaps
, b
->bitmaps
, sizeof(uint64_t) * common_n_bitmaps
) != 0)
229 c
= a
->n_bitmaps
> b
->n_bitmaps
? a
: b
;
230 for (i
= common_n_bitmaps
; i
< c
->n_bitmaps
; i
++)
231 if (c
->bitmaps
[i
] != 0)