00001 // bitmap.h 00002 // Data structures defining a bitmap -- an array of bits each of which 00003 // can be either on or off. 00004 // 00005 // Represented as an array of unsigned integers, on which we do 00006 // modulo arithmetic to find the bit we are interested in. 00007 // 00008 // The bitmap can be parameterized with with the number of bits being 00009 // managed. 00010 // 00011 // Copyright (c) 1992-1996 The Regents of the University of California. 00012 // All rights reserved. See copyright.h for copyright notice and limitation 00013 // of liability and disclaimer of warranty provisions. 00014 00015 #ifndef BITMAP_H 00016 #define BITMAP_H 00017 00018 #include "copyright.h" 00019 #include "utility.h" 00020 00021 // Definitions helpful for representing a bitmap as an array of integers 00022 const int BitsInByte = 8; 00023 const int BitsInWord = sizeof(unsigned int) * BitsInByte; 00024 00025 // The following class defines a "bitmap" -- an array of bits, 00026 // each of which can be independently set, cleared, and tested. 00027 // 00028 // Most useful for managing the allocation of the elements of an array -- 00029 // for instance, disk sectors, or main memory pages. 00030 // Each bit represents whether the corresponding sector or page is 00031 // in use or free. 00032 00033 class Bitmap { 00034 public: 00035 Bitmap(int numItems); // Initialize a bitmap, with "numItems" bits 00036 // initially, all bits are cleared. 00037 ~Bitmap(); // De-allocate bitmap 00038 00039 void Mark(int which); // Set the "nth" bit 00040 void Clear(int which); // Clear the "nth" bit 00041 bool Test(int which) const; // Is the "nth" bit set? 00042 int FindAndSet(); // Return the # of a clear bit, and as a side 00043 // effect, set the bit. 00044 // If no bits are clear, return -1. 00045 int NumClear() const; // Return the number of clear bits 00046 00047 void Print() const; // Print contents of bitmap 00048 void SelfTest(); // Test whether bitmap is working 00049 00050 protected: 00051 int numBits; // number of bits in the bitmap 00052 int numWords; // number of words of bitmap storage 00053 // (rounded up if numBits is not a 00054 // multiple of the number of bits in 00055 // a word) 00056 unsigned int *map; // bit storage 00057 }; 00058 00059 #endif // BITMAP_H