Back to index

lightning-sunbird  0.9+nobinonly
os_common.h
Go to the documentation of this file.
00001 /*
00002 ** 2004 May 22
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 ******************************************************************************
00012 **
00013 ** This file contains macros and a little bit of code that is common to
00014 ** all of the platform-specific files (os_*.c) and is #included into those
00015 ** files.
00016 **
00017 ** This file should be #included by the os_*.c files only.  It is not a
00018 ** general purpose header file.
00019 */
00020 
00021 /*
00022 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
00023 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
00024 ** switch.  The following code should catch this problem at compile-time.
00025 */
00026 #ifdef MEMORY_DEBUG
00027 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
00028 #endif
00029 
00030 
00031 /*
00032  * When testing, this global variable stores the location of the
00033  * pending-byte in the database file.
00034  */
00035 #ifdef SQLITE_TEST
00036 unsigned int sqlite3_pending_byte = 0x40000000;
00037 #endif
00038 
00039 int sqlite3_os_trace = 0;
00040 #ifdef SQLITE_DEBUG
00041 static int last_page = 0;
00042 #define SEEK(X)           last_page=(X)
00043 #define TRACE1(X)         if( sqlite3_os_trace ) sqlite3DebugPrintf(X)
00044 #define TRACE2(X,Y)       if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y)
00045 #define TRACE3(X,Y,Z)     if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z)
00046 #define TRACE4(X,Y,Z,A)   if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A)
00047 #define TRACE5(X,Y,Z,A,B) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A,B)
00048 #define TRACE6(X,Y,Z,A,B,C) if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
00049 #define TRACE7(X,Y,Z,A,B,C,D) \
00050     if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
00051 #else
00052 #define SEEK(X)
00053 #define TRACE1(X)
00054 #define TRACE2(X,Y)
00055 #define TRACE3(X,Y,Z)
00056 #define TRACE4(X,Y,Z,A)
00057 #define TRACE5(X,Y,Z,A,B)
00058 #define TRACE6(X,Y,Z,A,B,C)
00059 #define TRACE7(X,Y,Z,A,B,C,D)
00060 #endif
00061 
00062 /*
00063 ** Macros for performance tracing.  Normally turned off.  Only works
00064 ** on i486 hardware.
00065 */
00066 #ifdef SQLITE_PERFORMANCE_TRACE
00067 __inline__ unsigned long long int hwtime(void){
00068   unsigned long long int x;
00069   __asm__("rdtsc\n\t"
00070           "mov %%edx, %%ecx\n\t"
00071           :"=A" (x));
00072   return x;
00073 }
00074 static unsigned long long int g_start;
00075 static unsigned int elapse;
00076 #define TIMER_START       g_start=hwtime()
00077 #define TIMER_END         elapse=hwtime()-g_start
00078 #define TIMER_ELAPSED     elapse
00079 #else
00080 #define TIMER_START
00081 #define TIMER_END
00082 #define TIMER_ELAPSED     0
00083 #endif
00084 
00085 /*
00086 ** If we compile with the SQLITE_TEST macro set, then the following block
00087 ** of code will give us the ability to simulate a disk I/O error.  This
00088 ** is used for testing the I/O recovery logic.
00089 */
00090 #ifdef SQLITE_TEST
00091 int sqlite3_io_error_hit = 0;
00092 int sqlite3_io_error_pending = 0;
00093 int sqlite3_diskfull_pending = 0;
00094 int sqlite3_diskfull = 0;
00095 #define SimulateIOError(A)  \
00096    if( sqlite3_io_error_pending ) \
00097      if( sqlite3_io_error_pending-- == 1 ){ local_ioerr(); return A; }
00098 static void local_ioerr(){
00099   sqlite3_io_error_hit = 1;  /* Really just a place to set a breakpoint */
00100 }
00101 #define SimulateDiskfullError \
00102    if( sqlite3_diskfull_pending ){ \
00103      if( sqlite3_diskfull_pending == 1 ){ \
00104        local_ioerr(); \
00105        sqlite3_diskfull = 1; \
00106        return SQLITE_FULL; \
00107      }else{ \
00108        sqlite3_diskfull_pending--; \
00109      } \
00110    }
00111 #else
00112 #define SimulateIOError(A)
00113 #define SimulateDiskfullError
00114 #endif
00115 
00116 /*
00117 ** When testing, keep a count of the number of open files.
00118 */
00119 #ifdef SQLITE_TEST
00120 int sqlite3_open_file_count = 0;
00121 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
00122 #else
00123 #define OpenCounter(X)
00124 #endif
00125 
00126 /*
00127 ** sqlite3GenericMalloc
00128 ** sqlite3GenericRealloc
00129 ** sqlite3GenericOsFree
00130 ** sqlite3GenericAllocationSize
00131 **
00132 ** Implementation of the os level dynamic memory allocation interface in terms
00133 ** of the standard malloc(), realloc() and free() found in many operating
00134 ** systems. No rocket science here.
00135 **
00136 ** There are two versions of these four functions here. The version
00137 ** implemented here is only used if memory-management or memory-debugging is
00138 ** enabled. This version allocates an extra 8-bytes at the beginning of each
00139 ** block and stores the size of the allocation there.
00140 **
00141 ** If neither memory-management or debugging is enabled, the second
00142 ** set of implementations is used instead.
00143 */
00144 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || defined (SQLITE_MEMDEBUG)
00145 void *sqlite3GenericMalloc(int n){
00146   char *p = (char *)malloc(n+8);
00147   assert(n>0);
00148   assert(sizeof(int)<=8);
00149   if( p ){
00150     *(int *)p = n;
00151     p += 8;
00152   }
00153   return (void *)p;
00154 }
00155 void *sqlite3GenericRealloc(void *p, int n){
00156   char *p2 = ((char *)p - 8);
00157   assert(n>0);
00158   p2 = (char*)realloc(p2, n+8);
00159   if( p2 ){
00160     *(int *)p2 = n;
00161     p2 += 8;
00162   }
00163   return (void *)p2;
00164 }
00165 void sqlite3GenericFree(void *p){
00166   assert(p);
00167   free((void *)((char *)p - 8));
00168 }
00169 int sqlite3GenericAllocationSize(void *p){
00170   return p ? *(int *)((char *)p - 8) : 0;
00171 }
00172 #else
00173 void *sqlite3GenericMalloc(int n){
00174   char *p = (char *)malloc(n);
00175   return (void *)p;
00176 }
00177 void *sqlite3GenericRealloc(void *p, int n){
00178   assert(n>0);
00179   p = realloc(p, n);
00180   return p;
00181 }
00182 void sqlite3GenericFree(void *p){
00183   assert(p);
00184   free(p);
00185 }
00186 /* Never actually used, but needed for the linker */
00187 int sqlite3GenericAllocationSize(void *p){ return 0; }
00188 #endif