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 11 #include "Volume.h" 12 13 14 //#define TRACE_BTRFS 15 #ifdef TRACE_BTRFS 16 # define TRACE(x...) dprintf("\33[34mbtrfs:\33[0m " x) 17 #else 18 # define TRACE(x...) ; 19 #endif 20 21 22 class CachedBlock { 23 public: 24 CachedBlock(Volume* volume); 25 CachedBlock(Volume* volume, off_t block); 26 ~CachedBlock(); 27 28 void Keep(); 29 void Unset(); 30 31 const uint8* SetTo(off_t block); 32 uint8* SetToWritable(off_t block, int32 transactionId, 33 bool empty); 34 35 const uint8* Block() const { return fBlock; } 36 off_t BlockNumber() const { return fBlockNumber; } 37 bool IsWritable() const { return fWritable; } 38 39 private: 40 CachedBlock(const CachedBlock&); 41 CachedBlock& operator=(const CachedBlock&); 42 // no implementation 43 44 protected: 45 Volume* fVolume; 46 off_t fBlockNumber; 47 uint8* fBlock; 48 bool fWritable; 49 }; 50 51 52 // inlines 53 54 55 inline 56 CachedBlock::CachedBlock(Volume* volume) 57 : 58 fVolume(volume), 59 fBlockNumber(0), 60 fBlock(NULL), 61 fWritable(false) 62 { 63 } 64 65 66 inline 67 CachedBlock::CachedBlock(Volume* volume, off_t block) 68 : 69 fVolume(volume), 70 fBlockNumber(0), 71 fBlock(NULL), 72 fWritable(false) 73 { 74 SetTo(block); 75 } 76 77 78 inline 79 CachedBlock::~CachedBlock() 80 { 81 Unset(); 82 } 83 84 85 inline void 86 CachedBlock::Keep() 87 { 88 fBlock = NULL; 89 } 90 91 92 inline void 93 CachedBlock::Unset() 94 { 95 if (fBlock != NULL) { 96 block_cache_put(fVolume->BlockCache(), fBlockNumber); 97 fBlock = NULL; 98 } 99 } 100 101 102 inline const uint8* 103 CachedBlock::SetTo(off_t block) 104 { 105 Unset(); 106 fBlockNumber = block; 107 return fBlock = (uint8*)block_cache_get(fVolume->BlockCache(), block); 108 } 109 110 111 inline uint8* 112 CachedBlock::SetToWritable(off_t block, int32 transactionId, bool empty) 113 { 114 Unset(); 115 fBlockNumber = block; 116 fWritable = true; 117 if (empty) { 118 fBlock = (uint8*)block_cache_get_empty(fVolume->BlockCache(), 119 block, transactionId); 120 } else { 121 fBlock = (uint8*)block_cache_get_writable(fVolume->BlockCache(), 122 block, transactionId); 123 } 124 125 return fBlock; 126 } 127 128 129 #endif // CACHED_BLOCK_H 130