williamr@2: /* williamr@2: * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). williamr@2: * All rights reserved. williamr@2: * This component and the accompanying materials are made available williamr@2: * under the terms of the License "Eclipse Public License v1.0" williamr@2: * which accompanies this distribution, and is available williamr@2: * at the URL "http://www.eclipse.org/legal/epl-v10.html". williamr@2: * williamr@2: * Initial Contributors: williamr@2: * Nokia Corporation - initial contribution. williamr@2: * williamr@2: * Contributors: williamr@2: * williamr@2: * Description: williamr@2: * williamr@2: */ williamr@2: #ifndef _MEMORYMANAGER_H_ williamr@2: #define _MEMORYMANAGER_H_ williamr@2: williamr@2: // INCLUDES williamr@2: #include williamr@2: #include williamr@2: williamr@2: // CONSTANTS williamr@2: williamr@2: // MACROS williamr@2: // williamr@2: williamr@2: // DATA TYPES williamr@2: enum TOOMCheckResult williamr@2: { williamr@2: ENoOOM = 0x0000, // no oom williamr@2: ECheckOOM = 0x0001, // last memory check failed williamr@2: EUserAllocOOM = 0x0002, // system heap allocation failed williamr@2: ERescueOOM = 0x0004, // rescue buffer allocation failed williamr@2: ECollecting = 0x0010, // memory manager is collecting memory williamr@2: EStopping = 0x0020 // memory menager is stopping operations williamr@2: }; williamr@2: williamr@2: // simulate OOM, for debugging purpose williamr@2: enum TOOMType williamr@2: { williamr@2: EOOM_None = 0x0000, williamr@2: EOOM_Heap1 = 0x0001, // fail the first heap allocation williamr@2: EOOM_Heap2 = 0x0003, // fail the second heap allocation, after collection williamr@2: EOOM_Rescue = 0x0007 // fail the rescue allocation williamr@2: }; williamr@2: williamr@2: enum TOOMPriority williamr@2: { williamr@2: EOOM_PriorityLow = 0, williamr@2: EOOM_PriorityMiddle, williamr@2: EOOM_PriorityHigh, williamr@2: EOOM_PriorityCritical williamr@2: }; williamr@2: williamr@2: // FUNCTION PROTOTYPES williamr@2: williamr@2: // FORWARD DECLARATIONS williamr@2: class CAllocator; williamr@2: class FunctionLogger; williamr@2: williamr@2: // CLASS DECLARATION williamr@2: williamr@2: /** williamr@2: * williamr@2: * @lib memman.lib williamr@2: * @since 3.1 williamr@2: */ williamr@2: class MMemoryCollector williamr@2: { williamr@2: public: // New functions williamr@2: /** williamr@2: * Collect free memory, this function is called when allocation from williamr@2: * System heap fails williamr@2: * @since 3.1 williamr@2: * @param amount of memory needs to be collected williamr@2: * @return amount of memory collected williamr@2: */ williamr@2: virtual TUint Collect(TUint aRequired) = 0; williamr@2: williamr@2: /** williamr@2: * restore the entity controlled memory collector when there is enough memory williamr@2: * System heap fails williamr@2: * @since 3.1 williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: virtual void Restore() = 0; williamr@2: williamr@2: /** williamr@2: * Priority of this collector, 0 - lowest, 10 - highest; williamr@2: * the lower the priority, the earlier this collector is executed. williamr@2: * @since 3.1 williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: virtual TOOMPriority Priority() = 0; williamr@2: }; williamr@2: williamr@2: /** williamr@2: * williamr@2: * @lib memman.lib williamr@2: * @since 3.1 williamr@2: */ williamr@2: class MOOMStopper williamr@2: { williamr@2: public: // New functions williamr@2: /** williamr@2: * Stop currently on-going operations, called by the allocator williamr@2: * @since 3.1 williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: virtual void Stop() = 0; williamr@2: williamr@2: /** williamr@2: * return the priority of this stopper (1-100), allocator expects this value williamr@2: * to determine the stopping order. The higher the priority is, the sooner williamr@2: * this stopper will be called. williamr@2: * @since 3.1 williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: virtual TOOMPriority Priority() = 0; williamr@2: }; williamr@2: williamr@2: /** williamr@2: * williamr@2: * @lib memman.lib williamr@2: * @since 3.1 williamr@2: */ williamr@2: class MOOMNotifier williamr@2: { williamr@2: public: // New functions williamr@2: /** williamr@2: * client provided notifying utility function, called by the allocator williamr@2: * to inform the user williamr@2: * @since 3.1 williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: virtual TInt Notify() = 0; williamr@2: }; williamr@2: williamr@2: /** williamr@2: * A class handles all memory operations and OOM issues williamr@2: * @lib memman.lib williamr@2: * @since 3.1 williamr@2: */ williamr@2: class MemoryManager williamr@2: { williamr@2: public: williamr@2: /** williamr@2: * register a memory collector, memory manager doesn't own this collector williamr@2: * @since 3.1 williamr@2: * @param aCollector the collector to be registered williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: IMPORT_C static void AddCollector( MMemoryCollector* aCollector ); williamr@2: williamr@2: /** williamr@2: * unregister a memory collector williamr@2: * @since 3.1 williamr@2: * @param aCollector the collector to be unregistered williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: IMPORT_C static void RemoveCollector( MMemoryCollector* aCollector ); williamr@2: williamr@2: /** williamr@2: * register a stopper, memory manager doesn't own this stopper. williamr@2: * @since 3.1 williamr@2: * @param aStopper the stopper to be registered williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: IMPORT_C static void AddStopper( MOOMStopper* aStopper ); williamr@2: williamr@2: /** williamr@2: * unregister a stopper williamr@2: * @since 3.1 williamr@2: * @param aStopper the stopper to be unregistered williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: IMPORT_C static void RemoveStopper( MOOMStopper* aStopper ); williamr@2: williamr@2: /** williamr@2: * set the OOM notifier, memory manager doesn't own this notifier williamr@2: * @since 3.1 williamr@2: * @param aNotifier the notifier williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: IMPORT_C static void SetNotifier( MOOMNotifier* aNotifier ); williamr@2: williamr@2: /** williamr@2: * check if the memory manager is able to reserve enough memory for the coming operation. williamr@2: * @since 3.1 williamr@2: * @param aTotalSize total amount of memory williamr@2: * @param aMaxBufSizse the biggest contiguous memory buffer williamr@2: * @param aChecker the name of operation williamr@2: * @return result of prechecking, ETrue = successful williamr@2: */ williamr@2: IMPORT_C static TBool PreCheck( TUint aTotalSize, TUint aMaxBufSize, const TDesC8& aChecker=KNullDesC8 ); williamr@2: williamr@2: /** williamr@2: * An additional check after an operation is completed. williamr@2: * @since 3.1 williamr@2: * @param williamr@2: * @param williamr@2: * @return the status of memory during this operation. williamr@2: */ williamr@2: IMPORT_C static TUint PostCheck(); williamr@2: williamr@2: /** williamr@2: * Get the status of memory manager, specific state could be retrieve by AND (&) operator williamr@2: * with TOOMCheckResult enum williamr@2: * @since 3.1 williamr@2: * @param williamr@2: * @param williamr@2: * @return the status of memory during this operation. williamr@2: */ williamr@2: IMPORT_C static TUint Status(); williamr@2: williamr@2: /** williamr@2: * Trigger an OOM event, this function is only for debugging purpose williamr@2: * @since 3.1 williamr@2: * @param aType defines where memory allocation fails williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: IMPORT_C static void SetStatus( TOOMCheckResult aType = ENoOOM ); williamr@2: williamr@2: /** williamr@2: * Prepare Memory manager for the coming application exit. williamr@2: * @since 3.1 williamr@2: * @param williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: IMPORT_C static void PrepareForExit(); williamr@2: williamr@2: /** williamr@2: * Free some RAM. Calls collect on registered collectors williamr@2: * @since 3.1 williamr@2: * @param williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: IMPORT_C static void FreeRam(); williamr@2: williamr@2: /** williamr@2: * Called when application is no longer in OOM situation. williamr@2: * @since 3.1 williamr@2: * @param williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: IMPORT_C static void RestoreCollectors(); williamr@2: williamr@2: /** williamr@2: * Called when application wants to change the rescue buffer size williamr@2: * @since 3.1 williamr@2: * @param williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: IMPORT_C static void SetRescueBufferSize(TInt aSize); williamr@2: williamr@2: /** williamr@2: * retrieve the size of an allocated memory cell williamr@2: * @since 3.1 williamr@2: * @param williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: IMPORT_C static TUint MemorySize(TAny* aPtr); williamr@2: williamr@2: /** williamr@2: * switch to the allocator using fast dlmalloc williamr@2: * @since 3.1 williamr@2: * @param williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: IMPORT_C static RAllocator* SwitchToFastAllocator(); williamr@2: williamr@2: /** williamr@2: * switch to the default allocator provided by Symbian OS, williamr@2: * and close the current fast allocator. williamr@2: * @since 3.1 williamr@2: * @param williamr@2: * @param williamr@2: * @return williamr@2: */ williamr@2: IMPORT_C static void CloseFastAllocator(RAllocator* aDefaultAllocator); williamr@4: williamr@4: /** williamr@4: * Create fast allocator and switch as default heap. Special case if fast allocator williamr@4: * has to be created from SetupThreadHeap(). williamr@4: * williamr@4: * @since 9.2 williamr@4: * @param williamr@4: * @return williamr@4: */ williamr@4: IMPORT_C static void MemoryManager::CreateFastAllocator(); williamr@4: williamr@4: /** williamr@4: * Initialize MemoryManager library. Special case if CreateFastAllocator is williamr@4: * called from SetupThreadHeap(). williamr@4: * williamr@4: * @since 9.2 williamr@4: * @param williamr@4: * @return williamr@4: */ williamr@4: IMPORT_C static void MemoryManager::InitFastAllocator(); williamr@4: williamr@4: /** williamr@4: * initialize the OOM handler in the memorypool williamr@4: * @since 9.2 williamr@4: * @param williamr@4: * @param williamr@4: * @return williamr@4: */ williamr@4: IMPORT_C static void InitOOMDialog(); williamr@4: williamr@4: /** williamr@4: * Reset the OOM dialog display flag in the memorypool; we want to pop the OOM dialog once per page, williamr@4: * so we need to clear this when we are either done with the page (it unloads) or when we load a new one, williamr@4: * so that it will display again when we run out of memory williamr@4: * @since 9.2 williamr@4: * @param williamr@4: * @param williamr@4: * @return williamr@4: */ williamr@4: IMPORT_C static void ResetOOMDialogDisplayed(); williamr@4: williamr@2: }; williamr@2: williamr@2: #endif// !_MEMORYMANAGER_H_