2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
17 #ifndef _MEMORYMANAGER_H_
18 #define _MEMORYMANAGER_H_
32 ENoOOM = 0x0000, // no oom
33 ECheckOOM = 0x0001, // last memory check failed
34 EUserAllocOOM = 0x0002, // system heap allocation failed
35 ERescueOOM = 0x0004, // rescue buffer allocation failed
36 ECollecting = 0x0010, // memory manager is collecting memory
37 EStopping = 0x0020 // memory menager is stopping operations
40 // simulate OOM, for debugging purpose
44 EOOM_Heap1 = 0x0001, // fail the first heap allocation
45 EOOM_Heap2 = 0x0003, // fail the second heap allocation, after collection
46 EOOM_Rescue = 0x0007 // fail the rescue allocation
57 // FUNCTION PROTOTYPES
59 // FORWARD DECLARATIONS
70 class MMemoryCollector
72 public: // New functions
74 * Collect free memory, this function is called when allocation from
77 * @param amount of memory needs to be collected
78 * @return amount of memory collected
80 virtual TUint Collect(TUint aRequired) = 0;
83 * restore the entity controlled memory collector when there is enough memory
89 virtual void Restore() = 0;
92 * Priority of this collector, 0 - lowest, 10 - highest;
93 * the lower the priority, the earlier this collector is executed.
98 virtual TOOMPriority Priority() = 0;
108 public: // New functions
110 * Stop currently on-going operations, called by the allocator
115 virtual void Stop() = 0;
118 * return the priority of this stopper (1-100), allocator expects this value
119 * to determine the stopping order. The higher the priority is, the sooner
120 * this stopper will be called.
125 virtual TOOMPriority Priority() = 0;
135 public: // New functions
137 * client provided notifying utility function, called by the allocator
143 virtual TInt Notify() = 0;
147 * A class handles all memory operations and OOM issues
155 * register a memory collector, memory manager doesn't own this collector
157 * @param aCollector the collector to be registered
161 IMPORT_C static void AddCollector( MMemoryCollector* aCollector );
164 * unregister a memory collector
166 * @param aCollector the collector to be unregistered
170 IMPORT_C static void RemoveCollector( MMemoryCollector* aCollector );
173 * register a stopper, memory manager doesn't own this stopper.
175 * @param aStopper the stopper to be registered
179 IMPORT_C static void AddStopper( MOOMStopper* aStopper );
182 * unregister a stopper
184 * @param aStopper the stopper to be unregistered
188 IMPORT_C static void RemoveStopper( MOOMStopper* aStopper );
191 * set the OOM notifier, memory manager doesn't own this notifier
193 * @param aNotifier the notifier
197 IMPORT_C static void SetNotifier( MOOMNotifier* aNotifier );
200 * check if the memory manager is able to reserve enough memory for the coming operation.
202 * @param aTotalSize total amount of memory
203 * @param aMaxBufSizse the biggest contiguous memory buffer
204 * @param aChecker the name of operation
205 * @return result of prechecking, ETrue = successful
207 IMPORT_C static TBool PreCheck( TUint aTotalSize, TUint aMaxBufSize, const TDesC8& aChecker=KNullDesC8 );
210 * An additional check after an operation is completed.
214 * @return the status of memory during this operation.
216 IMPORT_C static TUint PostCheck();
219 * Get the status of memory manager, specific state could be retrieve by AND (&) operator
220 * with TOOMCheckResult enum
224 * @return the status of memory during this operation.
226 IMPORT_C static TUint Status();
229 * Trigger an OOM event, this function is only for debugging purpose
231 * @param aType defines where memory allocation fails
235 IMPORT_C static void SetStatus( TOOMCheckResult aType = ENoOOM );
238 * Prepare Memory manager for the coming application exit.
244 IMPORT_C static void PrepareForExit();
247 * Free some RAM. Calls collect on registered collectors
253 IMPORT_C static void FreeRam();
256 * Called when application is no longer in OOM situation.
262 IMPORT_C static void RestoreCollectors();
265 * Called when application wants to change the rescue buffer size
271 IMPORT_C static void SetRescueBufferSize(TInt aSize);
274 * retrieve the size of an allocated memory cell
280 IMPORT_C static TUint MemorySize(TAny* aPtr);
283 * switch to the allocator using fast dlmalloc
289 IMPORT_C static RAllocator* SwitchToFastAllocator();
292 * switch to the default allocator provided by Symbian OS,
293 * and close the current fast allocator.
299 IMPORT_C static void CloseFastAllocator(RAllocator* aDefaultAllocator);
302 * Create fast allocator and switch as default heap. Special case if fast allocator
303 * has to be created from SetupThreadHeap().
309 IMPORT_C static void MemoryManager::CreateFastAllocator();
312 * Initialize MemoryManager library. Special case if CreateFastAllocator is
313 * called from SetupThreadHeap().
319 IMPORT_C static void MemoryManager::InitFastAllocator();
322 * initialize the OOM handler in the memorypool
328 IMPORT_C static void InitOOMDialog();
331 * Reset the OOM dialog display flag in the memorypool; we want to pop the OOM dialog once per page,
332 * so we need to clear this when we are either done with the page (it unloads) or when we load a new one,
333 * so that it will display again when we run out of memory
339 IMPORT_C static void ResetOOMDialogDisplayed();
343 #endif// !_MEMORYMANAGER_H_