[freenos] [freenos commit] r177 - Added functions markRange(), setMap(), getUsed(), getFree() to BitMap.

  • From: codesite-noreply@xxxxxxxxxx
  • To: freenos@xxxxxxxxxxxxx
  • Date: Wed, 01 Jul 2009 18:38:16 +0000

Author: nieklinnenbank
Date: Wed Jul  1 10:57:18 2009
New Revision: 177

Modified:
   trunk/include/BitMap.h

Log:
Added functions markRange(), setMap(), getUsed(), getFree() to BitMap.
Additionally, a miscalculation in BitMap::isMarked() was corrected.


Modified: trunk/include/BitMap.h
==============================================================================
--- trunk/include/BitMap.h      (original)
+++ trunk/include/BitMap.h      Wed Jul  1 10:57:18 2009
@@ -33,9 +33,14 @@
         * Class constructor.
         * @param cnt Number of bits.
         */
-       BitMap(Size cnt) : count(cnt), free(cnt)
+       BitMap(Size cnt, u8 *newMap = ZERO)
+           : count(cnt), free(cnt)
        {
-           map = new u8[(cnt / 8) + 1];
+           if (newMap)
+               map = newMap;
+           else
+               map = new u8[(cnt / 8) + 1];
+       
            memset(map, 0, (cnt / 8) + 1);
        }

@@ -58,6 +63,19 @@
         }
        
        /**
+        * Mark a range of bits inside the map.
+        * @param from Bit to start with.
+        * @param to End bit (inclusive).
+        */
+       void markRange(Size from, Size to)
+       {
+           for (Size i = from; i <= to; i++)
+           {
+               mark(i);
+           }
+       }
+       
+       /**
         * Marks the next free bit used.
         * @return Bit number on success and -1 otherwise.
         */
@@ -124,7 +142,7 @@
            assert(bit < count);
            assertRead(map);
        
-           return map[bit / 8] & (bit % 8);
+           return map[bit / 8] & (1 << (bit % 8));
        }

        /**
@@ -134,6 +152,38 @@
        u8 * getMap() const
        {
            return map;
+       }
+
+       /**
+        * Use the given pointer as the bitmap buffer.
+        * @param newMap New bitmap pointer.
+        */
+       void setMap(u8 *newMap)
+       {
+           if (map)
+               delete map;
+               
+           /* Reassign to the new map, and clear it. */
+           map = newMap;
+           memset(map, 0, count);
+       }
+       
+       /**
+        * Get the number of bits used in the map.
+        * @return Number of used bits.
+        */
+       Size getUsed() const
+       {
+           return (count - free);
+       }
+       
+       /**
+        * Get the number of bits available in the map.
+        * @return Number of free bits.
+        */
+       Size getFree() const
+       {
+           return free;
        }

     private:

Other related posts:

  • » [freenos] [freenos commit] r177 - Added functions markRange(), setMap(), getUsed(), getFree() to BitMap. - codesite-noreply