drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 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. |
danielk1977 | 822a516 | 2008-05-16 04:51:54 +0000 | [diff] [blame] | 19 | ** |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 20 | ** $Id: os_common.h,v 1.37 2008/05/29 20:22:37 shane Exp $ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 21 | */ |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 22 | #ifndef _OS_COMMON_H_ |
| 23 | #define _OS_COMMON_H_ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 24 | |
drh | a9600bc | 2004-08-04 14:44:33 +0000 | [diff] [blame] | 25 | /* |
| 26 | ** At least two bugs have slipped in because we changed the MEMORY_DEBUG |
| 27 | ** macro to SQLITE_DEBUG and some older makefiles have not yet made the |
| 28 | ** switch. The following code should catch this problem at compile-time. |
| 29 | */ |
| 30 | #ifdef MEMORY_DEBUG |
| 31 | # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead." |
| 32 | #endif |
| 33 | |
| 34 | |
danielk1977 | 2349c3d | 2005-09-16 10:13:41 +0000 | [diff] [blame] | 35 | /* |
| 36 | * When testing, this global variable stores the location of the |
| 37 | * pending-byte in the database file. |
| 38 | */ |
| 39 | #ifdef SQLITE_TEST |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 40 | unsigned int sqlite3_pending_byte = 0x40000000; |
danielk1977 | 2349c3d | 2005-09-16 10:13:41 +0000 | [diff] [blame] | 41 | #endif |
| 42 | |
drh | 73be501 | 2007-08-08 12:11:21 +0000 | [diff] [blame] | 43 | #ifdef SQLITE_DEBUG |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 44 | int sqlite3OSTrace = 0; |
| 45 | #define OSTRACE1(X) if( sqlite3OSTrace ) sqlite3DebugPrintf(X) |
| 46 | #define OSTRACE2(X,Y) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y) |
| 47 | #define OSTRACE3(X,Y,Z) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z) |
| 48 | #define OSTRACE4(X,Y,Z,A) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A) |
| 49 | #define OSTRACE5(X,Y,Z,A,B) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A,B) |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 50 | #define OSTRACE6(X,Y,Z,A,B,C) \ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 51 | if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C) |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 52 | #define OSTRACE7(X,Y,Z,A,B,C,D) \ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 53 | if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D) |
drh | a9600bc | 2004-08-04 14:44:33 +0000 | [diff] [blame] | 54 | #else |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 55 | #define OSTRACE1(X) |
| 56 | #define OSTRACE2(X,Y) |
| 57 | #define OSTRACE3(X,Y,Z) |
| 58 | #define OSTRACE4(X,Y,Z,A) |
| 59 | #define OSTRACE5(X,Y,Z,A,B) |
| 60 | #define OSTRACE6(X,Y,Z,A,B,C) |
| 61 | #define OSTRACE7(X,Y,Z,A,B,C,D) |
drh | a9600bc | 2004-08-04 14:44:33 +0000 | [diff] [blame] | 62 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | ** Macros for performance tracing. Normally turned off. Only works |
| 66 | ** on i486 hardware. |
| 67 | */ |
drh | a9600bc | 2004-08-04 14:44:33 +0000 | [diff] [blame] | 68 | #ifdef SQLITE_PERFORMANCE_TRACE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | ** hwtime.h contains inline assembler code for implementing |
| 72 | ** high-performance timing routines. |
| 73 | */ |
| 74 | #include "hwtime.h" |
| 75 | |
| 76 | static sqlite_uint64 g_start; |
| 77 | static sqlite_uint64 g_elapsed; |
| 78 | #define TIMER_START g_start=sqlite3Hwtime() |
| 79 | #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start |
| 80 | #define TIMER_ELAPSED g_elapsed |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 81 | #else |
| 82 | #define TIMER_START |
| 83 | #define TIMER_END |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 84 | #define TIMER_ELAPSED ((sqlite_uint64)0) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 85 | #endif |
| 86 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 87 | /* |
| 88 | ** If we compile with the SQLITE_TEST macro set, then the following block |
| 89 | ** of code will give us the ability to simulate a disk I/O error. This |
| 90 | ** is used for testing the I/O recovery logic. |
| 91 | */ |
| 92 | #ifdef SQLITE_TEST |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 93 | int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */ |
| 94 | int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */ |
| 95 | int sqlite3_io_error_pending = 0; /* Count down to first I/O error */ |
| 96 | int sqlite3_io_error_persist = 0; /* True if I/O errors persist */ |
| 97 | int sqlite3_io_error_benign = 0; /* True if errors are benign */ |
drh | 047d483 | 2004-10-01 14:38:02 +0000 | [diff] [blame] | 98 | int sqlite3_diskfull_pending = 0; |
drh | f307a4a | 2005-09-09 10:46:19 +0000 | [diff] [blame] | 99 | int sqlite3_diskfull = 0; |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 100 | #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X) |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 101 | #define SimulateIOError(CODE) \ |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 102 | if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \ |
| 103 | || sqlite3_io_error_pending-- == 1 ) \ |
| 104 | { local_ioerr(); CODE; } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 105 | static void local_ioerr(){ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 106 | IOTRACE(("IOERR\n")); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 107 | sqlite3_io_error_hit++; |
| 108 | if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 109 | } |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 110 | #define SimulateDiskfullError(CODE) \ |
drh | f307a4a | 2005-09-09 10:46:19 +0000 | [diff] [blame] | 111 | if( sqlite3_diskfull_pending ){ \ |
| 112 | if( sqlite3_diskfull_pending == 1 ){ \ |
| 113 | local_ioerr(); \ |
| 114 | sqlite3_diskfull = 1; \ |
drh | a7aea3d | 2007-03-15 12:51:16 +0000 | [diff] [blame] | 115 | sqlite3_io_error_hit = 1; \ |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 116 | CODE; \ |
drh | f307a4a | 2005-09-09 10:46:19 +0000 | [diff] [blame] | 117 | }else{ \ |
| 118 | sqlite3_diskfull_pending--; \ |
| 119 | } \ |
| 120 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 121 | #else |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 122 | #define SimulateIOErrorBenign(X) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 123 | #define SimulateIOError(A) |
adamd | 4fc9308 | 2006-09-14 16:57:19 +0000 | [diff] [blame] | 124 | #define SimulateDiskfullError(A) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 125 | #endif |
| 126 | |
| 127 | /* |
| 128 | ** When testing, keep a count of the number of open files. |
| 129 | */ |
| 130 | #ifdef SQLITE_TEST |
| 131 | int sqlite3_open_file_count = 0; |
| 132 | #define OpenCounter(X) sqlite3_open_file_count+=(X) |
| 133 | #else |
| 134 | #define OpenCounter(X) |
| 135 | #endif |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 136 | |
| 137 | #endif /* !defined(_OS_COMMON_H_) */ |