Main Page   Modules   Class Hierarchy   Data Structures   File List   Data Fields   Globals   Related Pages  

oscl_file_cache.h

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
00003 
00004 //                     O S C L _ F I L E  _ C A C H E
00005 
00006 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
00007 
00019 #ifndef OSCL_FILE_CACHE_H_INCLUDED
00020 #define OSCL_FILE_CACHE_H_INCLUDED
00021 
00022 #ifndef OSCLCONFIG_IO_H_INCLUDED
00023 #include "osclconfig_io.h"
00024 #endif
00025 
00026 #ifndef OSCL_BASE_H_INCLUDED
00027 #include "oscl_base.h"
00028 #endif
00029 
00030 #include "oscl_file_io.h"
00031 
00032 class OsclFileCache;
00033 
00034 class OsclFileCacheBuffer
00035 {
00036     public:
00037         OsclFileCacheBuffer(): isFixed(false)
00038                 , capacity(0)
00039                 , usableSize(0)
00040                 , pBuffer(NULL)
00041                 , filePosition(0)
00042                 , currentPos(0)
00043                 , endPos(0)
00044                 , updateStart(0)
00045                 , updateEnd(0)
00046         {}
00047 
00048         OsclFileCache* iContainer;
00049 
00050         //Movable or fixed cache?
00051         bool isFixed;
00052 
00053         //Capacity of the cache buffer
00054         uint32 capacity;
00055 
00056         //Usable size of the cache buffer (may be l.t. its capacity)
00057         uint32 usableSize;
00058 
00059         //constant pointer to cache buffer
00060         uint8*    pBuffer;
00061 
00062         //the native file position corresponding to the start of the
00063         //cache
00064         TOsclFileOffset  filePosition;
00065 
00066         //current working position (virtual file pointer) in the cache.
00067         //units: 0-based byte offset from beginning of cache
00068         uint32    currentPos;
00069 
00070         //end of valid data in the cache.
00071         //units: 0-based byte offset from beginning of cache
00072         uint32    endPos;
00073 
00074         //variables to track the range of data in the cache that
00075         //has been updated by write operations, but has not yet
00076         //been written to disk.
00077         //units: 0-based byte offset from beginning of cache
00078         uint32    updateStart;
00079         uint32    updateEnd;
00080 
00081         int32 SetPosition(TOsclFileOffset  pos);
00082         int32 PrepRead();
00083         int32 PrepWrite();
00084         int32 WriteUpdatesToFile();
00085         int32 FillFromFile(uint32, uint32);
00086 
00087         bool IsUpdated()
00088         {
00089             return updateEnd > updateStart;
00090         }
00091 
00092         //return true if this cache contains the input position.
00093         bool Contains(TOsclFileOffset  pos)
00094         {
00095             return (filePosition <= pos && pos < filePosition + (TOsclFileOffset)usableSize);
00096         }
00097         //return true if this cache preceeds the input position.
00098         bool Preceeds(TOsclFileOffset  pos)
00099         {
00100             return (filePosition + (TOsclFileOffset)usableSize <= pos);
00101         }
00102 };
00103 
00104 class Oscl_File;
00105 
00106 class OsclFileCache : public HeapBase
00107 {
00108     public:
00109         OsclFileCache(Oscl_File& aContainer);
00110         ~OsclFileCache();
00111 
00112         int32 Open(uint32 mode, uint32 cache_size);
00113 
00114         void Close();
00115 
00116         uint32 Read(void* outputBuffer, uint32 size, uint32 numelements);
00117 
00118         uint32 Write(const void* inputBuffer, uint32 size, uint32 numelements);
00119 
00120         TOsclFileOffset  FileSize()
00121         {
00122             return _fileSize;
00123         }
00124 
00125         int32 Seek(TOsclFileOffset  offset, Oscl_File::seek_type origin);
00126 
00127         TOsclFileOffset  Tell()
00128         {
00129             return (_curCache) ? (_curCache->filePosition + _curCache->currentPos) : 0;
00130         }
00131 
00132         int32 Flush();
00133 
00134         int32 EndOfFile()
00135         {
00136             return (Tell() == FileSize()) ? 1 : 0;
00137         }
00138 
00139     private:
00140         friend class OsclFileCacheBuffer;
00141         Oscl_File& iContainer;
00142 
00143         //file mode from the Open call.
00144         uint32 _mode;
00145 
00146     public:
00147         OsclFileCacheBuffer _movableCache;
00148         Oscl_Vector<OsclFileCacheBuffer, OsclMemAllocator> _fixedCaches;
00149         OSCL_IMPORT_REF OsclFileCacheBuffer* AddFixedCache(const Oscl_File::OsclFixedCacheParam&);
00150 
00151     private:
00152         OsclFileCacheBuffer* _curCache;
00153         int32 SetCachePosition(TOsclFileOffset);
00154         int32 UpdateFixedCaches();
00155 
00156         //Current file size.  This is a virtual file size and
00157         //may not match the native file size when there is
00158         //cached data.
00159         TOsclFileOffset     _fileSize;
00160 
00161         //Current true native file position.
00162         TOsclFileOffset  _nativePosition;
00163 
00164         PVLogger* iLogger;
00165 };
00166 
00167 
00168 #endif // OSCL_FILE_CACHE_H_INCLUDED
00169 

OSCL API
Posting Version: CORE_8.000.1.1_RC4