xref: /sqlite-3.40.0/src/os_common.h (revision eb48b062)
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 #ifndef _OS_COMMON_H_
21 #define _OS_COMMON_H_
22 
23 /*
24 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
25 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
26 ** switch.  The following code should catch this problem at compile-time.
27 */
28 #ifdef MEMORY_DEBUG
29 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
30 #endif
31 
32 #if defined(SQLITE_DEBUG) && \
33     (defined(SQLITE_TEST) || defined(SQLITE_FORCE_OS_TRACE))
34 # ifndef SQLITE_DEBUG_OS_TRACE
35 #   define SQLITE_DEBUG_OS_TRACE 0
36 # endif
37   int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
38 # define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
39 #else
40 # define OSTRACE(X)
41 #endif
42 
43 /*
44 ** Macros for performance tracing.  Normally turned off.  Only works
45 ** on i486 hardware.
46 */
47 #ifdef SQLITE_PERFORMANCE_TRACE
48 
49 /*
50 ** hwtime.h contains inline assembler code for implementing
51 ** high-performance timing routines.
52 */
53 #include "hwtime.h"
54 
55 static sqlite_uint64 g_start;
56 static sqlite_uint64 g_elapsed;
57 #define TIMER_START       g_start=sqlite3Hwtime()
58 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
59 #define TIMER_ELAPSED     g_elapsed
60 #else
61 #define TIMER_START
62 #define TIMER_END
63 #define TIMER_ELAPSED     ((sqlite_uint64)0)
64 #endif
65 
66 /*
67 ** If we compile with the SQLITE_TEST macro set, then the following block
68 ** of code will give us the ability to simulate a disk I/O error.  This
69 ** is used for testing the I/O recovery logic.
70 */
71 #ifdef SQLITE_TEST
72 int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
73 int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
74 int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
75 int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
76 int sqlite3_io_error_benign = 0;         /* True if errors are benign */
77 int sqlite3_diskfull_pending = 0;
78 int sqlite3_diskfull = 0;
79 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
80 #define SimulateIOError(CODE)  \
81   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
82        || sqlite3_io_error_pending-- == 1 )  \
83               { local_ioerr(); CODE; }
84 static void local_ioerr(){
85   IOTRACE(("IOERR\n"));
86   sqlite3_io_error_hit++;
87   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
88 }
89 #define SimulateDiskfullError(CODE) \
90    if( sqlite3_diskfull_pending ){ \
91      if( sqlite3_diskfull_pending == 1 ){ \
92        local_ioerr(); \
93        sqlite3_diskfull = 1; \
94        sqlite3_io_error_hit = 1; \
95        CODE; \
96      }else{ \
97        sqlite3_diskfull_pending--; \
98      } \
99    }
100 #else
101 #define SimulateIOErrorBenign(X)
102 #define SimulateIOError(A)
103 #define SimulateDiskfullError(A)
104 #endif
105 
106 /*
107 ** When testing, keep a count of the number of open files.
108 */
109 #ifdef SQLITE_TEST
110 int sqlite3_open_file_count = 0;
111 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
112 #else
113 #define OpenCounter(X)
114 #endif
115 
116 #endif /* !defined(_OS_COMMON_H_) */
117