| sl@0 |      1 | /**
 | 
| sl@0 |      2 | * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
 | 
| sl@0 |      3 | * All rights reserved.
 | 
| sl@0 |      4 | * This component and the accompanying materials are made available
 | 
| sl@0 |      5 | * under the terms of "Eclipse Public License v1.0"
 | 
| sl@0 |      6 | * which accompanies this distribution, and is available
 | 
| sl@0 |      7 | * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 | 
| sl@0 |      8 | *
 | 
| sl@0 |      9 | * Initial Contributors:
 | 
| sl@0 |     10 | * Nokia Corporation - initial contribution.
 | 
| sl@0 |     11 | *
 | 
| sl@0 |     12 | * Contributors:
 | 
| sl@0 |     13 | *
 | 
| sl@0 |     14 | * Description:
 | 
| sl@0 |     15 | * Declarations for the multi-threaded Symbian OS porting layer.
 | 
| sl@0 |     16 | * 
 | 
| sl@0 |     17 | *
 | 
| sl@0 |     18 | */
 | 
| sl@0 |     19 | 
 | 
| sl@0 |     20 | 
 | 
| sl@0 |     21 | 
 | 
| sl@0 |     22 | /**
 | 
| sl@0 |     23 |  @file
 | 
| sl@0 |     24 | */
 | 
| sl@0 |     25 | #ifndef OS_SYMBIAN_H
 | 
| sl@0 |     26 | #define OS_SYMBIAN_H
 | 
| sl@0 |     27 | 
 | 
| sl@0 |     28 | #include <sqlite3.h>
 | 
| sl@0 |     29 | #include "FileBuf64.h"
 | 
| sl@0 |     30 | 
 | 
| sl@0 |     31 | #ifdef SQLITE_OS_SYMBIAN
 | 
| sl@0 |     32 | 
 | 
| sl@0 |     33 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |     34 | //////////////////////////  TStaticFs  /////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |     35 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |     36 | 
 | 
| sl@0 |     37 | /**
 | 
| sl@0 |     38 | This class encapsulates a RFs object. 
 | 
| sl@0 |     39 | Only one RFs object is created per process.
 | 
| sl@0 |     40 | The class has a build dependent implementation:
 | 
| sl@0 |     41 |  - a single global static TStaticFs variable is defined for the hardware builds;
 | 
| sl@0 |     42 |  - the process local storage (TPls) is used for storing TStaticFs object for the emulator builds;
 | 
| sl@0 |     43 | 
 | 
| sl@0 |     44 | @see TPls
 | 
| sl@0 |     45 | 
 | 
| sl@0 |     46 | @internalComponent
 | 
| sl@0 |     47 | */
 | 
| sl@0 |     48 | NONSHARABLE_CLASS(TStaticFs)
 | 
| sl@0 |     49 | 	{
 | 
| sl@0 |     50 | public:	
 | 
| sl@0 |     51 | 	TStaticFs();		//Build dependent implementation
 | 
| sl@0 |     52 | 	TInt Connect();
 | 
| sl@0 |     53 | 	static RFs& Fs();	//Build dependent implementation
 | 
| sl@0 |     54 | private:
 | 
| sl@0 |     55 | 	RFs	iFs;	
 | 
| sl@0 |     56 | 	};
 | 
| sl@0 |     57 | 
 | 
| sl@0 |     58 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |     59 | //////////////////////////  sqlite3_mutex  /////////////////////////////////////////////////////////////////////////////////////            
 | 
| sl@0 |     60 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |     61 | 
 | 
| sl@0 |     62 | const TInt KStaticMutexCount = SQLITE_MUTEX_STATIC_LRU2 - 1;//"-1": excluding fast and recoursive mutex types
 | 
| sl@0 |     63 | 
 | 
| sl@0 |     64 | /**
 | 
| sl@0 |     65 | This class describes a mutex object. 
 | 
| sl@0 |     66 | SQLite uses three mutex types: static, fast and recursive.
 | 
| sl@0 |     67 | 
 | 
| sl@0 |     68 | The static mutexes are expected to be created prior first SQLite function call.
 | 
| sl@0 |     69 | The OS porting layer provides build dependent implementation for the static mutextes:
 | 
| sl@0 |     70 |  - The static mutexes are defined as global variables for the hardware builds.
 | 
| sl@0 |     71 |    The mutex creation is performed in the global variable's constructor. 
 | 
| sl@0 |     72 |    If the mutex creation fails, the program will be terminated;
 | 
| sl@0 |     73 |  - The static mutexes are stored in the process local storage for the emulator builds.
 | 
| sl@0 |     74 |    If the mutex creation fails, the program will be terminated;
 | 
| sl@0 |     75 | 
 | 
| sl@0 |     76 | The difference between fast and recursive mutextes is that the recursive mutexes can be entered mutiple times
 | 
| sl@0 |     77 | by the same thread. The OS porting layer makes no difference between fast and recursive mutexes at the moment.
 | 
| sl@0 |     78 | Whether the SQLite requests fast or ecursive mutex, a recursive mutex will be created.
 | 
| sl@0 |     79 | The recursive mutex creation can fail, in which case the error will be reported back to the caller.
 | 
| sl@0 |     80 | 
 | 
| sl@0 |     81 | Note that even though sqlite3_mutex has virtual methods, it is not declared as a standard Symbian OS "C" class 
 | 
| sl@0 |     82 | because sqlite3_mutex is an abstract type, externally declared and used by SQLite (SQLite is a C library).
 | 
| sl@0 |     83 | SQLite deals only with pointers to sqlite3_mutex objects. See the declaration in sqlite3.h file. 
 | 
| sl@0 |     84 | 
 | 
| sl@0 |     85 | @see TPls
 | 
| sl@0 |     86 | @see CRecursiveMutex
 | 
| sl@0 |     87 | @see TStaticMutex
 | 
| sl@0 |     88 | 
 | 
| sl@0 |     89 | @internalComponent
 | 
| sl@0 |     90 | */
 | 
| sl@0 |     91 | NONSHARABLE_CLASS(sqlite3_mutex)
 | 
| sl@0 |     92 | 	{
 | 
| sl@0 |     93 | public:
 | 
| sl@0 |     94 | 	sqlite3_mutex();
 | 
| sl@0 |     95 | 	TInt 	Create();
 | 
| sl@0 |     96 | 	virtual ~sqlite3_mutex();
 | 
| sl@0 |     97 | 	void 	Enter();
 | 
| sl@0 |     98 | 	void	Leave();
 | 
| sl@0 |     99 | 	TBool	IsHeld() const;
 | 
| sl@0 |    100 | private:	
 | 
| sl@0 |    101 | 	TInt		iRefCount;
 | 
| sl@0 |    102 | 	TThreadId	iOwnerThreadId;
 | 
| sl@0 |    103 | 	RMutex		iMutex;
 | 
| sl@0 |    104 | 	};
 | 
| sl@0 |    105 | 
 | 
| sl@0 |    106 | /**
 | 
| sl@0 |    107 | sqlite3_mutex derived class. Describes a recursive mutex.
 | 
| sl@0 |    108 | The mutex creation can fail, the error will be reported back to the caller.
 | 
| sl@0 |    109 | 
 | 
| sl@0 |    110 | This is not a standard Symbian OS "C" class, not derived from CBase.
 | 
| sl@0 |    111 | CRecursiveMutex is a specialization of the sqlite3_mutex class, used for recursive mutexes. 
 | 
| sl@0 |    112 | 
 | 
| sl@0 |    113 | @see sqlite3_mutex
 | 
| sl@0 |    114 | 
 | 
| sl@0 |    115 | @internalComponent
 | 
| sl@0 |    116 | */
 | 
| sl@0 |    117 | NONSHARABLE_CLASS(CRecursiveMutex) : public sqlite3_mutex
 | 
| sl@0 |    118 | 	{
 | 
| sl@0 |    119 | public:	
 | 
| sl@0 |    120 | 	static CRecursiveMutex* New();
 | 
| sl@0 |    121 | 	virtual ~CRecursiveMutex();
 | 
| sl@0 |    122 | 	
 | 
| sl@0 |    123 | private:
 | 
| sl@0 |    124 | 	CRecursiveMutex()
 | 
| sl@0 |    125 | 		{
 | 
| sl@0 |    126 | 		}
 | 
| sl@0 |    127 | 	};
 | 
| sl@0 |    128 | 
 | 
| sl@0 |    129 | /**
 | 
| sl@0 |    130 | sqlite3_mutex derived class. Describes a static mutex.
 | 
| sl@0 |    131 | If the mutex creation fails, the program will be terminated.
 | 
| sl@0 |    132 | 
 | 
| sl@0 |    133 | @see sqlite3_mutex
 | 
| sl@0 |    134 | 
 | 
| sl@0 |    135 | @internalComponent
 | 
| sl@0 |    136 | */
 | 
| sl@0 |    137 | NONSHARABLE_CLASS(TStaticMutex) : public sqlite3_mutex
 | 
| sl@0 |    138 | 	{
 | 
| sl@0 |    139 | public:	
 | 
| sl@0 |    140 | 	TStaticMutex();						//Build dependent implementation
 | 
| sl@0 |    141 | 	};
 | 
| sl@0 |    142 | 	
 | 
| sl@0 |    143 | /**
 | 
| sl@0 |    144 | Returns a pointer to already created static mutex.
 | 
| sl@0 |    145 | The function has build dependet implementation:
 | 
| sl@0 |    146 |  - The static mutexes are defined as global objects for the hardware builds;
 | 
| sl@0 |    147 |  - The static mutexes are stored in th eprocess local storage for the emulator builds;
 | 
| sl@0 |    148 |  
 | 
| sl@0 |    149 | @see TPls
 | 
| sl@0 |    150 | @see sqlite3_mutex
 | 
| sl@0 |    151 | 
 | 
| sl@0 |    152 | @param  aType The static mutex type
 | 
| sl@0 |    153 | @return sqlite3_mutex pointer
 | 
| sl@0 |    154 | 
 | 
| sl@0 |    155 | @panic SqliteMt  6 Process local storage initialization failure (the emulator builds only)
 | 
| sl@0 |    156 | @panic SqliteMt 22 The mutex type is bigger than SQLITE_MUTEX_STATIC_LRU2
 | 
| sl@0 |    157 | 
 | 
| sl@0 |    158 | @internalComponent
 | 
| sl@0 |    159 | */
 | 
| sl@0 |    160 | sqlite3_mutex* StaticMutex(TInt aType);	//Build dependent implementation
 | 
| sl@0 |    161 | 
 | 
| sl@0 |    162 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |    163 | //////////////////////////  TMutexApi  ////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |    164 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |    165 | 
 | 
| sl@0 |    166 | /**
 | 
| sl@0 |    167 | Defines mutex API, that is needed by SQLite.
 | 
| sl@0 |    168 | 
 | 
| sl@0 |    169 | SQLite creates a mutex using the TMutexApi::Alloc() method.
 | 
| sl@0 |    170 | Depending on aType parameter value, either existing static mutex will be used or a new recursive mutex will be created.
 | 
| sl@0 |    171 | 
 | 
| sl@0 |    172 | @see TheMutexMethods
 | 
| sl@0 |    173 | @see sqlite3_mutex
 | 
| sl@0 |    174 | @see CRecursiveMutex
 | 
| sl@0 |    175 | @see TStaticMutex
 | 
| sl@0 |    176 | 
 | 
| sl@0 |    177 | @internalComponent
 | 
| sl@0 |    178 | */
 | 
| sl@0 |    179 | NONSHARABLE_CLASS(TMutexApi)
 | 
| sl@0 |    180 | 	{
 | 
| sl@0 |    181 | public:		
 | 
| sl@0 |    182 | 	static int Init();
 | 
| sl@0 |    183 | 	static int End();
 | 
| sl@0 |    184 | 	static sqlite3_mutex* Alloc(int aType);
 | 
| sl@0 |    185 | 	static void Free(sqlite3_mutex* aMutex);
 | 
| sl@0 |    186 | 	static void Enter(sqlite3_mutex* aMutex);
 | 
| sl@0 |    187 | 	static int Try(sqlite3_mutex* aMutex);
 | 
| sl@0 |    188 | 	static void Leave(sqlite3_mutex* aMutex);
 | 
| sl@0 |    189 | 	static int Held(sqlite3_mutex* aMutex);
 | 
| sl@0 |    190 | 	static int Notheld(sqlite3_mutex* aMutex);
 | 
| sl@0 |    191 | 	};
 | 
| sl@0 |    192 | 
 | 
| sl@0 |    193 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |    194 | //////////////////////////  TDbFile  //////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |    195 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |    196 | 
 | 
| sl@0 |    197 | /**
 | 
| sl@0 |    198 | TDbFile derives from the sqlite3_file structure, adding data members needed for processing the SQLite requests to the OS layer.
 | 
| sl@0 |    199 | When SQLite needs an access to a file, SQLite allocates memory for a new TDbFile instance and passes a pointer to that 
 | 
| sl@0 |    200 | instance to TVfs::Open(). TVfs::Open() creates/opens the file and initializes the TDbFile instance. 
 | 
| sl@0 |    201 | SQLite uses the initialized TDbFile instance (actually SQLite knows and uses the sqlite3_file, the base structure) 
 | 
| sl@0 |    202 | every time when needs to read or write from/to the file, using for that an appropriate TFileIo method.
 | 
| sl@0 |    203 | 
 | 
| sl@0 |    204 | No virtual methods here! sqlite3_file contains data members. If a virtual method is added, that will shift the offset of the
 | 
| sl@0 |    205 | data members from the beginning of the sqlite3_file  object by 4 bytes. This is not what SQLite (C code) expects.
 | 
| sl@0 |    206 | 
 | 
| sl@0 |    207 | @internalComponent
 | 
| sl@0 |    208 | 
 | 
| sl@0 |    209 | @see TVfs
 | 
| sl@0 |    210 | @see TFileIo
 | 
| sl@0 |    211 | @see TVfs::Open()
 | 
| sl@0 |    212 | */
 | 
| sl@0 |    213 | NONSHARABLE_STRUCT(TDbFile) : public sqlite3_file 
 | 
| sl@0 |    214 | 	{
 | 
| sl@0 |    215 | 	inline TDbFile();
 | 
| sl@0 |    216 | 	RFileBuf64	iFileBuf;
 | 
| sl@0 |    217 | 	HBufC*		iFullName;				//Used for the "delete file" operation
 | 
| sl@0 |    218 | 	TInt 		iSharedLockByte;
 | 
| sl@0 |    219 | 	TInt		iLockType;
 | 
| sl@0 |    220 | 	TBool		iReadOnly;
 | 
| sl@0 |    221 | 	TInt		iSectorSize;
 | 
| sl@0 |    222 | 	TInt		iDeviceCharacteristics;
 | 
| sl@0 |    223 | 	};
 | 
| sl@0 |    224 | 
 | 
| sl@0 |    225 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |    226 | //////////////////////////  TFileIo  //////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |    227 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |    228 | 
 | 
| sl@0 |    229 | /**
 | 
| sl@0 |    230 | TFileIo class offers static methods for performing operations on a file.
 | 
| sl@0 |    231 | Every TFileIo method has a pointer to a sqlite3_file instance (so, a TDbFile instance) as its first argument.
 | 
| sl@0 |    232 | 
 | 
| sl@0 |    233 | SQLite never accesses the file system directly, only through function pointers, data members of the sqlite3_io_methods structure.
 | 
| sl@0 |    234 | The OS porting layer defines a single instance of sqlite3_io_methods structure, TheFileIoApi, and uses the TFileIo to initialize the 
 | 
| sl@0 |    235 | sqlite3_io_methods data members (function pointers).
 | 
| sl@0 |    236 | Every time when SQLite creates/opens a file using TVfs::Open(), TVfs::Open() will pass back to SQLite a pointer to the single
 | 
| sl@0 |    237 | initialized sqlite3_io_methods instance (TheFileIoApi) that will be used later by SQLite for accessing the file.
 | 
| sl@0 |    238 | 
 | 
| sl@0 |    239 | @internalComponent
 | 
| sl@0 |    240 | 
 | 
| sl@0 |    241 | @see TVfs
 | 
| sl@0 |    242 | @see TVfs::Open()
 | 
| sl@0 |    243 | @see TDbFile
 | 
| sl@0 |    244 | */
 | 
| sl@0 |    245 | NONSHARABLE_CLASS(TFileIo)
 | 
| sl@0 |    246 | 	{
 | 
| sl@0 |    247 | public:	
 | 
| sl@0 |    248 | 	static int Close(sqlite3_file* aDbFile);
 | 
| sl@0 |    249 | 	static int Read(sqlite3_file* aDbFile, void* aBuf, int aAmt, sqlite3_int64 aOffset);
 | 
| sl@0 |    250 | 	static int Write(sqlite3_file* aDbFile, const void* aData, int aAmt, sqlite3_int64 aOffset);
 | 
| sl@0 |    251 | 	static int Truncate(sqlite3_file* aDbFile, sqlite3_int64 aLength);
 | 
| sl@0 |    252 | 	static int Sync(sqlite3_file* aDbFile, int aFlags);
 | 
| sl@0 |    253 | 	static int FileSize(sqlite3_file* aDbFile, sqlite3_int64* aSize);
 | 
| sl@0 |    254 | 	static int Lock(sqlite3_file* aDbFile, int aLockType);
 | 
| sl@0 |    255 | 	static int Unlock(sqlite3_file* aDbFile, int aLockType);
 | 
| sl@0 |    256 | 	static int CheckReservedLock(sqlite3_file* aDbFile, int *aResOut);
 | 
| sl@0 |    257 | 	static int FileControl(sqlite3_file* aDbFile, int aOp, void* aArg);
 | 
| sl@0 |    258 | 	static int SectorSize(sqlite3_file* aDbFile);
 | 
| sl@0 |    259 | 	static int DeviceCharacteristics(sqlite3_file* aDbFile);
 | 
| sl@0 |    260 | private:
 | 
| sl@0 |    261 | 	static TInt GetReadLock(TDbFile& aDbFile);	
 | 
| sl@0 |    262 | 	static TInt UnlockReadLock(TDbFile& aDbFile);	
 | 
| sl@0 |    263 | 	};
 | 
| sl@0 |    264 | 
 | 
| sl@0 |    265 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |    266 | //////////////////////////  TVfs     //////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |    267 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |    268 | 
 | 
| sl@0 |    269 | /**
 | 
| sl@0 |    270 | @return Pointer to the sqlite3_vfs interface
 | 
| sl@0 |    271 | 
 | 
| sl@0 |    272 | @see TVfs
 | 
| sl@0 |    273 | 
 | 
| sl@0 |    274 | @panic SqliteMt 7 Process local storage initialization failure (the emulator builds only)
 | 
| sl@0 |    275 | 
 | 
| sl@0 |    276 | @internalComponent
 | 
| sl@0 |    277 | */
 | 
| sl@0 |    278 | sqlite3_vfs* VfsApi();//Platform dependend implementation
 | 
| sl@0 |    279 | 
 | 
| sl@0 |    280 | /**
 | 
| sl@0 |    281 | TVfs ("VFS" - virtual file system) class offers methods for creating/openning a file, deleting a file,
 | 
| sl@0 |    282 | a "sleep" method, a "time" method, a "rand" method, etc.
 | 
| sl@0 |    283 | SQLite never accesses the OS API directly, only through the API offered by TVfs and TFileIo classes.
 | 
| sl@0 |    284 | 
 | 
| sl@0 |    285 | @internalComponent
 | 
| sl@0 |    286 | 
 | 
| sl@0 |    287 | @see TFileIo
 | 
| sl@0 |    288 | */
 | 
| sl@0 |    289 | NONSHARABLE_CLASS(TVfs)
 | 
| sl@0 |    290 | 	{
 | 
| sl@0 |    291 | public:		
 | 
| sl@0 |    292 | 	static int Open(sqlite3_vfs* aVfs, const char* aFileName, sqlite3_file* aDbFile, int aFlags, int* aOutFlags);
 | 
| sl@0 |    293 | 	static int Delete(sqlite3_vfs* aVfs, const char* aFileName, int aSyncDir);	
 | 
| sl@0 |    294 | 	static int Access(sqlite3_vfs* aVfs, const char* aFileName, int aFlags, int* aResOut);
 | 
| sl@0 |    295 | 	static int FullPathName(sqlite3_vfs* aVfs, const char* aRelative, int aBufLen, char* aBuf);
 | 
| sl@0 |    296 | 	static int Randomness(sqlite3_vfs* aVfs, int aBufLen, char* aBuf);
 | 
| sl@0 |    297 | 	static int Sleep(sqlite3_vfs* aVfs, int aMicrosec);
 | 
| sl@0 |    298 | 	static int CurrentTime(sqlite3_vfs* aVfs, double* aNow);
 | 
| sl@0 |    299 | 	static int GetLastError(sqlite3_vfs *sVfs, int aBufLen, char* aBuf);
 | 
| sl@0 |    300 | private:
 | 
| sl@0 |    301 | 	static inline TInt DoGetVolumeIoParamInfo(RFs& aFs, TInt aDriveNo, TVolumeIOParamInfo& aVolumeInfo);
 | 
| sl@0 |    302 | 	static TInt DoGetDeviceCharacteristics(const TDriveInfo& aDriveInfo, const TVolumeIOParamInfo& aVolumeInfo);
 | 
| sl@0 |    303 | 	static TInt DoGetSectorSize(const TDriveInfo& aDriveInfo, const TVolumeIOParamInfo& aVolumeInfo);
 | 
| sl@0 |    304 | 	static TInt DoGetDeviceCharacteristicsAndSectorSize(TDbFile& aDbFile, TInt& aRecReadBufSize);
 | 
| sl@0 |    305 | 	static TInt DoFileSizeCorruptionCheck(TDbFile& aDbFile, const TDesC& aFname, TInt aFmode);
 | 
| sl@0 |    306 | 	};
 | 
| sl@0 |    307 | 	
 | 
| sl@0 |    308 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |    309 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
| sl@0 |    310 | 
 | 
| sl@0 |    311 | #endif//SQLITE_OS_SYMBIAN
 | 
| sl@0 |    312 | 
 | 
| sl@0 |    313 | #endif//OS_SYMBIAN_H
 |