1*47dd1d1bSDag-Erling Smørgrav /* $OpenBSD: bitmap.h,v 1.2 2017/10/20 01:56:39 djm Exp $ */ 2bc5531deSDag-Erling Smørgrav /* 3bc5531deSDag-Erling Smørgrav * Copyright (c) 2015 Damien Miller <djm@mindrot.org> 4bc5531deSDag-Erling Smørgrav * 5bc5531deSDag-Erling Smørgrav * Permission to use, copy, modify, and distribute this software for any 6bc5531deSDag-Erling Smørgrav * purpose with or without fee is hereby granted, provided that the above 7bc5531deSDag-Erling Smørgrav * copyright notice and this permission notice appear in all copies. 8bc5531deSDag-Erling Smørgrav * 9bc5531deSDag-Erling Smørgrav * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10bc5531deSDag-Erling Smørgrav * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11bc5531deSDag-Erling Smørgrav * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12bc5531deSDag-Erling Smørgrav * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13bc5531deSDag-Erling Smørgrav * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14bc5531deSDag-Erling Smørgrav * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15bc5531deSDag-Erling Smørgrav * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16bc5531deSDag-Erling Smørgrav */ 17bc5531deSDag-Erling Smørgrav 18bc5531deSDag-Erling Smørgrav #ifndef _BITMAP_H 19bc5531deSDag-Erling Smørgrav #define _BITMAP_H 20bc5531deSDag-Erling Smørgrav 21bc5531deSDag-Erling Smørgrav #include <sys/types.h> 22bc5531deSDag-Erling Smørgrav 23bc5531deSDag-Erling Smørgrav /* Simple bit vector routines */ 24bc5531deSDag-Erling Smørgrav 25bc5531deSDag-Erling Smørgrav struct bitmap; 26bc5531deSDag-Erling Smørgrav 27bc5531deSDag-Erling Smørgrav /* Allocate a new bitmap. Returns NULL on allocation failure. */ 28bc5531deSDag-Erling Smørgrav struct bitmap *bitmap_new(void); 29bc5531deSDag-Erling Smørgrav 30bc5531deSDag-Erling Smørgrav /* Free a bitmap */ 31bc5531deSDag-Erling Smørgrav void bitmap_free(struct bitmap *b); 32bc5531deSDag-Erling Smørgrav 33bc5531deSDag-Erling Smørgrav /* Zero an existing bitmap */ 34bc5531deSDag-Erling Smørgrav void bitmap_zero(struct bitmap *b); 35bc5531deSDag-Erling Smørgrav 36bc5531deSDag-Erling Smørgrav /* Test whether a bit is set in a bitmap. */ 37bc5531deSDag-Erling Smørgrav int bitmap_test_bit(struct bitmap *b, u_int n); 38bc5531deSDag-Erling Smørgrav 39bc5531deSDag-Erling Smørgrav /* Set a bit in a bitmap. Returns 0 on success or -1 on error */ 40bc5531deSDag-Erling Smørgrav int bitmap_set_bit(struct bitmap *b, u_int n); 41bc5531deSDag-Erling Smørgrav 42bc5531deSDag-Erling Smørgrav /* Clear a bit in a bitmap */ 43bc5531deSDag-Erling Smørgrav void bitmap_clear_bit(struct bitmap *b, u_int n); 44bc5531deSDag-Erling Smørgrav 45bc5531deSDag-Erling Smørgrav /* Return the number of bits in a bitmap (i.e. the position of the MSB) */ 46bc5531deSDag-Erling Smørgrav size_t bitmap_nbits(struct bitmap *b); 47bc5531deSDag-Erling Smørgrav 48bc5531deSDag-Erling Smørgrav /* Return the number of bytes needed to represent a bitmap */ 49bc5531deSDag-Erling Smørgrav size_t bitmap_nbytes(struct bitmap *b); 50bc5531deSDag-Erling Smørgrav 51bc5531deSDag-Erling Smørgrav /* Convert a bitmap to a big endian byte string */ 52bc5531deSDag-Erling Smørgrav int bitmap_to_string(struct bitmap *b, void *p, size_t l); 53bc5531deSDag-Erling Smørgrav 54bc5531deSDag-Erling Smørgrav /* Convert a big endian byte string to a bitmap */ 55bc5531deSDag-Erling Smørgrav int bitmap_from_string(struct bitmap *b, const void *p, size_t l); 56bc5531deSDag-Erling Smørgrav 57bc5531deSDag-Erling Smørgrav #endif /* _BITMAP_H */ 58