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
86private:
87	friend class BlockCache;
88
89protected:
90	BlockCache		*fCache;
91	uint64			fNumber;
92	void			*fData;
93	uint32			fFlags;
94	int32			fRefCount;
95};
96
97// Node
98class Node : public Block {
99public:
100	uint16 GetLevel() const;
101	uint16 CountItems() const;
102	uint16 GetFreeSpace() const;
103	bool IsLeaf() const;
104	bool IsInternal() const;
105
106	status_t Check() const;
107
108private:
109	block_head *GetHeader() const;
110
111private:
112	Node();
113};
114
115// InternalNode
116class InternalNode : public Node {
117public:
118	const Key *GetKeys() const;
119	const Key *KeyAt(int32 index) const;
120	const DiskChild *GetChilds() const;
121	const DiskChild *ChildAt(int32 index) const;
122
123	status_t Check() const;
124};
125
126// LeafNode
127class LeafNode : public Node {
128public:
129	const ItemHeader *GetItemHeaders() const;
130	const ItemHeader *ItemHeaderAt(int32 index) const;
131	status_t GetLeftKey(VKey *k) const;
132	status_t GetRightKey(VKey *k) const;
133
134	status_t Check() const;
135
136	uint32 GetItemSpaceOffset() const;
137};
138
139// DiskChild
140class DiskChild : private disk_child {
141public:
142	DiskChild() {}
143
144	uint32 GetBlockNumber() const { return le2h(dc_block_number); }
145	uint16 GetSize() const { return le2h(dc_size); }
146};
147
148#endif	// BLOCK_H
149