*** empty log message ***
[yaffs/.git] / yaffs_guts.c
index dfef9def4c17a10e89afb2e304f93993baf5d068..d7246590925400795b7831e3e74fe9d4fac54c8e 100644 (file)
@@ -14,7 +14,7 @@
  */
  //yaffs_guts.c
 
-const char *yaffs_guts_c_version="$Id: yaffs_guts.c,v 1.22 2003-03-11 05:16:53 charles Exp $";
+const char *yaffs_guts_c_version="$Id: yaffs_guts.c,v 1.23 2003-04-03 17:32:11 charles Exp $";
 
 #include "yportenv.h"
 
@@ -3128,6 +3128,7 @@ static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
                                                                                                                        cache->nBytes,1);
 
                                cache->dirty = 0;
+                               cache->object = NULL;
                        }
                
                } while(cache && chunkWritten > 0);
@@ -3160,10 +3161,14 @@ static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
                        if(!dev->srCache[i].object)
                        {
                                //T(("Grabbing empty %d\n",i));
+                               
+                               printf("Grabbing empty %d\n",i);
                        
                                return &dev->srCache[i];
                        }
                }
+               
+               return NULL;
        
                theOne = -1; 
                usage = 0; // just to stop the compiler grizzling
@@ -3180,6 +3185,9 @@ static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
                }
        
                //T(("Grabbing non-empty %d\n",theOne));
+               
+               if(theOne >= 0) printf("Grabbed non-empty cache %d\n",theOne);
+               
                return  theOne >= 0 ?  &dev->srCache[theOne] : NULL;
        }
        else
@@ -3196,6 +3204,7 @@ static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
        yaffs_Object *theObj;
        int usage;
        int i;
+       int pushout;
        
        if(dev->nShortOpCaches > 0)
        {
@@ -3212,6 +3221,8 @@ static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
                
                        theObj = dev->srCache[0].object;
                        usage = dev->srCache[0].lastUse;
+                       cache = &dev->srCache[0];
+                       pushout = 0;
        
                        for(i = 1; i < dev->nShortOpCaches; i++)
                        {
@@ -3220,15 +3231,27 @@ static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
                                {
                                        usage  = dev->srCache[i].lastUse;
                                        theObj = dev->srCache[i].object;
+                                       cache = &dev->srCache[i];
+                                       pushout = i;
                                }
                        }
                
-                       yaffs_FlushFilesChunkCache(theObj);
+                       if(!cache || cache->dirty)
+                       {
+                       
+                               printf("Dirty ");
+                               yaffs_FlushFilesChunkCache(theObj);
                
-                       // Try again
-                       cache = yaffs_GrabChunkCacheWorker(dev);
+                               // Try again
+                               cache = yaffs_GrabChunkCacheWorker(dev);
+                       }
+                       else
+                       {
+                               printf(" pushout %d\n",pushout);
+                       }
+                       
                }
-       
+
                return cache;
        }
        else
@@ -3346,6 +3369,7 @@ int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 * buffer, __u32 offset, int nB
        int nToCopy;
        int n = nBytes;
        int nDone = 0;
+       yaffs_ChunkCache *cache;
        
        yaffs_Device *dev;
        
@@ -3367,14 +3391,18 @@ int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 * buffer, __u32 offset, int nB
                        nToCopy = YAFFS_BYTES_PER_CHUNK - start;
                }
        
-               if(nToCopy != YAFFS_BYTES_PER_CHUNK)
+               cache = yaffs_FindChunkCache(in,chunk);
+               
+               // If the chunk is already in the cache or it is less than a whole chunk
+               // then use the cache (if there is caching)
+               // else bypass the cache.
+               if( cache || nToCopy != YAFFS_BYTES_PER_CHUNK)
                {
-                       // An incomplete start or end chunk (or maybe both start and end chunk)
                        if(dev->nShortOpCaches > 0)
                        {
-                               yaffs_ChunkCache *cache;
+                               
                                // If we can't find the data in the cache, then load it up.
-                               cache = yaffs_FindChunkCache(in,chunk);
+                               
                                if(!cache)
                                {
                                        cache = yaffs_GrabChunkCache(in->myDev);