|
1 /* |
|
2 * Copyright (c) 2009 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 "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 * Helper system for handling Out-Of-Memory cases. |
|
16 * |
|
17 */ |
|
18 |
|
19 // System includes |
|
20 #include <hal.h> |
|
21 #include <e32std.h> |
|
22 #include <e32cmn.h> |
|
23 #include <aknnotewrappers.h> |
|
24 #include <e32svr.h> |
|
25 #include <StringLoader.h> |
|
26 #include <xnuiengine.rsg> |
|
27 |
|
28 // User includes |
|
29 #include "xnoomsyshandler.h" |
|
30 #include "xnpanic.h" |
|
31 |
|
32 //Constants |
|
33 const TInt64 KMinInterval = 5000000; // 5 seconds |
|
34 |
|
35 // ======== MEMBER FUNCTIONS ======== |
|
36 |
|
37 // --------------------------------------------------------------------------- |
|
38 // Two-phased constructor. |
|
39 // --------------------------------------------------------------------------- |
|
40 // |
|
41 CXnOomSysHandler* CXnOomSysHandler::NewL() |
|
42 { |
|
43 CXnOomSysHandler* self = new ( ELeave ) CXnOomSysHandler(); |
|
44 CleanupStack::PushL( self ); |
|
45 self->ConstructL(); |
|
46 CleanupStack::Pop(); |
|
47 return self; |
|
48 } |
|
49 |
|
50 // --------------------------------------------------------------------------- |
|
51 // Destructor. |
|
52 // --------------------------------------------------------------------------- |
|
53 // |
|
54 CXnOomSysHandler::~CXnOomSysHandler() |
|
55 { |
|
56 delete iMsg; |
|
57 iMsg = NULL; |
|
58 } |
|
59 |
|
60 // ----------------------------------------------------------------------------- |
|
61 // Checks if required amount of memory is available on heap |
|
62 // ----------------------------------------------------------------------------- |
|
63 // |
|
64 TBool CXnOomSysHandler::HeapAvailable( TInt aAmount ) |
|
65 { |
|
66 if( aAmount == EMinNotDef ) |
|
67 { |
|
68 aAmount = EMem512kB; |
|
69 } |
|
70 |
|
71 // Gather needed data... |
|
72 |
|
73 // Space already allocated. |
|
74 TInt allocated; |
|
75 User::Heap().AllocSize( allocated ); |
|
76 |
|
77 // Max space for heap. Heap can extend only up to this value. |
|
78 const TInt heapMax = User::Heap().MaxLength(); |
|
79 |
|
80 // Available memory in heap's current state. |
|
81 TInt obsolete = 0; |
|
82 const TInt availableHeap = User::Heap().Available( obsolete ); |
|
83 |
|
84 // Current heap size. |
|
85 const TInt heapSize = allocated + availableHeap; |
|
86 |
|
87 // Heap extension requirement. Can be negative. |
|
88 const TInt extensionRequirement = aAmount - availableHeap; |
|
89 |
|
90 // Max. heap extension possible limited by heap max. |
|
91 const TInt heapExtensionMax = heapMax - heapSize; |
|
92 |
|
93 // System free ram. |
|
94 TInt ramFree = 0; |
|
95 HAL::Get( HALData::EMemoryRAMFree, ramFree ); |
|
96 |
|
97 // Checking... |
|
98 |
|
99 // Fits into current heap allocation. |
|
100 if( availableHeap >= aAmount ) |
|
101 { |
|
102 return ETrue; |
|
103 } |
|
104 |
|
105 // Fits if current heap extends. heap size and therefore |
|
106 // it's possibilities to extend is limited by max heap |
|
107 // size and by available system memory. |
|
108 if( ( heapExtensionMax ) >= extensionRequirement && |
|
109 ( ramFree >= extensionRequirement ) ) |
|
110 { |
|
111 return ETrue; |
|
112 } |
|
113 |
|
114 // Does not fit. |
|
115 return EFalse; |
|
116 } |
|
117 |
|
118 // ----------------------------------------------------------------------------- |
|
119 // Handle potential OOM failure. |
|
120 // ----------------------------------------------------------------------------- |
|
121 // |
|
122 void CXnOomSysHandler::HandlePotentialOomL() |
|
123 { |
|
124 if( AllowDialog() ) |
|
125 { |
|
126 CAknErrorNote* dialog = new (ELeave) CAknErrorNote(); |
|
127 dialog->ExecuteLD( *iMsg ); |
|
128 dialog = NULL; |
|
129 } |
|
130 } |
|
131 |
|
132 // --------------------------------------------------------------------------- |
|
133 // Symbian 2nd phase constructor. |
|
134 // --------------------------------------------------------------------------- |
|
135 // |
|
136 void CXnOomSysHandler::ConstructL() |
|
137 { |
|
138 iMsg = StringLoader::LoadL( R_QTN_HS_HS_MEMORY_FULL ); |
|
139 } |
|
140 |
|
141 // --------------------------------------------------------------------------- |
|
142 // C++ constructor. |
|
143 // --------------------------------------------------------------------------- |
|
144 // |
|
145 CXnOomSysHandler::CXnOomSysHandler() |
|
146 { |
|
147 iPreviousDialogPopupTime = 0; |
|
148 } |
|
149 |
|
150 //------------------------------------------------------------------------------ |
|
151 // Check whether OOM dialog can be shown. |
|
152 // Prevents OOM dialogs popping up more often than |
|
153 // defined in KMinInterval. |
|
154 // ----------------------------------------------------------------------------- |
|
155 // |
|
156 TBool CXnOomSysHandler::AllowDialog() |
|
157 { |
|
158 TTime now; |
|
159 now.HomeTime(); |
|
160 |
|
161 TTimeIntervalMicroSeconds interval |
|
162 = now.MicroSecondsFrom( iPreviousDialogPopupTime ); |
|
163 |
|
164 if( iPreviousDialogPopupTime == 0 || // Not set yet. |
|
165 interval.Int64() >= KMinInterval ) // Proper interval passed. |
|
166 { |
|
167 iPreviousDialogPopupTime = now.Int64(); |
|
168 return ETrue; |
|
169 } |
|
170 else |
|
171 { |
|
172 return EFalse; |
|
173 } |
|
174 } |