os_common.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
  22. ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
  23. ** switch. The following code should catch this problem at compile-time.
  24. */
  25. #ifdef MEMORY_DEBUG
  26. # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
  27. #endif
  28. /*
  29. * When testing, this global variable stores the location of the
  30. * pending-byte in the database file.
  31. */
  32. #ifdef SQLITE_TEST
  33. unsigned int sqlite3_pending_byte = 0x40000000;
  34. #endif
  35. #ifdef SQLITE_DEBUG
  36. int sqlite3_os_trace = 0;
  37. #define OSTRACE1(X) if( sqlite3_os_trace ) sqlite3DebugPrintf(X)
  38. #define OSTRACE2(X,Y) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y)
  39. #define OSTRACE3(X,Y,Z) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z)
  40. #define OSTRACE4(X,Y,Z,A) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A)
  41. #define OSTRACE5(X,Y,Z,A,B) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A,B)
  42. #define OSTRACE6(X,Y,Z,A,B,C) \
  43. if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
  44. #define OSTRACE7(X,Y,Z,A,B,C,D) \
  45. if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
  46. #else
  47. #define OSTRACE1(X)
  48. #define OSTRACE2(X,Y)
  49. #define OSTRACE3(X,Y,Z)
  50. #define OSTRACE4(X,Y,Z,A)
  51. #define OSTRACE5(X,Y,Z,A,B)
  52. #define OSTRACE6(X,Y,Z,A,B,C)
  53. #define OSTRACE7(X,Y,Z,A,B,C,D)
  54. #endif
  55. /*
  56. ** Macros for performance tracing. Normally turned off. Only works
  57. ** on i486 hardware.
  58. */
  59. #ifdef SQLITE_PERFORMANCE_TRACE
  60. __inline__ unsigned long long int hwtime(void){
  61. unsigned long long int x;
  62. __asm__("rdtsc\n\t"
  63. "mov %%edx, %%ecx\n\t"
  64. :"=A" (x));
  65. return x;
  66. }
  67. static unsigned long long int g_start;
  68. static unsigned int elapse;
  69. #define TIMER_START g_start=hwtime()
  70. #define TIMER_END elapse=hwtime()-g_start
  71. #define TIMER_ELAPSED elapse
  72. #else
  73. #define TIMER_START
  74. #define TIMER_END
  75. #define TIMER_ELAPSED 0
  76. #endif
  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_hit = 0;
  84. int sqlite3_io_error_pending = 0;
  85. int sqlite3_io_error_persist = 0;
  86. int sqlite3_diskfull_pending = 0;
  87. int sqlite3_diskfull = 0;
  88. #define SimulateIOError(CODE) \
  89. if( sqlite3_io_error_pending || sqlite3_io_error_hit ) \
  90. if( sqlite3_io_error_pending-- == 1 \
  91. || (sqlite3_io_error_persist && sqlite3_io_error_hit) ) \
  92. { local_ioerr(); CODE; }
  93. static void local_ioerr(){
  94. IOTRACE(("IOERR\n"));
  95. sqlite3_io_error_hit = 1;
  96. }
  97. #define SimulateDiskfullError(CODE) \
  98. if( sqlite3_diskfull_pending ){ \
  99. if( sqlite3_diskfull_pending == 1 ){ \
  100. local_ioerr(); \
  101. sqlite3_diskfull = 1; \
  102. sqlite3_io_error_hit = 1; \
  103. CODE; \
  104. }else{ \
  105. sqlite3_diskfull_pending--; \
  106. } \
  107. }
  108. #else
  109. #define SimulateIOError(A)
  110. #define SimulateDiskfullError(A)
  111. #endif
  112. /*
  113. ** When testing, keep a count of the number of open files.
  114. */
  115. #ifdef SQLITE_TEST
  116. int sqlite3_open_file_count = 0;
  117. #define OpenCounter(X) sqlite3_open_file_count+=(X)
  118. #else
  119. #define OpenCounter(X)
  120. #endif