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