2009-12-08 00:01:24 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2009-12-11 00:59:53 +00:00
|
|
|
#include <cerrno>
|
2010-04-01 14:38:39 +00:00
|
|
|
#include <cstring>
|
2009-12-08 00:01:24 +00:00
|
|
|
|
2009-12-11 00:59:53 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
2009-12-12 19:27:08 +00:00
|
|
|
#include <unistd.h>
|
2009-12-11 00:59:53 +00:00
|
|
|
|
2010-04-01 14:38:39 +00:00
|
|
|
|
2010-03-22 01:12:51 +00:00
|
|
|
#include <Cache/BlockCache.h>
|
2009-12-16 01:33:28 +00:00
|
|
|
#include <Device/BlockDevice.h>
|
|
|
|
|
|
|
|
#include <ProFUSE/Exception.h>
|
|
|
|
#include <ProFUSE/auto.h>
|
2009-12-08 00:01:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-16 01:33:28 +00:00
|
|
|
using namespace Device;
|
|
|
|
|
|
|
|
using ProFUSE::Exception;
|
|
|
|
using ProFUSE::POSIXException;
|
2009-12-08 00:01:24 +00:00
|
|
|
|
2009-12-11 00:59:53 +00:00
|
|
|
#pragma mark -
|
2010-03-13 18:51:00 +00:00
|
|
|
#pragma mark BlockCache
|
|
|
|
|
2010-03-22 01:12:51 +00:00
|
|
|
BlockCache::BlockCache(BlockDevice *device)
|
2010-03-13 18:51:00 +00:00
|
|
|
{
|
2010-05-19 17:50:18 +00:00
|
|
|
_device = device;
|
2010-03-13 18:51:00 +00:00
|
|
|
_blocks = device->blocks();
|
|
|
|
_readOnly = device->readOnly();
|
|
|
|
}
|
2009-12-11 00:59:53 +00:00
|
|
|
|
2010-03-13 18:51:00 +00:00
|
|
|
BlockCache::~BlockCache()
|
2009-12-11 00:59:53 +00:00
|
|
|
{
|
2010-03-13 18:51:00 +00:00
|
|
|
delete _device;
|
2009-12-11 00:59:53 +00:00
|
|
|
}
|
|
|
|
|
2010-03-13 19:24:00 +00:00
|
|
|
void BlockCache::write(unsigned block, const void *bp)
|
|
|
|
{
|
|
|
|
void *address = acquire(block);
|
|
|
|
std::memcpy(address, bp, 512);
|
|
|
|
release(block, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockCache::read(unsigned block, void *bp)
|
|
|
|
{
|
|
|
|
void *address = acquire(block);
|
|
|
|
std::memcpy(bp, address, 512);
|
|
|
|
release(block, false);
|
|
|
|
}
|
2010-05-18 19:59:00 +00:00
|
|
|
|
|
|
|
|
2010-05-18 21:26:07 +00:00
|
|
|
BlockCache *BlockCache::Create(BlockDevice *device)
|
2010-05-18 19:59:00 +00:00
|
|
|
{
|
|
|
|
if (!device) return NULL;
|
|
|
|
|
2010-05-18 21:26:07 +00:00
|
|
|
return device->createBlockCache();
|
2010-05-18 19:59:00 +00:00
|
|
|
}
|
2010-05-20 22:30:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
void BlockCache::zeroBlock(unsigned block)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
void *address = acquire(block);
|
|
|
|
std::memset(address, 0, 512);
|
|
|
|
release(block, true);
|
|
|
|
*/
|
|
|
|
|
|
|
|
uint8_t buffer[512];
|
|
|
|
|
|
|
|
std::memset(buffer, 0, 512);
|
|
|
|
write(block, buffer);
|
|
|
|
}
|