[Common-cvs] system/platform/symbian new.cpp,1.2,1.3
gashish at helixcommunity.org gashish at helixcommunity.orgUpdate of /cvsroot/common/system/platform/symbian In directory cvs:/tmp/cvs-serv7990/platform/symbian Modified Files: new.cpp Log Message: Synopsis: Handle out of memory error "Nokia is bound by the terms of a commercial contribution agreement with RealNetworks, and I am authorized to contribute this code under said agreement." Modified by: ashish.as.gupta at nokia.com Error ID: TSW ID=EOVL-6GWTZX Reviewed by: Date: 11-17-2005. Project: Helix plugin for Symbian Synopsis: Handle out of memory error Overloaded new operator calls User::Leave on memory allocation failure. This is incorrect as code may not be running under TRAP. This results in panics. Following modifications are made: 1. Change overloaded operators so that User::Leave is not called. Functions should not leave unless they are trapped by higher level code. 2. A new class SymbianMemoryMonitor is added that receives an event from the "new" and other similar overloaded operators. On receiving the event, IHXErrorMessages API will report an error to engine. Ideally, whenever a memory allocation API returns an error, requester should handle the error code but it not the case always. In many cases CHXBuffer::SetSize and similar APIs returned error codes are not always handled by their respective users. 3. SymbainMemoryMonitor is created by MMF-TLC. 4. PrintCurrentState() is not called from so many places. It is only called when we change the state. Files Modified: Image Size and Heap Use impact: minor Platforms and Profiles Build Verified: helix-client-s60-mmf-basic, armv5 Platforms and Profiles Functionality verified: armv5 Branch: HEAD Index: new.cpp =================================================================== RCS file: /cvsroot/common/system/platform/symbian/new.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- new.cpp 13 Feb 2004 00:02:01 -0000 1.2 +++ new.cpp 22 Nov 2005 20:24:12 -0000 1.3 @@ -12,9 +12,19 @@ #include <e32base.h> #include <estlib.h> #include <string.h> +#include "memorymonitor.h" +void OutOfMemory() +{ + SymbianMemoryMonitor *pMonitor = SymbianMemoryMonitor::Instance(); + if (pMonitor) + { + pMonitor->SendEvent(SymbianMemoryMonitor::EOutOfMemory); + } +} + void* operator new(size_t size) { /* based on Symbian document, passing @@ -23,32 +33,40 @@ the exception such that the player will gracefully shutdown with out of memory condition, the following check is added.*/ - - if( size > (KMaxTInt/2) ) - User::Leave(KErrNoMemory); - - void *p = User::Alloc(size); + + void *p = NULL; + if (size < (KMaxTInt/2)) + { + p = User::Alloc(size); + } if (p) - memset(p, 0, size); - else // allocation failure - User::Leave(KErrNoMemory); - + { + memset(p, 0, size); + } + else + { + OutOfMemory(); + } return p; } void* operator new[](size_t size) { - if( size > (KMaxTInt/2) ) - User::Leave(KErrNoMemory); - - void *p = User::Alloc(size); + void *p = NULL; + if (size < (KMaxTInt/2)) + { + p = User::Alloc(size); + } if (p) - memset(p, 0, size); - else // allocation failure - User::Leave(KErrNoMemory); - + { + memset(p, 0, size); + } + else + { + OutOfMemory(); + } return p; } @@ -73,23 +91,36 @@ void* malloc(size_t size) { - if( size > (KMaxTInt/2) ) - User::Leave(KErrNoMemory); - - void *p = User::Alloc(size); + void *p = NULL; + if (size < (KMaxTInt/2)) + { + p = User::Alloc(size); + } - if (p == 0) // allocation failure - User::Leave(KErrNoMemory); + if (p) + { + memset(p, 0, size); + } + else + { + OutOfMemory(); + } return p; } void* realloc(void* pOld, size_t size) { - void* p = User::ReAlloc(pOld, size); + void *p = NULL; + if (size < (KMaxTInt/2)) + { + p = User::ReAlloc(pOld, size); + } - if (p == 0) // allocation failure - User::Leave(KErrNoMemory); + if (p == NULL) // allocation failure + { + OutOfMemory(); + } return p; } @@ -100,4 +131,5 @@ User::Free(p); } + #endif // __MARM__