u_bitmask.h revision 4a49301e
1/************************************************************************** 2 * 3 * Copyright 2009 VMware, Inc. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28/** 29 * @file 30 * Generic bitmask. 31 * 32 * @author Jose Fonseca <jfonseca@vmware.com> 33 */ 34 35#ifndef U_HANDLE_BITMASK_H_ 36#define U_HANDLE_BITMASK_H_ 37 38 39#ifdef __cplusplus 40extern "C" { 41#endif 42 43 44#define UTIL_BITMASK_INVALID_INDEX (~0U) 45 46 47/** 48 * Abstract data type to represent arbitrary set of bits. 49 */ 50struct util_bitmask; 51 52 53struct util_bitmask * 54util_bitmask_create(void); 55 56 57/** 58 * Search a cleared bit and set it. 59 * 60 * It searches for the first cleared bit. 61 * 62 * Returns the bit index on success, or UTIL_BITMASK_INVALID_INDEX on out of 63 * memory growing the bitmask. 64 */ 65unsigned 66util_bitmask_add(struct util_bitmask *bm); 67 68/** 69 * Set a bit. 70 * 71 * Returns the input index on success, or UTIL_BITMASK_INVALID_INDEX on out of 72 * memory growing the bitmask. 73 */ 74unsigned 75util_bitmask_set(struct util_bitmask *bm, 76 unsigned index); 77 78void 79util_bitmask_clear(struct util_bitmask *bm, 80 unsigned index); 81 82boolean 83util_bitmask_get(struct util_bitmask *bm, 84 unsigned index); 85 86 87void 88util_bitmask_destroy(struct util_bitmask *bm); 89 90 91/** 92 * Search for the first set bit. 93 * 94 * Returns UTIL_BITMASK_INVALID_INDEX if a set bit cannot be found. 95 */ 96unsigned 97util_bitmask_get_first_index(struct util_bitmask *bm); 98 99 100/** 101 * Search for the first set bit, starting from the giving index. 102 * 103 * Returns UTIL_BITMASK_INVALID_INDEX if a set bit cannot be found. 104 */ 105unsigned 106util_bitmask_get_next_index(struct util_bitmask *bm, 107 unsigned index); 108 109 110#ifdef __cplusplus 111} 112#endif 113 114#endif /* U_HANDLE_BITMASK_H_ */ 115