BTRFS: Node now holding Volume instead of cache to retrieve more values
[haiku.git] / src / add-ons / kernel / file_systems / btrfs / CachedBlock.h
blob769774f3e6fb1b3352f51103538965ccc89e9eef
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
8 //! interface for the block cache
11 #include "Volume.h"
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
22 class CachedBlock {
23 public:
24 CachedBlock(Volume* volume);
25 CachedBlock(Volume* volume, off_t block);
26 ~CachedBlock();
28 void Keep();
29 void Unset();
31 const uint8* SetTo(off_t block);
32 uint8* SetToWritable(off_t block, int32 transactionId,
33 bool empty);
35 const uint8* Block() const { return fBlock; }
36 off_t BlockNumber() const { return fBlockNumber; }
37 bool IsWritable() const { return fWritable; }
39 private:
40 CachedBlock(const CachedBlock&);
41 CachedBlock& operator=(const CachedBlock&);
42 // no implementation
44 protected:
45 Volume* fVolume;
46 off_t fBlockNumber;
47 uint8* fBlock;
48 bool fWritable;
52 // inlines
55 inline
56 CachedBlock::CachedBlock(Volume* volume)
58 fVolume(volume),
59 fBlockNumber(0),
60 fBlock(NULL),
61 fWritable(false)
66 inline
67 CachedBlock::CachedBlock(Volume* volume, off_t block)
69 fVolume(volume),
70 fBlockNumber(0),
71 fBlock(NULL),
72 fWritable(false)
74 SetTo(block);
78 inline
79 CachedBlock::~CachedBlock()
81 Unset();
85 inline void
86 CachedBlock::Keep()
88 fBlock = NULL;
92 inline void
93 CachedBlock::Unset()
95 if (fBlock != NULL) {
96 block_cache_put(fVolume->BlockCache(), fBlockNumber);
97 fBlock = NULL;
102 inline const uint8*
103 CachedBlock::SetTo(off_t block)
105 Unset();
106 fBlockNumber = block;
107 return fBlock = (uint8*)block_cache_get(fVolume->BlockCache(), block);
111 inline uint8*
112 CachedBlock::SetToWritable(off_t block, int32 transactionId, bool empty)
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);
125 return fBlock;
129 #endif // CACHED_BLOCK_H