Update contrib.
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\include\memmodel\epoc\mmubase\ramcache.h
21 #ifndef __MEMMODEL_FLEXIBLE__
22 #include <memmodel/epoc/mmubase/mmubase.h>
28 Base class for the ram caching or demand paging implementation.
30 This provides an interface between class Mmu and the implementation
31 of any form of dynamic use of the system's free memory, e.g. demand paging.
32 The chief functionality of this interface is for transferring ownership of
33 physical pages of RAM.
46 Intialisation called during MmuBase:Init2.
51 Initialisation called from M::DemandPagingInit.
53 virtual TInt Init3()=0;
56 Remove RAM pages from the cache and return them to the system's free pool.
59 This is called by MmuBase when it requires more free RAM to meet an
62 @param aNumPages The number of pages to free up.
63 @return True if all pages could be freed, false otherwise
64 @pre RamAlloc mutex held.
66 virtual TBool GetFreePages(TInt aNumPages)=0;
69 Give a RAM page to the cache system for managing.
70 This page of RAM may be reused for any purpose.
71 If the page has already been donated then no action is taken.
73 @param aPageInfo The page info for the donated page.
75 @see ReclaimRamCachePage.
78 @post System Lock left unchanged.
80 virtual void DonateRamCachePage(SPageInfo* aPageInfo)=0;
83 Attempt to reclaim a RAM page given to the cache system with #DonateRamCachePage.
84 If the RAM page has not been reused for other purposes then the page is
85 removed from the cache system's management.
86 If the page has not previousely been donated then no action is taken.
88 @param aPageInfo The page info for the page to reclaim.
90 @return True if page successfuly reclaimed, false otherwise.
93 @post System Lock left unchanged.
95 virtual TBool ReclaimRamCachePage(SPageInfo* aPageInfo)=0;
98 Called by MMU class when a page is unmapped from a chunk.
100 @param aPageInfo The page info for the page being unmapped.
102 @return True is cache system doesn't claim ownership of this page, false if it does.
104 virtual TBool PageUnmapped(SPageInfo* aPageInfo)=0;
107 Check whether the specified page can be discarded by the RAM cache.
109 @param aPageInfo The page info of the page being queried.
110 @return ETrue when the page can be discarded, EFalse otherwise.
111 @pre System lock held.
112 @post System lock held.
114 virtual TBool IsPageDiscardable(SPageInfo& aPageInfo) = 0;
117 Discard the specified page.
118 Should only be called on a page if a previous call to IsPageDiscardable()
119 returned ETrue and the system lock hasn't been released between the calls.
120 If necessary a new page may be allocated to replace the one being
121 discarded, however the new page should not be allocated into the RAM
122 zone of ID==aBlockedZoneId.
124 @param aPageInfo The page info of the page to be discarded
125 @param aBlockZoneId The ID of the RAM zone that shouldn't be allocated into.
126 @param aBlockRest Set to ETrue to stop allocation as soon as aBlockedZoneId is reached
127 in preference ordering. EFalse otherwise.
128 @return ETrue if the page could be discarded, EFalse otherwise.
130 @pre System lock held.
131 @post System lock held.
133 virtual TBool DoDiscardPage(SPageInfo& aPageInfo, TUint aBlockedZoneId, TBool aBlockRest) = 0;
137 First stage in discarding a list of pages.
139 Must ensure that the pages will still be discardable even if system lock
140 is released after this method has completed.
141 To be used in conjunction with RamCacheBase::DoDiscardPages1().
143 @param aPageList A NULL terminated list of the pages to be discarded
144 @return KErrNone on success.
146 @pre System lock held
147 @post System lock held
149 virtual TInt DoDiscardPages0(SPageInfo** aPageList) = 0;
153 Final stage in discarding a list of page
154 Finish discarding the pages previously removed by RamCacheBase::DoDiscardPages0().
156 @param aPageList A NULL terminated list of the pages to be discarded
157 @return KErrNone on success.
159 @pre System lock held
160 @post System lock held
162 virtual TInt DoDiscardPages1(SPageInfo** aPageList) = 0;
166 Flush (unmap) all the free memory which is currently cached.
168 virtual void FlushAll() = 0;
171 Attempt to allocate the required contiguous region of pages, freeing
172 RAM cache pages if required.
174 @param aNumPages The number of pages to free up.
175 @param aAlign The alignment of the region to free-up, (a power-of-2).
176 @param aBlockZoneId The ID of a zone that shouldn't be allocated into, when set to
177 KRamZoneInvalidId it will have no effect.
178 @param aBlockRest Set to ETrue to stop allocation as soon as aBlockedZoneId is reached
179 in preference ordering. EFalse otherwise.
181 @return KErrNone on success, KErrNoMemory otherwise
182 @pre RamAlloc mutex held.
184 TInt AllocFreeContiguousPages(TInt aNumPages, TInt aAlign, TZonePageType aType, TPhysAddr& aPhysAddr, TUint aBlockedZoneId, TBool aBlockRest);
187 Return the maximum number of RAM pages which could be obtained with #GetFreePages.
188 This value is used in the calculation of the 'free' RAM in the system.
190 @return Number of free RAM pages.
192 inline TInt NumberOfFreePages() { return iNumberOfFreePages; }
195 Put a page back on the system's free pool.
197 @pre RamAlloc mutex held.
199 void ReturnToSystem(SPageInfo* aPageInfo);
202 Get a RAM page from the system's free pool.
204 @param aBlockZoneId The ID of a zone that shouldn't be allocated into, when set to
205 KRamZoneInvalidId it will have no effect.
206 @param aBlockRest Set to ETrue to stop allocation as soon as aBlockedZoneId is reached
207 in preference ordering. EFalse otherwise.
209 @pre RamAlloc mutex held.
211 @return The page or NULL if no page is available.
213 SPageInfo* GetPageFromSystem(TUint aBlockedZone=KRamZoneInvalidId, TBool aBlockRest=EFalse);
218 MmuBase* iMmu; /**< Copy of MmuBase::TheMmu */
219 TInt iNumberOfFreePages; /**< Number of pages which could be freed by GetFreePages.
220 This value is protected by the RamAlloc mutex. */
221 static RamCacheBase* TheRamCache;
225 class RamCache : public RamCacheBase
229 virtual void Init2();
230 virtual TInt Init3();
231 virtual TBool GetFreePages(TInt aNumPages);
232 virtual void DonateRamCachePage(SPageInfo* aPageInfo);
233 virtual TBool ReclaimRamCachePage(SPageInfo* aPageInfo);
234 virtual TBool PageUnmapped(SPageInfo* aPageInfo);
235 virtual TBool IsPageDiscardable(SPageInfo& aPageInfo);
236 virtual TBool DoDiscardPage(SPageInfo& aPageInfo, TUint aBlockZoneId, TBool aBlockRest);
237 virtual TInt DoDiscardPages0(SPageInfo** aPageList);
238 virtual TInt DoDiscardPages1(SPageInfo** aPageList);
239 virtual void FlushAll();
241 virtual void SetFree(SPageInfo* aPageInfo);
244 EUnexpectedPageType = 3, /**< A page in the live page list had an unexpected type (SPageInfo::Attribs) */
246 static void Panic(TFault aFault);
247 void RemovePage(SPageInfo& aPageInfo);