]>
git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/include/spdk/bit_array.h
4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Bit array data structure
38 #ifndef SPDK_BIT_ARRAY_H
39 #define SPDK_BIT_ARRAY_H
41 #include "spdk/stdinc.h"
48 * Variable-length bit array.
50 struct spdk_bit_array
;
53 * Return the number of bits that a bit array is currently sized to hold.
55 * \param ba Bit array to query.
57 * \return the number of bits.
59 uint32_t spdk_bit_array_capacity(const struct spdk_bit_array
*ba
);
64 * \param num_bits Number of bits that the bit array is sized to hold.
66 * All bits in the array will be cleared.
68 * \return a pointer to the new bit array.
70 struct spdk_bit_array
*spdk_bit_array_create(uint32_t num_bits
);
73 * Free a bit array and set the pointer to NULL.
75 * \param bap Bit array to free.
77 void spdk_bit_array_free(struct spdk_bit_array
**bap
);
80 * Create or resize a bit array.
82 * To create a new bit array, pass a pointer to a spdk_bit_array pointer that is
85 * The bit array will be sized to hold at least num_bits.
87 * If num_bits is smaller than the previous size of the bit array,
88 * any data beyond the new num_bits size will be cleared.
90 * If num_bits is larger than the previous size of the bit array,
91 * any data beyond the old num_bits size will be cleared.
93 * \param bap Bit array to create/resize.
94 * \param num_bits Number of bits that the bit array is sized to hold.
96 * \return 0 on success, negative errno on failure.
98 int spdk_bit_array_resize(struct spdk_bit_array
**bap
, uint32_t num_bits
);
101 * Get the value of a bit from the bit array.
103 * If bit_index is beyond the end of the current size of the bit array, this
104 * function will return false (i.e. bits beyond the end of the array are implicitly 0).
106 * \param ba Bit array to query.
107 * \param bit_index The index of a bit to query.
109 * \return the value of a bit from the bit array on success, or false on failure.
111 bool spdk_bit_array_get(const struct spdk_bit_array
*ba
, uint32_t bit_index
);
114 * Set (to 1) a bit in the bit array.
116 * If bit_index is beyond the end of the bit array, this function will return -EINVAL.
118 * \param ba Bit array to set a bit.
119 * \param bit_index The index of a bit to set.
121 * \return 0 on success, negative errno on failure.
123 int spdk_bit_array_set(struct spdk_bit_array
*ba
, uint32_t bit_index
);
126 * Clear (to 0) a bit in the bit array.
128 * If bit_index is beyond the end of the bit array, no action is taken. Bits
129 * beyond the end of the bit array are implicitly 0.
131 * \param ba Bit array to clear a bit.
132 * \param bit_index The index of a bit to clear.
134 void spdk_bit_array_clear(struct spdk_bit_array
*ba
, uint32_t bit_index
);
137 * Find the index of the first set bit in the array.
139 * \param ba The bit array to search.
140 * \param start_bit_index The bit index from which to start searching (0 to start
141 * from the beginning of the array).
143 * \return the index of the first set bit. If no bits are set, returns UINT32_MAX.
145 uint32_t spdk_bit_array_find_first_set(const struct spdk_bit_array
*ba
, uint32_t start_bit_index
);
148 * Find the index of the first cleared bit in the array.
150 * \param ba The bit array to search.
151 * \param start_bit_index The bit index from which to start searching (0 to start
152 * from the beginning of the array).
154 * \return the index of the first cleared bit. If no bits are cleared, returns UINT32_MAX.
156 uint32_t spdk_bit_array_find_first_clear(const struct spdk_bit_array
*ba
, uint32_t start_bit_index
);
159 * Count the number of set bits in the array.
161 * \param ba The bit array to search.
163 * \return the number of bits set in the array.
165 uint32_t spdk_bit_array_count_set(const struct spdk_bit_array
*ba
);
168 * Count the number of cleared bits in the array.
170 * \param ba The bit array to search.
172 * \return the number of bits cleared in the array.
174 uint32_t spdk_bit_array_count_clear(const struct spdk_bit_array
*ba
);
177 * Store bitmask from bit array.
179 * \param ba Bit array.
180 * \param mask Destination mask. Mask and bit array capacity must be equal.
182 void spdk_bit_array_store_mask(const struct spdk_bit_array
*ba
, void *mask
);
185 * Load bitmask to bit array.
187 * \param ba Bit array.
188 * \param mask Source mask. Mask and bit array capacity must be equal.
190 void spdk_bit_array_load_mask(struct spdk_bit_array
*ba
, const void *mask
);
193 * Clear (to 0) bit array bitmask.
195 * \param ba Bit array.
197 void spdk_bit_array_clear_mask(struct spdk_bit_array
*ba
);