1/*
2 * Copyright 2009-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the NewOS License.
4 */
5#ifndef VM_KERNEL_AREA_H
6#define VM_KERNEL_AREA_H
7
8
9#include <util/AVLTree.h>
10
11#include <vm/VMArea.h>
12
13
14struct ObjectCache;
15struct VMKernelAddressSpace;
16struct VMKernelArea;
17
18
19struct VMKernelAddressRange : AVLTreeNode {
20public:
21	// range types
22	enum {
23		RANGE_FREE,
24		RANGE_RESERVED,
25		RANGE_AREA
26	};
27
28public:
29	DoublyLinkedListLink<VMKernelAddressRange>		listLink;
30	addr_t											base;
31	size_t											size;
32	union {
33		VMKernelArea*								area;
34		struct {
35			addr_t									base;
36			uint32									flags;
37		} reserved;
38		DoublyLinkedListLink<VMKernelAddressRange>	freeListLink;
39	};
40	int												type;
41
42public:
43	VMKernelAddressRange(addr_t base, size_t size, int type)
44		:
45		base(base),
46		size(size),
47		type(type)
48	{
49	}
50
51	VMKernelAddressRange(addr_t base, size_t size,
52		const VMKernelAddressRange* other)
53		:
54		base(base),
55		size(size),
56		type(other->type)
57	{
58		if (type == RANGE_RESERVED) {
59			reserved.base = other->reserved.base;
60			reserved.flags = other->reserved.flags;
61		}
62	}
63};
64
65
66struct VMKernelAddressRangeTreeDefinition {
67	typedef addr_t					Key;
68	typedef VMKernelAddressRange	Value;
69
70	AVLTreeNode* GetAVLTreeNode(Value* value) const
71	{
72		return value;
73	}
74
75	Value* GetValue(AVLTreeNode* node) const
76	{
77		return static_cast<Value*>(node);
78	}
79
80	int Compare(addr_t a, const Value* _b) const
81	{
82		addr_t b = _b->base;
83		if (a == b)
84			return 0;
85		return a < b ? -1 : 1;
86	}
87
88	int Compare(const Value* a, const Value* b) const
89	{
90		return Compare(a->base, b);
91	}
92};
93
94typedef AVLTree<VMKernelAddressRangeTreeDefinition> VMKernelAddressRangeTree;
95
96
97struct VMKernelAddressRangeGetFreeListLink {
98	typedef DoublyLinkedListLink<VMKernelAddressRange> Link;
99
100	inline Link* operator()(VMKernelAddressRange* range) const
101	{
102		return &range->freeListLink;
103	}
104
105	inline const Link* operator()(const VMKernelAddressRange* range) const
106	{
107		return &range->freeListLink;
108	}
109};
110
111
112struct VMKernelArea : VMArea, AVLTreeNode {
113								VMKernelArea(VMAddressSpace* addressSpace,
114									uint32 wiring, uint32 protection);
115								~VMKernelArea();
116
117	static	VMKernelArea*		Create(VMAddressSpace* addressSpace,
118									const char* name, uint32 wiring,
119									uint32 protection, ObjectCache* objectCache,
120									uint32 allocationFlags);
121
122			VMKernelAddressRange* Range() const
123									{ return fRange; }
124			void				SetRange(VMKernelAddressRange* range)
125									{ fRange = range; }
126
127private:
128			VMKernelAddressRange* fRange;
129};
130
131
132#endif	// VM_KERNEL_AREA_H
133