1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2022 Intel Corporation. 3 * All rights reserved. 4 */ 5 6 #ifndef FTL_BITMAP_H_ 7 #define FTL_BITMAP_H_ 8 9 #include "spdk/stdinc.h" 10 11 struct ftl_bitmap; 12 13 /** 14 * @brief The required alignment for buffer used for bitmap 15 */ 16 extern const size_t ftl_bitmap_buffer_alignment; 17 18 /** 19 * @brief Converts number of bits to bitmap size need to create it 20 * 21 * @param bits Number of bits 22 * 23 * @return Size needed to create bitmap which will hold space for specified number of bits 24 */ 25 uint64_t ftl_bitmap_bits_to_size(uint64_t bits); 26 27 /** 28 * @brief Converts number of bits to blocks 29 * 30 * @param bits Number of bits 31 * 32 * @return Number of blocks needed to create bitmap which will hold space for specified number of bits 33 */ 34 uint64_t ftl_bitmap_bits_to_blocks(uint64_t bits); 35 36 /** 37 * @brief Creates a bitmap object using a preallocated buffer 38 * 39 * @param buf The buffer 40 * @param size Size of the buffer 41 * 42 * @return On success - pointer to the allocated bitmap object, otherwise NULL 43 */ 44 struct ftl_bitmap *ftl_bitmap_create(void *buf, size_t size); 45 46 /** 47 * @brief Destroys the bitmap object 48 * 49 * @param bitmap The bitmap 50 */ 51 void ftl_bitmap_destroy(struct ftl_bitmap *bitmap); 52 53 /** 54 * @brief Gets the value of the specified bit 55 * 56 * @param bitmap The bitmap 57 * @param bit Index of the bit 58 * 59 * @return True if bit is set, otherwise false 60 */ 61 bool ftl_bitmap_get(const struct ftl_bitmap *bitmap, uint64_t bit); 62 63 /** 64 * @brief Sets the specified bit 65 * 66 * @param bitmap The bitmap 67 * @param bit Index of the bit 68 */ 69 void ftl_bitmap_set(struct ftl_bitmap *bitmap, uint64_t bit); 70 71 /** 72 * @brief Clears the specified bit 73 * 74 * @param bitmap The bitmap 75 * @param bit Index of the bit 76 */ 77 void ftl_bitmap_clear(struct ftl_bitmap *bitmap, uint64_t bit); 78 79 /** 80 * @brief Finds the first set bit 81 * 82 * @param bitmap The bitmap 83 * @param start_bit Index of the bit from which to begin searching 84 * @param end_bit Index of the bit up to which to search 85 * 86 * @return Index of the first set bit or UINT64_MAX if none found 87 */ 88 uint64_t ftl_bitmap_find_first_set(struct ftl_bitmap *bitmap, uint64_t start_bit, uint64_t end_bit); 89 90 /** 91 * @brief Finds the first clear bit 92 * 93 * @param bitmap The bitmap 94 * @param start_bit Index of the bit from which to begin searching 95 * @param end_bit Index of the bit up to which to search 96 * 97 * @return Index of the first clear bit or UINT64_MAX if none found 98 */ 99 uint64_t ftl_bitmap_find_first_clear(struct ftl_bitmap *bitmap, uint64_t start_bit, 100 uint64_t end_bit); 101 102 /** 103 * @brief Iterates over and counts set bits 104 * 105 * @param bitmap The bitmap 106 * 107 * @return Count of sets bits 108 */ 109 uint64_t ftl_bitmap_count_set(struct ftl_bitmap *bitmap); 110 111 #endif /* FTL_BITMAP_H_ */ 112