1// Block.h
2//
3// Copyright (c) 2003, Ingo Weinhold (bonefish@cs.tu-berlin.de)
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18//
19// You can alternatively use *this file* under the terms of the the MIT
20// license included in this package.
21
22#ifndef BLOCK_H
23#define BLOCK_H
24
25#include <SupportDefs.h>
26
27#include "BlockCache.h"
28#include "endianess.h"
29#include "reiserfs.h"
30
31class DiskChild;
32class InternalNode;
33class Item;
34class ItemHeader;
35class Key;
36class VKey;
37class LeafNode;
38class Node;
39
40// Block
41class Block {
42public:
43	Block();
44	~Block();
45
46	BlockCache *GetCache() const;
47	uint32 GetBlockSize() const;
48
49	void Get();
50	void Put();
51
52	uint64 GetNumber() const;
53	void *GetData() const;
54
55	void SetKind(uint32 kind);
56	uint32 GetKind() const			{ return (fFlags & KIND_MASK); }
57
58	void SetChecked(bool checked);
59	bool IsChecked() const			{ return (fFlags & CHECKED); }
60
61	bool IsFormatted() const;
62	bool IsLeaf() const;
63	bool IsInternal() const;
64
65	Node *ToNode();
66	InternalNode *ToInternalNode();
67	LeafNode *ToLeafNode();
68
69public:
70	enum {
71		KIND_FORMATTED		= 0x00,
72		KIND_UNFORMATTED	= 0x01,
73		KIND_UNKNOWN		= 0x02,
74		KIND_MASK			= 0x03,
75		CHECKED				= 0x80,
76	};
77
78private:
79	status_t _SetTo(BlockCache *cache, uint64 number);
80	void _Unset();
81
82	int32 _GetRefCount() const { return fRefCount; }
83	void _Get();
84	bool _Put();
85	void _SetAge(int64 age) { fAge = age; }
86	int64 _GetAge() { return fAge; }
87
88private:
89	friend class BlockCache;
90
91protected:
92	BlockCache		*fCache;
93	uint64			fNumber;
94	void			*fData;
95	uint32			fFlags;
96	int32			fRefCount;
97	int64			fAge;
98};
99
100// Node
101class Node : public Block {
102public:
103	uint16 GetLevel() const;
104	uint16 CountItems() const;
105	uint16 GetFreeSpace() const;
106	bool IsLeaf() const;
107	bool IsInternal() const;
108
109	status_t Check() const;
110
111private:
112	block_head *GetHeader() const;
113
114private:
115	Node();
116};
117
118// InternalNode
119class InternalNode : public Node {
120public:
121	const Key *GetKeys() const;
122	const Key *KeyAt(int32 index) const;
123	const DiskChild *GetChilds() const;
124	const DiskChild *ChildAt(int32 index) const;
125
126	status_t Check() const;
127};
128
129// LeafNode
130class LeafNode : public Node {
131public:
132	const ItemHeader *GetItemHeaders() const;
133	const ItemHeader *ItemHeaderAt(int32 index) const;
134	status_t GetLeftKey(VKey *k) const;
135	status_t GetRightKey(VKey *k) const;
136
137	status_t Check() const;
138
139	uint32 GetItemSpaceOffset() const;
140};
141
142// DiskChild
143class DiskChild : private disk_child {
144public:
145	DiskChild() {}
146
147	uint32 GetBlockNumber() const { return le2h(dc_block_number); }
148	uint16 GetSize() const { return le2h(dc_size); }
149};
150
151#endif	// BLOCK_H
152