X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fdrv%2Fiocache.c;h=ba822df8bf31534b8601ac3a48c9c27f706f3a21;hb=65c21ebcac30ca3c5a1303149f2c795bde5f990f;hp=f42957c755d7a58ab0b60e8efa02197909227399;hpb=a4ce2e60f783c9e71447edc03f20f937b8abf35a;p=tpg%2Facess2.git diff --git a/Kernel/drv/iocache.c b/Kernel/drv/iocache.c index f42957c7..ba822df8 100644 --- a/Kernel/drv/iocache.c +++ b/Kernel/drv/iocache.c @@ -3,6 +3,8 @@ * - IO Cache * * By thePowersGang (John Hodge) + * + * TODO: Convert to use spare physical pages instead */ #define DEBUG 0 #include @@ -10,6 +12,7 @@ // === TYPES === typedef struct sIOCache_Ent tIOCache_Ent; +typedef struct sIOCache_PageInfo tIOCache_PageInfo; // === STRUCTURES === struct sIOCache_Ent @@ -21,11 +24,20 @@ struct sIOCache_Ent Uint8 Data[]; }; +struct sIOCache_PageInfo +{ + tIOCache_PageInfo *GlobalNext; + tIOCache_PageInfo *CacheNext; + tIOCache *Owner; + tPAddr BasePhys; + Uint64 BaseOffset; +}; + struct sIOCache { tIOCache *Next; int SectorSize; - int Lock; + tMutex Lock; int Mode; Uint32 ID; tIOCache_WriteCallback Write; @@ -35,9 +47,10 @@ struct sIOCache }; // === GLOBALS === - int glIOCache_Caches; +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,14 +70,12 @@ 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 - LOCK( &glIOCache_Caches ); + SHORTLOCK( &glIOCache_Caches ); ret->Next = gIOCache_Caches; gIOCache_Caches = ret; - RELEASE( &glIOCache_Caches ); + SHORTREL( &glIOCache_Caches ); // Return return ret; @@ -87,9 +98,9 @@ int IOCache_Read( tIOCache *Cache, Uint64 Sector, void *Buffer ) } // Lock - LOCK( &Cache->Lock ); + Mutex_Acquire( &Cache->Lock ); if(Cache->CacheSize == 0) { - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); LEAVE('i', -1); return -1; } @@ -101,7 +112,7 @@ int IOCache_Read( tIOCache *Cache, Uint64 Sector, void *Buffer ) if( ent->Num == Sector ) { memcpy(Buffer, ent->Data, Cache->SectorSize); ent->LastAccess = now(); - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); LEAVE('i', 1); return 1; } @@ -110,7 +121,7 @@ int IOCache_Read( tIOCache *Cache, Uint64 Sector, void *Buffer ) if(ent->Num > Sector) break; } - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); LEAVE('i', 0); return 0; } @@ -130,9 +141,9 @@ int IOCache_Add( tIOCache *Cache, Uint64 Sector, void *Buffer ) return -1; // Lock - LOCK( &Cache->Lock ); + Mutex_Acquire( &Cache->Lock ); if(Cache->CacheSize == 0) { - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); return -1; } @@ -142,7 +153,7 @@ int IOCache_Add( tIOCache *Cache, Uint64 Sector, void *Buffer ) { // Is it already here? if( ent->Num == Sector ) { - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); return 0; } @@ -197,7 +208,7 @@ int IOCache_Add( tIOCache *Cache, Uint64 Sector, void *Buffer ) Cache->CacheUsed ++; // Release Spinlock - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); // Return success return 1; @@ -215,9 +226,9 @@ int IOCache_Write( tIOCache *Cache, Uint64 Sector, void *Buffer ) if(!Cache || !Buffer) return -1; // Lock - LOCK( &Cache->Lock ); + Mutex_Acquire( &Cache->Lock ); if(Cache->CacheSize == 0) { - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); return -1; } @@ -234,7 +245,7 @@ int IOCache_Write( tIOCache *Cache, Uint64 Sector, void *Buffer ) ent->LastWrite = 0; } - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); return 1; } // It's a sorted list, so as soon as we go past `Sector` we know @@ -242,7 +253,7 @@ int IOCache_Write( tIOCache *Cache, Uint64 Sector, void *Buffer ) if(ent->Num > Sector) break; } - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); return 0; } @@ -257,9 +268,9 @@ void IOCache_Flush( tIOCache *Cache ) if( Cache->Mode == IOCACHE_VIRTUAL ) return; // Lock - LOCK( &Cache->Lock ); + Mutex_Acquire( &Cache->Lock ); if(Cache->CacheSize == 0) { - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); return; } @@ -270,7 +281,7 @@ void IOCache_Flush( tIOCache *Cache ) ent->LastWrite = 0; } - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); } /** @@ -282,9 +293,9 @@ void IOCache_Destroy( tIOCache *Cache ) tIOCache_Ent *ent, *prev = NULL; // Lock - LOCK( &Cache->Lock ); + Mutex_Acquire( &Cache->Lock ); if(Cache->CacheSize == 0) { - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); return; } @@ -302,24 +313,24 @@ void IOCache_Destroy( tIOCache *Cache ) Cache->CacheSize = 0; - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); // Remove from list - LOCK( &glIOCache_Caches ); + 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; } } } - RELEASE( &glIOCache_Caches ); + SHORTREL( &glIOCache_Caches ); free(Cache); }