1 /* 2 * Copyright 2001-2008, Axel Dörfler, axeld@pinc-software.de. 3 * This file may be used under the terms of the MIT License. 4 */ 5 #ifndef CACHED_BLOCK_H 6 #define CACHED_BLOCK_H 7 8 //! interface for the block cache 9 10 #include <fs_cache.h> 11 12 #include "Volume.h" 13 14 15 class CachedBlock { 16 public: 17 CachedBlock(Volume* volume); 18 CachedBlock(Volume* volume, off_t block); 19 ~CachedBlock(); 20 21 void Keep(); 22 void Unset(); 23 24 const uint8* SetTo(off_t block); 25 26 const uint8* Block() const { return fBlock; } 27 off_t BlockNumber() const { return fBlockNumber; } 28 29 private: 30 CachedBlock(const CachedBlock &); 31 CachedBlock &operator=(const CachedBlock &); 32 // no implementation 33 34 protected: 35 Volume* fVolume; 36 off_t fBlockNumber; 37 uint8* fBlock; 38 }; 39 40 41 // inlines 42 43 44 inline 45 CachedBlock::CachedBlock(Volume* volume) 46 : 47 fVolume(volume), 48 fBlockNumber(0), 49 fBlock(NULL) 50 { 51 } 52 53 54 inline 55 CachedBlock::CachedBlock(Volume* volume, off_t block) 56 : 57 fVolume(volume), 58 fBlockNumber(0), 59 fBlock(NULL) 60 { 61 SetTo(block); 62 } 63 64 65 inline 66 CachedBlock::~CachedBlock() 67 { 68 Unset(); 69 } 70 71 72 inline void 73 CachedBlock::Keep() 74 { 75 fBlock = NULL; 76 } 77 78 79 inline void 80 CachedBlock::Unset() 81 { 82 if (fBlock != NULL) { 83 block_cache_put(fVolume->BlockCache(), fBlockNumber); 84 fBlock = NULL; 85 } 86 } 87 88 89 inline const uint8 * 90 CachedBlock::SetTo(off_t block) 91 { 92 Unset(); 93 fBlockNumber = block; 94 return fBlock = (uint8 *)block_cache_get(fVolume->BlockCache(), block); 95 } 96 97 #endif // CACHED_BLOCK_H 98