18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * tools/testing/selftests/kvm/include/sparsebit.h 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2018, Google LLC. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Header file that describes API to the sparsebit library. 88c2ecf20Sopenharmony_ci * This library provides a memory efficient means of storing 98c2ecf20Sopenharmony_ci * the settings of bits indexed via a uint64_t. Memory usage 108c2ecf20Sopenharmony_ci * is reasonable, significantly less than (2^64 / 8) bytes, as 118c2ecf20Sopenharmony_ci * long as bits that are mostly set or mostly cleared are close 128c2ecf20Sopenharmony_ci * to each other. This library is efficient in memory usage 138c2ecf20Sopenharmony_ci * even in the case where most bits are set. 148c2ecf20Sopenharmony_ci */ 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#ifndef SELFTEST_KVM_SPARSEBIT_H 178c2ecf20Sopenharmony_ci#define SELFTEST_KVM_SPARSEBIT_H 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include <stdbool.h> 208c2ecf20Sopenharmony_ci#include <stdint.h> 218c2ecf20Sopenharmony_ci#include <stdio.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#ifdef __cplusplus 248c2ecf20Sopenharmony_ciextern "C" { 258c2ecf20Sopenharmony_ci#endif 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_cistruct sparsebit; 288c2ecf20Sopenharmony_citypedef uint64_t sparsebit_idx_t; 298c2ecf20Sopenharmony_citypedef uint64_t sparsebit_num_t; 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistruct sparsebit *sparsebit_alloc(void); 328c2ecf20Sopenharmony_civoid sparsebit_free(struct sparsebit **sbitp); 338c2ecf20Sopenharmony_civoid sparsebit_copy(struct sparsebit *dstp, struct sparsebit *src); 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cibool sparsebit_is_set(struct sparsebit *sbit, sparsebit_idx_t idx); 368c2ecf20Sopenharmony_cibool sparsebit_is_set_num(struct sparsebit *sbit, 378c2ecf20Sopenharmony_ci sparsebit_idx_t idx, sparsebit_num_t num); 388c2ecf20Sopenharmony_cibool sparsebit_is_clear(struct sparsebit *sbit, sparsebit_idx_t idx); 398c2ecf20Sopenharmony_cibool sparsebit_is_clear_num(struct sparsebit *sbit, 408c2ecf20Sopenharmony_ci sparsebit_idx_t idx, sparsebit_num_t num); 418c2ecf20Sopenharmony_cisparsebit_num_t sparsebit_num_set(struct sparsebit *sbit); 428c2ecf20Sopenharmony_cibool sparsebit_any_set(struct sparsebit *sbit); 438c2ecf20Sopenharmony_cibool sparsebit_any_clear(struct sparsebit *sbit); 448c2ecf20Sopenharmony_cibool sparsebit_all_set(struct sparsebit *sbit); 458c2ecf20Sopenharmony_cibool sparsebit_all_clear(struct sparsebit *sbit); 468c2ecf20Sopenharmony_cisparsebit_idx_t sparsebit_first_set(struct sparsebit *sbit); 478c2ecf20Sopenharmony_cisparsebit_idx_t sparsebit_first_clear(struct sparsebit *sbit); 488c2ecf20Sopenharmony_cisparsebit_idx_t sparsebit_next_set(struct sparsebit *sbit, sparsebit_idx_t prev); 498c2ecf20Sopenharmony_cisparsebit_idx_t sparsebit_next_clear(struct sparsebit *sbit, sparsebit_idx_t prev); 508c2ecf20Sopenharmony_cisparsebit_idx_t sparsebit_next_set_num(struct sparsebit *sbit, 518c2ecf20Sopenharmony_ci sparsebit_idx_t start, sparsebit_num_t num); 528c2ecf20Sopenharmony_cisparsebit_idx_t sparsebit_next_clear_num(struct sparsebit *sbit, 538c2ecf20Sopenharmony_ci sparsebit_idx_t start, sparsebit_num_t num); 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_civoid sparsebit_set(struct sparsebit *sbitp, sparsebit_idx_t idx); 568c2ecf20Sopenharmony_civoid sparsebit_set_num(struct sparsebit *sbitp, sparsebit_idx_t start, 578c2ecf20Sopenharmony_ci sparsebit_num_t num); 588c2ecf20Sopenharmony_civoid sparsebit_set_all(struct sparsebit *sbitp); 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_civoid sparsebit_clear(struct sparsebit *sbitp, sparsebit_idx_t idx); 618c2ecf20Sopenharmony_civoid sparsebit_clear_num(struct sparsebit *sbitp, 628c2ecf20Sopenharmony_ci sparsebit_idx_t start, sparsebit_num_t num); 638c2ecf20Sopenharmony_civoid sparsebit_clear_all(struct sparsebit *sbitp); 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_civoid sparsebit_dump(FILE *stream, struct sparsebit *sbit, 668c2ecf20Sopenharmony_ci unsigned int indent); 678c2ecf20Sopenharmony_civoid sparsebit_validate_internal(struct sparsebit *sbit); 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci#ifdef __cplusplus 708c2ecf20Sopenharmony_ci} 718c2ecf20Sopenharmony_ci#endif 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci#endif /* SELFTEST_KVM_SPARSEBIT_H */ 74