git-svn-id: https://profuse.googlecode.com/svn/branches/v2@103 aa027e90-d47c-11dd-86d7-074df07e0730

This commit is contained in:
ksherlock 2009-11-24 04:57:41 +00:00
parent 5d10a7cbb6
commit cdd55ee726
2 changed files with 20 additions and 13 deletions

View File

@ -1,9 +1,12 @@
#include "Bitmap.h"
#include <cstring> #include <cstring>
#include "Bitmap.h"
#include "BlockDevice.h"
#include "auto.h" #include "auto.h"
using namespace ProFUSE;
// returns # of 1-bits set (0-8) // returns # of 1-bits set (0-8)
inline static unsigned popCount(uint8_t x) inline static unsigned popCount(uint8_t x)
{ {
@ -33,7 +36,7 @@ Bitmap::Bitmap(unsigned blocks)
unsigned bitmapSize = _bitmapBlocks * 512; unsigned bitmapSize = _bitmapBlocks * 512;
unsigned blockSize = blocks / 8; unsigned blockSize = blocks / 8;
auto_array bitmap(new uint8_t[bitmapSize]); auto_array<uint8_t> bitmap(new uint8_t[bitmapSize]);
// mark overflow in use, everything else free. // mark overflow in use, everything else free.
@ -61,11 +64,11 @@ Bitmap::Bitmap(BlockDevice *device, unsigned keyPointer, unsigned blocks)
unsigned bitmapSize = _bitmapBlocks * 512; unsigned bitmapSize = _bitmapBlocks * 512;
unsigned blockSize = blocks / 8; unsigned blockSize = blocks / 8;
auto_array bitmap(new uint8_t[bitmapSize]); auto_array<uint8_t> bitmap(new uint8_t[bitmapSize]);
for (unsigned i = 0; i < blockSize; ++i) 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. // 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]); _freeBlocks += popCount(bitmap[i]);
} }
if (_freeblocks) if (_freeBlocks)
{ {
for (unsigned i = 0; i < (blocks + 7) / 8; ++i) for (unsigned i = 0; i < (blocks + 7) / 8; ++i)
{ {
if (tmp[i]) if (bitmap[i])
{ {
_freeIndex = i; _freeIndex = i;
break; break;
@ -149,11 +152,11 @@ int Bitmap::allocBlock()
{ {
if (!_freeBlocks) return -1; if (!_freeBlocks) return -1;
unsigned firstIndex = _firstIndex; unsigned freeIndex = _freeIndex;
unsigned maxIndex = (_blocks + 7) / 8; unsigned maxIndex = (_blocks + 7) / 8;
for (unsigned index = _firstIndex; index < maxIndex; ++index) for (unsigned index = _freeIndex; index < maxIndex; ++index)
{ {
uint8_t tmp = _bitmap[index]; uint8_t tmp = _bitmap[index];
if (!tmp) continue; if (!tmp) continue;
@ -163,7 +166,7 @@ int Bitmap::allocBlock()
{ {
if (tmp & mask) if (tmp & mask)
{ {
_firstIndex = index; _freeIndex = index;
_bitmap[index] = tmp & ~mask; _bitmap[index] = tmp & ~mask;
--_freeBlocks; --_freeBlocks;
return index * 8 + offset; 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]; uint8_t tmp = _bitmap[index];
if (!tmp) continue; if (!tmp) continue;
@ -182,7 +185,7 @@ int Bitmap::allocBlock()
{ {
if (tmp & mask) if (tmp & mask)
{ {
_firstIndex = index; _freeIndex = index;
_bitmap[index] = tmp & ~mask; _bitmap[index] = tmp & ~mask;
--_freeBlocks; --_freeBlocks;
return index * 8 + offset; return index * 8 + offset;

View File

@ -2,15 +2,19 @@
#define __BITMAP_H__ #define __BITMAP_H__
#include <stdint.h> #include <stdint.h>
class BlockDevice;
namespace ProFUSE { namespace ProFUSE {
class BlockDevice;
class Bitmap { class Bitmap {
public: public:
Bitmap(unsigned blocks); Bitmap(unsigned blocks);
Bitmap(BlockDevice *device, int keyPointer, int blocks); Bitmap(BlockDevice *device, unsigned keyPointer, unsigned blocks);
//todo -- constructor by loading fro, block device... //todo -- constructor by loading fro, block device...
~Bitmap(); ~Bitmap();