Zephyr Project API 3.7.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
bitarray.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_SYS_BITARRAY_H_
8#define ZEPHYR_INCLUDE_SYS_BITARRAY_H_
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
14#include <stddef.h>
15#include <stdint.h>
16
17#include <zephyr/kernel.h>
18#include <zephyr/sys/util.h>
19
32struct sys_bitarray {
33 /* Number of bits */
34 uint32_t num_bits;
35
36 /* Number of bundles */
37 uint32_t num_bundles;
38
39 /* Bundle of bits */
40 uint32_t *bundles;
41
42 /* Spinlock guarding access to this bit array */
43 struct k_spinlock lock;
44};
48typedef struct sys_bitarray sys_bitarray_t;
49
57#define _SYS_BITARRAY_DEFINE(name, total_bits, sba_mod) \
58 sba_mod uint32_t _sys_bitarray_bundles_##name \
59 [DIV_ROUND_UP(DIV_ROUND_UP(total_bits, 8), \
60 sizeof(uint32_t))] = {0}; \
61 sba_mod sys_bitarray_t name = { \
62 .num_bits = (total_bits), \
63 .num_bundles = DIV_ROUND_UP( \
64 DIV_ROUND_UP(total_bits, 8), sizeof(uint32_t)), \
65 .bundles = _sys_bitarray_bundles_##name, \
66 }
67
74#define SYS_BITARRAY_DEFINE(name, total_bits) \
75 _SYS_BITARRAY_DEFINE(name, total_bits,)
76
83#define SYS_BITARRAY_DEFINE_STATIC(name, total_bits) \
84 _SYS_BITARRAY_DEFINE(name, total_bits, static)
85
96int sys_bitarray_set_bit(sys_bitarray_t *bitarray, size_t bit);
97
108int sys_bitarray_clear_bit(sys_bitarray_t *bitarray, size_t bit);
109
121int sys_bitarray_test_bit(sys_bitarray_t *bitarray, size_t bit, int *val);
122
134int sys_bitarray_test_and_set_bit(sys_bitarray_t *bitarray, size_t bit, int *prev_val);
135
147int sys_bitarray_test_and_clear_bit(sys_bitarray_t *bitarray, size_t bit, int *prev_val);
148
168int sys_bitarray_alloc(sys_bitarray_t *bitarray, size_t num_bits,
169 size_t *offset);
170
185int sys_bitarray_xor(sys_bitarray_t *dst, sys_bitarray_t *other, size_t num_bits, size_t offset);
186
206int sys_bitarray_find_nth_set(sys_bitarray_t *bitarray, size_t n, size_t num_bits, size_t offset,
207 size_t *found_at);
208
223int sys_bitarray_popcount_region(sys_bitarray_t *bitarray, size_t num_bits, size_t offset,
224 size_t *count);
225
241int sys_bitarray_free(sys_bitarray_t *bitarray, size_t num_bits,
242 size_t offset);
243
257bool sys_bitarray_is_region_set(sys_bitarray_t *bitarray, size_t num_bits,
258 size_t offset);
259
273bool sys_bitarray_is_region_cleared(sys_bitarray_t *bitarray, size_t num_bits,
274 size_t offset);
275
290int sys_bitarray_set_region(sys_bitarray_t *bitarray, size_t num_bits,
291 size_t offset);
292
316 size_t offset, bool to_set);
317
332int sys_bitarray_clear_region(sys_bitarray_t *bitarray, size_t num_bits,
333 size_t offset);
334
339#ifdef __cplusplus
340}
341#endif
342
343#endif /* ZEPHYR_INCLUDE_SYS_BITARRAY_H_ */
ZTEST_BMEM int count
Definition main.c:33
int sys_bitarray_find_nth_set(sys_bitarray_t *bitarray, size_t n, size_t num_bits, size_t offset, size_t *found_at)
Find nth bit set in region.
struct sys_bitarray sys_bitarray_t
Bitarray structure.
Definition bitarray.h:48
int sys_bitarray_xor(sys_bitarray_t *dst, sys_bitarray_t *other, size_t num_bits, size_t offset)
Calculates the bit-wise XOR of two bitarrays in a region.
bool sys_bitarray_is_region_cleared(sys_bitarray_t *bitarray, size_t num_bits, size_t offset)
Test if bits in a region is all cleared.
bool sys_bitarray_is_region_set(sys_bitarray_t *bitarray, size_t num_bits, size_t offset)
Test if bits in a region is all set.
int sys_bitarray_test_and_clear_bit(sys_bitarray_t *bitarray, size_t bit, int *prev_val)
Test the bit and clear it.
int sys_bitarray_set_bit(sys_bitarray_t *bitarray, size_t bit)
Set a bit in a bit array.
int sys_bitarray_clear_bit(sys_bitarray_t *bitarray, size_t bit)
Clear a bit in a bit array.
int sys_bitarray_popcount_region(sys_bitarray_t *bitarray, size_t num_bits, size_t offset, size_t *count)
Count bits set in a bit array region.
int sys_bitarray_set_region(sys_bitarray_t *bitarray, size_t num_bits, size_t offset)
Set all bits in a region.
int sys_bitarray_alloc(sys_bitarray_t *bitarray, size_t num_bits, size_t *offset)
Allocate bits in a bit array.
int sys_bitarray_clear_region(sys_bitarray_t *bitarray, size_t num_bits, size_t offset)
Clear all bits in a region.
int sys_bitarray_test_and_set_bit(sys_bitarray_t *bitarray, size_t bit, int *prev_val)
Test the bit and set it.
int sys_bitarray_test_and_set_region(sys_bitarray_t *bitarray, size_t num_bits, size_t offset, bool to_set)
Test if all bits in a region are cleared/set and set/clear them in a single atomic operation.
int sys_bitarray_test_bit(sys_bitarray_t *bitarray, size_t bit, int *val)
Test whether a bit is set or not.
int sys_bitarray_free(sys_bitarray_t *bitarray, size_t num_bits, size_t offset)
Free bits in a bit array.
Public kernel APIs.
static struct k_spinlock lock
Definition spinlock_error_case.c:13
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
Kernel Spin Lock.
Definition spinlock.h:45
Misc utilities.