os_common.h 3.4 KB

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