Commit 8eb09a29 authored by Jinlong Chen's avatar Jinlong Chen Committed by Tomasz Zawadzki
Browse files

lib/util: add spdk_bit_pool_set_bit_allocated



Add spdk_bit_pool_set_bit_allocated to set specified bit as allocated in
a bit pool.

This could be useful when we need to reconstruct the state of a bit pool
from some prior information.

Change-Id: Ia56c3e73b798c31c0c81b9ffdff4bf3d977238d2
Signed-off-by: default avatarJinlong Chen <chenjinlong.cjl@alibaba-inc.com>
Reviewed-on: https://review.spdk.io/c/spdk/spdk/+/26233


Community-CI: Mellanox Build Bot
Reviewed-by: default avatarChangpeng Liu <changpeliu@tencent.com>
Reviewed-by: default avatarJim Harris <jim.harris@nvidia.com>
Tested-by: default avatarSPDK Automated Test System <spdkbot@gmail.com>
parent e4aa9f24
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -103,6 +103,17 @@ bool spdk_bit_pool_is_allocated(const struct spdk_bit_pool *pool, uint32_t bit_i
 */
uint32_t spdk_bit_pool_allocate_bit(struct spdk_bit_pool *pool);

/**
 * Set the specified bit as allocated in the bit pool.
 *
 * \param pool Bit pool to set the bit in
 * \param bit_index Index of the bit to set
 *
 * \return 0 if the bit is set successfully, -EBUSY if the bit was already set,
 * -EINVAL if bit_index is out of range.
 */
int spdk_bit_pool_set_bit_allocated(struct spdk_bit_pool *pool, uint32_t bit_index);

/**
 * Free a bit back to the bit pool.
 *
+22 −0
Original line number Diff line number Diff line
@@ -446,6 +446,28 @@ spdk_bit_pool_allocate_bit(struct spdk_bit_pool *pool)
	return bit_index;
}

int
spdk_bit_pool_set_bit_allocated(struct spdk_bit_pool *pool, uint32_t bit_index)
{
	int rc;

	if (spdk_bit_array_get(pool->array, bit_index)) {
		return -EBUSY;
	}

	rc = spdk_bit_array_set(pool->array, bit_index);
	if (rc != 0) {
		return rc;
	}

	if (pool->lowest_free_bit == bit_index) {
		pool->lowest_free_bit = spdk_bit_array_find_first_clear(pool->array, bit_index);
	}
	pool->free_count--;

	return 0;
}

void
spdk_bit_pool_free_bit(struct spdk_bit_pool *pool, uint32_t bit_index)
{
+1 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@
	spdk_bit_pool_resize;
	spdk_bit_pool_is_allocated;
	spdk_bit_pool_allocate_bit;
	spdk_bit_pool_set_bit_allocated;
	spdk_bit_pool_free_bit;
	spdk_bit_pool_count_allocated;
	spdk_bit_pool_count_free;