// UseBitmap.h : Interface of the CUseBitmap class // // 'In use' bitmap structure // ///////////////////////////////////////////////////////////////////////////// #ifndef _USE_BITMAP_H #define _USE_BITMAP_H ///////////////////////////////////////////////////////////////////////////// // General Use Bitmap handler class CUseBitmapGeneral { private: UINT32 length; // In bits UINT32 used; UINT32* bitmap; protected: ///////////////////////////////////////////////////////////////////////// // Calculate number of words needed to hold specified number of bits, // rounded to the next 32 bit boundry // // Parameters: // length - Length of bitmap (bits) // // Returns: // Number of 32 bit words required // static UINT32 WordsRequired(const UINT32 length) { return (length >> 5) + 1; } ///////////////////////////////////////////////////////////////////////// // Calculate number of bytes needed to hold specified number of bits, // rounded to the next 32 bit boundry // // Parameters: // length - Length of bitmap (bits) // // Returns: // Number of bytes required // static UINT32 BytesRequired(const UINT32 length) { return ((length >> 5) + 1) << 2; } public: CUseBitmapGeneral(void) { length = 0; used = 0; bitmap = NULL; } ~CUseBitmapGeneral(void) { delete[] bitmap; } ///////////////////////////////////////////////////////////////////////// // Initialise - Sets all bits to false // // Parameters; // None // // Returns: // None. void Initialise(void) { UINT32 words = WordsRequired(length); for (UINT32 x = 0; x < words; x++) { bitmap[x] = 0; } used = 0; } ///////////////////////////////////////////////////////////////////////// // AllocateBitmap - Allocate a bitmap and initialise // // Parameters; // lng - Length of bitmap (bits) // // Returns: // success / Failure status - true = failed. false = success bool AllocateBitmap(const UINT32 lng) { bool fail = true; UINT32 words = WordsRequired(lng); length = lng; bitmap = new(UINT32[words]); if (bitmap) { Initialise(); fail = false; } return fail; } ///////////////////////////////////////////////////////////////////////// // Find free bit UINT32 FindFreeBitGen(const bool set); ///////////////////////////////////////////////////////////////////////// // Calculate word and bitmask to apply for extracting/examining specified // bit number // // Parameters; // bit - Number of bit to find (1 to NumOfBits()) // word - Ref. to word number (zero-based) - Updated by this function // bit_mask - Ref. to bitmask to apply to word // // Returns: // None. // void FindBit(UINT32 bit, UINT32& word, UINT32& bit_mask) { // Zero-base specified bit bit--; // Find zero-based word to check word = bit >> 5; // Create mask for bit within word bit_mask = 1 << (bit & 0x001f); } ///////////////////////////////////////////////////////////////////////// // Reset a bit // // Warning : This function assumes that this object has already been // mapped to a valid memory area. // // Parameters; // bit - Number of bit to reset (1 to NumOfBits()) // // Returns: // None. // void ResetBit(UINT32 bit) { // Word number and mask to apply to reset bit status UINT32 word; UINT32 mask; FindBit(bit, word, mask); UINT32 old_state = bitmap[word] & mask; // Keep used count up to date if (old_state) { used--; bitmap[word] &= (~mask); } } }; // End of CUseBitmapGeneral ///////////////////////////////////////////////////////////////////////////// #endif /////////////////////////////////////////////////////////////////////////////