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. |
| 19 | */ |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 20 | #ifndef _OS_COMMON_H_ |
| 21 | #define _OS_COMMON_H_ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 22 | |
drh | a9600bc | 2004-08-04 14:44:33 +0000 | [diff] [blame] | 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 | |
mistachkin | 34cf258 | 2015-04-02 17:46:52 +0000 | [diff] [blame] | 32 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 33 | ** Macros for performance tracing. Normally turned off. Only works |
| 34 | ** on i486 hardware. |
| 35 | */ |
drh | a9600bc | 2004-08-04 14:44:33 +0000 | [diff] [blame] | 36 | #ifdef SQLITE_PERFORMANCE_TRACE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 37 | |
mistachkin | c04c54b | 2016-02-11 21:28:16 +0000 | [diff] [blame] | 38 | /* |
| 39 | ** hwtime.h contains inline assembler code for implementing |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 40 | ** high-performance timing routines. |
| 41 | */ |
| 42 | #include "hwtime.h" |
| 43 | |
| 44 | static sqlite_uint64 g_start; |
| 45 | static sqlite_uint64 g_elapsed; |
| 46 | #define TIMER_START g_start=sqlite3Hwtime() |
| 47 | #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start |
| 48 | #define TIMER_ELAPSED g_elapsed |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 49 | #else |
| 50 | #define TIMER_START |
| 51 | #define TIMER_END |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 52 | #define TIMER_ELAPSED ((sqlite_uint64)0) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 55 | /* |
| 56 | ** If we compile with the SQLITE_TEST macro set, then the following block |
| 57 | ** of code will give us the ability to simulate a disk I/O error. This |
| 58 | ** is used for testing the I/O recovery logic. |
| 59 | */ |
mistachkin | c04c54b | 2016-02-11 21:28:16 +0000 | [diff] [blame] | 60 | #if defined(SQLITE_TEST) |
| 61 | extern int sqlite3_io_error_hit; |
| 62 | extern int sqlite3_io_error_hardhit; |
| 63 | extern int sqlite3_io_error_pending; |
| 64 | extern int sqlite3_io_error_persist; |
| 65 | extern int sqlite3_io_error_benign; |
| 66 | extern int sqlite3_diskfull_pending; |
| 67 | extern int sqlite3_diskfull; |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 68 | #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X) |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 69 | #define SimulateIOError(CODE) \ |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 70 | if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \ |
| 71 | || sqlite3_io_error_pending-- == 1 ) \ |
| 72 | { local_ioerr(); CODE; } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 73 | static void local_ioerr(){ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 74 | IOTRACE(("IOERR\n")); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 75 | sqlite3_io_error_hit++; |
| 76 | if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 77 | } |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 78 | #define SimulateDiskfullError(CODE) \ |
drh | f307a4a | 2005-09-09 10:46:19 +0000 | [diff] [blame] | 79 | if( sqlite3_diskfull_pending ){ \ |
| 80 | if( sqlite3_diskfull_pending == 1 ){ \ |
| 81 | local_ioerr(); \ |
| 82 | sqlite3_diskfull = 1; \ |
drh | a7aea3d | 2007-03-15 12:51:16 +0000 | [diff] [blame] | 83 | sqlite3_io_error_hit = 1; \ |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 84 | CODE; \ |
drh | f307a4a | 2005-09-09 10:46:19 +0000 | [diff] [blame] | 85 | }else{ \ |
| 86 | sqlite3_diskfull_pending--; \ |
| 87 | } \ |
| 88 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 89 | #else |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 90 | #define SimulateIOErrorBenign(X) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 91 | #define SimulateIOError(A) |
adamd | 4fc9308 | 2006-09-14 16:57:19 +0000 | [diff] [blame] | 92 | #define SimulateDiskfullError(A) |
mistachkin | c04c54b | 2016-02-11 21:28:16 +0000 | [diff] [blame] | 93 | #endif /* defined(SQLITE_TEST) */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | ** When testing, keep a count of the number of open files. |
| 97 | */ |
mistachkin | c04c54b | 2016-02-11 21:28:16 +0000 | [diff] [blame] | 98 | #if defined(SQLITE_TEST) |
| 99 | extern int sqlite3_open_file_count; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 100 | #define OpenCounter(X) sqlite3_open_file_count+=(X) |
| 101 | #else |
| 102 | #define OpenCounter(X) |
mistachkin | c04c54b | 2016-02-11 21:28:16 +0000 | [diff] [blame] | 103 | #endif /* defined(SQLITE_TEST) */ |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 104 | |
| 105 | #endif /* !defined(_OS_COMMON_H_) */ |