Path: blob/master/tools/testing/selftests/kvm/include/sparsebit.h
38235 views
/* SPDX-License-Identifier: GPL-2.0-only */1/*2* tools/testing/selftests/kvm/include/sparsebit.h3*4* Copyright (C) 2018, Google LLC.5*6* Header file that describes API to the sparsebit library.7* This library provides a memory efficient means of storing8* the settings of bits indexed via a uint64_t. Memory usage9* is reasonable, significantly less than (2^64 / 8) bytes, as10* long as bits that are mostly set or mostly cleared are close11* to each other. This library is efficient in memory usage12* even in the case where most bits are set.13*/1415#ifndef SELFTEST_KVM_SPARSEBIT_H16#define SELFTEST_KVM_SPARSEBIT_H1718#include <stdbool.h>19#include <stdint.h>20#include <stdio.h>2122#ifdef __cplusplus23extern "C" {24#endif2526struct sparsebit;27typedef uint64_t sparsebit_idx_t;28typedef uint64_t sparsebit_num_t;2930struct sparsebit *sparsebit_alloc(void);31void sparsebit_free(struct sparsebit **sbitp);32void sparsebit_copy(struct sparsebit *dstp, const struct sparsebit *src);3334bool sparsebit_is_set(const struct sparsebit *sbit, sparsebit_idx_t idx);35bool sparsebit_is_set_num(const struct sparsebit *sbit,36sparsebit_idx_t idx, sparsebit_num_t num);37bool sparsebit_is_clear(const struct sparsebit *sbit, sparsebit_idx_t idx);38bool sparsebit_is_clear_num(const struct sparsebit *sbit,39sparsebit_idx_t idx, sparsebit_num_t num);40sparsebit_num_t sparsebit_num_set(const struct sparsebit *sbit);41bool sparsebit_any_set(const struct sparsebit *sbit);42bool sparsebit_any_clear(const struct sparsebit *sbit);43bool sparsebit_all_set(const struct sparsebit *sbit);44bool sparsebit_all_clear(const struct sparsebit *sbit);45sparsebit_idx_t sparsebit_first_set(const struct sparsebit *sbit);46sparsebit_idx_t sparsebit_first_clear(const struct sparsebit *sbit);47sparsebit_idx_t sparsebit_next_set(const struct sparsebit *sbit, sparsebit_idx_t prev);48sparsebit_idx_t sparsebit_next_clear(const struct sparsebit *sbit, sparsebit_idx_t prev);49sparsebit_idx_t sparsebit_next_set_num(const struct sparsebit *sbit,50sparsebit_idx_t start, sparsebit_num_t num);51sparsebit_idx_t sparsebit_next_clear_num(const struct sparsebit *sbit,52sparsebit_idx_t start, sparsebit_num_t num);5354void sparsebit_set(struct sparsebit *sbitp, sparsebit_idx_t idx);55void sparsebit_set_num(struct sparsebit *sbitp, sparsebit_idx_t start,56sparsebit_num_t num);57void sparsebit_set_all(struct sparsebit *sbitp);5859void sparsebit_clear(struct sparsebit *sbitp, sparsebit_idx_t idx);60void sparsebit_clear_num(struct sparsebit *sbitp,61sparsebit_idx_t start, sparsebit_num_t num);62void sparsebit_clear_all(struct sparsebit *sbitp);6364void sparsebit_dump(FILE *stream, const struct sparsebit *sbit,65unsigned int indent);66void sparsebit_validate_internal(const struct sparsebit *sbit);6768/*69* Iterate over an inclusive ranges within sparsebit @s. In each iteration,70* @range_begin and @range_end will take the beginning and end of the set71* range, which are of type sparsebit_idx_t.72*73* For example, if the range [3, 7] (inclusive) is set, within the74* iteration,@range_begin will take the value 3 and @range_end will take75* the value 7.76*77* Ensure that there is at least one bit set before using this macro with78* sparsebit_any_set(), because sparsebit_first_set() will abort if none79* are set.80*/81#define sparsebit_for_each_set_range(s, range_begin, range_end) \82for (range_begin = sparsebit_first_set(s), \83range_end = sparsebit_next_clear(s, range_begin) - 1; \84range_begin && range_end; \85range_begin = sparsebit_next_set(s, range_end), \86range_end = sparsebit_next_clear(s, range_begin) - 1)8788#ifdef __cplusplus89}90#endif9192#endif /* SELFTEST_KVM_SPARSEBIT_H */939495