From 0f9dda9f00200c24373c4e90911605e013d42afb Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Tue, 20 Jul 2010 21:19:11 +0000 Subject: [PATCH] Moved Block implementation to its own source file. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37633 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../kernel/file_corruption/fs/Block.cpp | 105 ++++++++++++ .../system/kernel/file_corruption/fs/Block.h | 149 +++++------------- .../kernel/file_corruption/fs/Directory.cpp | 2 + .../system/kernel/file_corruption/fs/File.cpp | 3 + .../system/kernel/file_corruption/fs/Jamfile | 1 + .../system/kernel/file_corruption/fs/Node.cpp | 1 + .../kernel/file_corruption/fs/Volume.cpp | 1 + 7 files changed, 149 insertions(+), 113 deletions(-) create mode 100644 src/tests/system/kernel/file_corruption/fs/Block.cpp diff --git a/src/tests/system/kernel/file_corruption/fs/Block.cpp b/src/tests/system/kernel/file_corruption/fs/Block.cpp new file mode 100644 index 0000000000..6504d74dcb --- /dev/null +++ b/src/tests/system/kernel/file_corruption/fs/Block.cpp @@ -0,0 +1,105 @@ +/* + * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ + + +#include "Block.h" + +#include + +#include "Transaction.h" +#include "Volume.h" + + +void +Block::TransferFrom(Block& other) +{ + Put(); + + fVolume = other.fVolume; + fData = other.fData; + fIndex = other.fIndex; + fWritable = other.fWritable; + + other.fVolume = NULL; + other.fData = NULL; +} + + +bool +Block::GetReadable(Volume* volume, uint64 blockIndex) +{ + Put(); + + return _Init(volume, blockIndex, + block_cache_get(volume->BlockCache(), blockIndex), false); +} + + +bool +Block::GetWritable(Volume* volume, uint64 blockIndex, Transaction& transaction) +{ + Put(); + + return _Init(volume, blockIndex, + block_cache_get_writable(volume->BlockCache(), blockIndex, + transaction.ID()), + true); +} + + +bool +Block::GetZero(Volume* volume, uint64 blockIndex, Transaction& transaction) +{ + Put(); + + return _Init(volume, blockIndex, + block_cache_get_empty(volume->BlockCache(), blockIndex, + transaction.ID()), + true); +} + + +status_t +Block::MakeWritable(Transaction& transaction) +{ + if (fVolume == NULL) + return B_BAD_VALUE; + if (fWritable) + return B_OK; + + status_t error = block_cache_make_writable(fVolume->BlockCache(), + fIndex, transaction.ID()); + if (error != B_OK) + return error; + + fWritable = true; + return B_OK; +} + + +void +Block::Put() +{ + if (fVolume != NULL) { + block_cache_put(fVolume->BlockCache(), fIndex); + fVolume = NULL; + fData = NULL; + } +} + + +bool +Block::_Init(Volume* volume, uint64 blockIndex, const void* data, bool writable) +{ + if (data == NULL) + return false; + + fVolume = volume; + fData = const_cast(data); + fIndex = blockIndex; + fWritable = writable; + + return true; +} diff --git a/src/tests/system/kernel/file_corruption/fs/Block.h b/src/tests/system/kernel/file_corruption/fs/Block.h index d8baf19006..a56efe64a1 100644 --- a/src/tests/system/kernel/file_corruption/fs/Block.h +++ b/src/tests/system/kernel/file_corruption/fs/Block.h @@ -6,134 +6,57 @@ #define BLOCK_H -#include +#include -#include "Transaction.h" -#include "Volume.h" + +class Transaction; +class Volume; class Block { public: - Block() - : - fVolume(NULL), - fData(NULL) - { - } + inline Block(); + inline ~Block(); - ~Block() - { - Put(); - } + void TransferFrom(Block& other); - void TransferFrom(Block& other) - { - Put(); + bool GetReadable(Volume* volume, uint64 blockIndex); + bool GetWritable(Volume* volume, uint64 blockIndex, + Transaction& transaction); + bool GetZero(Volume* volume, uint64 blockIndex, + Transaction& transaction); - fVolume = other.fVolume; - fData = other.fData; - fIndex = other.fIndex; - fWritable = other.fWritable; + status_t MakeWritable(Transaction& transaction); - other.fVolume = NULL; - other.fData = NULL; - } + void Put(); - bool GetReadable(Volume* volume, uint64 blockIndex) - { - Put(); - - return _Init(volume, blockIndex, - block_cache_get(volume->BlockCache(), blockIndex), false); - } - - bool GetWritable(Volume* volume, uint64 blockIndex, - Transaction& transaction) - { - Put(); - - return _Init(volume, blockIndex, - block_cache_get_writable(volume->BlockCache(), blockIndex, - transaction.ID()), - true); - } - - bool GetZero(Volume* volume, uint64 blockIndex, Transaction& transaction) - { - Put(); - - return _Init(volume, blockIndex, - block_cache_get_empty(volume->BlockCache(), blockIndex, - transaction.ID()), - true); - } - - status_t MakeWritable(Transaction& transaction) - { - if (fVolume == NULL) - return B_BAD_VALUE; - if (fWritable) - return B_OK; - - status_t error = block_cache_make_writable(fVolume->BlockCache(), - fIndex, transaction.ID()); - if (error != B_OK) - return error; - - fWritable = true; - return B_OK; - } - - void Put() - { - if (fVolume != NULL) { - block_cache_put(fVolume->BlockCache(), fIndex); - fVolume = NULL; - fData = NULL; - } - } - - void Discard() - { - if (fVolume != NULL) { - block_cache_discard(fVolume->BlockCache(), fIndex, 1); - fVolume = NULL; - fData = NULL; - } - } - - void* Data() const - { - return fData; - } - - uint64 Index() const - { - return fIndex; - } + void* Data() const { return fData; } + uint64 Index() const { return fIndex; } private: - bool _Init(Volume* volume, uint64 blockIndex, const void* data, - bool writable) - { - if (data == NULL) - return false; - - fVolume = volume; - fData = const_cast(data); - fIndex = blockIndex; - fWritable = writable; - - return true; - } - + bool _Init(Volume* volume, uint64 blockIndex, + const void* data, bool writable); private: - Volume* fVolume; - void* fData; - uint64 fIndex; - bool fWritable; + Volume* fVolume; + void* fData; + uint64 fIndex; + bool fWritable; }; +Block::Block() + : + fVolume(NULL), + fData(NULL) +{ +} + + +Block::~Block() +{ + Put(); +} + + #endif // BLOCK_H diff --git a/src/tests/system/kernel/file_corruption/fs/Directory.cpp b/src/tests/system/kernel/file_corruption/fs/Directory.cpp index 73272cb9f6..f1647e089a 100644 --- a/src/tests/system/kernel/file_corruption/fs/Directory.cpp +++ b/src/tests/system/kernel/file_corruption/fs/Directory.cpp @@ -14,6 +14,8 @@ #include "Block.h" #include "BlockAllocator.h" #include "DebugSupport.h" +#include "Transaction.h" +#include "Volume.h" class DirEntryBlock { diff --git a/src/tests/system/kernel/file_corruption/fs/File.cpp b/src/tests/system/kernel/file_corruption/fs/File.cpp index a4742e779c..594010af4e 100644 --- a/src/tests/system/kernel/file_corruption/fs/File.cpp +++ b/src/tests/system/kernel/file_corruption/fs/File.cpp @@ -11,11 +11,14 @@ #include #include +#include + #include #include "Block.h" #include "BlockAllocator.h" #include "DebugSupport.h" +#include "Transaction.h" #include "Volume.h" diff --git a/src/tests/system/kernel/file_corruption/fs/Jamfile b/src/tests/system/kernel/file_corruption/fs/Jamfile index a853e62397..159b5b4279 100644 --- a/src/tests/system/kernel/file_corruption/fs/Jamfile +++ b/src/tests/system/kernel/file_corruption/fs/Jamfile @@ -17,6 +17,7 @@ SubDirC++Flags -Werror ; HAIKU_CHECKSUM_FS_SOURCES = + Block.cpp BlockAllocator.cpp checksumfs.cpp Directory.cpp diff --git a/src/tests/system/kernel/file_corruption/fs/Node.cpp b/src/tests/system/kernel/file_corruption/fs/Node.cpp index 167c775b56..b556df01ab 100644 --- a/src/tests/system/kernel/file_corruption/fs/Node.cpp +++ b/src/tests/system/kernel/file_corruption/fs/Node.cpp @@ -12,6 +12,7 @@ #include "Block.h" #include "DebugSupport.h" +#include "Volume.h" static inline uint64 diff --git a/src/tests/system/kernel/file_corruption/fs/Volume.cpp b/src/tests/system/kernel/file_corruption/fs/Volume.cpp index 6ab3620e69..90a01caa0e 100644 --- a/src/tests/system/kernel/file_corruption/fs/Volume.cpp +++ b/src/tests/system/kernel/file_corruption/fs/Volume.cpp @@ -29,6 +29,7 @@ #include "File.h" #include "SuperBlock.h" #include "SymLink.h" +#include "Transaction.h" Volume::Volume(uint32 flags)