* Update CachedBlock.h

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27019 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Salvatore Benedetto
2008-08-18 09:43:14 +00:00
parent 5fc06217df
commit 0e7990cd61
@@ -1,11 +1,9 @@
//---------------------------------------------------------------------- /*
// This software is part of the OpenBeOS distribution and is covered * Copyright 2008, Salvatore Benedetto, [email protected]
// by the OpenBeOS license. * Copyright 2003, Tyler Dauwalder, [email protected]
// * Copyright 2002, Axel Dörfler, [email protected]
// Copyright (c) 2003 Tyler Dauwalder, t[email protected] * Distributed under the terms of the MIT License.
// Based on the CachedBlock class from OpenBFS, */
// Copyright (c) 2002 Axel Dörfler, [email protected]
//---------------------------------------------------------------------
#ifndef _UDF_CACHED_BLOCK_H #ifndef _UDF_CACHED_BLOCK_H
#define _UDF_CACHED_BLOCK_H #define _UDF_CACHED_BLOCK_H
@@ -15,145 +13,133 @@
Axel Dörfler, [email protected] Axel Dörfler, [email protected]
*/ */
#ifdef COMPILE_FOR_R5 #include <fs_cache.h>
extern "C" { #include <util/kernel_cpp.h>
#endif
#include "fsproto.h"
#ifdef COMPILE_FOR_R5
}
#endif
extern "C" {
#ifndef _IMPEXP_KERNEL
# define _IMPEXP_KERNEL
#endif
#include "lock.h"
#include "cache.h"
}
#include "kernel_cpp.h"
#include "UdfDebug.h" #include "UdfDebug.h"
#include "UdfStructures.h" #include "UdfStructures.h"
#include "Volume.h" #include "Volume.h"
namespace Udf {
class CachedBlock { class CachedBlock {
public: public:
CachedBlock(Volume *volume); CachedBlock(Volume *volume);
CachedBlock(Volume *volume, off_t block, bool empty = false); CachedBlock(Volume *volume, off_t block);
CachedBlock(CachedBlock *cached); CachedBlock(CachedBlock *cached);
~CachedBlock(); ~CachedBlock();
inline void Keep(); uint8 *Block() const { return fBlock; }
inline void Unset(); off_t BlockNumber() const { return fBlockNumber; }
inline uint8 *SetTo(off_t block, bool empty = false); uint32 BlockSize() const { return fVolume->BlockSize(); }
inline uint8 *SetTo(long_address address, bool empty = false); uint32 BlockShift() const { return fVolume->BlockShift(); }
template <class Accessor, class Descriptor>
inline uint8* SetTo(Accessor &accessor, Descriptor &descriptor,
bool empty = false);
uint8 *Block() const { return fBlock; } inline void Keep();
off_t BlockNumber() const { return fBlockNumber; } inline void Unset();
uint32 BlockSize() const { return fVolume->BlockSize(); }
uint32 BlockShift() const { return fVolume->BlockShift(); }
private: inline uint8 *SetTo(off_t block);
CachedBlock(const CachedBlock &); // unimplemented inline uint8 *SetTo(off_t block, off_t base, size_t length);
CachedBlock &operator=(const CachedBlock &); // unimplemented inline uint8 *SetTo(long_address address);
template <class Accessor, class Descriptor>
inline uint8* SetTo(Accessor &accessor, Descriptor &descriptor);
protected: protected:
Volume *fVolume; uint8 *fBlock;
off_t fBlockNumber; off_t fBlockNumber;
uint8 *fBlock; Volume *fVolume;
}; };
inline inline
CachedBlock::CachedBlock(Volume *volume) CachedBlock::CachedBlock(Volume *volume)
: :
fVolume(volume), fBlock(NULL),
fBlock(NULL) fBlockNumber(0),
fVolume(volume)
{ {
} }
inline inline
CachedBlock::CachedBlock(Volume *volume, off_t block, bool empty = false) CachedBlock::CachedBlock(Volume *volume, off_t block)
: :
fVolume(volume), fBlock(NULL),
fBlock(NULL) fBlockNumber(0),
fVolume(volume)
{ {
SetTo(block, empty); SetTo(block);
} }
inline inline
CachedBlock::CachedBlock(CachedBlock *cached) CachedBlock::CachedBlock(CachedBlock *cached)
: fVolume(cached->fVolume) :
, fBlockNumber(cached->BlockNumber()) fBlock(cached->fBlock),
, fBlock(cached->fBlock) fBlockNumber(cached->BlockNumber()),
fVolume(cached->fVolume)
{ {
cached->Keep(); cached->Keep();
} }
inline inline
CachedBlock::~CachedBlock() CachedBlock::~CachedBlock()
{ {
Unset(); Unset();
} }
inline void inline void
CachedBlock::Keep() CachedBlock::Keep()
{ {
fBlock = NULL; fBlock = NULL;
} }
inline void inline void
CachedBlock::Unset() CachedBlock::Unset()
{ {
DEBUG_INIT("CachedBlock");
if (fBlock) { if (fBlock) {
PRINT(("releasing block #%Ld\n", BlockNumber())); block_cache_put(fVolume->BlockCache(), fBlockNumber);
release_block(fVolume->Device(), fBlockNumber); fBlock = NULL;
} else {
PRINT(("no block to release\n"));
} }
} }
inline uint8 *
CachedBlock::SetTo(off_t block, bool empty = false) inline uint8*
CachedBlock::SetTo(off_t block)
{ {
DEBUG_INIT_ETC("CachedBlock", ("block: %Ld, empty: %s", return SetTo(block, block, 1);
block, (empty ? "true" : "false")));
Unset();
fBlockNumber = block;
PRINT(("getting block #%Ld\n", block));
return fBlock = empty ? (uint8 *)get_empty_block(fVolume->Device(), block, BlockSize())
: (uint8 *)get_block(fVolume->Device(), block, BlockSize());
} }
inline uint8*
CachedBlock::SetTo(off_t block, off_t base, size_t length)
{
Unset();
fBlockNumber = block;
return fBlock = (uint8 *)block_cache_get_etc(fVolume->BlockCache(),
block, base, length);
}
inline uint8 * inline uint8 *
CachedBlock::SetTo(long_address address, bool empty = false) CachedBlock::SetTo(long_address address)
{ {
off_t block; off_t block;
if (fVolume->MapBlock(address, &block) == B_OK) if (fVolume->MapBlock(address, &block) == B_OK)
return SetTo(block, empty); return SetTo(block, block, 1);
else else
return NULL; return NULL;
} }
template <class Accessor, class Descriptor> template <class Accessor, class Descriptor>
inline uint8* inline uint8*
CachedBlock::SetTo(Accessor &accessor, Descriptor &descriptor, bool empty = false) CachedBlock::SetTo(Accessor &accessor, Descriptor &descriptor)
{ {
// Make a long_address out of the descriptor and call it a day // Make a long_address out of the descriptor and call it a day
long_address address; long_address address;
address.set_to(accessor.GetBlock(descriptor), address.set_to(accessor.GetBlock(descriptor),
accessor.GetPartition(descriptor)); accessor.GetPartition(descriptor));
return SetTo(address, empty); return SetTo(address);
} }
}; // namespace Udf
#endif // _UDF_CACHED_BLOCK_H #endif // _UDF_CACHED_BLOCK_H