xref: /haiku/src/system/kernel/vm/VMAnonymousCache.h (revision 362efe0c9f36d3dd38b22d2c24ac02e54b189d7c)
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 
23 
24 extern "C" {
25 	void swap_init(void);
26 	void swap_init_post_modules(void);
27 	bool swap_free_page_swap_space(vm_page* page);
28 	uint32 swap_available_pages(void);
29 	uint32 swap_total_swap_pages(void);
30 }
31 
32 
33 class VMAnonymousCache : public VMCache {
34 public:
35 	virtual						~VMAnonymousCache();
36 
37 			status_t			Init(bool canOvercommit,
38 									int32 numPrecommittedPages,
39 									int32 numGuardPages,
40 									uint32 allocationFlags);
41 
42 	virtual	status_t			Resize(off_t newSize, int priority);
43 
44 	virtual	status_t			Commit(off_t size, int priority);
45 	virtual	bool				HasPage(off_t offset);
46 	virtual	bool				DebugHasPage(off_t offset);
47 
48 	virtual	status_t			Read(off_t offset, const generic_io_vec* vecs,
49 									size_t count, uint32 flags,
50 									generic_size_t* _numBytes);
51 	virtual	status_t			Write(off_t offset, const generic_io_vec* vecs,
52 									size_t count, uint32 flags,
53 									generic_size_t* _numBytes);
54 	virtual	status_t			WriteAsync(off_t offset,
55 									const generic_io_vec* vecs, size_t count,
56 									generic_size_t numBytes, uint32 flags,
57 									AsyncIOCallback* callback);
58 	virtual	bool				CanWritePage(off_t offset);
59 
60 	virtual	int32				MaxPagesPerAsyncWrite() const;
61 
62 	virtual	status_t			Fault(struct VMAddressSpace* aspace,
63 									off_t offset);
64 
65 	virtual	void				Merge(VMCache* source);
66 
67 protected:
68 	virtual	void				DeleteObject();
69 
70 private:
71 			class WriteCallback;
72 			friend class WriteCallback;
73 
74 			void				_SwapBlockBuild(off_t pageIndex,
75 									swap_addr_t slotIndex, uint32 count);
76 			void        		_SwapBlockFree(off_t pageIndex, uint32 count);
77 			swap_addr_t			_SwapBlockGetAddress(off_t pageIndex);
78 			status_t			_Commit(off_t size, int priority);
79 
80 			void				_MergePagesSmallerSource(
81 									VMAnonymousCache* source);
82 			void				_MergePagesSmallerConsumer(
83 									VMAnonymousCache* source);
84 			void				_MergeSwapPages(VMAnonymousCache* source);
85 
86 private:
87 	friend bool swap_free_page_swap_space(vm_page* page);
88 
89 			bool				fCanOvercommit;
90 			bool				fHasPrecommitted;
91 			uint8				fPrecommittedPages;
92 			int32				fGuardedSize;
93 			off_t   			fCommittedSwapSize;
94 			off_t   			fAllocatedSwapSize;
95 };
96 
97 #endif	// ENABLE_SWAP_SUPPORT
98 
99 
100 extern "C" void swap_get_info(struct system_memory_info* info);
101 
102 
103 #endif	/* _KERNEL_VM_STORE_ANONYMOUS_H */
104