xref: /sqlite-3.40.0/src/os_common.h (revision 97c8ec32)
1 /*
2 ** 2004 May 22
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 ******************************************************************************
12 **
13 ** This file contains macros and a little bit of code that is common to
14 ** all of the platform-specific files (os_*.c) and is #included into those
15 ** files.
16 **
17 ** This file should be #included by the os_*.c files only.  It is not a
18 ** general purpose header file.
19 */
20 
21 /*
22 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
23 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
24 ** switch.  The following code should catch this problem at compile-time.
25 */
26 #ifdef MEMORY_DEBUG
27 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
28 #endif
29 
30 
31 /*
32  * When testing, this global variable stores the location of the
33  * pending-byte in the database file.
34  */
35 #ifdef SQLITE_TEST
36 unsigned int sqlite3_pending_byte = 0x40000000;
37 #endif
38 
39 #ifdef SQLITE_DEBUG
40 int sqlite3_os_trace = 0;
41 #define OSTRACE1(X)         if( sqlite3_os_trace ) sqlite3DebugPrintf(X)
42 #define OSTRACE2(X,Y)       if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y)
43 #define OSTRACE3(X,Y,Z)     if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z)
44 #define OSTRACE4(X,Y,Z,A)   if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A)
45 #define OSTRACE5(X,Y,Z,A,B) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A,B)
46 #define OSTRACE6(X,Y,Z,A,B,C) \
47     if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
48 #define OSTRACE7(X,Y,Z,A,B,C,D) \
49     if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
50 #else
51 #define OSTRACE1(X)
52 #define OSTRACE2(X,Y)
53 #define OSTRACE3(X,Y,Z)
54 #define OSTRACE4(X,Y,Z,A)
55 #define OSTRACE5(X,Y,Z,A,B)
56 #define OSTRACE6(X,Y,Z,A,B,C)
57 #define OSTRACE7(X,Y,Z,A,B,C,D)
58 #endif
59 
60 /*
61 ** Macros for performance tracing.  Normally turned off.  Only works
62 ** on i486 hardware.
63 */
64 #ifdef SQLITE_PERFORMANCE_TRACE
65 __inline__ unsigned long long int hwtime(void){
66   unsigned long long int x;
67   __asm__("rdtsc\n\t"
68           "mov %%edx, %%ecx\n\t"
69           :"=A" (x));
70   return x;
71 }
72 static unsigned long long int g_start;
73 static unsigned int elapse;
74 #define TIMER_START       g_start=hwtime()
75 #define TIMER_END         elapse=hwtime()-g_start
76 #define TIMER_ELAPSED     elapse
77 #else
78 #define TIMER_START
79 #define TIMER_END
80 #define TIMER_ELAPSED     0
81 #endif
82 
83 /*
84 ** If we compile with the SQLITE_TEST macro set, then the following block
85 ** of code will give us the ability to simulate a disk I/O error.  This
86 ** is used for testing the I/O recovery logic.
87 */
88 #ifdef SQLITE_TEST
89 int sqlite3_io_error_hit = 0;
90 int sqlite3_io_error_pending = 0;
91 int sqlite3_io_error_persist = 0;
92 int sqlite3_diskfull_pending = 0;
93 int sqlite3_diskfull = 0;
94 #define SimulateIOError(CODE)  \
95   if( sqlite3_io_error_pending || sqlite3_io_error_hit ) \
96      if( sqlite3_io_error_pending-- == 1 \
97          || (sqlite3_io_error_persist && sqlite3_io_error_hit) ) \
98                 { local_ioerr(); CODE; }
99 static void local_ioerr(){
100   IOTRACE(("IOERR\n"));
101   sqlite3_io_error_hit = 1;
102 }
103 #define SimulateDiskfullError(CODE) \
104    if( sqlite3_diskfull_pending ){ \
105      if( sqlite3_diskfull_pending == 1 ){ \
106        local_ioerr(); \
107        sqlite3_diskfull = 1; \
108        sqlite3_io_error_hit = 1; \
109        CODE; \
110      }else{ \
111        sqlite3_diskfull_pending--; \
112      } \
113    }
114 #else
115 #define SimulateIOError(A)
116 #define SimulateDiskfullError(A)
117 #endif
118 
119 /*
120 ** When testing, keep a count of the number of open files.
121 */
122 #ifdef SQLITE_TEST
123 int sqlite3_open_file_count = 0;
124 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
125 #else
126 #define OpenCounter(X)
127 #endif
128