(original) (raw)

#ifndef BMAP_H #include #include #include #include #include /** * This structure is the in-memory representation of a bitmap. * We read and write the bitmap into the "bitmap" memory buffer * and we have helper functions to query and modify the bitmap state * e.g., set_bit, get_bit, unset_bit, etc. */ struct bitmap { // the number of **bytes** in the bitmap (not bits!) // Note: the bitmap must be padded to a multiple of 8 bytes; // This means that up to 63 bits in the last uint64_t may be // "unused" and serve only as "padding" uint64_t n_bytes; // Due to padding, the number of valid bits may be less // than the number of bits that the bitmap *COULD* represent. // n_valid bits is the number of meaningful bits uint64_t n_valid_bits; // The actual bitmap. This memory buffer must be malloc'ed and freed // by your program during allocate_bitmap(uint64_t num_bits) and // free_bitmap(struct bitmap *bmap) unsigned char *bits; }; struct bitmap *allocate_bitmap(uint64_t num_bits); void free_bitmap(struct bitmap *bmap); char get_bit(struct bitmap *map, uint64_t i); void set_bit(struct bitmap *map, uint64_t i); void unset_bit(struct bitmap *map, uint64_t i); uint64_t first_unset_bit(struct bitmap *map); /** Debugging utility functions */ void dump_bitmap(struct bitmap *map); // this may be useful for debugging! #endif