epoc32/include/mw/MemoryManager.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
parent 2 2fe1408b6811
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
     1 /*
     2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     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".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description:  
    15 *
    16 */
    17 #ifndef _MEMORYMANAGER_H_
    18 #define _MEMORYMANAGER_H_
    19 
    20 //  INCLUDES
    21 #include <e32std.h>
    22 #include <e32base.h>
    23 
    24 // CONSTANTS
    25 
    26 // MACROS
    27 //
    28 
    29 // DATA TYPES
    30 enum TOOMCheckResult
    31     {
    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 
    38     };
    39 
    40 // simulate OOM, for debugging purpose
    41 enum TOOMType
    42     {
    43     EOOM_None   =   0x0000,
    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
    47     };
    48 
    49 enum TOOMPriority
    50     {
    51     EOOM_PriorityLow    = 0,
    52     EOOM_PriorityMiddle,
    53     EOOM_PriorityHigh,
    54     EOOM_PriorityCritical
    55     };
    56 
    57 // FUNCTION PROTOTYPES
    58 
    59 // FORWARD DECLARATIONS
    60 class CAllocator;
    61 class FunctionLogger;
    62 
    63 // CLASS DECLARATION
    64 
    65 /**
    66 *
    67 *  @lib memman.lib
    68 *  @since 3.1
    69 */
    70 class MMemoryCollector
    71     {
    72     public: // New functions
    73         /**
    74         * Collect free memory, this function is called when allocation from 
    75         * System heap fails
    76         * @since 3.1
    77         * @param amount of memory needs to be collected
    78         * @return amount of memory collected
    79         */
    80         virtual TUint Collect(TUint aRequired) = 0;
    81         
    82         /**
    83         * restore the entity controlled memory collector when there is enough memory
    84         * System heap fails
    85         * @since 3.1
    86         * @param
    87         * @return
    88         */
    89         virtual void Restore() = 0;
    90         
    91         /**
    92         * Priority of this collector, 0 - lowest, 10 - highest;
    93         * the lower the priority, the earlier this collector is executed.
    94         * @since 3.1
    95         * @param
    96         * @return
    97         */
    98         virtual TOOMPriority Priority() = 0;
    99     };
   100 
   101 /**
   102 *
   103 *  @lib memman.lib
   104 *  @since 3.1
   105 */
   106 class MOOMStopper
   107     {
   108     public:  // New functions
   109         /**
   110         * Stop currently on-going operations, called by the allocator
   111         * @since 3.1
   112         * @param
   113         * @return
   114         */  
   115         virtual void Stop() = 0;
   116         
   117         /**
   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.
   121         * @since 3.1
   122         * @param 
   123         * @return
   124         */  
   125         virtual TOOMPriority Priority() = 0;
   126     };
   127 
   128 /**
   129 *
   130 *  @lib memman.lib
   131 *  @since 3.1
   132 */
   133 class MOOMNotifier
   134     {
   135     public: // New functions
   136         /**
   137         * client provided notifying utility function, called by the allocator 
   138         * to inform the user
   139         * @since 3.1
   140         * @param
   141         * @return
   142         */
   143         virtual TInt Notify() = 0;
   144     };
   145 
   146 /**
   147 *  A class handles all memory operations and OOM issues
   148 *  @lib memman.lib
   149 *  @since 3.1
   150 */
   151 class MemoryManager
   152     {
   153     public:           
   154         /**
   155         * register a memory collector, memory manager doesn't own this collector
   156         * @since 3.1
   157         * @param aCollector the collector to be registered
   158         * @param 
   159         * @return 
   160         */
   161         IMPORT_C static void AddCollector( MMemoryCollector* aCollector );
   162     
   163         /**
   164         * unregister a memory collector
   165         * @since 3.1
   166         * @param aCollector the collector to be unregistered
   167         * @param 
   168         * @return 
   169         */
   170         IMPORT_C static void RemoveCollector( MMemoryCollector* aCollector );
   171         
   172         /**
   173         * register a stopper, memory manager doesn't own this stopper.
   174         * @since 3.1
   175         * @param aStopper the stopper to be registered
   176         * @param 
   177         * @return 
   178         */
   179         IMPORT_C static void AddStopper( MOOMStopper* aStopper );
   180     
   181         /**
   182         * unregister a stopper
   183         * @since 3.1
   184         * @param aStopper the stopper to be unregistered
   185         * @param 
   186         * @return 
   187         */
   188         IMPORT_C static void RemoveStopper( MOOMStopper* aStopper );
   189         
   190         /**
   191         * set the OOM notifier, memory manager doesn't own this notifier
   192         * @since 3.1
   193         * @param aNotifier the notifier
   194         * @param 
   195         * @return 
   196         */
   197         IMPORT_C static void SetNotifier( MOOMNotifier* aNotifier );
   198             
   199         /**
   200         * check if the memory manager is able to reserve enough memory for the coming operation.
   201         * @since 3.1
   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
   206         */
   207         IMPORT_C static TBool PreCheck( TUint aTotalSize, TUint aMaxBufSize, const TDesC8& aChecker=KNullDesC8 );
   208     
   209         /**
   210         * An additional check after an operation is completed. 
   211         * @since 3.1
   212         * @param 
   213         * @param
   214         * @return the status of memory during this operation.
   215         */
   216         IMPORT_C static TUint PostCheck();
   217     
   218         /**
   219         * Get the status of memory manager, specific state could be retrieve by AND (&) operator
   220         * with TOOMCheckResult enum
   221         * @since 3.1
   222         * @param 
   223         * @param
   224         * @return the status of memory during this operation.
   225         */
   226         IMPORT_C static TUint Status();
   227                 
   228         /**
   229         * Trigger an OOM event, this function is only for debugging purpose
   230         * @since 3.1
   231         * @param aType defines where memory allocation fails
   232         * @param
   233         * @return
   234         */
   235         IMPORT_C static void SetStatus( TOOMCheckResult aType = ENoOOM );
   236 
   237         /**
   238         * Prepare Memory manager for the coming application exit.
   239         * @since 3.1
   240         * @param
   241         * @param
   242         * @return
   243         */
   244         IMPORT_C static void PrepareForExit();
   245 
   246         /**
   247         * Free some RAM. Calls collect on registered collectors
   248         * @since 3.1
   249         * @param
   250         * @param
   251         * @return
   252         */
   253         IMPORT_C static void FreeRam();
   254 
   255         /**
   256         * Called when application is no longer in OOM situation.
   257         * @since 3.1
   258         * @param
   259         * @param
   260         * @return
   261         */
   262         IMPORT_C static void RestoreCollectors();
   263 
   264         /**
   265         * Called when application wants to change the rescue buffer size
   266         * @since 3.1
   267         * @param
   268         * @param
   269         * @return
   270         */
   271         IMPORT_C static void SetRescueBufferSize(TInt aSize);
   272 
   273         /**
   274         * retrieve the size of an allocated memory cell
   275         * @since 3.1
   276         * @param
   277         * @param
   278         * @return
   279         */
   280         IMPORT_C static TUint MemorySize(TAny* aPtr);
   281 
   282         /**
   283         * switch to the allocator using fast dlmalloc
   284         * @since 3.1
   285         * @param
   286         * @param
   287         * @return
   288         */
   289         IMPORT_C static RAllocator* SwitchToFastAllocator();
   290 
   291         /**
   292         * switch to the default allocator provided by Symbian OS,
   293         * and close the current fast allocator.
   294         * @since 3.1
   295         * @param
   296         * @param
   297         * @return
   298         */
   299         IMPORT_C static void CloseFastAllocator(RAllocator* aDefaultAllocator);
   300 
   301         /**
   302         * Create fast allocator and switch as default heap. Special case if fast allocator
   303         * has to be created from SetupThreadHeap().
   304         * 
   305         * @since 9.2
   306         * @param
   307         * @return
   308         */        
   309         IMPORT_C static void MemoryManager::CreateFastAllocator();
   310 
   311         /**
   312         * Initialize MemoryManager library. Special case if CreateFastAllocator is
   313         * called from SetupThreadHeap().
   314         * 
   315         * @since 9.2
   316         * @param
   317         * @return
   318         */
   319         IMPORT_C static void MemoryManager::InitFastAllocator();
   320 
   321         /**
   322         * initialize the OOM handler in the memorypool
   323         * @since 9.2
   324         * @param
   325         * @param
   326         * @return
   327         */
   328         IMPORT_C static void InitOOMDialog();
   329 
   330         /**
   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
   334         * @since 9.2
   335         * @param
   336         * @param
   337         * @return
   338         */
   339         IMPORT_C static void ResetOOMDialogDisplayed();
   340         
   341     };
   342 
   343 #endif// !_MEMORYMANAGER_H_