xref: /sqlite-3.40.0/src/os_common.h (revision 4dcbdbff)
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 int sqlite3_os_trace = 0;
32 #ifdef SQLITE_DEBUG
33 static int last_page = 0;
34 #define SEEK(X)           last_page=(X)
35 #define TRACE1(X)         if( sqlite3_os_trace ) sqlite3DebugPrintf(X)
36 #define TRACE2(X,Y)       if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y)
37 #define TRACE3(X,Y,Z)     if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z)
38 #define TRACE4(X,Y,Z,A)   if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A)
39 #define TRACE5(X,Y,Z,A,B) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A,B)
40 #define TRACE6(X,Y,Z,A,B,C) if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
41 #define TRACE7(X,Y,Z,A,B,C,D) \
42     if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
43 #else
44 #define SEEK(X)
45 #define TRACE1(X)
46 #define TRACE2(X,Y)
47 #define TRACE3(X,Y,Z)
48 #define TRACE4(X,Y,Z,A)
49 #define TRACE5(X,Y,Z,A,B)
50 #define TRACE6(X,Y,Z,A,B,C)
51 #define TRACE7(X,Y,Z,A,B,C,D)
52 #endif
53 
54 /*
55 ** Macros for performance tracing.  Normally turned off.  Only works
56 ** on i486 hardware.
57 */
58 #ifdef SQLITE_PERFORMANCE_TRACE
59 __inline__ unsigned long long int hwtime(void){
60   unsigned long long int x;
61   __asm__("rdtsc\n\t"
62           "mov %%edx, %%ecx\n\t"
63           :"=A" (x));
64   return x;
65 }
66 static unsigned long long int g_start;
67 static unsigned int elapse;
68 #define TIMER_START       g_start=hwtime()
69 #define TIMER_END         elapse=hwtime()-g_start
70 #define TIMER_ELAPSED     elapse
71 #else
72 #define TIMER_START
73 #define TIMER_END
74 #define TIMER_ELAPSED     0
75 #endif
76 
77 /*
78 ** If we compile with the SQLITE_TEST macro set, then the following block
79 ** of code will give us the ability to simulate a disk I/O error.  This
80 ** is used for testing the I/O recovery logic.
81 */
82 #ifdef SQLITE_TEST
83 int sqlite3_io_error_pending = 0;
84 int sqlite3_diskfull_pending = 0;
85 #define SimulateIOError(A)  \
86    if( sqlite3_io_error_pending ) \
87      if( sqlite3_io_error_pending-- == 1 ){ local_ioerr(); return A; }
88 static void local_ioerr(){
89   sqlite3_io_error_pending = 0;  /* Really just a place to set a breakpoint */
90 }
91 #define SimulateDiskfullError \
92    if( sqlite3_diskfull_pending ) \
93      if( sqlite3_diskfull_pending-- == 1 ){ local_ioerr(); return SQLITE_FULL; }
94 #else
95 #define SimulateIOError(A)
96 #define SimulateDiskfullError
97 #endif
98 
99 /*
100 ** When testing, keep a count of the number of open files.
101 */
102 #ifdef SQLITE_TEST
103 int sqlite3_open_file_count = 0;
104 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
105 #else
106 #define OpenCounter(X)
107 #endif
108