mtpfws/mtpfw/datatypes/src/cmtptypefile.cpp
branchRCL_3
changeset 32 f85613f12947
parent 24 523717cdb0ad
child 39 dbd1c5e08735
--- a/mtpfws/mtpfw/datatypes/src/cmtptypefile.cpp	Wed Jun 09 10:36:33 2010 +0300
+++ b/mtpfws/mtpfw/datatypes/src/cmtptypefile.cpp	Mon Jun 21 16:36:59 2010 +0300
@@ -234,16 +234,8 @@
     
     iRemainingDataSize = (TInt64)aSize;//Current implemenation does not support file size with 2 x64 
 
-    if(iRemainingDataSize> KMTPFileChunkSizeForLargeFile) //512K
-        {
-        iBuffer1.CreateMaxL(KMTPFileChunkSizeForLargeFile);
-        iBuffer2.CreateMaxL(KMTPFileChunkSizeForLargeFile);
-        }
-    else
-        {
-        iBuffer1.CreateMaxL(KMTPFileChunkSizeForSmallFile);
-        iBuffer2.CreateMaxL(KMTPFileChunkSizeForSmallFile);
-        }
+    CreateDoubleBufferL(iRemainingDataSize);
+    
     if(iRemainingDataSize> KMTPFileSetSizeChunk)
         {
         //split the setSize to multiple calling of 512M
@@ -599,16 +591,7 @@
         iRemainingDataSize = size;
 
         //For File reading, NO "SetSizeL()" will be called, therefore, create the buffer here.
-        if (iRemainingDataSize > KMTPFileChunkSizeForLargeFile) //512K
-            {
-            iBuffer1.CreateMaxL(KMTPFileChunkSizeForLargeFile);
-            iBuffer2.CreateMaxL(KMTPFileChunkSizeForLargeFile);
-            }
-        else
-            {
-            iBuffer1.CreateMaxL(KMTPFileChunkSizeForSmallFile);
-            iBuffer2.CreateMaxL(KMTPFileChunkSizeForSmallFile);
-            }
+        CreateDoubleBufferL(iRemainingDataSize);
         }
     }
 
@@ -647,16 +630,7 @@
         iRemainingDataSize = iTargetFileSize;
         
         //For File reading, NO "SetSizeL()" will be called, therefore, create the buffer here.
-        if (iRemainingDataSize > KMTPFileChunkSizeForLargeFile) //512K
-            {
-            iBuffer1.CreateMaxL(KMTPFileChunkSizeForLargeFile);
-            iBuffer2.CreateMaxL(KMTPFileChunkSizeForLargeFile);
-            }
-        else
-            {
-            iBuffer1.CreateMaxL(KMTPFileChunkSizeForSmallFile);
-            iBuffer2.CreateMaxL(KMTPFileChunkSizeForSmallFile);
-            }
+        CreateDoubleBufferL(iRemainingDataSize);
         }
 	}
 
@@ -743,3 +717,46 @@
 
     iBuffer1AvailForWrite = !iBuffer1AvailForWrite;//toggle the flag.
     }
+
+/**
+ * Allocate double buffers to write to/read from file
+ * @param aFileSize: the size of the file to be written to or read from
+ * @return void
+ * leave code: KErrNoMemory if there is insufficient memory
+ */
+void CMTPTypeFile::CreateDoubleBufferL(TInt64 aFileSize)
+    {
+    if(aFileSize > KMTPFileChunkSizeForLargeFile) //512KB
+        {
+        TInt err = iBuffer1.CreateMax(KMTPFileChunkSizeForLargeFile);
+        TInt err2 = iBuffer2.CreateMax(KMTPFileChunkSizeForLargeFile);
+        TInt bufferSize = KMTPFileChunkSizeForLargeFile;
+        
+        //if one of buffer allocation fails, decrease the buffer size by 
+        //a half of it until :
+        //we finally succeed in the allocation Or 
+        //the smallest acceptable buffer size KMTPFileChunkSizeForSmallFile(64KB) reaches.
+        while ((err != KErrNone || err2 != KErrNone) && bufferSize != KMTPFileChunkSizeForSmallFile)
+            {
+            iBuffer1.Close();
+            iBuffer2.Close();
+            
+            bufferSize /= 2;
+            err = iBuffer1.CreateMax(bufferSize);
+            err2 = iBuffer2.CreateMax(bufferSize);
+            }
+        
+        if ( err != KErrNone || err2 != KErrNone)
+            {
+            //We still can not allocate 2*64KB buffer, just leave under this case
+            iBuffer1.Close();
+            iBuffer2.Close();
+            User::Leave(KErrNoMemory);
+            }
+        }
+    else
+        {
+        iBuffer1.CreateMaxL(KMTPFileChunkSizeForSmallFile);
+        iBuffer2.CreateMaxL(KMTPFileChunkSizeForSmallFile);
+        }
+    }