1488570ebSJim Harris /* SPDX-License-Identifier: BSD-3-Clause
2*a6dbe372Spaul luse * Copyright (C) 2016 Intel Corporation.
36c5f05f1SDaniel Verkamp * All rights reserved.
46c5f05f1SDaniel Verkamp */
56c5f05f1SDaniel Verkamp
6b961d9ccSBen Walker #include "spdk/stdinc.h"
7b961d9ccSBen Walker
86c5f05f1SDaniel Verkamp #include "spdk/bit_array.h"
90d2a5bbeSJim Harris #include "spdk/bit_pool.h"
10621f96f7SGangCao #include "spdk/env.h"
116c5f05f1SDaniel Verkamp
129d524f53SDaniel Verkamp #include "spdk/likely.h"
1384d90484SDaniel Verkamp #include "spdk/util.h"
149d524f53SDaniel Verkamp
156c5f05f1SDaniel Verkamp typedef uint64_t spdk_bit_array_word;
166c5f05f1SDaniel Verkamp #define SPDK_BIT_ARRAY_WORD_TZCNT(x) (__builtin_ctzll(x))
17a1c7c58fSDaniel Verkamp #define SPDK_BIT_ARRAY_WORD_POPCNT(x) (__builtin_popcountll(x))
186c5f05f1SDaniel Verkamp #define SPDK_BIT_ARRAY_WORD_C(x) ((spdk_bit_array_word)(x))
196c5f05f1SDaniel Verkamp #define SPDK_BIT_ARRAY_WORD_BYTES sizeof(spdk_bit_array_word)
206c5f05f1SDaniel Verkamp #define SPDK_BIT_ARRAY_WORD_BITS (SPDK_BIT_ARRAY_WORD_BYTES * 8)
2184d90484SDaniel Verkamp #define SPDK_BIT_ARRAY_WORD_INDEX_SHIFT spdk_u32log2(SPDK_BIT_ARRAY_WORD_BITS)
226c5f05f1SDaniel Verkamp #define SPDK_BIT_ARRAY_WORD_INDEX_MASK ((1u << SPDK_BIT_ARRAY_WORD_INDEX_SHIFT) - 1)
236c5f05f1SDaniel Verkamp
246c5f05f1SDaniel Verkamp struct spdk_bit_array {
256c5f05f1SDaniel Verkamp uint32_t bit_count;
266c5f05f1SDaniel Verkamp spdk_bit_array_word words[];
276c5f05f1SDaniel Verkamp };
286c5f05f1SDaniel Verkamp
296c5f05f1SDaniel Verkamp struct spdk_bit_array *
spdk_bit_array_create(uint32_t num_bits)306c5f05f1SDaniel Verkamp spdk_bit_array_create(uint32_t num_bits)
316c5f05f1SDaniel Verkamp {
326c5f05f1SDaniel Verkamp struct spdk_bit_array *ba = NULL;
336c5f05f1SDaniel Verkamp
346c5f05f1SDaniel Verkamp spdk_bit_array_resize(&ba, num_bits);
356c5f05f1SDaniel Verkamp
366c5f05f1SDaniel Verkamp return ba;
376c5f05f1SDaniel Verkamp }
386c5f05f1SDaniel Verkamp
396c5f05f1SDaniel Verkamp void
spdk_bit_array_free(struct spdk_bit_array ** bap)406c5f05f1SDaniel Verkamp spdk_bit_array_free(struct spdk_bit_array **bap)
416c5f05f1SDaniel Verkamp {
426c5f05f1SDaniel Verkamp struct spdk_bit_array *ba;
436c5f05f1SDaniel Verkamp
446c5f05f1SDaniel Verkamp if (!bap) {
456c5f05f1SDaniel Verkamp return;
466c5f05f1SDaniel Verkamp }
476c5f05f1SDaniel Verkamp
486c5f05f1SDaniel Verkamp ba = *bap;
496c5f05f1SDaniel Verkamp *bap = NULL;
5020d575bbSDarek Stojaczyk spdk_free(ba);
516c5f05f1SDaniel Verkamp }
526c5f05f1SDaniel Verkamp
536c5f05f1SDaniel Verkamp static inline uint32_t
bit_array_word_count(uint32_t num_bits)54b869b4b9SSeth Howell bit_array_word_count(uint32_t num_bits)
556c5f05f1SDaniel Verkamp {
566c5f05f1SDaniel Verkamp return (num_bits + SPDK_BIT_ARRAY_WORD_BITS - 1) >> SPDK_BIT_ARRAY_WORD_INDEX_SHIFT;
576c5f05f1SDaniel Verkamp }
586c5f05f1SDaniel Verkamp
596c5f05f1SDaniel Verkamp static inline spdk_bit_array_word
bit_array_word_mask(uint32_t num_bits)60b869b4b9SSeth Howell bit_array_word_mask(uint32_t num_bits)
616c5f05f1SDaniel Verkamp {
626c5f05f1SDaniel Verkamp assert(num_bits < SPDK_BIT_ARRAY_WORD_BITS);
636c5f05f1SDaniel Verkamp return (SPDK_BIT_ARRAY_WORD_C(1) << num_bits) - 1;
646c5f05f1SDaniel Verkamp }
656c5f05f1SDaniel Verkamp
666c5f05f1SDaniel Verkamp int
spdk_bit_array_resize(struct spdk_bit_array ** bap,uint32_t num_bits)676c5f05f1SDaniel Verkamp spdk_bit_array_resize(struct spdk_bit_array **bap, uint32_t num_bits)
686c5f05f1SDaniel Verkamp {
696c5f05f1SDaniel Verkamp struct spdk_bit_array *new_ba;
706c5f05f1SDaniel Verkamp uint32_t old_word_count, new_word_count;
716c5f05f1SDaniel Verkamp size_t new_size;
726c5f05f1SDaniel Verkamp
737c56c393SJim Harris /*
747c56c393SJim Harris * Max number of bits allowed is UINT32_MAX - 1, because we use UINT32_MAX to denote
757c56c393SJim Harris * when a set or cleared bit cannot be found.
767c56c393SJim Harris */
777c56c393SJim Harris if (!bap || num_bits == UINT32_MAX) {
786c5f05f1SDaniel Verkamp return -EINVAL;
796c5f05f1SDaniel Verkamp }
806c5f05f1SDaniel Verkamp
81b869b4b9SSeth Howell new_word_count = bit_array_word_count(num_bits);
826c5f05f1SDaniel Verkamp new_size = offsetof(struct spdk_bit_array, words) + new_word_count * SPDK_BIT_ARRAY_WORD_BYTES;
836c5f05f1SDaniel Verkamp
846c5f05f1SDaniel Verkamp /*
856c5f05f1SDaniel Verkamp * Always keep one extra word with a 0 and a 1 past the actual required size so that the
866c5f05f1SDaniel Verkamp * find_first functions can just keep going until they match.
876c5f05f1SDaniel Verkamp */
886c5f05f1SDaniel Verkamp new_size += SPDK_BIT_ARRAY_WORD_BYTES;
896c5f05f1SDaniel Verkamp
9020d575bbSDarek Stojaczyk new_ba = (struct spdk_bit_array *)spdk_realloc(*bap, new_size, 64);
916c5f05f1SDaniel Verkamp if (!new_ba) {
926c5f05f1SDaniel Verkamp return -ENOMEM;
936c5f05f1SDaniel Verkamp }
946c5f05f1SDaniel Verkamp
956c5f05f1SDaniel Verkamp /*
966c5f05f1SDaniel Verkamp * Set up special extra word (see above comment about find_first_clear).
976c5f05f1SDaniel Verkamp *
986c5f05f1SDaniel Verkamp * This is set to 0b10 so that find_first_clear will find a 0 at the very first
996c5f05f1SDaniel Verkamp * bit past the end of the buffer, and find_first_set will find a 1 at the next bit
1006c5f05f1SDaniel Verkamp * past that.
1016c5f05f1SDaniel Verkamp */
1026c5f05f1SDaniel Verkamp new_ba->words[new_word_count] = 0x2;
1036c5f05f1SDaniel Verkamp
1046c5f05f1SDaniel Verkamp if (*bap == NULL) {
1056c5f05f1SDaniel Verkamp old_word_count = 0;
1066c5f05f1SDaniel Verkamp new_ba->bit_count = 0;
1076c5f05f1SDaniel Verkamp } else {
108b869b4b9SSeth Howell old_word_count = bit_array_word_count(new_ba->bit_count);
1096c5f05f1SDaniel Verkamp }
1106c5f05f1SDaniel Verkamp
1116c5f05f1SDaniel Verkamp if (new_word_count > old_word_count) {
1126c5f05f1SDaniel Verkamp /* Zero out new entries */
1136c5f05f1SDaniel Verkamp memset(&new_ba->words[old_word_count], 0,
1146c5f05f1SDaniel Verkamp (new_word_count - old_word_count) * SPDK_BIT_ARRAY_WORD_BYTES);
1156c5f05f1SDaniel Verkamp } else if (new_word_count == old_word_count && num_bits < new_ba->bit_count) {
1166c5f05f1SDaniel Verkamp /* Make sure any existing partial last word is cleared beyond the new num_bits. */
1176c5f05f1SDaniel Verkamp uint32_t last_word_bits;
1186c5f05f1SDaniel Verkamp spdk_bit_array_word mask;
1196c5f05f1SDaniel Verkamp
1206c5f05f1SDaniel Verkamp last_word_bits = num_bits & SPDK_BIT_ARRAY_WORD_INDEX_MASK;
121b869b4b9SSeth Howell mask = bit_array_word_mask(last_word_bits);
1226c5f05f1SDaniel Verkamp new_ba->words[old_word_count - 1] &= mask;
1236c5f05f1SDaniel Verkamp }
1246c5f05f1SDaniel Verkamp
1256c5f05f1SDaniel Verkamp new_ba->bit_count = num_bits;
1266c5f05f1SDaniel Verkamp *bap = new_ba;
1276c5f05f1SDaniel Verkamp return 0;
1286c5f05f1SDaniel Verkamp }
1296c5f05f1SDaniel Verkamp
1306c5f05f1SDaniel Verkamp uint32_t
spdk_bit_array_capacity(const struct spdk_bit_array * ba)1316c5f05f1SDaniel Verkamp spdk_bit_array_capacity(const struct spdk_bit_array *ba)
1326c5f05f1SDaniel Verkamp {
1336c5f05f1SDaniel Verkamp return ba->bit_count;
1346c5f05f1SDaniel Verkamp }
1356c5f05f1SDaniel Verkamp
1366c5f05f1SDaniel Verkamp static inline int
bit_array_get_word(const struct spdk_bit_array * ba,uint32_t bit_index,uint32_t * word_index,uint32_t * word_bit_index)13757c2b0c5SSeth Howell bit_array_get_word(const struct spdk_bit_array *ba, uint32_t bit_index,
1386c5f05f1SDaniel Verkamp uint32_t *word_index, uint32_t *word_bit_index)
1396c5f05f1SDaniel Verkamp {
1409d524f53SDaniel Verkamp if (spdk_unlikely(bit_index >= ba->bit_count)) {
1416c5f05f1SDaniel Verkamp return -EINVAL;
1426c5f05f1SDaniel Verkamp }
1436c5f05f1SDaniel Verkamp
1446c5f05f1SDaniel Verkamp *word_index = bit_index >> SPDK_BIT_ARRAY_WORD_INDEX_SHIFT;
1456c5f05f1SDaniel Verkamp *word_bit_index = bit_index & SPDK_BIT_ARRAY_WORD_INDEX_MASK;
1466c5f05f1SDaniel Verkamp
1476c5f05f1SDaniel Verkamp return 0;
1486c5f05f1SDaniel Verkamp }
1496c5f05f1SDaniel Verkamp
1506c5f05f1SDaniel Verkamp bool
spdk_bit_array_get(const struct spdk_bit_array * ba,uint32_t bit_index)1516c5f05f1SDaniel Verkamp spdk_bit_array_get(const struct spdk_bit_array *ba, uint32_t bit_index)
1526c5f05f1SDaniel Verkamp {
1536c5f05f1SDaniel Verkamp uint32_t word_index, word_bit_index;
1546c5f05f1SDaniel Verkamp
15557c2b0c5SSeth Howell if (bit_array_get_word(ba, bit_index, &word_index, &word_bit_index)) {
1566c5f05f1SDaniel Verkamp return false;
1576c5f05f1SDaniel Verkamp }
1586c5f05f1SDaniel Verkamp
1596c5f05f1SDaniel Verkamp return (ba->words[word_index] >> word_bit_index) & 1U;
1606c5f05f1SDaniel Verkamp }
1616c5f05f1SDaniel Verkamp
1626c5f05f1SDaniel Verkamp int
spdk_bit_array_set(struct spdk_bit_array * ba,uint32_t bit_index)1636c5f05f1SDaniel Verkamp spdk_bit_array_set(struct spdk_bit_array *ba, uint32_t bit_index)
1646c5f05f1SDaniel Verkamp {
1656c5f05f1SDaniel Verkamp uint32_t word_index, word_bit_index;
1666c5f05f1SDaniel Verkamp
16757c2b0c5SSeth Howell if (bit_array_get_word(ba, bit_index, &word_index, &word_bit_index)) {
1686c5f05f1SDaniel Verkamp return -EINVAL;
1696c5f05f1SDaniel Verkamp }
1706c5f05f1SDaniel Verkamp
1716c5f05f1SDaniel Verkamp ba->words[word_index] |= (SPDK_BIT_ARRAY_WORD_C(1) << word_bit_index);
1726c5f05f1SDaniel Verkamp return 0;
1736c5f05f1SDaniel Verkamp }
1746c5f05f1SDaniel Verkamp
1756c5f05f1SDaniel Verkamp void
spdk_bit_array_clear(struct spdk_bit_array * ba,uint32_t bit_index)1766c5f05f1SDaniel Verkamp spdk_bit_array_clear(struct spdk_bit_array *ba, uint32_t bit_index)
1776c5f05f1SDaniel Verkamp {
1786c5f05f1SDaniel Verkamp uint32_t word_index, word_bit_index;
1796c5f05f1SDaniel Verkamp
18057c2b0c5SSeth Howell if (bit_array_get_word(ba, bit_index, &word_index, &word_bit_index)) {
1816c5f05f1SDaniel Verkamp /*
1826c5f05f1SDaniel Verkamp * Clearing past the end of the bit array is a no-op, since bit past the end
1836c5f05f1SDaniel Verkamp * are implicitly 0.
1846c5f05f1SDaniel Verkamp */
1856c5f05f1SDaniel Verkamp return;
1866c5f05f1SDaniel Verkamp }
1876c5f05f1SDaniel Verkamp
1886c5f05f1SDaniel Verkamp ba->words[word_index] &= ~(SPDK_BIT_ARRAY_WORD_C(1) << word_bit_index);
1896c5f05f1SDaniel Verkamp }
1906c5f05f1SDaniel Verkamp
1916c5f05f1SDaniel Verkamp static inline uint32_t
bit_array_find_first(const struct spdk_bit_array * ba,uint32_t start_bit_index,spdk_bit_array_word xor_mask)19257c2b0c5SSeth Howell bit_array_find_first(const struct spdk_bit_array *ba, uint32_t start_bit_index,
1936c5f05f1SDaniel Verkamp spdk_bit_array_word xor_mask)
1946c5f05f1SDaniel Verkamp {
1956c5f05f1SDaniel Verkamp uint32_t word_index, first_word_bit_index;
1966c5f05f1SDaniel Verkamp spdk_bit_array_word word, first_word_mask;
1976c5f05f1SDaniel Verkamp const spdk_bit_array_word *words, *cur_word;
1986c5f05f1SDaniel Verkamp
1999d524f53SDaniel Verkamp if (spdk_unlikely(start_bit_index >= ba->bit_count)) {
2006c5f05f1SDaniel Verkamp return ba->bit_count;
2016c5f05f1SDaniel Verkamp }
2026c5f05f1SDaniel Verkamp
2036c5f05f1SDaniel Verkamp word_index = start_bit_index >> SPDK_BIT_ARRAY_WORD_INDEX_SHIFT;
2046c5f05f1SDaniel Verkamp words = ba->words;
2056c5f05f1SDaniel Verkamp cur_word = &words[word_index];
2066c5f05f1SDaniel Verkamp
2076c5f05f1SDaniel Verkamp /*
2086c5f05f1SDaniel Verkamp * Special case for first word: skip start_bit_index % SPDK_BIT_ARRAY_WORD_BITS bits
2096c5f05f1SDaniel Verkamp * within the first word.
2106c5f05f1SDaniel Verkamp */
2116c5f05f1SDaniel Verkamp first_word_bit_index = start_bit_index & SPDK_BIT_ARRAY_WORD_INDEX_MASK;
212b869b4b9SSeth Howell first_word_mask = bit_array_word_mask(first_word_bit_index);
2136c5f05f1SDaniel Verkamp
2146c5f05f1SDaniel Verkamp word = (*cur_word ^ xor_mask) & ~first_word_mask;
2156c5f05f1SDaniel Verkamp
2166c5f05f1SDaniel Verkamp /*
2176c5f05f1SDaniel Verkamp * spdk_bit_array_resize() guarantees that an extra word with a 1 and a 0 will always be
2186c5f05f1SDaniel Verkamp * at the end of the words[] array, so just keep going until a word matches.
2196c5f05f1SDaniel Verkamp */
2206c5f05f1SDaniel Verkamp while (word == 0) {
2216c5f05f1SDaniel Verkamp word = *++cur_word ^ xor_mask;
2226c5f05f1SDaniel Verkamp }
2236c5f05f1SDaniel Verkamp
2246c5f05f1SDaniel Verkamp return ((uintptr_t)cur_word - (uintptr_t)words) * 8 + SPDK_BIT_ARRAY_WORD_TZCNT(word);
2256c5f05f1SDaniel Verkamp }
2266c5f05f1SDaniel Verkamp
2276c5f05f1SDaniel Verkamp
2286c5f05f1SDaniel Verkamp uint32_t
spdk_bit_array_find_first_set(const struct spdk_bit_array * ba,uint32_t start_bit_index)2296c5f05f1SDaniel Verkamp spdk_bit_array_find_first_set(const struct spdk_bit_array *ba, uint32_t start_bit_index)
2306c5f05f1SDaniel Verkamp {
2316c5f05f1SDaniel Verkamp uint32_t bit_index;
2326c5f05f1SDaniel Verkamp
23357c2b0c5SSeth Howell bit_index = bit_array_find_first(ba, start_bit_index, 0);
2346c5f05f1SDaniel Verkamp
2356c5f05f1SDaniel Verkamp /*
2366c5f05f1SDaniel Verkamp * If we ran off the end of the array and found the 1 bit in the extra word,
2376c5f05f1SDaniel Verkamp * return UINT32_MAX to indicate no actual 1 bits were found.
2386c5f05f1SDaniel Verkamp */
2396c5f05f1SDaniel Verkamp if (bit_index >= ba->bit_count) {
2406c5f05f1SDaniel Verkamp bit_index = UINT32_MAX;
2416c5f05f1SDaniel Verkamp }
2426c5f05f1SDaniel Verkamp
2436c5f05f1SDaniel Verkamp return bit_index;
2446c5f05f1SDaniel Verkamp }
2456c5f05f1SDaniel Verkamp
2466c5f05f1SDaniel Verkamp uint32_t
spdk_bit_array_find_first_clear(const struct spdk_bit_array * ba,uint32_t start_bit_index)2476c5f05f1SDaniel Verkamp spdk_bit_array_find_first_clear(const struct spdk_bit_array *ba, uint32_t start_bit_index)
2486c5f05f1SDaniel Verkamp {
2497c56c393SJim Harris uint32_t bit_index;
2507c56c393SJim Harris
25157c2b0c5SSeth Howell bit_index = bit_array_find_first(ba, start_bit_index, SPDK_BIT_ARRAY_WORD_C(-1));
2527c56c393SJim Harris
2537c56c393SJim Harris /*
2547c56c393SJim Harris * If we ran off the end of the array and found the 0 bit in the extra word,
2557c56c393SJim Harris * return UINT32_MAX to indicate no actual 0 bits were found.
2567c56c393SJim Harris */
2577c56c393SJim Harris if (bit_index >= ba->bit_count) {
2587c56c393SJim Harris bit_index = UINT32_MAX;
2597c56c393SJim Harris }
2607c56c393SJim Harris
2617c56c393SJim Harris return bit_index;
2626c5f05f1SDaniel Verkamp }
263a1c7c58fSDaniel Verkamp
264a1c7c58fSDaniel Verkamp uint32_t
spdk_bit_array_count_set(const struct spdk_bit_array * ba)265a1c7c58fSDaniel Verkamp spdk_bit_array_count_set(const struct spdk_bit_array *ba)
266a1c7c58fSDaniel Verkamp {
267a1c7c58fSDaniel Verkamp const spdk_bit_array_word *cur_word = ba->words;
268b869b4b9SSeth Howell uint32_t word_count = bit_array_word_count(ba->bit_count);
269a1c7c58fSDaniel Verkamp uint32_t set_count = 0;
270a1c7c58fSDaniel Verkamp
271a1c7c58fSDaniel Verkamp while (word_count--) {
272a1c7c58fSDaniel Verkamp /*
273a1c7c58fSDaniel Verkamp * No special treatment is needed for the last (potentially partial) word, since
274a1c7c58fSDaniel Verkamp * spdk_bit_array_resize() makes sure the bits past bit_count are cleared.
275a1c7c58fSDaniel Verkamp */
276a1c7c58fSDaniel Verkamp set_count += SPDK_BIT_ARRAY_WORD_POPCNT(*cur_word++);
277a1c7c58fSDaniel Verkamp }
278a1c7c58fSDaniel Verkamp
279a1c7c58fSDaniel Verkamp return set_count;
280a1c7c58fSDaniel Verkamp }
281a1c7c58fSDaniel Verkamp
282a1c7c58fSDaniel Verkamp uint32_t
spdk_bit_array_count_clear(const struct spdk_bit_array * ba)283a1c7c58fSDaniel Verkamp spdk_bit_array_count_clear(const struct spdk_bit_array *ba)
284a1c7c58fSDaniel Verkamp {
285a1c7c58fSDaniel Verkamp return ba->bit_count - spdk_bit_array_count_set(ba);
286a1c7c58fSDaniel Verkamp }
287212eb893SWojciech Malikowski
288212eb893SWojciech Malikowski void
spdk_bit_array_store_mask(const struct spdk_bit_array * ba,void * mask)289212eb893SWojciech Malikowski spdk_bit_array_store_mask(const struct spdk_bit_array *ba, void *mask)
290212eb893SWojciech Malikowski {
291212eb893SWojciech Malikowski uint32_t size, i;
292212eb893SWojciech Malikowski uint32_t num_bits = spdk_bit_array_capacity(ba);
293212eb893SWojciech Malikowski
294212eb893SWojciech Malikowski size = num_bits / CHAR_BIT;
295212eb893SWojciech Malikowski memcpy(mask, ba->words, size);
296212eb893SWojciech Malikowski
297212eb893SWojciech Malikowski for (i = 0; i < num_bits % CHAR_BIT; i++) {
298212eb893SWojciech Malikowski if (spdk_bit_array_get(ba, i + size * CHAR_BIT)) {
299212eb893SWojciech Malikowski ((uint8_t *)mask)[size] |= (1U << i);
300212eb893SWojciech Malikowski } else {
301212eb893SWojciech Malikowski ((uint8_t *)mask)[size] &= ~(1U << i);
302212eb893SWojciech Malikowski }
303212eb893SWojciech Malikowski }
304212eb893SWojciech Malikowski }
305212eb893SWojciech Malikowski
306212eb893SWojciech Malikowski void
spdk_bit_array_load_mask(struct spdk_bit_array * ba,const void * mask)307212eb893SWojciech Malikowski spdk_bit_array_load_mask(struct spdk_bit_array *ba, const void *mask)
308212eb893SWojciech Malikowski {
309212eb893SWojciech Malikowski uint32_t size, i;
310212eb893SWojciech Malikowski uint32_t num_bits = spdk_bit_array_capacity(ba);
311212eb893SWojciech Malikowski
312212eb893SWojciech Malikowski size = num_bits / CHAR_BIT;
313212eb893SWojciech Malikowski memcpy(ba->words, mask, size);
314212eb893SWojciech Malikowski
315212eb893SWojciech Malikowski for (i = 0; i < num_bits % CHAR_BIT; i++) {
316212eb893SWojciech Malikowski if (((uint8_t *)mask)[size] & (1U << i)) {
317212eb893SWojciech Malikowski spdk_bit_array_set(ba, i + size * CHAR_BIT);
318212eb893SWojciech Malikowski } else {
319212eb893SWojciech Malikowski spdk_bit_array_clear(ba, i + size * CHAR_BIT);
320212eb893SWojciech Malikowski }
321212eb893SWojciech Malikowski }
322212eb893SWojciech Malikowski }
323212eb893SWojciech Malikowski
324212eb893SWojciech Malikowski void
spdk_bit_array_clear_mask(struct spdk_bit_array * ba)325212eb893SWojciech Malikowski spdk_bit_array_clear_mask(struct spdk_bit_array *ba)
326212eb893SWojciech Malikowski {
327212eb893SWojciech Malikowski uint32_t size, i;
328212eb893SWojciech Malikowski uint32_t num_bits = spdk_bit_array_capacity(ba);
329212eb893SWojciech Malikowski
330212eb893SWojciech Malikowski size = num_bits / CHAR_BIT;
331212eb893SWojciech Malikowski memset(ba->words, 0, size);
332212eb893SWojciech Malikowski
333212eb893SWojciech Malikowski for (i = 0; i < num_bits % CHAR_BIT; i++) {
334212eb893SWojciech Malikowski spdk_bit_array_clear(ba, i + size * CHAR_BIT);
335212eb893SWojciech Malikowski }
336212eb893SWojciech Malikowski }
3370d2a5bbeSJim Harris
3380d2a5bbeSJim Harris struct spdk_bit_pool {
3390d2a5bbeSJim Harris struct spdk_bit_array *array;
3400d2a5bbeSJim Harris uint32_t lowest_free_bit;
3410d2a5bbeSJim Harris uint32_t free_count;
3420d2a5bbeSJim Harris };
3430d2a5bbeSJim Harris
3440d2a5bbeSJim Harris struct spdk_bit_pool *
spdk_bit_pool_create(uint32_t num_bits)3450d2a5bbeSJim Harris spdk_bit_pool_create(uint32_t num_bits)
3460d2a5bbeSJim Harris {
3470d2a5bbeSJim Harris struct spdk_bit_pool *pool = NULL;
3480d2a5bbeSJim Harris struct spdk_bit_array *array;
3490d2a5bbeSJim Harris
3500d2a5bbeSJim Harris array = spdk_bit_array_create(num_bits);
3510d2a5bbeSJim Harris if (array == NULL) {
3520d2a5bbeSJim Harris return NULL;
3530d2a5bbeSJim Harris }
3540d2a5bbeSJim Harris
3550d2a5bbeSJim Harris pool = calloc(1, sizeof(*pool));
3560d2a5bbeSJim Harris if (pool == NULL) {
3570d2a5bbeSJim Harris spdk_bit_array_free(&array);
3580d2a5bbeSJim Harris return NULL;
3590d2a5bbeSJim Harris }
3600d2a5bbeSJim Harris
3610d2a5bbeSJim Harris pool->array = array;
3620d2a5bbeSJim Harris pool->lowest_free_bit = 0;
3630d2a5bbeSJim Harris pool->free_count = num_bits;
3640d2a5bbeSJim Harris
3650d2a5bbeSJim Harris return pool;
3660d2a5bbeSJim Harris }
3670d2a5bbeSJim Harris
3680d2a5bbeSJim Harris struct spdk_bit_pool *
spdk_bit_pool_create_from_array(struct spdk_bit_array * array)3690d2a5bbeSJim Harris spdk_bit_pool_create_from_array(struct spdk_bit_array *array)
3700d2a5bbeSJim Harris {
3710d2a5bbeSJim Harris struct spdk_bit_pool *pool = NULL;
3720d2a5bbeSJim Harris
3730d2a5bbeSJim Harris pool = calloc(1, sizeof(*pool));
3740d2a5bbeSJim Harris if (pool == NULL) {
3750d2a5bbeSJim Harris return NULL;
3760d2a5bbeSJim Harris }
3770d2a5bbeSJim Harris
3780d2a5bbeSJim Harris pool->array = array;
3790d2a5bbeSJim Harris pool->lowest_free_bit = spdk_bit_array_find_first_clear(array, 0);
3800d2a5bbeSJim Harris pool->free_count = spdk_bit_array_count_clear(array);
3810d2a5bbeSJim Harris
3820d2a5bbeSJim Harris return pool;
3830d2a5bbeSJim Harris }
3840d2a5bbeSJim Harris
3850d2a5bbeSJim Harris void
spdk_bit_pool_free(struct spdk_bit_pool ** ppool)3860d2a5bbeSJim Harris spdk_bit_pool_free(struct spdk_bit_pool **ppool)
3870d2a5bbeSJim Harris {
3880d2a5bbeSJim Harris struct spdk_bit_pool *pool;
3890d2a5bbeSJim Harris
3900d2a5bbeSJim Harris if (!ppool) {
3910d2a5bbeSJim Harris return;
3920d2a5bbeSJim Harris }
3930d2a5bbeSJim Harris
3940d2a5bbeSJim Harris pool = *ppool;
3950d2a5bbeSJim Harris *ppool = NULL;
3960d2a5bbeSJim Harris if (pool != NULL) {
3970d2a5bbeSJim Harris spdk_bit_array_free(&pool->array);
3980d2a5bbeSJim Harris free(pool);
3990d2a5bbeSJim Harris }
4000d2a5bbeSJim Harris }
4010d2a5bbeSJim Harris
4020d2a5bbeSJim Harris int
spdk_bit_pool_resize(struct spdk_bit_pool ** ppool,uint32_t num_bits)4030d2a5bbeSJim Harris spdk_bit_pool_resize(struct spdk_bit_pool **ppool, uint32_t num_bits)
4040d2a5bbeSJim Harris {
4050d2a5bbeSJim Harris struct spdk_bit_pool *pool;
4060d2a5bbeSJim Harris int rc;
4070d2a5bbeSJim Harris
4080d2a5bbeSJim Harris assert(ppool != NULL);
4090d2a5bbeSJim Harris
4100d2a5bbeSJim Harris pool = *ppool;
4110d2a5bbeSJim Harris rc = spdk_bit_array_resize(&pool->array, num_bits);
4120d2a5bbeSJim Harris if (rc) {
4130d2a5bbeSJim Harris return rc;
4140d2a5bbeSJim Harris }
4150d2a5bbeSJim Harris
4160d2a5bbeSJim Harris pool->lowest_free_bit = spdk_bit_array_find_first_clear(pool->array, 0);
4170d2a5bbeSJim Harris pool->free_count = spdk_bit_array_count_clear(pool->array);
4180d2a5bbeSJim Harris
4190d2a5bbeSJim Harris return 0;
4200d2a5bbeSJim Harris }
4210d2a5bbeSJim Harris
4220d2a5bbeSJim Harris uint32_t
spdk_bit_pool_capacity(const struct spdk_bit_pool * pool)4230d2a5bbeSJim Harris spdk_bit_pool_capacity(const struct spdk_bit_pool *pool)
4240d2a5bbeSJim Harris {
4250d2a5bbeSJim Harris return spdk_bit_array_capacity(pool->array);
4260d2a5bbeSJim Harris }
4270d2a5bbeSJim Harris
4280d2a5bbeSJim Harris bool
spdk_bit_pool_is_allocated(const struct spdk_bit_pool * pool,uint32_t bit_index)4290d2a5bbeSJim Harris spdk_bit_pool_is_allocated(const struct spdk_bit_pool *pool, uint32_t bit_index)
4300d2a5bbeSJim Harris {
4310d2a5bbeSJim Harris return spdk_bit_array_get(pool->array, bit_index);
4320d2a5bbeSJim Harris }
4330d2a5bbeSJim Harris
4340d2a5bbeSJim Harris uint32_t
spdk_bit_pool_allocate_bit(struct spdk_bit_pool * pool)4350d2a5bbeSJim Harris spdk_bit_pool_allocate_bit(struct spdk_bit_pool *pool)
4360d2a5bbeSJim Harris {
4370d2a5bbeSJim Harris uint32_t bit_index = pool->lowest_free_bit;
4380d2a5bbeSJim Harris
4390d2a5bbeSJim Harris if (bit_index == UINT32_MAX) {
4400d2a5bbeSJim Harris return UINT32_MAX;
4410d2a5bbeSJim Harris }
4420d2a5bbeSJim Harris
4430d2a5bbeSJim Harris spdk_bit_array_set(pool->array, bit_index);
4440d2a5bbeSJim Harris pool->lowest_free_bit = spdk_bit_array_find_first_clear(pool->array, bit_index);
4450d2a5bbeSJim Harris pool->free_count--;
4460d2a5bbeSJim Harris return bit_index;
4470d2a5bbeSJim Harris }
4480d2a5bbeSJim Harris
4490d2a5bbeSJim Harris void
spdk_bit_pool_free_bit(struct spdk_bit_pool * pool,uint32_t bit_index)4500d2a5bbeSJim Harris spdk_bit_pool_free_bit(struct spdk_bit_pool *pool, uint32_t bit_index)
4510d2a5bbeSJim Harris {
4520d2a5bbeSJim Harris assert(spdk_bit_array_get(pool->array, bit_index) == true);
4530d2a5bbeSJim Harris
4540d2a5bbeSJim Harris spdk_bit_array_clear(pool->array, bit_index);
4550d2a5bbeSJim Harris if (pool->lowest_free_bit > bit_index) {
4560d2a5bbeSJim Harris pool->lowest_free_bit = bit_index;
4570d2a5bbeSJim Harris }
4580d2a5bbeSJim Harris pool->free_count++;
4590d2a5bbeSJim Harris }
4600d2a5bbeSJim Harris
4610d2a5bbeSJim Harris uint32_t
spdk_bit_pool_count_allocated(const struct spdk_bit_pool * pool)4620d2a5bbeSJim Harris spdk_bit_pool_count_allocated(const struct spdk_bit_pool *pool)
4630d2a5bbeSJim Harris {
4640d2a5bbeSJim Harris return spdk_bit_array_capacity(pool->array) - pool->free_count;
4650d2a5bbeSJim Harris }
4660d2a5bbeSJim Harris
4670d2a5bbeSJim Harris uint32_t
spdk_bit_pool_count_free(const struct spdk_bit_pool * pool)4680d2a5bbeSJim Harris spdk_bit_pool_count_free(const struct spdk_bit_pool *pool)
4690d2a5bbeSJim Harris {
4700d2a5bbeSJim Harris return pool->free_count;
4710d2a5bbeSJim Harris }
4720d2a5bbeSJim Harris
4730d2a5bbeSJim Harris void
spdk_bit_pool_store_mask(const struct spdk_bit_pool * pool,void * mask)4740d2a5bbeSJim Harris spdk_bit_pool_store_mask(const struct spdk_bit_pool *pool, void *mask)
4750d2a5bbeSJim Harris {
4760d2a5bbeSJim Harris spdk_bit_array_store_mask(pool->array, mask);
4770d2a5bbeSJim Harris }
4780d2a5bbeSJim Harris
4790d2a5bbeSJim Harris void
spdk_bit_pool_load_mask(struct spdk_bit_pool * pool,const void * mask)4800d2a5bbeSJim Harris spdk_bit_pool_load_mask(struct spdk_bit_pool *pool, const void *mask)
4810d2a5bbeSJim Harris {
4820d2a5bbeSJim Harris spdk_bit_array_load_mask(pool->array, mask);
4830d2a5bbeSJim Harris pool->lowest_free_bit = spdk_bit_array_find_first_clear(pool->array, 0);
4840d2a5bbeSJim Harris pool->free_count = spdk_bit_array_count_clear(pool->array);
4850d2a5bbeSJim Harris }
4860d2a5bbeSJim Harris
4870d2a5bbeSJim Harris void
spdk_bit_pool_free_all_bits(struct spdk_bit_pool * pool)4880d2a5bbeSJim Harris spdk_bit_pool_free_all_bits(struct spdk_bit_pool *pool)
4890d2a5bbeSJim Harris {
4900d2a5bbeSJim Harris spdk_bit_array_clear_mask(pool->array);
4910d2a5bbeSJim Harris pool->lowest_free_bit = 0;
4920d2a5bbeSJim Harris pool->free_count = spdk_bit_array_capacity(pool->array);
4930d2a5bbeSJim Harris }
494