xref: /haiku/src/system/kernel/vm/VMAnonymousCache.h (revision a127b88ecbfab58f64944c98aa47722a18e363b2)
1 /*
2  * Copyright 2008-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2004-2008, 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_STORE_ANONYMOUS_H
10 #define _KERNEL_VM_STORE_ANONYMOUS_H
11 
12 
13 #include <vm/VMCache.h>
14 
15 
16 #if ENABLE_SWAP_SUPPORT
17 
18 typedef uint32 swap_addr_t;
19 	// TODO: Should be wider, but RadixBitmap supports only a 32 bit type ATM!
20 struct swap_block;
21 struct system_memory_info;
22 namespace BKernel { class Bitmap; }
23 
24 
25 extern "C" {
26 	void swap_init(void);
27 	void swap_init_post_modules(void);
28 	bool swap_free_page_swap_space(vm_page* page);
29 	uint32 swap_available_pages(void);
30 	uint32 swap_total_swap_pages(void);
31 }
32 
33 
34 class VMAnonymousCache : public VMCache {
35 public:
36 	virtual						~VMAnonymousCache();
37 
38 			status_t			Init(bool canOvercommit,
39 									int32 numPrecommittedPages,
40 									int32 numGuardPages,
41 									uint32 allocationFlags);
42 
43 			status_t			SetCanSwapPages(off_t base, size_t size, bool canSwap);
44 
45 	virtual	status_t			Resize(off_t newSize, int priority);
46 	virtual	status_t			Rebase(off_t newBase, int priority);
47 	virtual	status_t			Adopt(VMCache* source, off_t offset,
48 									off_t size, off_t newOffset);
49 
50 	virtual	status_t			Discard(off_t offset, off_t size);
51 
52 	virtual	status_t			Commit(off_t size, int priority);
53 	virtual	bool				HasPage(off_t offset);
54 	virtual	bool				DebugHasPage(off_t offset);
55 
56 	virtual	int32				GuardSize()	{ return fGuardedSize; }
57 	virtual	void				SetGuardSize(int32 guardSize)
58 									{ fGuardedSize = guardSize; }
59 
60 	virtual	status_t			Read(off_t offset, const generic_io_vec* vecs,
61 									size_t count, uint32 flags,
62 									generic_size_t* _numBytes);
63 	virtual	status_t			Write(off_t offset, const generic_io_vec* vecs,
64 									size_t count, uint32 flags,
65 									generic_size_t* _numBytes);
66 	virtual	status_t			WriteAsync(off_t offset,
67 									const generic_io_vec* vecs, size_t count,
68 									generic_size_t numBytes, uint32 flags,
69 									AsyncIOCallback* callback);
70 	virtual	bool				CanWritePage(off_t offset);
71 
72 	virtual	int32				MaxPagesPerAsyncWrite() const;
73 
74 	virtual	status_t			Fault(struct VMAddressSpace* aspace,
75 									off_t offset);
76 
77 	virtual	void				Merge(VMCache* source);
78 
79 protected:
80 	virtual	void				DeleteObject();
81 
82 private:
83 			class WriteCallback;
84 			friend class WriteCallback;
85 
86 			void				_SwapBlockBuild(off_t pageIndex,
87 									swap_addr_t slotIndex, uint32 count);
88 			void				_SwapBlockFree(off_t pageIndex, uint32 count);
89 			swap_addr_t			_SwapBlockGetAddress(off_t pageIndex);
90 			status_t			_Commit(off_t size, int priority);
91 
92 			void				_MergePagesSmallerSource(
93 									VMAnonymousCache* source);
94 			void				_MergePagesSmallerConsumer(
95 									VMAnonymousCache* source);
96 			void				_MergeSwapPages(VMAnonymousCache* source);
97 
98 			void				_FreeSwapPageRange(off_t fromOffset,
99 									off_t toOffset, bool skipBusyPages = true);
100 
101 private:
102 	friend bool swap_free_page_swap_space(vm_page* page);
103 
104 			bool				fCanOvercommit;
105 			bool				fHasPrecommitted;
106 			uint8				fPrecommittedPages;
107 			int32				fGuardedSize;
108 			BKernel::Bitmap*	fNoSwapPages;
109 			off_t				fCommittedSwapSize;
110 			off_t				fAllocatedSwapSize;
111 };
112 
113 #endif	// ENABLE_SWAP_SUPPORT
114 
115 
116 extern "C" void swap_get_info(system_info* info);
117 
118 
119 #endif	/* _KERNEL_VM_STORE_ANONYMOUS_H */
120