diff --git a/Bitmap.cpp b/Bitmap.cpp index 3605d9e..1e3d246 100644 --- a/Bitmap.cpp +++ b/Bitmap.cpp @@ -1,9 +1,12 @@ -#include "Bitmap.h" #include +#include "Bitmap.h" +#include "BlockDevice.h" #include "auto.h" +using namespace ProFUSE; + // returns # of 1-bits set (0-8) inline static unsigned popCount(uint8_t x) { @@ -33,7 +36,7 @@ Bitmap::Bitmap(unsigned blocks) unsigned bitmapSize = _bitmapBlocks * 512; unsigned blockSize = blocks / 8; - auto_array bitmap(new uint8_t[bitmapSize]); + auto_array bitmap(new uint8_t[bitmapSize]); // mark overflow in use, everything else free. @@ -61,11 +64,11 @@ Bitmap::Bitmap(BlockDevice *device, unsigned keyPointer, unsigned blocks) unsigned bitmapSize = _bitmapBlocks * 512; unsigned blockSize = blocks / 8; - auto_array bitmap(new uint8_t[bitmapSize]); + auto_array bitmap(new uint8_t[bitmapSize]); for (unsigned i = 0; i < blockSize; ++i) { - device->readBlock(keyPointer + i, bitmap + 512 * i); + device->read(keyPointer + i, bitmap + 512 * i); } // make sure all trailing bits are marked in use. @@ -83,11 +86,11 @@ Bitmap::Bitmap(BlockDevice *device, unsigned keyPointer, unsigned blocks) _freeBlocks += popCount(bitmap[i]); } - if (_freeblocks) + if (_freeBlocks) { for (unsigned i = 0; i < (blocks + 7) / 8; ++i) { - if (tmp[i]) + if (bitmap[i]) { _freeIndex = i; break; @@ -149,11 +152,11 @@ int Bitmap::allocBlock() { if (!_freeBlocks) return -1; - unsigned firstIndex = _firstIndex; + unsigned freeIndex = _freeIndex; unsigned maxIndex = (_blocks + 7) / 8; - for (unsigned index = _firstIndex; index < maxIndex; ++index) + for (unsigned index = _freeIndex; index < maxIndex; ++index) { uint8_t tmp = _bitmap[index]; if (!tmp) continue; @@ -163,7 +166,7 @@ int Bitmap::allocBlock() { if (tmp & mask) { - _firstIndex = index; + _freeIndex = index; _bitmap[index] = tmp & ~mask; --_freeBlocks; return index * 8 + offset; @@ -172,7 +175,7 @@ int Bitmap::allocBlock() } } - for (unsigned index = 0; index < _firstIndex; ++index) + for (unsigned index = 0; index < freeIndex; ++index) { uint8_t tmp = _bitmap[index]; if (!tmp) continue; @@ -182,7 +185,7 @@ int Bitmap::allocBlock() { if (tmp & mask) { - _firstIndex = index; + _freeIndex = index; _bitmap[index] = tmp & ~mask; --_freeBlocks; return index * 8 + offset; diff --git a/Bitmap.h b/Bitmap.h index b4dcf40..243db9d 100644 --- a/Bitmap.h +++ b/Bitmap.h @@ -2,15 +2,19 @@ #define __BITMAP_H__ #include -class BlockDevice; + + namespace ProFUSE { +class BlockDevice; + + class Bitmap { public: Bitmap(unsigned blocks); - Bitmap(BlockDevice *device, int keyPointer, int blocks); + Bitmap(BlockDevice *device, unsigned keyPointer, unsigned blocks); //todo -- constructor by loading fro, block device... ~Bitmap();