// useBitmap.cpp : Implimentation of the CUseBitmap class // ///////////////////////////////////////////////////////////////////////////// #include "std.h" #include "UseBitmap.h" ///////////////////////////////////////////////////////////////////////// // Find a free bit // // Parameters; // set - true = Free Bit is automatically set if found // false = Bit is not automatically set if found // // Returns: // Number of a free bit (1 to NumOfBits()). A value of // zero is returned if no free bit can be found // UINT32 CUseBitmapGeneral::FindFreeBitGen(const bool set) { // Returned bit number UINT32 bit_number = 0; // Loop control bool stop = false; // Word being searched UINT32 word = 0; // Calculate how many words are defined in bitmap and then search // a word at a time UINT32 max_word = WordsRequired(length) - 1; // Search for a non-full word while (!stop && (count < length)) { if (bitmap[word] != ~0) { // Free bit found. This could be bogus if the last word is padded // with blank bits // Find free bit in word UINT32* p_word = &bitmap[word]; // Pointer to word with free bit // Max number of bit defined in this word. This is somewhat less than // 32 bits if the free bit resides in the last word and the number of // bits defined in the entire bitmask does not reside on a 32 bit // boundry UINT32 max_bit = (word < max_word) ? 31 : (length-1) % 32; UINT32 bit = 0; // Bit number being examined UINT32 bit_mask = 1; // Mask for examining bit do { if (!((*p_word) & bit_mask)) { // Free bit found stop = true; // If required, set bit (and keep 'used' count up // to date if (set) { *p_word |= bit_mask; used++; } // Calculate true bit number to be returned // (Based to 1) bit_number = bit + (word << 5) + 1; } else { // Check next bit if (bit >= max_bit) { // Free bit was bogus stop = true; } else { // Step on to next bit bit++; bit_mask <<= 1; } } } while (!stop); } else { if (++word > max_word) stop = true; } } return bit_number; } // EOF