xref: /haiku/headers/private/kernel/vm/VMArea.h (revision fa66a805cce4fd4e4fc501ed6e22c0ed684fab9a)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2002-2009, Axel Dörfler, axeld@pinc-software.de.
4  * Distributed under the terms of the MIT License.
5  *
6  * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
7  * Distributed under the terms of the NewOS License.
8  */
9 #ifndef _KERNEL_VM_VM_AREA_H
10 #define _KERNEL_VM_VM_AREA_H
11 
12 
13 #include <lock.h>
14 #include <util/DoublyLinkedList.h>
15 #include <util/OpenHashTable.h>
16 #include <vm/vm_types.h>
17 
18 
19 struct VMAddressSpace;
20 struct VMCache;
21 struct VMKernelAddressSpace;
22 struct VMUserAddressSpace;
23 
24 
25 struct VMArea {
26 	char*					name;
27 	area_id					id;
28 	uint32					protection;
29 	uint16					wiring;
30 	uint16					memory_type;
31 
32 	VMCache*				cache;
33 	vint32					no_cache_change;
34 	off_t					cache_offset;
35 	uint32					cache_type;
36 	VMAreaMappings			mappings;
37 	uint8*					page_protections;
38 
39 	struct VMAddressSpace*	address_space;
40 	struct VMArea*			cache_next;
41 	struct VMArea*			cache_prev;
42 	struct VMArea*			hash_next;
43 
44 			addr_t				Base() const	{ return fBase; }
45 			size_t				Size() const	{ return fSize; }
46 
47 			bool				ContainsAddress(addr_t address) const
48 									{ return address >= fBase
49 										&& address <= fBase + (fSize - 1); }
50 
51 protected:
52 								VMArea(VMAddressSpace* addressSpace,
53 									uint32 wiring, uint32 protection);
54 								~VMArea();
55 
56 			status_t			Init(const char* name);
57 
58 protected:
59 			friend class VMAddressSpace;
60 			friend class VMKernelAddressSpace;
61 			friend class VMUserAddressSpace;
62 
63 protected:
64 			void				SetBase(addr_t base)	{ fBase = base; }
65 			void				SetSize(size_t size)	{ fSize = size; }
66 
67 protected:
68 			addr_t				fBase;
69 			size_t				fSize;
70 };
71 
72 
73 struct VMAreaHashDefinition {
74 	typedef area_id		KeyType;
75 	typedef VMArea		ValueType;
76 
77 	size_t HashKey(area_id key) const
78 	{
79 		return key;
80 	}
81 
82 	size_t Hash(const VMArea* value) const
83 	{
84 		return HashKey(value->id);
85 	}
86 
87 	bool Compare(area_id key, const VMArea* value) const
88 	{
89 		return value->id == key;
90 	}
91 
92 	VMArea*& GetLink(VMArea* value) const
93 	{
94 		return value->hash_next;
95 	}
96 };
97 
98 typedef BOpenHashTable<VMAreaHashDefinition> VMAreaHashTable;
99 
100 
101 struct VMAreaHash {
102 	static	status_t			Init();
103 
104 	static	status_t			ReadLock()
105 									{ return rw_lock_read_lock(&sLock); }
106 	static	void				ReadUnlock()
107 									{ rw_lock_read_unlock(&sLock); }
108 	static	status_t			WriteLock()
109 									{ return rw_lock_write_lock(&sLock); }
110 	static	void				WriteUnlock()
111 									{ rw_lock_write_unlock(&sLock); }
112 
113 	static	VMArea*				LookupLocked(area_id id)
114 									{ return sTable.Lookup(id); }
115 	static	VMArea*				Lookup(area_id id);
116 	static	area_id				Find(const char* name);
117 	static	void				Insert(VMArea* area);
118 	static	void				Remove(VMArea* area);
119 
120 	static	VMAreaHashTable::Iterator GetIterator()
121 									{ return sTable.GetIterator(); }
122 
123 private:
124 	static	rw_lock				sLock;
125 	static	VMAreaHashTable		sTable;
126 };
127 
128 
129 #endif	// _KERNEL_VM_VM_AREA_H
130