Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2021 Intel Corporation
3 : : *
4 : : * SPDX-License-Identifier: Apache-2.0
5 : : */
6 : :
7 : : #include <errno.h>
8 : : #include <stddef.h>
9 : : #include <stdbool.h>
10 : : #include <stdio.h>
11 : : #include <zephyr/sys/bitarray.h>
12 : : #include <zephyr/sys/check.h>
13 : : #include <zephyr/sys/sys_io.h>
14 : :
15 : : /* Number of bits represented by one bundle */
16 : : #define bundle_bitness(ba) (sizeof((ba)->bundles[0]) * 8)
17 : :
18 : : struct bundle_data {
19 : : /* Start and end index of bundles */
20 : : size_t sidx, eidx;
21 : :
22 : : /* Offset inside start and end bundles */
23 : : size_t soff, eoff;
24 : :
25 : : /* Masks for start/end bundles */
26 : : uint32_t smask, emask;
27 : : };
28 : :
29 : 0 : static void setup_bundle_data(sys_bitarray_t *bitarray,
30 : : struct bundle_data *bd,
31 : : size_t offset, size_t num_bits)
32 : : {
33 : 0 : bd->sidx = offset / bundle_bitness(bitarray);
34 : 0 : bd->soff = offset % bundle_bitness(bitarray);
35 : :
36 : 0 : bd->eidx = (offset + num_bits - 1) / bundle_bitness(bitarray);
37 : 0 : bd->eoff = (offset + num_bits - 1) % bundle_bitness(bitarray);
38 : :
39 : 0 : bd->smask = ~(BIT(bd->soff) - 1);
40 : 0 : bd->emask = (BIT(bd->eoff) - 1) | BIT(bd->eoff);
41 : :
42 [ # # ]: 0 : if (bd->sidx == bd->eidx) {
43 : : /* The region lies within the same bundle. So combine the masks. */
44 : 0 : bd->smask &= bd->emask;
45 : : }
46 : 0 : }
47 : :
48 : : /*
49 : : * Find out if the bits in a region is all set or all clear.
50 : : *
51 : : * @param[in] bitarray Bitarray struct
52 : : * @param[in] offset Starting bit location
53 : : * @param[in] num_bits Number of bits in the region
54 : : * @param[in] match_set True if matching all set bits,
55 : : * False if matching all cleared bits
56 : : * @param[out] bd Data related to matching which can be
57 : : * used later to find out where the region
58 : : * lies in the bitarray bundles.
59 : : * @param[out] mismatch Offset to the mismatched bit.
60 : : * Can be NULL.
61 : : *
62 : : * @retval true If all bits are set or cleared
63 : : * @retval false Not all bits are set or cleared
64 : : */
65 : 0 : static bool match_region(sys_bitarray_t *bitarray, size_t offset,
66 : : size_t num_bits, bool match_set,
67 : : struct bundle_data *bd,
68 : : size_t *mismatch)
69 : : {
70 : 0 : size_t idx;
71 : 0 : uint32_t bundle;
72 : 0 : uint32_t mismatch_bundle;
73 : 0 : size_t mismatch_bundle_idx;
74 : 0 : size_t mismatch_bit_off;
75 : :
76 : 0 : setup_bundle_data(bitarray, bd, offset, num_bits);
77 : :
78 [ # # ]: 0 : if (bd->sidx == bd->eidx) {
79 : 0 : bundle = bitarray->bundles[bd->sidx];
80 [ # # ]: 0 : if (!match_set) {
81 : 0 : bundle = ~bundle;
82 : : }
83 : :
84 [ # # ]: 0 : if ((bundle & bd->smask) != bd->smask) {
85 : : /* Not matching to mask. */
86 : 0 : mismatch_bundle = ~bundle & bd->smask;
87 : 0 : mismatch_bundle_idx = bd->sidx;
88 : 0 : goto mismatch;
89 : : } else {
90 : : /* Matching to mask. */
91 : 0 : goto out;
92 : : }
93 : : }
94 : :
95 : : /* Region lies in a number of bundles. Need to loop through them. */
96 : :
97 : : /* Start of bundles */
98 : 0 : bundle = bitarray->bundles[bd->sidx];
99 [ # # ]: 0 : if (!match_set) {
100 : 0 : bundle = ~bundle;
101 : : }
102 : :
103 [ # # ]: 0 : if ((bundle & bd->smask) != bd->smask) {
104 : : /* Start bundle not matching to mask. */
105 : 0 : mismatch_bundle = ~bundle & bd->smask;
106 : 0 : mismatch_bundle_idx = bd->sidx;
107 : 0 : goto mismatch;
108 : : }
109 : :
110 : : /* End of bundles */
111 : 0 : bundle = bitarray->bundles[bd->eidx];
112 [ # # ]: 0 : if (!match_set) {
113 : 0 : bundle = ~bundle;
114 : : }
115 : :
116 [ # # ]: 0 : if ((bundle & bd->emask) != bd->emask) {
117 : : /* End bundle not matching to mask. */
118 : 0 : mismatch_bundle = ~bundle & bd->emask;
119 : 0 : mismatch_bundle_idx = bd->eidx;
120 : 0 : goto mismatch;
121 : : }
122 : :
123 : : /* In-between bundles */
124 [ # # ]: 0 : for (idx = bd->sidx + 1; idx < bd->eidx; idx++) {
125 : : /* Note that this is opposite from above so that
126 : : * we are simply checking if bundle == 0.
127 : : */
128 : 0 : bundle = bitarray->bundles[idx];
129 [ # # ]: 0 : if (match_set) {
130 : 0 : bundle = ~bundle;
131 : : }
132 : :
133 [ # # ]: 0 : if (bundle != 0U) {
134 : : /* Bits in "between bundles" do not match */
135 : 0 : mismatch_bundle = bundle;
136 : 0 : mismatch_bundle_idx = idx;
137 : 0 : goto mismatch;
138 : : }
139 : : }
140 : :
141 : 0 : out:
142 : : /* All bits in region matched. */
143 : : return true;
144 : :
145 : 0 : mismatch:
146 [ # # ]: 0 : if (mismatch != NULL) {
147 : : /* Must have at least 1 bit set to indicate
148 : : * where the mismatch is.
149 : : */
150 [ # # ]: 0 : __ASSERT_NO_MSG(mismatch_bundle != 0);
151 : :
152 : 0 : mismatch_bit_off = find_lsb_set(mismatch_bundle) - 1;
153 : 0 : mismatch_bit_off += mismatch_bundle_idx *
154 : : bundle_bitness(bitarray);
155 : 0 : *mismatch = (uint32_t)mismatch_bit_off;
156 : : }
157 : : return false;
158 : : }
159 : :
160 : : /*
161 : : * Set or clear a region of bits.
162 : : *
163 : : * @param bitarray Bitarray struct
164 : : * @param offset Starting bit location
165 : : * @param num_bits Number of bits in the region
166 : : * @param to_set True if to set all bits.
167 : : * False if to clear all bits.
168 : : * @param bd Bundle data. Can reuse the output from
169 : : * match_region(). NULL if there is no
170 : : * prior call to match_region().
171 : : */
172 : 0 : static void set_region(sys_bitarray_t *bitarray, size_t offset,
173 : : size_t num_bits, bool to_set,
174 : : struct bundle_data *bd)
175 : : {
176 : 0 : size_t idx;
177 : 0 : struct bundle_data bdata;
178 : :
179 [ # # ]: 0 : if (bd == NULL) {
180 : 0 : bd = &bdata;
181 : 0 : setup_bundle_data(bitarray, bd, offset, num_bits);
182 : : }
183 : :
184 [ # # ]: 0 : if (bd->sidx == bd->eidx) {
185 : : /* Start/end at same bundle */
186 [ # # ]: 0 : if (to_set) {
187 : 0 : bitarray->bundles[bd->sidx] |= bd->smask;
188 : : } else {
189 : 0 : bitarray->bundles[bd->sidx] &= ~bd->smask;
190 : : }
191 : : } else {
192 : : /* Start/end at different bundle.
193 : : * So set/clear the bits in start and end bundles
194 : : * separately. For in-between bundles,
195 : : * set/clear all bits.
196 : : */
197 [ # # ]: 0 : if (to_set) {
198 : 0 : bitarray->bundles[bd->sidx] |= bd->smask;
199 : 0 : bitarray->bundles[bd->eidx] |= bd->emask;
200 [ # # ]: 0 : for (idx = bd->sidx + 1; idx < bd->eidx; idx++) {
201 : 0 : bitarray->bundles[idx] = ~0U;
202 : : }
203 : : } else {
204 : 0 : bitarray->bundles[bd->sidx] &= ~bd->smask;
205 : 0 : bitarray->bundles[bd->eidx] &= ~bd->emask;
206 [ # # ]: 0 : for (idx = bd->sidx + 1; idx < bd->eidx; idx++) {
207 : 0 : bitarray->bundles[idx] = 0U;
208 : : }
209 : : }
210 : : }
211 : 0 : }
212 : :
213 : 0 : int sys_bitarray_popcount_region(sys_bitarray_t *bitarray, size_t num_bits, size_t offset,
214 : : size_t *count)
215 : : {
216 : 0 : k_spinlock_key_t key;
217 : 0 : size_t idx;
218 : 0 : struct bundle_data bd;
219 : 0 : int ret;
220 : :
221 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray != NULL);
222 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray->num_bits > 0);
223 : :
224 : 0 : key = k_spin_lock(&bitarray->lock);
225 : :
226 [ # # # # ]: 0 : if (num_bits == 0 || num_bits > bitarray->num_bits ||
227 [ # # ]: 0 : offset > bitarray->num_bits - num_bits) {
228 : 0 : ret = -EINVAL;
229 : 0 : goto out;
230 : : }
231 : :
232 [ # # ]: 0 : CHECKIF(count == NULL) {
233 : 0 : ret = -EINVAL;
234 : 0 : goto out;
235 : : }
236 : :
237 : 0 : setup_bundle_data(bitarray, &bd, offset, num_bits);
238 : :
239 [ # # ]: 0 : if (bd.sidx == bd.eidx) {
240 : : /* Start/end at same bundle */
241 : 0 : *count = POPCOUNT(bitarray->bundles[bd.sidx] & bd.smask);
242 : : } else {
243 : : /* Start/end at different bundle.
244 : : * So count the bits in start and end bundles
245 : : * separately with correct mask applied. For in-between bundles,
246 : : * count all bits.
247 : : */
248 : 0 : *count = 0;
249 : 0 : *count += POPCOUNT(bitarray->bundles[bd.sidx] & bd.smask);
250 : 0 : *count += POPCOUNT(bitarray->bundles[bd.eidx] & bd.emask);
251 [ # # ]: 0 : for (idx = bd.sidx + 1; idx < bd.eidx; idx++) {
252 : 0 : *count += POPCOUNT(bitarray->bundles[idx]);
253 : : }
254 : : }
255 : :
256 : : ret = 0;
257 : :
258 : 0 : out:
259 : 0 : k_spin_unlock(&bitarray->lock, key);
260 : 0 : return ret;
261 : : }
262 : :
263 : 0 : int sys_bitarray_xor(sys_bitarray_t *dst, sys_bitarray_t *other, size_t num_bits, size_t offset)
264 : : {
265 : 0 : k_spinlock_key_t key_dst, key_other;
266 : 0 : int ret;
267 : 0 : size_t idx;
268 : 0 : struct bundle_data bd;
269 : :
270 [ # # ]: 0 : __ASSERT_NO_MSG(dst != NULL);
271 [ # # ]: 0 : __ASSERT_NO_MSG(dst->num_bits > 0);
272 [ # # ]: 0 : __ASSERT_NO_MSG(other != NULL);
273 [ # # ]: 0 : __ASSERT_NO_MSG(other->num_bits > 0);
274 : :
275 : 0 : key_dst = k_spin_lock(&dst->lock);
276 : 0 : key_other = k_spin_lock(&other->lock);
277 : :
278 : :
279 [ # # ]: 0 : if (dst->num_bits != other->num_bits) {
280 : 0 : ret = -EINVAL;
281 : 0 : goto out;
282 : : }
283 : :
284 [ # # # # : 0 : if (num_bits == 0 || num_bits > dst->num_bits || offset > dst->num_bits - num_bits) {
# # ]
285 : 0 : ret = -EINVAL;
286 : 0 : goto out;
287 : : }
288 : :
289 : 0 : setup_bundle_data(other, &bd, offset, num_bits);
290 : :
291 [ # # ]: 0 : if (bd.sidx == bd.eidx) {
292 : : /* Start/end at same bundle */
293 : 0 : dst->bundles[bd.sidx] =
294 : 0 : ((other->bundles[bd.sidx] ^ dst->bundles[bd.sidx]) & bd.smask) |
295 : : (dst->bundles[bd.sidx] & ~bd.smask);
296 : : } else {
297 : : /* Start/end at different bundle.
298 : : * So xor the bits in start and end bundles according to their bitmasks
299 : : * separately. For in-between bundles,
300 : : * xor all bits.
301 : : */
302 : 0 : dst->bundles[bd.sidx] =
303 : 0 : ((other->bundles[bd.sidx] ^ dst->bundles[bd.sidx]) & bd.smask) |
304 : : (dst->bundles[bd.sidx] & ~bd.smask);
305 : 0 : dst->bundles[bd.eidx] =
306 : 0 : ((other->bundles[bd.eidx] ^ dst->bundles[bd.eidx]) & bd.emask) |
307 : : (dst->bundles[bd.eidx] & ~bd.emask);
308 [ # # ]: 0 : for (idx = bd.sidx + 1; idx < bd.eidx; idx++) {
309 : 0 : dst->bundles[idx] ^= other->bundles[idx];
310 : : }
311 : : }
312 : :
313 : : ret = 0;
314 : :
315 : 0 : out:
316 : 0 : k_spin_unlock(&other->lock, key_other);
317 : 0 : k_spin_unlock(&dst->lock, key_dst);
318 : 0 : return ret;
319 : : }
320 : :
321 : 0 : int sys_bitarray_set_bit(sys_bitarray_t *bitarray, size_t bit)
322 : : {
323 : 0 : k_spinlock_key_t key;
324 : 0 : int ret;
325 : 0 : size_t idx, off;
326 : :
327 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray != NULL);
328 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray->num_bits > 0);
329 : :
330 : 0 : key = k_spin_lock(&bitarray->lock);
331 : :
332 [ # # ]: 0 : if (bit >= bitarray->num_bits) {
333 : 0 : ret = -EINVAL;
334 : 0 : goto out;
335 : : }
336 : :
337 : 0 : idx = bit / bundle_bitness(bitarray);
338 : 0 : off = bit % bundle_bitness(bitarray);
339 : :
340 : 0 : bitarray->bundles[idx] |= BIT(off);
341 : :
342 : 0 : ret = 0;
343 : :
344 : 0 : out:
345 : 0 : k_spin_unlock(&bitarray->lock, key);
346 : 0 : return ret;
347 : : }
348 : :
349 : 0 : int sys_bitarray_clear_bit(sys_bitarray_t *bitarray, size_t bit)
350 : : {
351 : 0 : k_spinlock_key_t key;
352 : 0 : int ret;
353 : 0 : size_t idx, off;
354 : :
355 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray != NULL);
356 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray->num_bits > 0);
357 : :
358 : 0 : key = k_spin_lock(&bitarray->lock);
359 : :
360 [ # # ]: 0 : if (bit >= bitarray->num_bits) {
361 : 0 : ret = -EINVAL;
362 : 0 : goto out;
363 : : }
364 : :
365 : 0 : idx = bit / bundle_bitness(bitarray);
366 : 0 : off = bit % bundle_bitness(bitarray);
367 : :
368 : 0 : bitarray->bundles[idx] &= ~BIT(off);
369 : :
370 : 0 : ret = 0;
371 : :
372 : 0 : out:
373 : 0 : k_spin_unlock(&bitarray->lock, key);
374 : 0 : return ret;
375 : : }
376 : :
377 : 0 : int sys_bitarray_test_bit(sys_bitarray_t *bitarray, size_t bit, int *val)
378 : : {
379 : 0 : k_spinlock_key_t key;
380 : 0 : int ret;
381 : 0 : size_t idx, off;
382 : :
383 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray != NULL);
384 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray->num_bits > 0);
385 : :
386 : 0 : key = k_spin_lock(&bitarray->lock);
387 : :
388 [ # # ]: 0 : CHECKIF(val == NULL) {
389 : 0 : ret = -EINVAL;
390 : 0 : goto out;
391 : : }
392 : :
393 [ # # ]: 0 : if (bit >= bitarray->num_bits) {
394 : 0 : ret = -EINVAL;
395 : 0 : goto out;
396 : : }
397 : :
398 : 0 : idx = bit / bundle_bitness(bitarray);
399 : 0 : off = bit % bundle_bitness(bitarray);
400 : :
401 [ # # ]: 0 : if ((bitarray->bundles[idx] & BIT(off)) != 0) {
402 : 0 : *val = 1;
403 : : } else {
404 : 0 : *val = 0;
405 : : }
406 : :
407 : : ret = 0;
408 : :
409 : 0 : out:
410 : 0 : k_spin_unlock(&bitarray->lock, key);
411 : 0 : return ret;
412 : : }
413 : :
414 : 0 : int sys_bitarray_test_and_set_bit(sys_bitarray_t *bitarray, size_t bit, int *prev_val)
415 : : {
416 : 0 : k_spinlock_key_t key;
417 : 0 : int ret;
418 : 0 : size_t idx, off;
419 : :
420 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray != NULL);
421 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray->num_bits > 0);
422 : :
423 : 0 : key = k_spin_lock(&bitarray->lock);
424 : :
425 [ # # ]: 0 : CHECKIF(prev_val == NULL) {
426 : 0 : ret = -EINVAL;
427 : 0 : goto out;
428 : : }
429 : :
430 [ # # ]: 0 : if (bit >= bitarray->num_bits) {
431 : 0 : ret = -EINVAL;
432 : 0 : goto out;
433 : : }
434 : :
435 : 0 : idx = bit / bundle_bitness(bitarray);
436 : 0 : off = bit % bundle_bitness(bitarray);
437 : :
438 [ # # ]: 0 : if ((bitarray->bundles[idx] & BIT(off)) != 0) {
439 : 0 : *prev_val = 1;
440 : : } else {
441 : 0 : *prev_val = 0;
442 : : }
443 : :
444 : 0 : bitarray->bundles[idx] |= BIT(off);
445 : :
446 : 0 : ret = 0;
447 : :
448 : 0 : out:
449 : 0 : k_spin_unlock(&bitarray->lock, key);
450 : 0 : return ret;
451 : : }
452 : :
453 : 0 : int sys_bitarray_test_and_clear_bit(sys_bitarray_t *bitarray, size_t bit, int *prev_val)
454 : : {
455 : 0 : k_spinlock_key_t key;
456 : 0 : int ret;
457 : 0 : size_t idx, off;
458 : :
459 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray != NULL);
460 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray->num_bits > 0);
461 : :
462 : 0 : key = k_spin_lock(&bitarray->lock);
463 : :
464 [ # # ]: 0 : CHECKIF(prev_val == NULL) {
465 : 0 : ret = -EINVAL;
466 : 0 : goto out;
467 : : }
468 : :
469 [ # # ]: 0 : if (bit >= bitarray->num_bits) {
470 : 0 : ret = -EINVAL;
471 : 0 : goto out;
472 : : }
473 : :
474 : 0 : idx = bit / bundle_bitness(bitarray);
475 : 0 : off = bit % bundle_bitness(bitarray);
476 : :
477 [ # # ]: 0 : if ((bitarray->bundles[idx] & BIT(off)) != 0) {
478 : 0 : *prev_val = 1;
479 : : } else {
480 : 0 : *prev_val = 0;
481 : : }
482 : :
483 : 0 : bitarray->bundles[idx] &= ~BIT(off);
484 : :
485 : 0 : ret = 0;
486 : :
487 : 0 : out:
488 : 0 : k_spin_unlock(&bitarray->lock, key);
489 : 0 : return ret;
490 : : }
491 : :
492 : 0 : int sys_bitarray_alloc(sys_bitarray_t *bitarray, size_t num_bits,
493 : : size_t *offset)
494 : : {
495 : 0 : k_spinlock_key_t key;
496 : 0 : uint32_t bit_idx;
497 : 0 : int ret;
498 : 0 : struct bundle_data bd;
499 : 0 : size_t off_start, off_end;
500 : 0 : size_t mismatch;
501 : :
502 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray != NULL);
503 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray->num_bits > 0);
504 : :
505 : 0 : key = k_spin_lock(&bitarray->lock);
506 : :
507 [ # # ]: 0 : CHECKIF(offset == NULL) {
508 : 0 : ret = -EINVAL;
509 : 0 : goto out;
510 : : }
511 : :
512 [ # # # # ]: 0 : if ((num_bits == 0) || (num_bits > bitarray->num_bits)) {
513 : 0 : ret = -EINVAL;
514 : 0 : goto out;
515 : : }
516 : :
517 [ # # ]: 0 : if (bitarray->num_bits <= 32) {
518 : 0 : int off;
519 : :
520 : 0 : off = bitmask_find_gap(bitarray->bundles[0], num_bits, bitarray->num_bits, false);
521 [ # # ]: 0 : if (off < 0) {
522 : : ret = -ENOSPC;
523 : : } else {
524 : 0 : bitarray->bundles[0] |= BIT_MASK(num_bits) << off;
525 : 0 : *offset = off;
526 : 0 : ret = 0;
527 : : }
528 : 0 : goto out;
529 : : }
530 : :
531 : : bit_idx = 0;
532 : :
533 : : /* Find the first non-allocated bit by looking at bundles
534 : : * instead of individual bits.
535 : : *
536 : : * On RISC-V 64-bit, it complains about undefined reference to `ffs`.
537 : : * So don't use this on RISCV64.
538 : : */
539 [ # # ]: 0 : for (size_t idx = 0; idx < bitarray->num_bundles; idx++) {
540 [ # # ]: 0 : if (~bitarray->bundles[idx] == 0U) {
541 : : /* bundle is all 1s => all allocated, skip */
542 : 0 : bit_idx += bundle_bitness(bitarray);
543 : 0 : continue;
544 : : }
545 : :
546 [ # # ]: 0 : if (bitarray->bundles[idx] != 0U) {
547 : : /* Find the first free bit in bundle if not all free */
548 : 0 : off_start = find_lsb_set(~bitarray->bundles[idx]) - 1;
549 : 0 : bit_idx += off_start;
550 : : }
551 : :
552 : : break;
553 : : }
554 : :
555 : 0 : off_end = bitarray->num_bits - num_bits;
556 : 0 : ret = -ENOSPC;
557 [ # # ]: 0 : while (bit_idx <= off_end) {
558 [ # # ]: 0 : if (match_region(bitarray, bit_idx, num_bits, false,
559 : : &bd, &mismatch)) {
560 : 0 : set_region(bitarray, bit_idx, num_bits, true, &bd);
561 : :
562 : 0 : *offset = bit_idx;
563 : 0 : ret = 0;
564 : 0 : break;
565 : : }
566 : :
567 : : /* Fast-forward to the bit just after
568 : : * the mismatched bit.
569 : : */
570 : 0 : bit_idx = mismatch + 1;
571 : : }
572 : :
573 : 0 : out:
574 : 0 : k_spin_unlock(&bitarray->lock, key);
575 : 0 : return ret;
576 : : }
577 : :
578 : 0 : int sys_bitarray_find_nth_set(sys_bitarray_t *bitarray, size_t n, size_t num_bits, size_t offset,
579 : : size_t *found_at)
580 : : {
581 : 0 : k_spinlock_key_t key;
582 : 0 : size_t count, idx;
583 : 0 : uint32_t mask;
584 : 0 : struct bundle_data bd;
585 : 0 : int ret;
586 : :
587 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray != NULL);
588 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray->num_bits > 0);
589 : :
590 : 0 : key = k_spin_lock(&bitarray->lock);
591 : :
592 [ # # # # ]: 0 : if (n == 0 || num_bits == 0 || num_bits > bitarray->num_bits ||
593 [ # # ]: 0 : offset > bitarray->num_bits - num_bits) {
594 : 0 : ret = -EINVAL;
595 : 0 : goto out;
596 : : }
597 : :
598 : 0 : ret = 1;
599 : 0 : mask = 0;
600 : 0 : setup_bundle_data(bitarray, &bd, offset, num_bits);
601 : :
602 : 0 : count = POPCOUNT(bitarray->bundles[bd.sidx] & bd.smask);
603 : : /* If we already found more bits set than n, we found the target bundle */
604 [ # # ]: 0 : if (count >= n) {
605 : 0 : idx = bd.sidx;
606 : 0 : mask = bd.smask;
607 : 0 : goto found;
608 : : }
609 : : /* Keep looking if there are more bundles */
610 [ # # ]: 0 : if (bd.sidx != bd.eidx) {
611 : : /* We are now only looking for the remaining bits */
612 : 0 : n -= count;
613 : : /* First bundle was already checked, keep looking in middle (complete)
614 : : * bundles.
615 : : */
616 [ # # ]: 0 : for (idx = bd.sidx + 1; idx < bd.eidx; idx++) {
617 : 0 : count = POPCOUNT(bitarray->bundles[idx]);
618 [ # # ]: 0 : if (count >= n) {
619 : 0 : mask = ~(mask & 0);
620 : 0 : goto found;
621 : : }
622 : 0 : n -= count;
623 : : }
624 : : /* Continue searching in last bundle */
625 : 0 : count = POPCOUNT(bitarray->bundles[bd.eidx] & bd.emask);
626 [ # # ]: 0 : if (count >= n) {
627 : 0 : idx = bd.eidx;
628 : 0 : mask = bd.emask;
629 : 0 : goto found;
630 : : }
631 : : }
632 : :
633 : 0 : goto out;
634 : :
635 : 0 : found:
636 : : /* The bit we are looking for must be in the current bundle idx.
637 : : * Find out the exact index of the bit.
638 : : */
639 [ # # ]: 0 : for (size_t j = 0; j <= bundle_bitness(bitarray) - 1; j++) {
640 [ # # ]: 0 : if (bitarray->bundles[idx] & mask & BIT(j)) {
641 [ # # ]: 0 : if (--n <= 0) {
642 : 0 : *found_at = idx * bundle_bitness(bitarray) + j;
643 : 0 : ret = 0;
644 : 0 : break;
645 : : }
646 : : }
647 : : }
648 : :
649 : 0 : out:
650 : 0 : k_spin_unlock(&bitarray->lock, key);
651 : 0 : return ret;
652 : : }
653 : :
654 : 0 : int sys_bitarray_free(sys_bitarray_t *bitarray, size_t num_bits,
655 : : size_t offset)
656 : : {
657 : 0 : k_spinlock_key_t key;
658 : 0 : int ret;
659 : 0 : size_t off_end = offset + num_bits - 1;
660 : 0 : struct bundle_data bd;
661 : :
662 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray != NULL);
663 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray->num_bits > 0);
664 : :
665 : 0 : key = k_spin_lock(&bitarray->lock);
666 : :
667 [ # # ]: 0 : if ((num_bits == 0)
668 [ # # ]: 0 : || (num_bits > bitarray->num_bits)
669 [ # # ]: 0 : || (offset >= bitarray->num_bits)
670 [ # # ]: 0 : || (off_end >= bitarray->num_bits)) {
671 : 0 : ret = -EINVAL;
672 : 0 : goto out;
673 : : }
674 : :
675 : : /* Note that we need to make sure the bits in specified region
676 : : * (offset to offset + num_bits) are all allocated before we clear
677 : : * them.
678 : : */
679 [ # # ]: 0 : if (bitarray->num_bits <= 32) {
680 : 0 : uint32_t mask = BIT_MASK(num_bits) << offset;
681 : :
682 [ # # ]: 0 : if ((mask & bitarray->bundles[0]) != mask) {
683 : : ret = -EFAULT;
684 : : } else {
685 : 0 : bitarray->bundles[0] &= ~mask;
686 : 0 : ret = 0;
687 : : }
688 [ # # ]: 0 : } else if (match_region(bitarray, offset, num_bits, true, &bd, NULL)) {
689 : 0 : set_region(bitarray, offset, num_bits, false, &bd);
690 : 0 : ret = 0;
691 : : } else {
692 : : ret = -EFAULT;
693 : : }
694 : :
695 : 0 : out:
696 : 0 : k_spin_unlock(&bitarray->lock, key);
697 : 0 : return ret;
698 : : }
699 : :
700 : 0 : static bool is_region_set_clear(sys_bitarray_t *bitarray, size_t num_bits,
701 : : size_t offset, bool to_set)
702 : : {
703 : 0 : bool ret;
704 : 0 : struct bundle_data bd;
705 : 0 : size_t off_end = offset + num_bits - 1;
706 : 0 : k_spinlock_key_t key = k_spin_lock(&bitarray->lock);
707 : :
708 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray != NULL);
709 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray->num_bits > 0);
710 : :
711 [ # # ]: 0 : if ((num_bits == 0)
712 [ # # ]: 0 : || (num_bits > bitarray->num_bits)
713 [ # # ]: 0 : || (offset >= bitarray->num_bits)
714 [ # # ]: 0 : || (off_end >= bitarray->num_bits)) {
715 : 0 : ret = false;
716 : 0 : goto out;
717 : : }
718 : :
719 : 0 : ret = match_region(bitarray, offset, num_bits, to_set, &bd, NULL);
720 : :
721 : 0 : out:
722 : 0 : k_spin_unlock(&bitarray->lock, key);
723 : 0 : return ret;
724 : : }
725 : :
726 : 0 : bool sys_bitarray_is_region_set(sys_bitarray_t *bitarray, size_t num_bits,
727 : : size_t offset)
728 : : {
729 : 0 : return is_region_set_clear(bitarray, num_bits, offset, true);
730 : : }
731 : :
732 : 0 : bool sys_bitarray_is_region_cleared(sys_bitarray_t *bitarray, size_t num_bits,
733 : : size_t offset)
734 : : {
735 : 0 : return is_region_set_clear(bitarray, num_bits, offset, false);
736 : : }
737 : :
738 : 0 : static int set_clear_region(sys_bitarray_t *bitarray, size_t num_bits,
739 : : size_t offset, bool to_set)
740 : : {
741 : 0 : int ret;
742 : 0 : size_t off_end = offset + num_bits - 1;
743 : 0 : k_spinlock_key_t key = k_spin_lock(&bitarray->lock);
744 : :
745 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray != NULL);
746 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray->num_bits > 0);
747 : :
748 [ # # ]: 0 : if ((num_bits == 0)
749 [ # # ]: 0 : || (num_bits > bitarray->num_bits)
750 [ # # ]: 0 : || (offset >= bitarray->num_bits)
751 [ # # ]: 0 : || (off_end >= bitarray->num_bits)) {
752 : 0 : ret = -EINVAL;
753 : 0 : goto out;
754 : : }
755 : :
756 : 0 : set_region(bitarray, offset, num_bits, to_set, NULL);
757 : 0 : ret = 0;
758 : :
759 : 0 : out:
760 : 0 : k_spin_unlock(&bitarray->lock, key);
761 : 0 : return ret;
762 : : }
763 : :
764 : 0 : int sys_bitarray_test_and_set_region(sys_bitarray_t *bitarray, size_t num_bits,
765 : : size_t offset, bool to_set)
766 : : {
767 : 0 : int ret;
768 : 0 : bool region_clear;
769 : 0 : struct bundle_data bd;
770 : :
771 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray != NULL);
772 [ # # ]: 0 : __ASSERT_NO_MSG(bitarray->num_bits > 0);
773 : :
774 : 0 : size_t off_end = offset + num_bits - 1;
775 : 0 : k_spinlock_key_t key = k_spin_lock(&bitarray->lock);
776 : :
777 : :
778 [ # # ]: 0 : if ((num_bits == 0)
779 [ # # ]: 0 : || (num_bits > bitarray->num_bits)
780 [ # # ]: 0 : || (offset >= bitarray->num_bits)
781 [ # # ]: 0 : || (off_end >= bitarray->num_bits)) {
782 : 0 : ret = -EINVAL;
783 : 0 : goto out;
784 : : }
785 : :
786 : 0 : region_clear = match_region(bitarray, offset, num_bits, !to_set, &bd, NULL);
787 [ # # ]: 0 : if (region_clear) {
788 : 0 : set_region(bitarray, offset, num_bits, to_set, &bd);
789 : 0 : ret = 0;
790 : : } else {
791 : : ret = -EEXIST;
792 : : }
793 : :
794 : 0 : out:
795 : 0 : k_spin_unlock(&bitarray->lock, key);
796 : 0 : return ret;
797 : : }
798 : :
799 : 0 : int sys_bitarray_set_region(sys_bitarray_t *bitarray, size_t num_bits,
800 : : size_t offset)
801 : : {
802 : 0 : return set_clear_region(bitarray, num_bits, offset, true);
803 : : }
804 : :
805 : 0 : int sys_bitarray_clear_region(sys_bitarray_t *bitarray, size_t num_bits,
806 : : size_t offset)
807 : : {
808 : 0 : return set_clear_region(bitarray, num_bits, offset, false);
809 : : }
|