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