From 78d35bba08836c2eee12c8392c448d6092b72e8b Mon Sep 17 00:00:00 2001 From: ksherlock Date: Tue, 22 Feb 2011 02:59:33 +0000 Subject: [PATCH] use shared_ptr for device, cache git-svn-id: https://profuse.googlecode.com/svn/branches/v2@345 aa027e90-d47c-11dd-86d7-074df07e0730 --- Cache/BlockCache.cpp | 9 ++++----- Cache/BlockCache.h | 13 +++++++------ Cache/ConcreteBlockCache.cpp | 6 +++++- Cache/ConcreteBlockCache.h | 9 +++++++-- Cache/MappedBlockCache.cpp | 7 ++++++- Cache/MappedBlockCache.h | 7 +++++-- Device/BlockDevice.cpp | 4 ++-- Device/BlockDevice.h | 10 +++++----- Device/DavexDiskImage.cpp | 5 +++-- Device/DavexDiskImage.h | 2 +- Device/Device.h | 26 ++++++++++++++++++++++++++ Device/DiskCopy42Image.cpp | 4 ++-- Device/DiskCopy42Image.h | 2 +- Device/DiskImage.cpp | 4 ++-- Device/DiskImage.h | 2 +- Device/UniversalDiskImage.cpp | 4 ++-- Device/UniversalDiskImage.h | 2 +- 17 files changed, 80 insertions(+), 36 deletions(-) create mode 100644 Device/Device.h diff --git a/Cache/BlockCache.cpp b/Cache/BlockCache.cpp index b6670b5..61462ed 100644 --- a/Cache/BlockCache.cpp +++ b/Cache/BlockCache.cpp @@ -23,16 +23,15 @@ using ProFUSE::Exception; using ProFUSE::POSIXException; -BlockCache::BlockCache(BlockDevice *device) +BlockCache::BlockCache(BlockDevicePointer device) : + _device(device) { - _device = device; _blocks = device->blocks(); _readOnly = device->readOnly(); } BlockCache::~BlockCache() { - delete _device; } 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(); } diff --git a/Cache/BlockCache.h b/Cache/BlockCache.h index c4587d1..2b9e839 100644 --- a/Cache/BlockCache.h +++ b/Cache/BlockCache.h @@ -3,12 +3,13 @@ #include #include +#include + class MappedFile; namespace Device { -class BlockDevice; enum BlockReleaseFlags { kBlockDirty = 1, @@ -19,13 +20,13 @@ enum BlockReleaseFlags { class BlockCache { public: - static BlockCache *Create(BlockDevice *device); + static BlockCachePointer Create(BlockDevicePointer device); virtual ~BlockCache(); bool readOnly() { return _readOnly; } unsigned blocks() { return _blocks; } - BlockDevice *device() { return _device; } + BlockDevicePointer device() { return _device; } virtual void sync() = 0; @@ -46,16 +47,16 @@ public: } protected: - BlockCache(BlockDevice *device); + BlockCache(BlockDevicePointer device); - BlockDevice *_device; + BlockDevicePointer _device; private: unsigned _blocks; bool _readOnly; }; - + } // namespace #endif diff --git a/Cache/ConcreteBlockCache.cpp b/Cache/ConcreteBlockCache.cpp index 5e0bd69..5bf7ede 100644 --- a/Cache/ConcreteBlockCache.cpp +++ b/Cache/ConcreteBlockCache.cpp @@ -58,8 +58,12 @@ using ProFUSE::POSIXException; //typedef std::vector::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) { if (size < 16) size = 16; diff --git a/Cache/ConcreteBlockCache.h b/Cache/ConcreteBlockCache.h index 2ca5698..aead0e6 100644 --- a/Cache/ConcreteBlockCache.h +++ b/Cache/ConcreteBlockCache.h @@ -9,7 +9,9 @@ namespace Device { class ConcreteBlockCache : public BlockCache { public: - ConcreteBlockCache(BlockDevice *device, unsigned size = 16); + + static BlockCachePointer Create(BlockDevicePointer device, unsigned size = 16); + virtual ~ConcreteBlockCache(); virtual void sync(); @@ -21,8 +23,11 @@ public: virtual void markDirty(unsigned block); - private: + + + ConcreteBlockCache(BlockDevicePointer device, unsigned size); + struct Entry { unsigned block; unsigned count; diff --git a/Cache/MappedBlockCache.cpp b/Cache/MappedBlockCache.cpp index 1d1be58..4a65fb1 100644 --- a/Cache/MappedBlockCache.cpp +++ b/Cache/MappedBlockCache.cpp @@ -19,8 +19,13 @@ using namespace Device; using ProFUSE::Exception; 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) { _data = (uint8_t *)data; diff --git a/Cache/MappedBlockCache.h b/Cache/MappedBlockCache.h index a3402ff..e02a854 100644 --- a/Cache/MappedBlockCache.h +++ b/Cache/MappedBlockCache.h @@ -8,7 +8,8 @@ namespace Device { class MappedBlockCache : public BlockCache { public: - MappedBlockCache(BlockDevice *, void *data); + static BlockCachePointer Create(BlockDevicePointer device, void *data); + virtual ~MappedBlockCache(); virtual void sync(); @@ -22,7 +23,9 @@ class MappedBlockCache : public BlockCache { virtual void markDirty(unsigned block); private: - + + MappedBlockCache(BlockDevicePointer device, void *data); + void sync(unsigned block); uint8_t *_data; diff --git a/Device/BlockDevice.cpp b/Device/BlockDevice.cpp index d86f4d7..d421746 100644 --- a/Device/BlockDevice.cpp +++ b/Device/BlockDevice.cpp @@ -233,9 +233,9 @@ void BlockDevice::sync(TrackSector ts) } */ -BlockCache *BlockDevice::createBlockCache() +BlockCachePointer BlockDevice::createBlockCache() { unsigned b = blocks(); unsigned size = std::max(16u, b / 16); - return new ConcreteBlockCache(this, size); + return ConcreteBlockCache::Create(shared_from_this(), size); } diff --git a/Device/BlockDevice.h b/Device/BlockDevice.h index 8e8feb5..08eea1d 100644 --- a/Device/BlockDevice.h +++ b/Device/BlockDevice.h @@ -4,17 +4,17 @@ #include #include -#include - +#include #include -#include +#include + #include namespace Device { -class BlockDevice { + class BlockDevice : public std::tr1::enable_shared_from_this { public: @@ -30,7 +30,7 @@ public: virtual ~BlockDevice(); - virtual BlockCache *createBlockCache(); + virtual BlockCachePointer createBlockCache(); virtual void read(unsigned block, void *bp) = 0; diff --git a/Device/DavexDiskImage.cpp b/Device/DavexDiskImage.cpp index e63a04f..7600d5b 100644 --- a/Device/DavexDiskImage.cpp +++ b/Device/DavexDiskImage.cpp @@ -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()); } diff --git a/Device/DavexDiskImage.h b/Device/DavexDiskImage.h index ff659e0..1750897 100644 --- a/Device/DavexDiskImage.h +++ b/Device/DavexDiskImage.h @@ -20,7 +20,7 @@ public: static DavexDiskImage *Create(const char *name, size_t blocks, const char *vname); static DavexDiskImage *Open(MappedFile *); - virtual BlockCache *createBlockCache(); + virtual BlockCachePointer createBlockCache(); private: diff --git a/Device/Device.h b/Device/Device.h new file mode 100644 index 0000000..3c280d0 --- /dev/null +++ b/Device/Device.h @@ -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 + +namespace Device { + + class BlockDevice; + class BlockCache; + + typedef std::tr1::shared_ptr BlockDevicePointer; + typedef std::tr1::shared_ptr BlockCachePointer; + + +} + + +#endif diff --git a/Device/DiskCopy42Image.cpp b/Device/DiskCopy42Image.cpp index 9af488b..3d121f3 100644 --- a/Device/DiskCopy42Image.cpp +++ b/Device/DiskCopy42Image.cpp @@ -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 (!readOnly()) _changed = true; - return new MappedBlockCache(this, address()); + return MappedBlockCache::Create(shared_from_this(), address()); } \ No newline at end of file diff --git a/Device/DiskCopy42Image.h b/Device/DiskCopy42Image.h index f4ce443..f807f78 100644 --- a/Device/DiskCopy42Image.h +++ b/Device/DiskCopy42Image.h @@ -22,7 +22,7 @@ public: virtual void write(unsigned block, const void *bp); - virtual BlockCache *createBlockCache(); + virtual BlockCachePointer createBlockCache(); private: diff --git a/Device/DiskImage.cpp b/Device/DiskImage.cpp index 113eb22..091d789 100644 --- a/Device/DiskImage.cpp +++ b/Device/DiskImage.cpp @@ -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 - diff --git a/Device/DiskImage.h b/Device/DiskImage.h index 52a7303..a560e3e 100644 --- a/Device/DiskImage.h +++ b/Device/DiskImage.h @@ -61,7 +61,7 @@ public: static ProDOSOrderDiskImage *Open(MappedFile *); - virtual BlockCache *createBlockCache(); + virtual BlockCachePointer createBlockCache(); private: ProDOSOrderDiskImage(); diff --git a/Device/UniversalDiskImage.cpp b/Device/UniversalDiskImage.cpp index dedb9c9..1d7014e 100644 --- a/Device/UniversalDiskImage.cpp +++ b/Device/UniversalDiskImage.cpp @@ -141,11 +141,11 @@ bool UniversalDiskImage::readOnly() } -BlockCache *UniversalDiskImage::createBlockCache() +BlockCachePointer UniversalDiskImage::createBlockCache() { if (_format == 1) { - return new MappedBlockCache(this, _dataOffset + (uint8_t *)address()); + return MappedBlockCache::Create(shared_from_this(), _dataOffset + (uint8_t *)address()); } return DiskImage::createBlockCache(); diff --git a/Device/UniversalDiskImage.h b/Device/UniversalDiskImage.h index e552af5..557e935 100644 --- a/Device/UniversalDiskImage.h +++ b/Device/UniversalDiskImage.h @@ -19,7 +19,7 @@ public: virtual bool readOnly(); - virtual BlockCache *createBlockCache(); + virtual BlockCachePointer createBlockCache(); private: