Kernel/vterm - Changed VTerm to be less x86 centric
[tpg/acess2.git] / Kernel / drv / iocache.c
index 743dabc..8f73541 100644 (file)
@@ -3,6 +3,8 @@
  * - IO Cache
  * 
  * By thePowersGang (John Hodge)
+ * 
+ * TODO: Convert to use spare physical pages instead
  */
 #define DEBUG  0
 #include <acess.h>
@@ -10,6 +12,7 @@
 
 // === TYPES ===
 typedef struct sIOCache_Ent    tIOCache_Ent;
+typedef struct sIOCache_PageInfo       tIOCache_PageInfo;
 
 // === STRUCTURES ===
 struct sIOCache_Ent
@@ -21,6 +24,15 @@ struct sIOCache_Ent
        Uint8   Data[];
 };
 
+struct sIOCache_PageInfo
+{
+       tIOCache_PageInfo       *GlobalNext;
+       tIOCache_PageInfo       *CacheNext;
+       tIOCache        *Owner;
+       tPAddr  BasePhys;
+       Uint64  BaseOffset;
+};
+
 struct sIOCache
 {
        tIOCache        *Next;
@@ -38,6 +50,7 @@ struct sIOCache
 tShortSpinlock glIOCache_Caches;
 tIOCache       *gIOCache_Caches = NULL;
  int   giIOCache_NumCaches = 0;
+tIOCache_PageInfo      *gIOCache_GlobalPages;
 
 // === CODE ===
 /**
@@ -46,7 +59,7 @@ tIOCache      *gIOCache_Caches = NULL;
  */
 tIOCache *IOCache_Create( tIOCache_WriteCallback Write, Uint32 ID, int SectorSize, int CacheSize )
 {
-       tIOCache        *ret = malloc( sizeof(tIOCache) );
+       tIOCache        *ret = calloc( 1, sizeof(tIOCache) );
        
        // Sanity Check
        if(!ret)        return NULL;
@@ -57,8 +70,6 @@ tIOCache *IOCache_Create( tIOCache_WriteCallback Write, Uint32 ID, int SectorSiz
        ret->ID = ID;
        ret->Write = Write;
        ret->CacheSize = CacheSize;
-       ret->CacheUsed = 0;
-       ret->Entries = 0;
        
        // Append to list
        SHORTLOCK( &glIOCache_Caches );
@@ -76,7 +87,6 @@ tIOCache *IOCache_Create( tIOCache_WriteCallback Write, Uint32 ID, int SectorSiz
  */
 int IOCache_Read( tIOCache *Cache, Uint64 Sector, void *Buffer )
 {
-       tIOCache_Ent    *ent;
        
        ENTER("pCache XSector pBuffer", Cache, Sector, Buffer);
        
@@ -93,7 +103,22 @@ int IOCache_Read( tIOCache *Cache, Uint64 Sector, void *Buffer )
                LEAVE('i', -1);
                return -1;
        }
-       
+
+       #if IOCACHE_USE_PAGES
+       tIOCache_PageInfo       *page;
+       size_t  offset = (Sector*Cache->SectorSize) % PAGE_SIZE;
+       Uint64  wanted_base = (Sector*Cache->SectorSize) & ~(PAGE_SIZE-1);
+       for( page = Cache->Pages; page; page = page->CacheNext )
+       {
+               void    *tmp;
+               if(page->BaseOffset < WantedBase)       continue;
+               if(page->BaseOffset > WantedBase)       break;
+               tmp = MM_MapTemp( page->BasePhys );
+               memcpy( Buffer, tmp + offset, Cache->SectorSize ); 
+               MM_FreeTemp( tmp );
+       }
+       #else   
+       tIOCache_Ent    *ent;
        // Search the list
        for( ent = Cache->Entries; ent; ent = ent->Next )
        {
@@ -109,6 +134,7 @@ int IOCache_Read( tIOCache *Cache, Uint64 Sector, void *Buffer )
                // it's not there
                if(ent->Num > Sector)   break;
        }
+       #endif
        
        Mutex_Release( &Cache->Lock );
        LEAVE('i', 0);
@@ -179,6 +205,10 @@ int IOCache_Add( tIOCache *Cache, Uint64 Sector, void *Buffer )
                                oldestPrev = prev;
                        }
                }
+               if( !oldest ) {
+                       Log_Error("IOCache", "Cache full, but also empty");
+                       return -1;
+               }
                // Remove from list, write back and free
                oldestPrev->Next = oldest->Next;
                if(oldest->LastWrite && Cache->Mode != IOCACHE_VIRTUAL)
@@ -307,14 +337,14 @@ void IOCache_Destroy( tIOCache *Cache )
        // Remove from list
        SHORTLOCK( &glIOCache_Caches );
        {
-               tIOCache        *ent;
-               tIOCache        *prev = (tIOCache*)&gIOCache_Caches;
-               for(ent = gIOCache_Caches;
-                       ent;
-                       prev = ent, ent = ent->Next )
+               tIOCache        *cache;
+               tIOCache        *prev_cache = (tIOCache*)&gIOCache_Caches;
+               for(cache = gIOCache_Caches;
+                       cache;
+                       prev_cache = cache, cache = cache->Next )
                {
-                       if(ent == Cache) {
-                               prev->Next = ent->Next;
+                       if(cache == Cache) {
+                               prev_cache->Next = cache->Next;
                                break;
                        }
                }

UCC git Repository :: git.ucc.asn.au