+// Grab us a chunk for use.
+// First look for an empty one.
+// Then look for the least recently used non-dirty one.
+// Then look for the least recently used dirty one...., flush and look again.
+static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
+{
+ int i;
+ int usage;
+ int theOne;
+
+ for(i = 0; i < YAFFS_N_CACHE_CHUNKS; i++)
+ {
+ if(!dev->srCache[i].object)
+ {
+ //T(("Grabbing empty %d\n",i));
+
+ return &dev->srCache[i];
+ }
+ }
+
+ theOne = -1;
+ usage = 0; // just to stop the compiler grizzling
+
+ for(i = 0; i < YAFFS_N_CACHE_CHUNKS; i++)
+ {
+ if(!dev->srCache[i].dirty &&
+ ((dev->srCache[i].lastUse < usage && theOne >= 0)||
+ theOne < 0))
+ {
+ usage = dev->srCache[i].lastUse;
+ theOne = i;
+ }
+ }
+
+ //T(("Grabbing non-empty %d\n",theOne));
+ return theOne >= 0 ? &dev->srCache[theOne] : NULL;
+
+}
+
+
+static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
+{
+ yaffs_ChunkCache *cache;
+ yaffs_Object *theObj;
+ int usage;
+ int i;
+
+ // Try find a non-dirty one...
+
+ cache = yaffs_GrabChunkCacheWorker(dev);
+
+ if(!cache)
+ {
+ // They were all dirty, find the last recently used object and flush
+ // its cache, then find again.
+ // NB what's here is not very accurate, we actually flush the object
+ // the last recently used page.
+
+ theObj = dev->srCache[0].object;
+ usage = dev->srCache[0].lastUse;
+
+ for(i = 1; i < YAFFS_N_CACHE_CHUNKS; i++)
+ {
+ if( dev->srCache[i].object &&
+ dev->srCache[i].lastUse < usage)
+ {
+ usage = dev->srCache[i].lastUse;
+ theObj = dev->srCache[i].object;
+ }
+ }
+
+ yaffs_FlushFilesChunkCache(theObj);
+
+ // Try again
+ cache = yaffs_GrabChunkCacheWorker(dev);
+ }
+
+ return cache;
+
+}
+
+
+// Find a cached chunk
+static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj, int chunkId)
+{
+ yaffs_Device *dev = obj->myDev;
+ int i;
+
+ for(i = 0; i < YAFFS_N_CACHE_CHUNKS; i++)
+ {
+ if(dev->srCache[i].object == obj &&
+ dev->srCache[i].chunkId == chunkId)
+ {
+ dev->cacheHits++;
+
+ return &dev->srCache[i];
+ }
+ }
+
+ return NULL;
+}
+
+// Mark the chunk for the least recently used algorithym
+static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache, int isAWrite)
+{
+ if( dev->srLastUse < 0 ||
+ dev->srLastUse > 100000000)
+ {
+ // Reset the cache usages
+ int i;
+ for(i = 1; i < YAFFS_N_CACHE_CHUNKS; i++)
+ {
+ dev->srCache[i].lastUse = 0;
+ }
+ dev->srLastUse = 0;
+ }
+
+ dev->srLastUse++;
+
+ cache->lastUse = dev->srLastUse;
+
+ if(isAWrite)
+ {
+ cache->dirty = 1;
+ }
+}
+
+// Invalidate a single cache page.
+// Do this when a whole page gets written,
+// ie the short cache for this page is no longer valid.
+static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
+{
+ yaffs_ChunkCache *cache = yaffs_FindChunkCache(object,chunkId);
+
+ if(cache)
+ {
+ cache->object = NULL;
+ }
+}
+
+
+// Invalidate all the cache pages associated with this object
+// Do this whenever ther file is deleted or resized.
+static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
+{
+ int i;
+ yaffs_Device *dev = in->myDev;
+
+ // Now invalidate it.
+ for(i = 0; i < YAFFS_N_CACHE_CHUNKS; i++)
+ {
+ if(dev->srCache[i].object == in)
+ {
+ dev->srCache[i].object = NULL;
+ }
+ }
+}
+
+
+#else
+// Stubs for disabling short op caching
+
+static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
+{}
+
+static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
+{
+ return NULL;
+}
+
+
+static yaffs_ChunkCache *yaffs_FindChunkCache(yaffs_Device *dev, int objectId, int chunkId)
+{
+ return NULL;
+}
+
+static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache)
+{
+}
+
+static void yaffs_InvalidateChunkCache(yaffs_Object *obj, int chunkId)
+{
+}
+
+static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
+{
+}
+
+
+
+#endif
+
+