0
|
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 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 |
|
|
18 |
|
|
19 |
|
|
20 |
#include <platform.h> // For DPlatChunkHw
|
|
21 |
#include <nk_priv.h> // For __ASSERT_NO_FAST_MUTEX (published to partners)
|
|
22 |
|
|
23 |
#include "memmanager.h" // For MemManager
|
|
24 |
#include "memmanagertrace.h" // For C_TRACE...
|
|
25 |
|
|
26 |
DMemManager* DMemManager::iThisptr = NULL;
|
|
27 |
TDfcQue* DMemManager::iDfcQueue = NULL;
|
|
28 |
|
|
29 |
|
|
30 |
// Memory management fault enumerations
|
|
31 |
enum TMemFault
|
|
32 |
{
|
|
33 |
EMemBlockAllocationFailed = 0,
|
|
34 |
EMemBlockSizeZero,
|
|
35 |
EExtensionMemoryAllocationFailed,
|
|
36 |
EDfcCreateFailed,
|
|
37 |
EDfcQueueCreateFailed,
|
|
38 |
EInvalidParameter,
|
|
39 |
EPhysicalMemReleaseFailed,
|
|
40 |
EMemoryAllocationFailed,
|
|
41 |
EHWMemAllocFailed,
|
|
42 |
EPhysicalMemAllocFailed,
|
|
43 |
EInvalidQueueCount,
|
|
44 |
EAllocNotThreadContext,
|
|
45 |
EDeallocNotThreadContext,
|
|
46 |
EMemBlockInvalidReleaseDetected
|
|
47 |
};
|
|
48 |
|
|
49 |
/*
|
|
50 |
* Constructor.
|
|
51 |
*/
|
|
52 |
DMemManager::DMemManager()
|
|
53 |
{
|
|
54 |
|
|
55 |
C_TRACE( ( _T( "DMemManager::DMemManager>" ) ) );
|
|
56 |
|
|
57 |
iFastMutex = new NFastMutex();
|
|
58 |
ASSERT_RESET_ALWAYS( iFastMutex, ( EMemoryAllocationFailed | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
59 |
|
|
60 |
//Priority must be larger than the threads calling alloc and dealloc
|
|
61 |
Kern::DfcQCreate( iDfcQueue, 28, &KMemManagerDfcQThreadName );
|
|
62 |
ASSERT_RESET_ALWAYS( ( iDfcQueue ), ( EDfcQueueCreateFailed | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
63 |
|
|
64 |
iPoolAllocateDfc = new TDfc( PoolAllocateDfc, this, iDfcQueue, KPoolAllocateDfcPriority );
|
|
65 |
ASSERT_RESET_ALWAYS( ( iPoolAllocateDfc ), ( EDfcCreateFailed | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
66 |
|
|
67 |
iPoolDeleteDfc = new TDfc( PoolDeleteDfc, this, iDfcQueue , KPoolDeleteDfcPriority );
|
|
68 |
ASSERT_RESET_ALWAYS( ( iPoolDeleteDfc ), ( EDfcCreateFailed | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
69 |
|
|
70 |
//Static array configuration
|
|
71 |
iMemPond.Append( new DMemPool( 16, 384 ) );
|
|
72 |
iMemPond.Append( new DMemPool( 32, 128 ) );
|
|
73 |
iMemPond.Append( new DMemPool( 64, 64 ) );
|
|
74 |
iMemPond.Append( new DMemPool( 128, 80 ) );
|
|
75 |
iMemPond.Append( new DMemPool( 256, 60 ) );
|
|
76 |
iMemPond.Append( new DMemPool( 2048, 100 ) );
|
|
77 |
iMemPond.Append( new DMemPool( 4096, 100 ) );
|
|
78 |
iMemPond.Append( new DMemPool( 65524, 4 ) );
|
|
79 |
|
|
80 |
C_TRACE( ( _T( "DMemManager::DMemManager<" ) ) );
|
|
81 |
}
|
|
82 |
|
|
83 |
/*
|
|
84 |
* Destructor.
|
|
85 |
*/
|
|
86 |
DMemManager::~DMemManager()
|
|
87 |
{
|
|
88 |
C_TRACE( ( _T( "DMemManager::~DMemManager>" ) ) );
|
|
89 |
|
|
90 |
for( int i = 0; i < iMemPond.Count(); i++ )
|
|
91 |
{
|
|
92 |
delete iMemPond[i];
|
|
93 |
}
|
|
94 |
|
|
95 |
iMemPond.Reset();
|
|
96 |
|
|
97 |
for( int i = 0; i < iPoolCreateQueue.Count(); i++ )
|
|
98 |
{
|
|
99 |
delete iPoolCreateQueue[i];
|
|
100 |
}
|
|
101 |
|
|
102 |
iPoolCreateQueue.Reset();
|
|
103 |
|
|
104 |
for( TInt i = 0; i < iPoolDeleteQueue.Count(); i++ )
|
|
105 |
{
|
|
106 |
delete iPoolDeleteQueue[i];
|
|
107 |
}
|
|
108 |
|
|
109 |
iPoolDeleteQueue.Reset();
|
|
110 |
|
|
111 |
delete iFastMutex;
|
|
112 |
iFastMutex = NULL;
|
|
113 |
|
|
114 |
C_TRACE( ( _T( "DMemManager::~DMemManager<" ) ) );
|
|
115 |
}
|
|
116 |
|
|
117 |
/*
|
|
118 |
* DFC for dynamic pool allocation.
|
|
119 |
*/
|
|
120 |
void DMemManager::PoolAllocateDfc(
|
|
121 |
TAny* aPtr // Pointer to this object.
|
|
122 |
)
|
|
123 |
{
|
|
124 |
C_TRACE( ( _T( "DMemManager::PoolAllocateDfc aPtr 0x%x>" ), aPtr ) );
|
|
125 |
|
|
126 |
DMemManager& tmp = *reinterpret_cast<DMemManager*>( aPtr );
|
9
|
127 |
TRACE_ASSERT_ALWAYS;
|
0
|
128 |
|
|
129 |
NKern::FMWait( tmp.iFastMutex );
|
|
130 |
|
|
131 |
ASSERT_RESET_ALWAYS( ( tmp.iPoolCreateQueue.Count() > 0 ), ( EInvalidQueueCount | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
132 |
TInt index = tmp.iMemPond.Find( tmp.iPoolCreateQueue[0] );
|
|
133 |
( tmp.iMemPond[ index ] )->iCopyPoolInUse = ETrue;
|
|
134 |
tmp.iMemPond.Insert( (new DMemPool( tmp.iPoolCreateQueue[0]->iBlockSize, tmp.iPoolCreateQueue[0]->iBlockNum ) ), index );
|
|
135 |
tmp.iPoolCreateQueue.Remove(0);
|
|
136 |
|
|
137 |
NKern::FMSignal( tmp.iFastMutex );
|
|
138 |
|
|
139 |
C_TRACE( ( _T( "DMemManager::PoolAllocateDfc<" ) ) );
|
|
140 |
}
|
|
141 |
|
|
142 |
/*
|
|
143 |
* DFC for dynamic pool deletion.
|
|
144 |
*/
|
|
145 |
void DMemManager::PoolDeleteDfc(
|
|
146 |
TAny* aPtr // Pointer to this object.
|
|
147 |
)
|
|
148 |
{
|
|
149 |
C_TRACE( ( _T( "DMemManager::PoolDeleteDfc aPtr 0x%x>" ), aPtr ) );
|
|
150 |
|
|
151 |
DMemManager& tmp = *reinterpret_cast<DMemManager*>( aPtr );
|
9
|
152 |
TRACE_ASSERT_ALWAYS;
|
0
|
153 |
NKern::FMWait( tmp.iFastMutex );
|
|
154 |
|
|
155 |
ASSERT_RESET_ALWAYS( ( tmp.iPoolDeleteQueue.Count() > 0 ), ( EInvalidQueueCount | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
156 |
delete tmp.iPoolDeleteQueue[0];
|
|
157 |
tmp.iPoolDeleteQueue.Remove(0);
|
|
158 |
|
|
159 |
NKern::FMSignal( tmp.iFastMutex );
|
|
160 |
|
|
161 |
C_TRACE( ( _T( "DMemManager::PoolDeleteDfc<" ) ) );
|
|
162 |
}
|
|
163 |
|
|
164 |
|
|
165 |
/*
|
|
166 |
* Constructor.
|
|
167 |
*/
|
|
168 |
DMemManager::DMemPool::DMemPool( const TUint16 aUnitSize, const TUint16 aUnitNum ) :
|
|
169 |
iAllocatedMemBlock(NULL), iFreeMemBlock(NULL), iMemoryArea(NULL),
|
|
170 |
iPoolSize( aUnitNum * ( aUnitSize + sizeof(struct sUnit) ) ),
|
|
171 |
iBlockSize( aUnitSize ), iBlockNum( aUnitNum ),
|
|
172 |
iBlockUsage(0), iHwChunk(NULL), iPhysicalAddress(0)
|
|
173 |
{
|
|
174 |
C_TRACE( ( _T( "DMemManager::DMemPool::DMemPool aUnitSize 0x%x, aUnitNum 0x%x>" ), aUnitSize, aUnitNum ) );
|
|
175 |
|
|
176 |
ASSERT_RESET_ALWAYS( ((aUnitSize || aUnitNum) != 0 ), ( EInvalidParameter | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
177 |
|
|
178 |
#ifndef __WINS__
|
|
179 |
TInt r = Epoc::AllocPhysicalRam( iPoolSize, iPhysicalAddress );
|
|
180 |
ASSERT_RESET_ALWAYS( ( r == KErrNone ), ( EPhysicalMemAllocFailed | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
181 |
|
|
182 |
// Create chunk as: non accessible from user side and non-cached.
|
|
183 |
|
|
184 |
r = DPlatChunkHw::New( iHwChunk, iPhysicalAddress, iPoolSize, EMapAttrSupRw | EMapAttrFullyBlocking );
|
|
185 |
ASSERT_RESET_ALWAYS( ( r == KErrNone ), ( EHWMemAllocFailed | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
186 |
|
|
187 |
iMemoryArea = (TUint8*)(iHwChunk->LinearAddress());
|
|
188 |
ASSERT_RESET_ALWAYS( ( iMemoryArea ), ( EPhysicalMemAllocFailed | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
189 |
|
|
190 |
#else
|
|
191 |
iMemoryArea = (TUint8*)Kern::Alloc( iPoolSize );
|
|
192 |
//ASSERT_RESET_ALWAYS( ( r == KErrNone ), ( EPhysicalMemAllocFailed | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
193 |
|
|
194 |
// Create chunk as: non accessible from user side and non-cached.
|
|
195 |
|
|
196 |
//iMemoryArea = (TUint8*)(iPoolSize/*->LinearAddress()*/);
|
|
197 |
ASSERT_RESET_ALWAYS( ( iMemoryArea ), ( EPhysicalMemAllocFailed | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
198 |
#endif // __WINS__
|
|
199 |
|
|
200 |
for( TUint16 i = 0; i < aUnitNum; i++ ) //Link all mem unit . Create linked list.
|
|
201 |
{
|
|
202 |
struct sUnit *pCurUnit = (struct sUnit *)( iMemoryArea + i*(aUnitSize+sizeof(struct sUnit)) );
|
|
203 |
|
|
204 |
pCurUnit->iPrev = NULL;
|
|
205 |
pCurUnit->iNext = iFreeMemBlock; //Insert the new unit at head.
|
|
206 |
|
|
207 |
if(NULL != iFreeMemBlock)
|
|
208 |
{
|
|
209 |
iFreeMemBlock->iPrev = pCurUnit;
|
|
210 |
}
|
|
211 |
|
|
212 |
pCurUnit->iMemPtr = new TPtr8( ((TUint8*)pCurUnit + sizeof(struct sUnit)) , 0, iBlockSize );
|
|
213 |
|
|
214 |
iFreeMemBlock = pCurUnit;
|
|
215 |
}
|
|
216 |
|
|
217 |
iHighWaterMark = ( KPoolHighWaterLimit * aUnitNum ) / 100;
|
|
218 |
|
|
219 |
C_TRACE( ( _T( "DMemManager::DMemPool::DMemPool<" ) ) );
|
|
220 |
}
|
|
221 |
|
|
222 |
/*
|
|
223 |
* Destructor.
|
|
224 |
*/
|
|
225 |
DMemManager::DMemPool::~DMemPool()
|
|
226 |
{
|
|
227 |
C_TRACE( ( _T( "DMemManager::DMemPool::~DMemPool>" ) ) );
|
|
228 |
|
|
229 |
for( TUint16 i = 0; i < iBlockNum; i++ )
|
|
230 |
{
|
|
231 |
struct sUnit *pCurUnit = (struct sUnit *)( iMemoryArea + i*(iBlockSize+sizeof(struct sUnit)) );
|
|
232 |
|
|
233 |
if( pCurUnit->iMemPtr )
|
|
234 |
{
|
|
235 |
delete pCurUnit->iMemPtr;
|
|
236 |
pCurUnit->iMemPtr = NULL;
|
|
237 |
}
|
|
238 |
}
|
|
239 |
#ifndef __WINS__
|
|
240 |
TInt r = Epoc::FreePhysicalRam( iPhysicalAddress, iPoolSize );
|
|
241 |
ASSERT_RESET_ALWAYS( ( r == KErrNone ), ( EPhysicalMemReleaseFailed | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
242 |
|
|
243 |
iHwChunk->Close(NULL);
|
|
244 |
|
|
245 |
iHwChunk = NULL;
|
|
246 |
#else
|
|
247 |
|
|
248 |
Kern::Free( iMemoryArea );
|
|
249 |
|
|
250 |
#endif // __WINS__
|
|
251 |
|
|
252 |
C_TRACE( ( _T( "DMemManager::DMemPool::~DMemPool<" ) ) );
|
|
253 |
}
|
|
254 |
|
|
255 |
|
|
256 |
/*
|
|
257 |
* Allocate memory unit.
|
|
258 |
*/
|
|
259 |
TPtr8* DMemManager::DMemPool::Alloc( const TUint16 aSize )
|
|
260 |
{
|
9
|
261 |
// not when FM C_TRACE( ( _T( "DMemManager::DMemPool::Alloc>" ) ) );
|
0
|
262 |
|
|
263 |
ASSERT_RESET_ALWAYS( (iMemoryArea || iFreeMemBlock), ( EMemBlockAllocationFailed | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
264 |
|
|
265 |
struct sUnit *pCurUnit = iFreeMemBlock;
|
|
266 |
|
|
267 |
iFreeMemBlock = pCurUnit->iNext;
|
|
268 |
|
|
269 |
if( iFreeMemBlock )
|
|
270 |
{
|
|
271 |
iFreeMemBlock->iPrev = NULL;
|
|
272 |
}
|
|
273 |
|
|
274 |
pCurUnit->iNext = iAllocatedMemBlock;
|
|
275 |
|
|
276 |
if( iAllocatedMemBlock )
|
|
277 |
{
|
|
278 |
iAllocatedMemBlock->iPrev = pCurUnit;
|
|
279 |
}
|
|
280 |
|
|
281 |
iAllocatedMemBlock = pCurUnit;
|
|
282 |
|
|
283 |
iBlockUsage++;
|
|
284 |
|
9
|
285 |
// not when FM C_TRACE( ( _T( "DMemManager::DMemPool::Alloc<" ) ) );
|
0
|
286 |
return iAllocatedMemBlock->iMemPtr;
|
|
287 |
}
|
|
288 |
|
|
289 |
|
|
290 |
/*
|
|
291 |
* Free memory unit.
|
|
292 |
*/
|
|
293 |
TBool DMemManager::DMemPool::Free( const TUint8* aBlockAddress )
|
|
294 |
{
|
9
|
295 |
// not when FM C_TRACE( ( _T( "DMemManager::DMemPool::Free>" ) ) );
|
0
|
296 |
|
|
297 |
struct sUnit *pCurUnit = (struct sUnit *)(aBlockAddress - sizeof(struct sUnit) );
|
|
298 |
|
|
299 |
iAllocatedMemBlock = pCurUnit->iNext;
|
|
300 |
|
|
301 |
if(NULL != iAllocatedMemBlock)
|
|
302 |
{
|
|
303 |
iAllocatedMemBlock->iPrev = NULL;
|
|
304 |
}
|
|
305 |
|
|
306 |
pCurUnit->iNext = iFreeMemBlock;
|
|
307 |
|
|
308 |
if(NULL != iFreeMemBlock)
|
|
309 |
{
|
|
310 |
iFreeMemBlock->iPrev = pCurUnit;
|
|
311 |
}
|
|
312 |
|
|
313 |
iFreeMemBlock = pCurUnit;
|
|
314 |
iFreeMemBlock->iMemPtr->Zero();
|
|
315 |
iBlockUsage--;
|
|
316 |
|
9
|
317 |
// not when FM C_TRACE( ( _T( "DMemManager::DMemPool::Free<" ) ) );
|
0
|
318 |
|
|
319 |
//If empty & ready to be deleted
|
|
320 |
return ( iCopyPoolInUse && iBlockUsage == 0 ) ? ETrue : EFalse;
|
|
321 |
|
|
322 |
}
|
|
323 |
|
|
324 |
EXPORT_C TDes8& MemApi::AllocBlock( const TUint16 aSize )
|
|
325 |
{
|
|
326 |
C_TRACE( ( _T( "MemApi::AllocBlock 0x%x>" ), aSize ) );
|
|
327 |
|
|
328 |
__ASSERT_NO_FAST_MUTEX;
|
|
329 |
|
|
330 |
ASSERT_RESET_ALWAYS( ( aSize > 0 ), ( EMemBlockSizeZero | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
331 |
ASSERT_THREAD_CONTEXT_ALWAYS( ( EAllocNotThreadContext | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
332 |
|
|
333 |
TPtr8* ptr( NULL );
|
|
334 |
|
|
335 |
NKern::FMWait( DMemManager::iThisptr->iFastMutex );
|
|
336 |
|
|
337 |
for( TUint8 i( 0 ); i < DMemManager::iThisptr->iMemPond.Count(); ++i )
|
|
338 |
{
|
|
339 |
if( aSize <= DMemManager::iThisptr->iMemPond[ i ]->iBlockSize )
|
|
340 |
{
|
|
341 |
ptr = DMemManager::iThisptr->iMemPond[i]->Alloc( aSize );
|
|
342 |
|
|
343 |
if( DMemManager::iThisptr->iMemPond[i]->iBlockUsage > DMemManager::iThisptr->iMemPond[i]->iHighWaterMark )
|
|
344 |
{
|
|
345 |
DMemManager::iThisptr->iPoolCreateQueue.Append( DMemManager::iThisptr->iMemPond[i] );
|
|
346 |
DMemManager::iThisptr->iPoolAllocateDfc->Enque();
|
|
347 |
}
|
|
348 |
|
|
349 |
NKern::FMSignal( DMemManager::iThisptr->iFastMutex );
|
|
350 |
break;
|
|
351 |
}
|
|
352 |
}
|
|
353 |
|
|
354 |
ASSERT_RESET_ALWAYS( ptr, ( EMemBlockAllocationFailed | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
355 |
ASSERT_RESET_ALWAYS( ptr->Size() == 0, ( EMemBlockInvalidReleaseDetected | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
356 |
|
|
357 |
C_TRACE( ( _T( "MemApi::AllocBlock 0x%x<" ), ptr ) );
|
|
358 |
return *ptr;
|
|
359 |
}
|
|
360 |
|
|
361 |
|
|
362 |
EXPORT_C void MemApi::DeallocBlock( TDes8& aBlock )
|
|
363 |
{
|
|
364 |
C_TRACE( ( _T( "MemApi::DeallocBlock aBlock 0x%x>" ), &aBlock ) );
|
|
365 |
|
|
366 |
__ASSERT_NO_FAST_MUTEX;
|
|
367 |
|
|
368 |
ASSERT_THREAD_CONTEXT_ALWAYS( ( EDeallocNotThreadContext | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
9
|
369 |
aBlock.FillZ( aBlock.MaxLength() );
|
|
370 |
aBlock.SetLength(0);
|
0
|
371 |
TBool removePool = EFalse;
|
|
372 |
NKern::FMWait( DMemManager::iThisptr->iFastMutex );
|
|
373 |
|
|
374 |
for( TUint8 i( 0 ); i < DMemManager::iThisptr->iMemPond.Count(); ++i )
|
|
375 |
{
|
|
376 |
//Check if inside pools memory area
|
|
377 |
if( ( (DMemManager::iThisptr->iMemPond[i]->iMemoryArea) < aBlock.Ptr() ) &&
|
|
378 |
( aBlock.Ptr() < (DMemManager::iThisptr->iMemPond[i]->iMemoryArea + DMemManager::iThisptr->iMemPond[i]->iPoolSize) ) )
|
|
379 |
{
|
|
380 |
removePool = DMemManager::iThisptr->iMemPond[i]->Free( aBlock.Ptr() );
|
|
381 |
|
|
382 |
if( removePool )
|
|
383 |
{
|
|
384 |
DMemManager::iThisptr->iPoolDeleteQueue.Append( DMemManager::iThisptr->iMemPond[i] );
|
|
385 |
DMemManager::iThisptr->iMemPond.Remove( i );
|
|
386 |
DMemManager::iThisptr->iPoolDeleteDfc->Enque();
|
|
387 |
}
|
|
388 |
|
|
389 |
break;
|
|
390 |
}
|
|
391 |
}
|
|
392 |
|
|
393 |
NKern::FMSignal( DMemManager::iThisptr->iFastMutex );
|
|
394 |
|
|
395 |
}
|
|
396 |
|
|
397 |
DECLARE_STANDARD_EXTENSION()
|
|
398 |
{
|
|
399 |
Kern::Printf( "Memory Manager Extension>" );
|
|
400 |
DMemManager* extension = new DMemManager();
|
|
401 |
ASSERT_RESET_ALWAYS( ( extension ), ( EExtensionMemoryAllocationFailed | EDMemmanagerTraceId << KClassIdentifierShift ) );
|
|
402 |
DMemManager::iThisptr = static_cast< DMemManager* >( extension );
|
|
403 |
Kern::Printf( "Memory Manager Extension<" );
|
|
404 |
return KErrNone;
|
|
405 |
}
|
|
406 |
|
|
407 |
// End of File
|
|
408 |
|