use shared_ptr for device, cache

git-svn-id: https://profuse.googlecode.com/svn/branches/v2@345 aa027e90-d47c-11dd-86d7-074df07e0730
This commit is contained in:
ksherlock 2011-02-22 02:59:33 +00:00
parent 8e259ef1f2
commit 78d35bba08
17 changed files with 80 additions and 36 deletions

View File

@ -23,16 +23,15 @@ using ProFUSE::Exception;
using ProFUSE::POSIXException; using ProFUSE::POSIXException;
BlockCache::BlockCache(BlockDevice *device) BlockCache::BlockCache(BlockDevicePointer device) :
_device(device)
{ {
_device = device;
_blocks = device->blocks(); _blocks = device->blocks();
_readOnly = device->readOnly(); _readOnly = device->readOnly();
} }
BlockCache::~BlockCache() BlockCache::~BlockCache()
{ {
delete _device;
} }
void BlockCache::write(unsigned block, const void *bp) void BlockCache::write(unsigned block, const void *bp)
@ -50,9 +49,9 @@ void BlockCache::read(unsigned block, void *bp)
} }
BlockCache *BlockCache::Create(BlockDevice *device) BlockCachePointer BlockCache::Create(BlockDevicePointer device)
{ {
if (!device) return NULL; if (!device.get()) return BlockCachePointer();
return device->createBlockCache(); return device->createBlockCache();
} }

View File

@ -3,12 +3,13 @@
#include <stdint.h> #include <stdint.h>
#include <vector> #include <vector>
#include <Device/Device.h>
class MappedFile; class MappedFile;
namespace Device { namespace Device {
class BlockDevice;
enum BlockReleaseFlags { enum BlockReleaseFlags {
kBlockDirty = 1, kBlockDirty = 1,
@ -19,13 +20,13 @@ enum BlockReleaseFlags {
class BlockCache { class BlockCache {
public: public:
static BlockCache *Create(BlockDevice *device); static BlockCachePointer Create(BlockDevicePointer device);
virtual ~BlockCache(); virtual ~BlockCache();
bool readOnly() { return _readOnly; } bool readOnly() { return _readOnly; }
unsigned blocks() { return _blocks; } unsigned blocks() { return _blocks; }
BlockDevice *device() { return _device; } BlockDevicePointer device() { return _device; }
virtual void sync() = 0; virtual void sync() = 0;
@ -46,16 +47,16 @@ public:
} }
protected: protected:
BlockCache(BlockDevice *device); BlockCache(BlockDevicePointer device);
BlockDevice *_device; BlockDevicePointer _device;
private: private:
unsigned _blocks; unsigned _blocks;
bool _readOnly; bool _readOnly;
}; };
} // namespace } // namespace
#endif #endif

View File

@ -58,8 +58,12 @@ using ProFUSE::POSIXException;
//typedef std::vector<ConcreteBlockCache::Entry *>::iterator EntryIter; //typedef std::vector<ConcreteBlockCache::Entry *>::iterator EntryIter;
BlockCachePointer ConcreteBlockCache::Create(BlockDevicePointer device, unsigned size)
{
return BlockCachePointer(new ConcreteBlockCache(device, size));
}
ConcreteBlockCache::ConcreteBlockCache(BlockDevice *device, unsigned size) : ConcreteBlockCache::ConcreteBlockCache(BlockDevicePointer device, unsigned size) :
BlockCache(device) BlockCache(device)
{ {
if (size < 16) size = 16; if (size < 16) size = 16;

View File

@ -9,7 +9,9 @@ namespace Device {
class ConcreteBlockCache : public BlockCache { class ConcreteBlockCache : public BlockCache {
public: public:
ConcreteBlockCache(BlockDevice *device, unsigned size = 16);
static BlockCachePointer Create(BlockDevicePointer device, unsigned size = 16);
virtual ~ConcreteBlockCache(); virtual ~ConcreteBlockCache();
virtual void sync(); virtual void sync();
@ -21,8 +23,11 @@ public:
virtual void markDirty(unsigned block); virtual void markDirty(unsigned block);
private: private:
ConcreteBlockCache(BlockDevicePointer device, unsigned size);
struct Entry { struct Entry {
unsigned block; unsigned block;
unsigned count; unsigned count;

View File

@ -19,8 +19,13 @@ using namespace Device;
using ProFUSE::Exception; using ProFUSE::Exception;
using ProFUSE::POSIXException; using ProFUSE::POSIXException;
BlockCachePointer MappedBlockCache::Create(BlockDevicePointer device, void *data)
{
return BlockCachePointer(new MappedBlockCache(device, data));
}
MappedBlockCache::MappedBlockCache(BlockDevice *device, void *data) :
MappedBlockCache::MappedBlockCache(BlockDevicePointer device, void *data) :
BlockCache(device) BlockCache(device)
{ {
_data = (uint8_t *)data; _data = (uint8_t *)data;

View File

@ -8,7 +8,8 @@ namespace Device {
class MappedBlockCache : public BlockCache { class MappedBlockCache : public BlockCache {
public: public:
MappedBlockCache(BlockDevice *, void *data); static BlockCachePointer Create(BlockDevicePointer device, void *data);
virtual ~MappedBlockCache(); virtual ~MappedBlockCache();
virtual void sync(); virtual void sync();
@ -22,7 +23,9 @@ class MappedBlockCache : public BlockCache {
virtual void markDirty(unsigned block); virtual void markDirty(unsigned block);
private: private:
MappedBlockCache(BlockDevicePointer device, void *data);
void sync(unsigned block); void sync(unsigned block);
uint8_t *_data; uint8_t *_data;

View File

@ -233,9 +233,9 @@ void BlockDevice::sync(TrackSector ts)
} }
*/ */
BlockCache *BlockDevice::createBlockCache() BlockCachePointer BlockDevice::createBlockCache()
{ {
unsigned b = blocks(); unsigned b = blocks();
unsigned size = std::max(16u, b / 16); unsigned size = std::max(16u, b / 16);
return new ConcreteBlockCache(this, size); return ConcreteBlockCache::Create(shared_from_this(), size);
} }

View File

@ -4,17 +4,17 @@
#include <stdint.h> #include <stdint.h>
#include <sys/types.h> #include <sys/types.h>
#include <ProFUSE/Exception.h> #include <Device/Device.h>
#include <Device/TrackSector.h> #include <Device/TrackSector.h>
#include <Cache/BlockCache.h> #include <ProFUSE/Exception.h>
#include <File/File.h> #include <File/File.h>
namespace Device { namespace Device {
class BlockDevice { class BlockDevice : public std::tr1::enable_shared_from_this<BlockDevice> {
public: public:
@ -30,7 +30,7 @@ public:
virtual ~BlockDevice(); virtual ~BlockDevice();
virtual BlockCache *createBlockCache(); virtual BlockCachePointer createBlockCache();
virtual void read(unsigned block, void *bp) = 0; virtual void read(unsigned block, void *bp) = 0;

View File

@ -158,8 +158,9 @@ DavexDiskImage *DavexDiskImage::Create(const char *name, size_t blocks, const ch
} }
BlockCache *DavexDiskImage::createBlockCache() BlockCachePointer DavexDiskImage::createBlockCache()
{ {
return new MappedBlockCache(this, 512 + (uint8_t *)address()); // need a smart pointer, but only have this....
return MappedBlockCache::Create(shared_from_this(), 512 + (uint8_t *)address());
} }

View File

@ -20,7 +20,7 @@ public:
static DavexDiskImage *Create(const char *name, size_t blocks, const char *vname); static DavexDiskImage *Create(const char *name, size_t blocks, const char *vname);
static DavexDiskImage *Open(MappedFile *); static DavexDiskImage *Open(MappedFile *);
virtual BlockCache *createBlockCache(); virtual BlockCachePointer createBlockCache();
private: private:

26
Device/Device.h Normal file
View File

@ -0,0 +1,26 @@
//
// Device.h
// profuse
//
// Created by Kelvin Sherlock on 2/19/2011.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#ifndef __DEVICE_DEVICE_H__
#define __DEVICE_DEVICE_H__
#include <tr1/memory>
namespace Device {
class BlockDevice;
class BlockCache;
typedef std::tr1::shared_ptr<BlockDevice> BlockDevicePointer;
typedef std::tr1::shared_ptr<BlockCache> BlockCachePointer;
}
#endif

View File

@ -218,11 +218,11 @@ void DiskCopy42Image::write(unsigned block, const void *bp)
} }
BlockCache *DiskCopy42Image::createBlockCache() BlockCachePointer DiskCopy42Image::createBlockCache()
{ {
// if not readonly, mark changed so crc will be updated at close. // if not readonly, mark changed so crc will be updated at close.
if (!readOnly()) _changed = true; if (!readOnly()) _changed = true;
return new MappedBlockCache(this, address()); return MappedBlockCache::Create(shared_from_this(), address());
} }

View File

@ -22,7 +22,7 @@ public:
virtual void write(unsigned block, const void *bp); virtual void write(unsigned block, const void *bp);
virtual BlockCache *createBlockCache(); virtual BlockCachePointer createBlockCache();
private: private:

View File

@ -138,9 +138,9 @@ void ProDOSOrderDiskImage::Validate(MappedFile *f)
} }
BlockCache *ProDOSOrderDiskImage::createBlockCache() BlockCachePointer ProDOSOrderDiskImage::createBlockCache()
{ {
return new MappedBlockCache(this, address()); return MappedBlockCache::Create(shared_from_this(), address());
} }
#pragma mark - #pragma mark -

View File

@ -61,7 +61,7 @@ public:
static ProDOSOrderDiskImage *Open(MappedFile *); static ProDOSOrderDiskImage *Open(MappedFile *);
virtual BlockCache *createBlockCache(); virtual BlockCachePointer createBlockCache();
private: private:
ProDOSOrderDiskImage(); ProDOSOrderDiskImage();

View File

@ -141,11 +141,11 @@ bool UniversalDiskImage::readOnly()
} }
BlockCache *UniversalDiskImage::createBlockCache() BlockCachePointer UniversalDiskImage::createBlockCache()
{ {
if (_format == 1) if (_format == 1)
{ {
return new MappedBlockCache(this, _dataOffset + (uint8_t *)address()); return MappedBlockCache::Create(shared_from_this(), _dataOffset + (uint8_t *)address());
} }
return DiskImage::createBlockCache(); return DiskImage::createBlockCache();

View File

@ -19,7 +19,7 @@ public:
virtual bool readOnly(); virtual bool readOnly();
virtual BlockCache *createBlockCache(); virtual BlockCachePointer createBlockCache();
private: private: