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 : /*
22 : ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
23 : ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
24 : ** switch. The following code should catch this problem at compile-time.
25 : */
26 : #ifdef MEMORY_DEBUG
27 : # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
28 : #endif
29 :
30 :
31 : /*
32 : * When testing, this global variable stores the location of the
33 : * pending-byte in the database file.
34 : */
35 : #ifdef SQLITE_TEST
36 : unsigned int sqlite3_pending_byte = 0x40000000;
37 : #endif
38 :
39 : int sqlite3_os_trace = 0;
40 : #ifdef SQLITE_DEBUG
41 : #define OSTRACE1(X) if( sqlite3_os_trace ) sqlite3DebugPrintf(X)
42 : #define OSTRACE2(X,Y) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y)
43 : #define OSTRACE3(X,Y,Z) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z)
44 : #define OSTRACE4(X,Y,Z,A) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A)
45 : #define OSTRACE5(X,Y,Z,A,B) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A,B)
46 : #define OSTRACE6(X,Y,Z,A,B,C) \
47 : if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
48 : #define OSTRACE7(X,Y,Z,A,B,C,D) \
49 : if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
50 : #else
51 : #define OSTRACE1(X)
52 : #define OSTRACE2(X,Y)
53 : #define OSTRACE3(X,Y,Z)
54 : #define OSTRACE4(X,Y,Z,A)
55 : #define OSTRACE5(X,Y,Z,A,B)
56 : #define OSTRACE6(X,Y,Z,A,B,C)
57 : #define OSTRACE7(X,Y,Z,A,B,C,D)
58 : #endif
59 :
60 : /*
61 : ** Macros for performance tracing. Normally turned off. Only works
62 : ** on i486 hardware.
63 : */
64 : #ifdef SQLITE_PERFORMANCE_TRACE
65 : __inline__ unsigned long long int hwtime(void){
66 : unsigned long long int x;
67 : __asm__("rdtsc\n\t"
68 : "mov %%edx, %%ecx\n\t"
69 : :"=A" (x));
70 : return x;
71 : }
72 : static unsigned long long int g_start;
73 : static unsigned int elapse;
74 : #define TIMER_START g_start=hwtime()
75 : #define TIMER_END elapse=hwtime()-g_start
76 : #define TIMER_ELAPSED elapse
77 : #else
78 : #define TIMER_START
79 : #define TIMER_END
80 : #define TIMER_ELAPSED 0
81 : #endif
82 :
83 : /*
84 : ** If we compile with the SQLITE_TEST macro set, then the following block
85 : ** of code will give us the ability to simulate a disk I/O error. This
86 : ** is used for testing the I/O recovery logic.
87 : */
88 : #ifdef SQLITE_TEST
89 : int sqlite3_io_error_hit = 0;
90 : int sqlite3_io_error_pending = 0;
91 : int sqlite3_io_error_persist = 0;
92 : int sqlite3_diskfull_pending = 0;
93 : int sqlite3_diskfull = 0;
94 : #define SimulateIOError(CODE) \
95 : if( sqlite3_io_error_pending || sqlite3_io_error_hit ) \
96 : if( sqlite3_io_error_pending-- == 1 \
97 : || (sqlite3_io_error_persist && sqlite3_io_error_hit) ) \
98 : { local_ioerr(); CODE; }
99 : static void local_ioerr(){
100 : IOTRACE(("IOERR\n"));
101 : sqlite3_io_error_hit = 1;
102 : }
103 : #define SimulateDiskfullError(CODE) \
104 : if( sqlite3_diskfull_pending ){ \
105 : if( sqlite3_diskfull_pending == 1 ){ \
106 : local_ioerr(); \
107 : sqlite3_diskfull = 1; \
108 : sqlite3_io_error_hit = 1; \
109 : CODE; \
110 : }else{ \
111 : sqlite3_diskfull_pending--; \
112 : } \
113 : }
114 : #else
115 : #define SimulateIOError(A)
116 : #define SimulateDiskfullError(A)
117 : #endif
118 :
119 : /*
120 : ** When testing, keep a count of the number of open files.
121 : */
122 : #ifdef SQLITE_TEST
123 : int sqlite3_open_file_count = 0;
124 : #define OpenCounter(X) sqlite3_open_file_count+=(X)
125 : #else
126 : #define OpenCounter(X)
127 : #endif
128 :
129 : /*
130 : ** sqlite3GenericMalloc
131 : ** sqlite3GenericRealloc
132 : ** sqlite3GenericOsFree
133 : ** sqlite3GenericAllocationSize
134 : **
135 : ** Implementation of the os level dynamic memory allocation interface in terms
136 : ** of the standard malloc(), realloc() and free() found in many operating
137 : ** systems. No rocket science here.
138 : **
139 : ** There are two versions of these four functions here. The version
140 : ** implemented here is only used if memory-management or memory-debugging is
141 : ** enabled. This version allocates an extra 8-bytes at the beginning of each
142 : ** block and stores the size of the allocation there.
143 : **
144 : ** If neither memory-management or debugging is enabled, the second
145 : ** set of implementations is used instead.
146 : */
147 : #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || defined (SQLITE_MEMDEBUG)
148 : void *sqlite3GenericMalloc(int n){
149 : char *p = (char *)malloc(n+8);
150 : assert(n>0);
151 : assert(sizeof(int)<=8);
152 : if( p ){
153 : *(int *)p = n;
154 : p += 8;
155 : }
156 : return (void *)p;
157 : }
158 : void *sqlite3GenericRealloc(void *p, int n){
159 : char *p2 = ((char *)p - 8);
160 : assert(n>0);
161 : p2 = (char*)realloc(p2, n+8);
162 : if( p2 ){
163 : *(int *)p2 = n;
164 : p2 += 8;
165 : }
166 : return (void *)p2;
167 : }
168 : void sqlite3GenericFree(void *p){
169 : assert(p);
170 : free((void *)((char *)p - 8));
171 : }
172 : int sqlite3GenericAllocationSize(void *p){
173 : return p ? *(int *)((char *)p - 8) : 0;
174 : }
175 : #else
176 36897 : void *sqlite3GenericMalloc(int n){
177 36897 : char *p = (char *)malloc(n);
178 36897 : return (void *)p;
179 : }
180 734 : void *sqlite3GenericRealloc(void *p, int n){
181 : assert(n>0);
182 734 : p = realloc(p, n);
183 734 : return p;
184 : }
185 36723 : void sqlite3GenericFree(void *p){
186 : assert(p);
187 36723 : free(p);
188 36723 : }
189 : /* Never actually used, but needed for the linker */
190 0 : int sqlite3GenericAllocationSize(void *p){ return 0; }
191 : #endif
192 :
193 : /*
194 : ** The default size of a disk sector
195 : */
196 : #ifndef PAGER_SECTOR_SIZE
197 : # define PAGER_SECTOR_SIZE 512
198 : #endif
|