blob: 5fab7a464217c7e3d445ebd04cb9bc30bd2bf19a [file] [log] [blame]
drhbbd42a62004-05-22 17:41:58 +00001/*
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.
danielk1977822a5162008-05-16 04:51:54 +000019**
shane9bcbdad2008-05-29 20:22:37 +000020** $Id: os_common.h,v 1.37 2008/05/29 20:22:37 shane Exp $
drhbbd42a62004-05-22 17:41:58 +000021*/
shane9bcbdad2008-05-29 20:22:37 +000022#ifndef _OS_COMMON_H_
23#define _OS_COMMON_H_
drhbbd42a62004-05-22 17:41:58 +000024
drha9600bc2004-08-04 14:44:33 +000025/*
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
danielk19772349c3d2005-09-16 10:13:41 +000035/*
36 * When testing, this global variable stores the location of the
37 * pending-byte in the database file.
38 */
39#ifdef SQLITE_TEST
drh49285702005-09-17 15:20:26 +000040unsigned int sqlite3_pending_byte = 0x40000000;
danielk19772349c3d2005-09-16 10:13:41 +000041#endif
42
drh73be5012007-08-08 12:11:21 +000043#ifdef SQLITE_DEBUG
mlcreech3a00f902008-03-04 17:45:01 +000044int 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)
drh4f0c5872007-03-26 22:05:01 +000050#define OSTRACE6(X,Y,Z,A,B,C) \
mlcreech3a00f902008-03-04 17:45:01 +000051 if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
drh4f0c5872007-03-26 22:05:01 +000052#define OSTRACE7(X,Y,Z,A,B,C,D) \
mlcreech3a00f902008-03-04 17:45:01 +000053 if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
drha9600bc2004-08-04 14:44:33 +000054#else
drh4f0c5872007-03-26 22:05:01 +000055#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)
drha9600bc2004-08-04 14:44:33 +000062#endif
drhbbd42a62004-05-22 17:41:58 +000063
64/*
65** Macros for performance tracing. Normally turned off. Only works
66** on i486 hardware.
67*/
drha9600bc2004-08-04 14:44:33 +000068#ifdef SQLITE_PERFORMANCE_TRACE
shane9bcbdad2008-05-29 20:22:37 +000069
70/*
71** hwtime.h contains inline assembler code for implementing
72** high-performance timing routines.
73*/
74#include "hwtime.h"
75
76static sqlite_uint64 g_start;
77static 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
drhbbd42a62004-05-22 17:41:58 +000081#else
82#define TIMER_START
83#define TIMER_END
shane9bcbdad2008-05-29 20:22:37 +000084#define TIMER_ELAPSED ((sqlite_uint64)0)
drhbbd42a62004-05-22 17:41:58 +000085#endif
86
drhbbd42a62004-05-22 17:41:58 +000087/*
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
drh1aa5af12008-03-07 19:51:14 +000093int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
94int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
95int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
96int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
97int sqlite3_io_error_benign = 0; /* True if errors are benign */
drh047d4832004-10-01 14:38:02 +000098int sqlite3_diskfull_pending = 0;
drhf307a4a2005-09-09 10:46:19 +000099int sqlite3_diskfull = 0;
drh1aa5af12008-03-07 19:51:14 +0000100#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
drh59685932006-09-14 13:47:11 +0000101#define SimulateIOError(CODE) \
drh1aa5af12008-03-07 19:51:14 +0000102 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
103 || sqlite3_io_error_pending-- == 1 ) \
104 { local_ioerr(); CODE; }
drhbbd42a62004-05-22 17:41:58 +0000105static void local_ioerr(){
drh538f5702007-04-13 02:14:30 +0000106 IOTRACE(("IOERR\n"));
drh1aa5af12008-03-07 19:51:14 +0000107 sqlite3_io_error_hit++;
108 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
drhbbd42a62004-05-22 17:41:58 +0000109}
drh59685932006-09-14 13:47:11 +0000110#define SimulateDiskfullError(CODE) \
drhf307a4a2005-09-09 10:46:19 +0000111 if( sqlite3_diskfull_pending ){ \
112 if( sqlite3_diskfull_pending == 1 ){ \
113 local_ioerr(); \
114 sqlite3_diskfull = 1; \
drha7aea3d2007-03-15 12:51:16 +0000115 sqlite3_io_error_hit = 1; \
drh59685932006-09-14 13:47:11 +0000116 CODE; \
drhf307a4a2005-09-09 10:46:19 +0000117 }else{ \
118 sqlite3_diskfull_pending--; \
119 } \
120 }
drhbbd42a62004-05-22 17:41:58 +0000121#else
drh1aa5af12008-03-07 19:51:14 +0000122#define SimulateIOErrorBenign(X)
drhbbd42a62004-05-22 17:41:58 +0000123#define SimulateIOError(A)
adamd4fc93082006-09-14 16:57:19 +0000124#define SimulateDiskfullError(A)
drhbbd42a62004-05-22 17:41:58 +0000125#endif
126
127/*
128** When testing, keep a count of the number of open files.
129*/
130#ifdef SQLITE_TEST
131int sqlite3_open_file_count = 0;
132#define OpenCounter(X) sqlite3_open_file_count+=(X)
133#else
134#define OpenCounter(X)
135#endif
shane9bcbdad2008-05-29 20:22:37 +0000136
137#endif /* !defined(_OS_COMMON_H_) */