annotate sqlite/util.c @ 1509:ae369659010a trunk

[svn] Added awareness of the difference between stopped and playing.
author deitarion
date Sun, 06 Aug 2006 02:52:27 -0700
parents b6b61becdf4e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1434
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
2 ** 2001 September 15
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
3 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
4 ** The author disclaims copyright to this source code. In place of
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
5 ** a legal notice, here is a blessing:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
6 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
7 ** May you do good and not evil.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
8 ** May you find forgiveness for yourself and forgive others.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
9 ** May you share freely, never taking more than you give.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
10 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
11 *************************************************************************
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
12 ** Utility functions used throughout sqlite.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
13 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
14 ** This file contains functions for allocating memory, comparing
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
15 ** strings, and stuff like that.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
16 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
17 ** $Id: util.c,v 1.189 2006/04/08 19:14:53 drh Exp $
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
18 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
19 #include "sqliteInt.h"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
20 #include "os.h"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
21 #include <stdarg.h>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
22 #include <ctype.h>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
23
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
24 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
25 ** MALLOC WRAPPER ARCHITECTURE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
26 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
27 ** The sqlite code accesses dynamic memory allocation/deallocation by invoking
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
28 ** the following six APIs (which may be implemented as macros).
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
29 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
30 ** sqlite3Malloc()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
31 ** sqlite3MallocRaw()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
32 ** sqlite3Realloc()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
33 ** sqlite3ReallocOrFree()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
34 ** sqlite3Free()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
35 ** sqlite3AllocSize()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
36 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
37 ** The function sqlite3FreeX performs the same task as sqlite3Free and is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
38 ** guaranteed to be a real function. The same holds for sqlite3MallocX
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
39 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
40 ** The above APIs are implemented in terms of the functions provided in the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
41 ** operating-system interface. The OS interface is never accessed directly
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
42 ** by code outside of this file.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
43 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
44 ** sqlite3OsMalloc()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
45 ** sqlite3OsRealloc()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
46 ** sqlite3OsFree()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
47 ** sqlite3OsAllocationSize()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
48 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
49 ** Functions sqlite3MallocRaw() and sqlite3Realloc() may invoke
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
50 ** sqlite3_release_memory() if a call to sqlite3OsMalloc() or
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
51 ** sqlite3OsRealloc() fails (or if the soft-heap-limit for the thread is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
52 ** exceeded). Function sqlite3Malloc() usually invokes
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
53 ** sqlite3MallocRaw().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
54 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
55 ** MALLOC TEST WRAPPER ARCHITECTURE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
56 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
57 ** The test wrapper provides extra test facilities to ensure the library
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
58 ** does not leak memory and handles the failure of the underlying OS level
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
59 ** allocation system correctly. It is only present if the library is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
60 ** compiled with the SQLITE_MEMDEBUG macro set.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
61 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
62 ** * Guardposts to detect overwrites.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
63 ** * Ability to cause a specific Malloc() or Realloc() to fail.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
64 ** * Audit outstanding memory allocations (i.e check for leaks).
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
65 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
66
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
67 #define MAX(x,y) ((x)>(y)?(x):(y))
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
68
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
69 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
70 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
71 ** Set the soft heap-size limit for the current thread. Passing a negative
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
72 ** value indicates no limit.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
73 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
74 void sqlite3_soft_heap_limit(int n){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
75 ThreadData *pTd = sqlite3ThreadData();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
76 if( pTd ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
77 pTd->nSoftHeapLimit = n;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
78 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
79 sqlite3ReleaseThreadData();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
80 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
81
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
82 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
83 ** Release memory held by SQLite instances created by the current thread.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
84 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
85 int sqlite3_release_memory(int n){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
86 return sqlite3pager_release_memory(n);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
87 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
88 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
89 /* If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, then define a version
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
90 ** of sqlite3_release_memory() to be used by other code in this file.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
91 ** This is done for no better reason than to reduce the number of
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
92 ** pre-processor #ifndef statements.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
93 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
94 #define sqlite3_release_memory(x) 0 /* 0 == no memory freed */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
95 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
96
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
97 #ifdef SQLITE_MEMDEBUG
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
98 /*--------------------------------------------------------------------------
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
99 ** Begin code for memory allocation system test layer.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
100 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
101 ** Memory debugging is turned on by defining the SQLITE_MEMDEBUG macro.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
102 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
103 ** SQLITE_MEMDEBUG==1 -> Fence-posting only (thread safe)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
104 ** SQLITE_MEMDEBUG==2 -> Fence-posting + linked list of allocations (not ts)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
105 ** SQLITE_MEMDEBUG==3 -> Above + backtraces (not thread safe, req. glibc)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
106 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
107
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
108 /* Figure out whether or not to store backtrace() information for each malloc.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
109 ** The backtrace() function is only used if SQLITE_MEMDEBUG is set to 2 or
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
110 ** greater and glibc is in use. If we don't want to use backtrace(), then just
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
111 ** define it as an empty macro and set the amount of space reserved to 0.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
112 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
113 #if defined(__GLIBC__) && SQLITE_MEMDEBUG>2
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
114 extern int backtrace(void **, int);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
115 #define TESTALLOC_STACKSIZE 128
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
116 #define TESTALLOC_STACKFRAMES ((TESTALLOC_STACKSIZE-8)/sizeof(void*))
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
117 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
118 #define backtrace(x, y)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
119 #define TESTALLOC_STACKSIZE 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
120 #define TESTALLOC_STACKFRAMES 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
121 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
122
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
123 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
124 ** Number of 32-bit guard words. This should probably be a multiple of
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
125 ** 2 since on 64-bit machines we want the value returned by sqliteMalloc()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
126 ** to be 8-byte aligned.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
127 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
128 #ifndef TESTALLOC_NGUARD
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
129 # define TESTALLOC_NGUARD 2
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
130 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
131
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
132 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
133 ** Size reserved for storing file-name along with each malloc()ed blob.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
134 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
135 #define TESTALLOC_FILESIZE 64
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
136
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
137 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
138 ** Size reserved for storing the user string. Each time a Malloc() or Realloc()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
139 ** call succeeds, up to TESTALLOC_USERSIZE bytes of the string pointed to by
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
140 ** sqlite3_malloc_id are stored along with the other test system metadata.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
141 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
142 #define TESTALLOC_USERSIZE 64
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
143 const char *sqlite3_malloc_id = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
144
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
145 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
146 ** Blocks used by the test layer have the following format:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
147 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
148 ** <sizeof(void *) pNext pointer>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
149 ** <sizeof(void *) pPrev pointer>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
150 ** <TESTALLOC_NGUARD 32-bit guard words>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
151 ** <The application level allocation>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
152 ** <TESTALLOC_NGUARD 32-bit guard words>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
153 ** <32-bit line number>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
154 ** <TESTALLOC_FILESIZE bytes containing null-terminated file name>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
155 ** <TESTALLOC_STACKSIZE bytes of backtrace() output>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
156 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
157
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
158 #define TESTALLOC_OFFSET_GUARD1(p) (sizeof(void *) * 2)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
159 #define TESTALLOC_OFFSET_DATA(p) ( \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
160 TESTALLOC_OFFSET_GUARD1(p) + sizeof(u32) * TESTALLOC_NGUARD \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
161 )
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
162 #define TESTALLOC_OFFSET_GUARD2(p) ( \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
163 TESTALLOC_OFFSET_DATA(p) + sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
164 )
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
165 #define TESTALLOC_OFFSET_LINENUMBER(p) ( \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
166 TESTALLOC_OFFSET_GUARD2(p) + sizeof(u32) * TESTALLOC_NGUARD \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
167 )
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
168 #define TESTALLOC_OFFSET_FILENAME(p) ( \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
169 TESTALLOC_OFFSET_LINENUMBER(p) + sizeof(u32) \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
170 )
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
171 #define TESTALLOC_OFFSET_USER(p) ( \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
172 TESTALLOC_OFFSET_FILENAME(p) + TESTALLOC_FILESIZE \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
173 )
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
174 #define TESTALLOC_OFFSET_STACK(p) ( \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
175 TESTALLOC_OFFSET_USER(p) + TESTALLOC_USERSIZE + 8 - \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
176 (TESTALLOC_OFFSET_USER(p) % 8) \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
177 )
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
178
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
179 #define TESTALLOC_OVERHEAD ( \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
180 sizeof(void *)*2 + /* pPrev and pNext pointers */ \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
181 TESTALLOC_NGUARD*sizeof(u32)*2 + /* Guard words */ \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
182 sizeof(u32) + TESTALLOC_FILESIZE + /* File and line number */ \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
183 TESTALLOC_USERSIZE + /* User string */ \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
184 TESTALLOC_STACKSIZE /* backtrace() stack */ \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
185 )
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
186
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
187
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
188 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
189 ** For keeping track of the number of mallocs and frees. This
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
190 ** is used to check for memory leaks. The iMallocFail and iMallocReset
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
191 ** values are used to simulate malloc() failures during testing in
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
192 ** order to verify that the library correctly handles an out-of-memory
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
193 ** condition.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
194 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
195 int sqlite3_nMalloc; /* Number of sqliteMalloc() calls */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
196 int sqlite3_nFree; /* Number of sqliteFree() calls */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
197 int sqlite3_memUsed; /* TODO Total memory obtained from malloc */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
198 int sqlite3_memMax; /* TODO Mem usage high-water mark */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
199 int sqlite3_iMallocFail; /* Fail sqliteMalloc() after this many calls */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
200 int sqlite3_iMallocReset = -1; /* When iMallocFail reaches 0, set to this */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
201
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
202 void *sqlite3_pFirst = 0; /* Pointer to linked list of allocations */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
203 int sqlite3_nMaxAlloc = 0; /* High water mark of ThreadData.nAlloc */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
204 int sqlite3_mallocDisallowed = 0; /* assert() in sqlite3Malloc() if set */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
205 int sqlite3_isFail = 0; /* True if all malloc calls should fail */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
206 const char *sqlite3_zFile = 0; /* Filename to associate debug info with */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
207 int sqlite3_iLine = 0; /* Line number for debug info */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
208
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
209 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
210 ** Check for a simulated memory allocation failure. Return true if
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
211 ** the failure should be simulated. Return false to proceed as normal.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
212 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
213 int sqlite3TestMallocFail(){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
214 if( sqlite3_isFail ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
215 return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
216 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
217 if( sqlite3_iMallocFail>=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
218 sqlite3_iMallocFail--;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
219 if( sqlite3_iMallocFail==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
220 sqlite3_iMallocFail = sqlite3_iMallocReset;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
221 sqlite3_isFail = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
222 return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
223 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
224 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
225 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
226 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
227
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
228 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
229 ** The argument is a pointer returned by sqlite3OsMalloc() or xRealloc().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
230 ** assert() that the first and last (TESTALLOC_NGUARD*4) bytes are set to the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
231 ** values set by the applyGuards() function.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
232 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
233 static void checkGuards(u32 *p)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
234 {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
235 int i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
236 char *zAlloc = (char *)p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
237 char *z;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
238
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
239 /* First set of guard words */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
240 z = &zAlloc[TESTALLOC_OFFSET_GUARD1(p)];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
241 for(i=0; i<TESTALLOC_NGUARD; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
242 assert(((u32 *)z)[i]==0xdead1122);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
243 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
244
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
245 /* Second set of guard words */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
246 z = &zAlloc[TESTALLOC_OFFSET_GUARD2(p)];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
247 for(i=0; i<TESTALLOC_NGUARD; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
248 u32 guard = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
249 memcpy(&guard, &z[i*sizeof(u32)], sizeof(u32));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
250 assert(guard==0xdead3344);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
251 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
252 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
253
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
254 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
255 ** The argument is a pointer returned by sqlite3OsMalloc() or Realloc(). The
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
256 ** first and last (TESTALLOC_NGUARD*4) bytes are set to known values for use as
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
257 ** guard-posts.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
258 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
259 static void applyGuards(u32 *p)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
260 {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
261 int i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
262 char *z;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
263 char *zAlloc = (char *)p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
264
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
265 /* First set of guard words */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
266 z = &zAlloc[TESTALLOC_OFFSET_GUARD1(p)];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
267 for(i=0; i<TESTALLOC_NGUARD; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
268 ((u32 *)z)[i] = 0xdead1122;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
269 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
270
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
271 /* Second set of guard words */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
272 z = &zAlloc[TESTALLOC_OFFSET_GUARD2(p)];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
273 for(i=0; i<TESTALLOC_NGUARD; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
274 static const int guard = 0xdead3344;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
275 memcpy(&z[i*sizeof(u32)], &guard, sizeof(u32));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
276 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
277
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
278 /* Line number */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
279 z = &((char *)z)[TESTALLOC_NGUARD*sizeof(u32)]; /* Guard words */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
280 z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
281 memcpy(z, &sqlite3_iLine, sizeof(u32));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
282
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
283 /* File name */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
284 z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
285 strncpy(z, sqlite3_zFile, TESTALLOC_FILESIZE);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
286 z[TESTALLOC_FILESIZE - 1] = '\0';
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
287
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
288 /* User string */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
289 z = &zAlloc[TESTALLOC_OFFSET_USER(p)];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
290 z[0] = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
291 if( sqlite3_malloc_id ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
292 strncpy(z, sqlite3_malloc_id, TESTALLOC_USERSIZE);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
293 z[TESTALLOC_USERSIZE-1] = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
294 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
295
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
296 /* backtrace() stack */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
297 z = &zAlloc[TESTALLOC_OFFSET_STACK(p)];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
298 backtrace((void **)z, TESTALLOC_STACKFRAMES);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
299
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
300 /* Sanity check to make sure checkGuards() is working */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
301 checkGuards(p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
302 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
303
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
304 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
305 ** The argument is a malloc()ed pointer as returned by the test-wrapper.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
306 ** Return a pointer to the Os level allocation.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
307 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
308 static void *getOsPointer(void *p)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
309 {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
310 char *z = (char *)p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
311 return (void *)(&z[-1 * TESTALLOC_OFFSET_DATA(p)]);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
312 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
313
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
314
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
315 #if SQLITE_MEMDEBUG>1
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
316 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
317 ** The argument points to an Os level allocation. Link it into the threads list
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
318 ** of allocations.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
319 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
320 static void linkAlloc(void *p){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
321 void **pp = (void **)p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
322 pp[0] = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
323 pp[1] = sqlite3_pFirst;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
324 if( sqlite3_pFirst ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
325 ((void **)sqlite3_pFirst)[0] = p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
326 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
327 sqlite3_pFirst = p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
328 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
329
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
330 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
331 ** The argument points to an Os level allocation. Unlinke it from the threads
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
332 ** list of allocations.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
333 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
334 static void unlinkAlloc(void *p)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
335 {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
336 void **pp = (void **)p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
337 if( p==sqlite3_pFirst ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
338 assert(!pp[0]);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
339 assert(!pp[1] || ((void **)(pp[1]))[0]==p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
340 sqlite3_pFirst = pp[1];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
341 if( sqlite3_pFirst ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
342 ((void **)sqlite3_pFirst)[0] = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
343 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
344 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
345 void **pprev = pp[0];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
346 void **pnext = pp[1];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
347 assert(pprev);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
348 assert(pprev[1]==p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
349 pprev[1] = (void *)pnext;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
350 if( pnext ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
351 assert(pnext[0]==p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
352 pnext[0] = (void *)pprev;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
353 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
354 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
355 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
356
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
357 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
358 ** Pointer p is a pointer to an OS level allocation that has just been
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
359 ** realloc()ed. Set the list pointers that point to this entry to it's new
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
360 ** location.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
361 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
362 static void relinkAlloc(void *p)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
363 {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
364 void **pp = (void **)p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
365 if( pp[0] ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
366 ((void **)(pp[0]))[1] = p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
367 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
368 sqlite3_pFirst = p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
369 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
370 if( pp[1] ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
371 ((void **)(pp[1]))[0] = p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
372 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
373 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
374 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
375 #define linkAlloc(x)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
376 #define relinkAlloc(x)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
377 #define unlinkAlloc(x)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
378 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
379
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
380 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
381 ** This function sets the result of the Tcl interpreter passed as an argument
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
382 ** to a list containing an entry for each currently outstanding call made to
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
383 ** sqliteMalloc and friends by the current thread. Each list entry is itself a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
384 ** list, consisting of the following (in order):
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
385 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
386 ** * The number of bytes allocated
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
387 ** * The __FILE__ macro at the time of the sqliteMalloc() call.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
388 ** * The __LINE__ macro ...
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
389 ** * The value of the sqlite3_malloc_id variable ...
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
390 ** * The output of backtrace() (if available) ...
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
391 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
392 ** Todo: We could have a version of this function that outputs to stdout,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
393 ** to debug memory leaks when Tcl is not available.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
394 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
395 #if defined(TCLSH) && defined(SQLITE_DEBUG) && SQLITE_MEMDEBUG>1
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
396 #include <tcl.h>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
397 int sqlite3OutstandingMallocs(Tcl_Interp *interp){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
398 void *p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
399 Tcl_Obj *pRes = Tcl_NewObj();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
400 Tcl_IncrRefCount(pRes);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
401
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
402
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
403 for(p=sqlite3_pFirst; p; p=((void **)p)[1]){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
404 Tcl_Obj *pEntry = Tcl_NewObj();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
405 Tcl_Obj *pStack = Tcl_NewObj();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
406 char *z;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
407 u32 iLine;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
408 int nBytes = sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
409 char *zAlloc = (char *)p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
410 int i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
411
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
412 Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(nBytes));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
413
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
414 z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
415 Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
416
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
417 z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
418 memcpy(&iLine, z, sizeof(u32));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
419 Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(iLine));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
420
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
421 z = &zAlloc[TESTALLOC_OFFSET_USER(p)];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
422 Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
423
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
424 z = &zAlloc[TESTALLOC_OFFSET_STACK(p)];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
425 for(i=0; i<TESTALLOC_STACKFRAMES; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
426 char zHex[128];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
427 sprintf(zHex, "%p", ((void **)z)[i]);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
428 Tcl_ListObjAppendElement(0, pStack, Tcl_NewStringObj(zHex, -1));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
429 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
430
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
431 Tcl_ListObjAppendElement(0, pEntry, pStack);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
432 Tcl_ListObjAppendElement(0, pRes, pEntry);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
433 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
434
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
435 Tcl_ResetResult(interp);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
436 Tcl_SetObjResult(interp, pRes);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
437 Tcl_DecrRefCount(pRes);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
438 return TCL_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
439 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
440 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
441
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
442 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
443 ** This is the test layer's wrapper around sqlite3OsMalloc().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
444 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
445 static void * OSMALLOC(int n){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
446 sqlite3OsEnterMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
447 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
448 sqlite3_nMaxAlloc =
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
449 MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
450 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
451 assert( !sqlite3_mallocDisallowed );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
452 if( !sqlite3TestMallocFail() ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
453 u32 *p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
454 p = (u32 *)sqlite3OsMalloc(n + TESTALLOC_OVERHEAD);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
455 assert(p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
456 sqlite3_nMalloc++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
457 applyGuards(p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
458 linkAlloc(p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
459 sqlite3OsLeaveMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
460 return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
461 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
462 sqlite3OsLeaveMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
463 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
464 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
465
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
466 static int OSSIZEOF(void *p){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
467 if( p ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
468 u32 *pOs = (u32 *)getOsPointer(p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
469 return sqlite3OsAllocationSize(pOs) - TESTALLOC_OVERHEAD;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
470 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
471 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
472 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
473
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
474 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
475 ** This is the test layer's wrapper around sqlite3OsFree(). The argument is a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
476 ** pointer to the space allocated for the application to use.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
477 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
478 static void OSFREE(void *pFree){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
479 u32 *p; /* Pointer to the OS-layer allocation */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
480 sqlite3OsEnterMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
481 p = (u32 *)getOsPointer(pFree);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
482 checkGuards(p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
483 unlinkAlloc(p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
484 memset(pFree, 0x55, OSSIZEOF(pFree));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
485 sqlite3OsFree(p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
486 sqlite3_nFree++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
487 sqlite3OsLeaveMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
488 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
489
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
490 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
491 ** This is the test layer's wrapper around sqlite3OsRealloc().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
492 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
493 static void * OSREALLOC(void *pRealloc, int n){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
494 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
495 sqlite3_nMaxAlloc =
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
496 MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
497 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
498 assert( !sqlite3_mallocDisallowed );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
499 if( !sqlite3TestMallocFail() ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
500 u32 *p = (u32 *)getOsPointer(pRealloc);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
501 checkGuards(p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
502 p = sqlite3OsRealloc(p, n + TESTALLOC_OVERHEAD);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
503 applyGuards(p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
504 relinkAlloc(p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
505 return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
506 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
507 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
508 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
509
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
510 static void OSMALLOC_FAILED(){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
511 sqlite3_isFail = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
512 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
513
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
514 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
515 /* Define macros to call the sqlite3OsXXX interface directly if
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
516 ** the SQLITE_MEMDEBUG macro is not defined.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
517 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
518 #define OSMALLOC(x) sqlite3OsMalloc(x)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
519 #define OSREALLOC(x,y) sqlite3OsRealloc(x,y)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
520 #define OSFREE(x) sqlite3OsFree(x)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
521 #define OSSIZEOF(x) sqlite3OsAllocationSize(x)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
522 #define OSMALLOC_FAILED()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
523
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
524 #endif /* SQLITE_MEMDEBUG */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
525 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
526 ** End code for memory allocation system test layer.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
527 **--------------------------------------------------------------------------*/
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
528
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
529 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
530 ** This routine is called when we are about to allocate n additional bytes
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
531 ** of memory. If the new allocation will put is over the soft allocation
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
532 ** limit, then invoke sqlite3_release_memory() to try to release some
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
533 ** memory before continuing with the allocation.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
534 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
535 ** This routine also makes sure that the thread-specific-data (TSD) has
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
536 ** be allocated. If it has not and can not be allocated, then return
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
537 ** false. The updateMemoryUsedCount() routine below will deallocate
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
538 ** the TSD if it ought to be.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
539 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
540 ** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
541 ** a no-op
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
542 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
543 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
544 static int enforceSoftLimit(int n){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
545 ThreadData *pTsd = sqlite3ThreadData();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
546 if( pTsd==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
547 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
548 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
549 assert( pTsd->nAlloc>=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
550 if( n>0 && pTsd->nSoftHeapLimit>0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
551 while( pTsd->nAlloc+n>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) ){}
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
552 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
553 return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
554 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
555 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
556 # define enforceSoftLimit(X) 1
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
557 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
558
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
559 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
560 ** Update the count of total outstanding memory that is held in
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
561 ** thread-specific-data (TSD). If after this update the TSD is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
562 ** no longer being used, then deallocate it.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
563 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
564 ** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
565 ** a no-op
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
566 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
567 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
568 static void updateMemoryUsedCount(int n){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
569 ThreadData *pTsd = sqlite3ThreadData();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
570 if( pTsd ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
571 pTsd->nAlloc += n;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
572 assert( pTsd->nAlloc>=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
573 if( pTsd->nAlloc==0 && pTsd->nSoftHeapLimit==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
574 sqlite3ReleaseThreadData();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
575 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
576 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
577 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
578 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
579 #define updateMemoryUsedCount(x) /* no-op */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
580 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
581
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
582 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
583 ** Allocate and return N bytes of uninitialised memory by calling
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
584 ** sqlite3OsMalloc(). If the Malloc() call fails, attempt to free memory
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
585 ** by calling sqlite3_release_memory().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
586 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
587 void *sqlite3MallocRaw(int n, int doMemManage){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
588 void *p = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
589 if( n>0 && !sqlite3MallocFailed() && (!doMemManage || enforceSoftLimit(n)) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
590 while( (p = OSMALLOC(n))==0 && sqlite3_release_memory(n) ){}
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
591 if( !p ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
592 sqlite3FailedMalloc();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
593 OSMALLOC_FAILED();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
594 }else if( doMemManage ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
595 updateMemoryUsedCount(OSSIZEOF(p));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
596 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
597 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
598 return p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
599 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
600
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
601 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
602 ** Resize the allocation at p to n bytes by calling sqlite3OsRealloc(). The
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
603 ** pointer to the new allocation is returned. If the Realloc() call fails,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
604 ** attempt to free memory by calling sqlite3_release_memory().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
605 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
606 void *sqlite3Realloc(void *p, int n){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
607 if( sqlite3MallocFailed() ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
608 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
609 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
610
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
611 if( !p ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
612 return sqlite3Malloc(n, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
613 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
614 void *np = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
615 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
616 int origSize = OSSIZEOF(p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
617 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
618 if( enforceSoftLimit(n - origSize) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
619 while( (np = OSREALLOC(p, n))==0 && sqlite3_release_memory(n) ){}
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
620 if( !np ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
621 sqlite3FailedMalloc();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
622 OSMALLOC_FAILED();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
623 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
624 updateMemoryUsedCount(OSSIZEOF(np) - origSize);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
625 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
626 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
627 return np;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
628 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
629 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
630
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
631 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
632 ** Free the memory pointed to by p. p must be either a NULL pointer or a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
633 ** value returned by a previous call to sqlite3Malloc() or sqlite3Realloc().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
634 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
635 void sqlite3FreeX(void *p){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
636 if( p ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
637 updateMemoryUsedCount(0 - OSSIZEOF(p));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
638 OSFREE(p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
639 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
640 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
641
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
642 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
643 ** A version of sqliteMalloc() that is always a function, not a macro.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
644 ** Currently, this is used only to alloc to allocate the parser engine.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
645 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
646 void *sqlite3MallocX(int n){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
647 return sqliteMalloc(n);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
648 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
649
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
650 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
651 ** sqlite3Malloc
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
652 ** sqlite3ReallocOrFree
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
653 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
654 ** These two are implemented as wrappers around sqlite3MallocRaw(),
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
655 ** sqlite3Realloc() and sqlite3Free().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
656 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
657 void *sqlite3Malloc(int n, int doMemManage){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
658 void *p = sqlite3MallocRaw(n, doMemManage);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
659 if( p ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
660 memset(p, 0, n);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
661 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
662 return p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
663 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
664 void sqlite3ReallocOrFree(void **pp, int n){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
665 void *p = sqlite3Realloc(*pp, n);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
666 if( !p ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
667 sqlite3FreeX(*pp);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
668 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
669 *pp = p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
670 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
671
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
672 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
673 ** sqlite3ThreadSafeMalloc() and sqlite3ThreadSafeFree() are used in those
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
674 ** rare scenarios where sqlite may allocate memory in one thread and free
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
675 ** it in another. They are exactly the same as sqlite3Malloc() and
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
676 ** sqlite3Free() except that:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
677 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
678 ** * The allocated memory is not included in any calculations with
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
679 ** respect to the soft-heap-limit, and
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
680 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
681 ** * sqlite3ThreadSafeMalloc() must be matched with ThreadSafeFree(),
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
682 ** not sqlite3Free(). Calling sqlite3Free() on memory obtained from
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
683 ** ThreadSafeMalloc() will cause an error somewhere down the line.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
684 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
685 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
686 void *sqlite3ThreadSafeMalloc(int n){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
687 ENTER_MALLOC;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
688 return sqlite3Malloc(n, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
689 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
690 void sqlite3ThreadSafeFree(void *p){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
691 ENTER_MALLOC;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
692 if( p ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
693 OSFREE(p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
694 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
695 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
696 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
697
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
698
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
699 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
700 ** Return the number of bytes allocated at location p. p must be either
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
701 ** a NULL pointer (in which case 0 is returned) or a pointer returned by
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
702 ** sqlite3Malloc(), sqlite3Realloc() or sqlite3ReallocOrFree().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
703 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
704 ** The number of bytes allocated does not include any overhead inserted by
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
705 ** any malloc() wrapper functions that may be called. So the value returned
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
706 ** is the number of bytes that were available to SQLite using pointer p,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
707 ** regardless of how much memory was actually allocated.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
708 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
709 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
710 int sqlite3AllocSize(void *p){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
711 return OSSIZEOF(p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
712 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
713 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
714
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
715 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
716 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
717 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
718 ** is because when memory debugging is turned on, these two functions are
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
719 ** called via macros that record the current file and line number in the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
720 ** ThreadData structure.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
721 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
722 char *sqlite3StrDup(const char *z){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
723 char *zNew;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
724 if( z==0 ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
725 zNew = sqlite3MallocRaw(strlen(z)+1, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
726 if( zNew ) strcpy(zNew, z);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
727 return zNew;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
728 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
729 char *sqlite3StrNDup(const char *z, int n){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
730 char *zNew;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
731 if( z==0 ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
732 zNew = sqlite3MallocRaw(n+1, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
733 if( zNew ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
734 memcpy(zNew, z, n);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
735 zNew[n] = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
736 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
737 return zNew;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
738 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
739
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
740 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
741 ** Create a string from the 2nd and subsequent arguments (up to the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
742 ** first NULL argument), store the string in memory obtained from
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
743 ** sqliteMalloc() and make the pointer indicated by the 1st argument
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
744 ** point to that string. The 1st argument must either be NULL or
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
745 ** point to memory obtained from sqliteMalloc().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
746 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
747 void sqlite3SetString(char **pz, ...){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
748 va_list ap;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
749 int nByte;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
750 const char *z;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
751 char *zResult;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
752
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
753 if( pz==0 ) return;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
754 nByte = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
755 va_start(ap, pz);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
756 while( (z = va_arg(ap, const char*))!=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
757 nByte += strlen(z);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
758 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
759 va_end(ap);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
760 sqliteFree(*pz);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
761 *pz = zResult = sqliteMallocRaw( nByte );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
762 if( zResult==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
763 return;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
764 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
765 *zResult = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
766 va_start(ap, pz);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
767 while( (z = va_arg(ap, const char*))!=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
768 strcpy(zResult, z);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
769 zResult += strlen(zResult);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
770 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
771 va_end(ap);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
772 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
773
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
774 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
775 ** Set the most recent error code and error string for the sqlite
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
776 ** handle "db". The error code is set to "err_code".
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
777 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
778 ** If it is not NULL, string zFormat specifies the format of the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
779 ** error string in the style of the printf functions: The following
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
780 ** format characters are allowed:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
781 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
782 ** %s Insert a string
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
783 ** %z A string that should be freed after use
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
784 ** %d Insert an integer
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
785 ** %T Insert a token
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
786 ** %S Insert the first element of a SrcList
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
787 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
788 ** zFormat and any string tokens that follow it are assumed to be
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
789 ** encoded in UTF-8.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
790 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
791 ** To clear the most recent error for sqlite handle "db", sqlite3Error
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
792 ** should be called with err_code set to SQLITE_OK and zFormat set
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
793 ** to NULL.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
794 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
795 void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
796 if( db && (db->pErr || (db->pErr = sqlite3ValueNew())!=0) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
797 db->errCode = err_code;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
798 if( zFormat ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
799 char *z;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
800 va_list ap;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
801 va_start(ap, zFormat);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
802 z = sqlite3VMPrintf(zFormat, ap);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
803 va_end(ap);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
804 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3FreeX);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
805 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
806 sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
807 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
808 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
809 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
810
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
811 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
812 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
813 ** The following formatting characters are allowed:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
814 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
815 ** %s Insert a string
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
816 ** %z A string that should be freed after use
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
817 ** %d Insert an integer
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
818 ** %T Insert a token
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
819 ** %S Insert the first element of a SrcList
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
820 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
821 ** This function should be used to report any error that occurs whilst
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
822 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
823 ** last thing the sqlite3_prepare() function does is copy the error
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
824 ** stored by this function into the database handle using sqlite3Error().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
825 ** Function sqlite3Error() should be used during statement execution
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
826 ** (sqlite3_step() etc.).
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
827 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
828 void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
829 va_list ap;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
830 pParse->nErr++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
831 sqliteFree(pParse->zErrMsg);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
832 va_start(ap, zFormat);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
833 pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
834 va_end(ap);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
835 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
836
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
837 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
838 ** Clear the error message in pParse, if any
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
839 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
840 void sqlite3ErrorClear(Parse *pParse){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
841 sqliteFree(pParse->zErrMsg);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
842 pParse->zErrMsg = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
843 pParse->nErr = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
844 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
845
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
846 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
847 ** Convert an SQL-style quoted string into a normal string by removing
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
848 ** the quote characters. The conversion is done in-place. If the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
849 ** input does not begin with a quote character, then this routine
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
850 ** is a no-op.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
851 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
852 ** 2002-Feb-14: This routine is extended to remove MS-Access style
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
853 ** brackets from around identifers. For example: "[a-b-c]" becomes
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
854 ** "a-b-c".
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
855 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
856 void sqlite3Dequote(char *z){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
857 int quote;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
858 int i, j;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
859 if( z==0 ) return;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
860 quote = z[0];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
861 switch( quote ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
862 case '\'': break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
863 case '"': break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
864 case '`': break; /* For MySQL compatibility */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
865 case '[': quote = ']'; break; /* For MS SqlServer compatibility */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
866 default: return;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
867 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
868 for(i=1, j=0; z[i]; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
869 if( z[i]==quote ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
870 if( z[i+1]==quote ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
871 z[j++] = quote;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
872 i++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
873 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
874 z[j++] = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
875 break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
876 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
877 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
878 z[j++] = z[i];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
879 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
880 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
881 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
882
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
883 /* An array to map all upper-case characters into their corresponding
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
884 ** lower-case character.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
885 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
886 const unsigned char sqlite3UpperToLower[] = {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
887 #ifdef SQLITE_ASCII
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
888 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
889 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
890 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
891 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
892 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
893 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
894 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
895 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
896 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
897 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
898 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
899 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
900 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
901 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
902 252,253,254,255
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
903 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
904 #ifdef SQLITE_EBCDIC
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
905 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
906 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
907 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
908 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
909 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
910 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
911 96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
912 112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
913 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
914 144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
915 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
916 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
917 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
918 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
919 224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
920 239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
921 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
922 };
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
923 #define UpperToLower sqlite3UpperToLower
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
924
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
925 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
926 ** Some systems have stricmp(). Others have strcasecmp(). Because
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
927 ** there is no consistency, we will define our own.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
928 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
929 int sqlite3StrICmp(const char *zLeft, const char *zRight){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
930 register unsigned char *a, *b;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
931 a = (unsigned char *)zLeft;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
932 b = (unsigned char *)zRight;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
933 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
934 return UpperToLower[*a] - UpperToLower[*b];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
935 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
936 int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
937 register unsigned char *a, *b;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
938 a = (unsigned char *)zLeft;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
939 b = (unsigned char *)zRight;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
940 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
941 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
942 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
943
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
944 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
945 ** Return TRUE if z is a pure numeric string. Return FALSE if the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
946 ** string contains any character which is not part of a number. If
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
947 ** the string is numeric and contains the '.' character, set *realnum
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
948 ** to TRUE (otherwise FALSE).
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
949 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
950 ** An empty string is considered non-numeric.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
951 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
952 int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
953 int incr = (enc==SQLITE_UTF8?1:2);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
954 if( enc==SQLITE_UTF16BE ) z++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
955 if( *z=='-' || *z=='+' ) z += incr;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
956 if( !isdigit(*(u8*)z) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
957 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
958 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
959 z += incr;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
960 if( realnum ) *realnum = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
961 while( isdigit(*(u8*)z) ){ z += incr; }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
962 if( *z=='.' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
963 z += incr;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
964 if( !isdigit(*(u8*)z) ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
965 while( isdigit(*(u8*)z) ){ z += incr; }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
966 if( realnum ) *realnum = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
967 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
968 if( *z=='e' || *z=='E' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
969 z += incr;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
970 if( *z=='+' || *z=='-' ) z += incr;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
971 if( !isdigit(*(u8*)z) ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
972 while( isdigit(*(u8*)z) ){ z += incr; }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
973 if( realnum ) *realnum = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
974 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
975 return *z==0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
976 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
977
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
978 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
979 ** The string z[] is an ascii representation of a real number.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
980 ** Convert this string to a double.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
981 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
982 ** This routine assumes that z[] really is a valid number. If it
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
983 ** is not, the result is undefined.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
984 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
985 ** This routine is used instead of the library atof() function because
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
986 ** the library atof() might want to use "," as the decimal point instead
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
987 ** of "." depending on how locale is set. But that would cause problems
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
988 ** for SQL. So this routine always uses "." regardless of locale.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
989 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
990 int sqlite3AtoF(const char *z, double *pResult){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
991 #ifndef SQLITE_OMIT_FLOATING_POINT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
992 int sign = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
993 const char *zBegin = z;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
994 LONGDOUBLE_TYPE v1 = 0.0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
995 while( isspace(*z) ) z++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
996 if( *z=='-' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
997 sign = -1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
998 z++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
999 }else if( *z=='+' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1000 z++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1001 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1002 while( isdigit(*(u8*)z) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1003 v1 = v1*10.0 + (*z - '0');
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1004 z++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1005 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1006 if( *z=='.' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1007 LONGDOUBLE_TYPE divisor = 1.0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1008 z++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1009 while( isdigit(*(u8*)z) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1010 v1 = v1*10.0 + (*z - '0');
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1011 divisor *= 10.0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1012 z++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1013 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1014 v1 /= divisor;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1015 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1016 if( *z=='e' || *z=='E' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1017 int esign = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1018 int eval = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1019 LONGDOUBLE_TYPE scale = 1.0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1020 z++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1021 if( *z=='-' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1022 esign = -1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1023 z++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1024 }else if( *z=='+' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1025 z++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1026 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1027 while( isdigit(*(u8*)z) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1028 eval = eval*10 + *z - '0';
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1029 z++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1030 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1031 while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1032 while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1033 while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1034 while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1035 if( esign<0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1036 v1 /= scale;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1037 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1038 v1 *= scale;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1039 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1040 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1041 *pResult = sign<0 ? -v1 : v1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1042 return z - zBegin;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1043 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1044 return sqlite3atoi64(z, pResult);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1045 #endif /* SQLITE_OMIT_FLOATING_POINT */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1046 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1047
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1048 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1049 ** Return TRUE if zNum is a 64-bit signed integer and write
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1050 ** the value of the integer into *pNum. If zNum is not an integer
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1051 ** or is an integer that is too large to be expressed with 64 bits,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1052 ** then return false. If n>0 and the integer is string is not
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1053 ** exactly n bytes long, return false.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1054 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1055 ** When this routine was originally written it dealt with only
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1056 ** 32-bit numbers. At that time, it was much faster than the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1057 ** atoi() library routine in RedHat 7.2.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1058 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1059 int sqlite3atoi64(const char *zNum, i64 *pNum){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1060 i64 v = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1061 int neg;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1062 int i, c;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1063 while( isspace(*zNum) ) zNum++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1064 if( *zNum=='-' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1065 neg = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1066 zNum++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1067 }else if( *zNum=='+' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1068 neg = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1069 zNum++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1070 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1071 neg = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1072 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1073 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1074 v = v*10 + c - '0';
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1075 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1076 *pNum = neg ? -v : v;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1077 return c==0 && i>0 &&
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1078 (i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1079 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1080
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1081 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1082 ** The string zNum represents an integer. There might be some other
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1083 ** information following the integer too, but that part is ignored.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1084 ** If the integer that the prefix of zNum represents will fit in a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1085 ** 32-bit signed integer, return TRUE. Otherwise return FALSE.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1086 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1087 ** This routine returns FALSE for the string -2147483648 even that
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1088 ** that number will in fact fit in a 32-bit integer. But positive
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1089 ** 2147483648 will not fit in 32 bits. So it seems safer to return
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1090 ** false.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1091 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1092 static int sqlite3FitsIn32Bits(const char *zNum){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1093 int i, c;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1094 if( *zNum=='-' || *zNum=='+' ) zNum++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1095 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1096 return i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1097 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1098
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1099 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1100 ** If zNum represents an integer that will fit in 32-bits, then set
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1101 ** *pValue to that integer and return true. Otherwise return false.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1102 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1103 int sqlite3GetInt32(const char *zNum, int *pValue){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1104 if( sqlite3FitsIn32Bits(zNum) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1105 *pValue = atoi(zNum);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1106 return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1107 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1108 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1109 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1110
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1111 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1112 ** The string zNum represents an integer. There might be some other
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1113 ** information following the integer too, but that part is ignored.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1114 ** If the integer that the prefix of zNum represents will fit in a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1115 ** 64-bit signed integer, return TRUE. Otherwise return FALSE.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1116 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1117 ** This routine returns FALSE for the string -9223372036854775808 even that
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1118 ** that number will, in theory fit in a 64-bit integer. Positive
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1119 ** 9223373036854775808 will not fit in 64 bits. So it seems safer to return
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1120 ** false.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1121 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1122 int sqlite3FitsIn64Bits(const char *zNum){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1123 int i, c;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1124 if( *zNum=='-' || *zNum=='+' ) zNum++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1125 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1126 return i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1127 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1128
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1129
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1130 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1131 ** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1132 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1133 ** when this routine is called.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1134 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1135 ** This routine is a attempt to detect if two threads use the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1136 ** same sqlite* pointer at the same time. There is a race
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1137 ** condition so it is possible that the error is not detected.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1138 ** But usually the problem will be seen. The result will be an
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1139 ** error which can be used to debug the application that is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1140 ** using SQLite incorrectly.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1141 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1142 ** Ticket #202: If db->magic is not a valid open value, take care not
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1143 ** to modify the db structure at all. It could be that db is a stale
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1144 ** pointer. In other words, it could be that there has been a prior
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1145 ** call to sqlite3_close(db) and db has been deallocated. And we do
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1146 ** not want to write into deallocated memory.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1147 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1148 int sqlite3SafetyOn(sqlite3 *db){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1149 if( db->magic==SQLITE_MAGIC_OPEN ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1150 db->magic = SQLITE_MAGIC_BUSY;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1151 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1152 }else if( db->magic==SQLITE_MAGIC_BUSY ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1153 db->magic = SQLITE_MAGIC_ERROR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1154 db->flags |= SQLITE_Interrupt;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1155 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1156 return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1157 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1158
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1159 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1160 ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1161 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1162 ** when this routine is called.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1163 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1164 int sqlite3SafetyOff(sqlite3 *db){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1165 if( db->magic==SQLITE_MAGIC_BUSY ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1166 db->magic = SQLITE_MAGIC_OPEN;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1167 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1168 }else if( db->magic==SQLITE_MAGIC_OPEN ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1169 db->magic = SQLITE_MAGIC_ERROR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1170 db->flags |= SQLITE_Interrupt;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1171 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1172 return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1173 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1174
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1175 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1176 ** Check to make sure we have a valid db pointer. This test is not
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1177 ** foolproof but it does provide some measure of protection against
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1178 ** misuse of the interface such as passing in db pointers that are
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1179 ** NULL or which have been previously closed. If this routine returns
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1180 ** TRUE it means that the db pointer is invalid and should not be
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1181 ** dereferenced for any reason. The calling function should invoke
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1182 ** SQLITE_MISUSE immediately.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1183 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1184 int sqlite3SafetyCheck(sqlite3 *db){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1185 int magic;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1186 if( db==0 ) return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1187 magic = db->magic;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1188 if( magic!=SQLITE_MAGIC_CLOSED &&
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1189 magic!=SQLITE_MAGIC_OPEN &&
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1190 magic!=SQLITE_MAGIC_BUSY ) return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1191 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1192 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1193
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1194 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1195 ** The variable-length integer encoding is as follows:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1196 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1197 ** KEY:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1198 ** A = 0xxxxxxx 7 bits of data and one flag bit
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1199 ** B = 1xxxxxxx 7 bits of data and one flag bit
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1200 ** C = xxxxxxxx 8 bits of data
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1201 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1202 ** 7 bits - A
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1203 ** 14 bits - BA
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1204 ** 21 bits - BBA
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1205 ** 28 bits - BBBA
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1206 ** 35 bits - BBBBA
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1207 ** 42 bits - BBBBBA
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1208 ** 49 bits - BBBBBBA
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1209 ** 56 bits - BBBBBBBA
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1210 ** 64 bits - BBBBBBBBC
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1211 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1212
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1213 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1214 ** Write a 64-bit variable-length integer to memory starting at p[0].
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1215 ** The length of data write will be between 1 and 9 bytes. The number
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1216 ** of bytes written is returned.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1217 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1218 ** A variable-length integer consists of the lower 7 bits of each byte
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1219 ** for all bytes that have the 8th bit set and one byte with the 8th
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1220 ** bit clear. Except, if we get to the 9th byte, it stores the full
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1221 ** 8 bits and is the last byte.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1222 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1223 int sqlite3PutVarint(unsigned char *p, u64 v){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1224 int i, j, n;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1225 u8 buf[10];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1226 if( v & (((u64)0xff000000)<<32) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1227 p[8] = v;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1228 v >>= 8;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1229 for(i=7; i>=0; i--){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1230 p[i] = (v & 0x7f) | 0x80;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1231 v >>= 7;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1232 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1233 return 9;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1234 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1235 n = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1236 do{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1237 buf[n++] = (v & 0x7f) | 0x80;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1238 v >>= 7;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1239 }while( v!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1240 buf[0] &= 0x7f;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1241 assert( n<=9 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1242 for(i=0, j=n-1; j>=0; j--, i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1243 p[i] = buf[j];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1244 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1245 return n;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1246 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1247
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1248 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1249 ** Read a 64-bit variable-length integer from memory starting at p[0].
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1250 ** Return the number of bytes read. The value is stored in *v.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1251 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1252 int sqlite3GetVarint(const unsigned char *p, u64 *v){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1253 u32 x;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1254 u64 x64;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1255 int n;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1256 unsigned char c;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1257 if( ((c = p[0]) & 0x80)==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1258 *v = c;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1259 return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1260 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1261 x = c & 0x7f;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1262 if( ((c = p[1]) & 0x80)==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1263 *v = (x<<7) | c;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1264 return 2;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1265 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1266 x = (x<<7) | (c&0x7f);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1267 if( ((c = p[2]) & 0x80)==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1268 *v = (x<<7) | c;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1269 return 3;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1270 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1271 x = (x<<7) | (c&0x7f);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1272 if( ((c = p[3]) & 0x80)==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1273 *v = (x<<7) | c;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1274 return 4;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1275 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1276 x64 = (x<<7) | (c&0x7f);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1277 n = 4;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1278 do{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1279 c = p[n++];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1280 if( n==9 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1281 x64 = (x64<<8) | c;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1282 break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1283 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1284 x64 = (x64<<7) | (c&0x7f);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1285 }while( (c & 0x80)!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1286 *v = x64;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1287 return n;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1288 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1289
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1290 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1291 ** Read a 32-bit variable-length integer from memory starting at p[0].
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1292 ** Return the number of bytes read. The value is stored in *v.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1293 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1294 int sqlite3GetVarint32(const unsigned char *p, u32 *v){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1295 u32 x;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1296 int n;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1297 unsigned char c;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1298 if( ((signed char*)p)[0]>=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1299 *v = p[0];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1300 return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1301 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1302 x = p[0] & 0x7f;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1303 if( ((signed char*)p)[1]>=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1304 *v = (x<<7) | p[1];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1305 return 2;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1306 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1307 x = (x<<7) | (p[1] & 0x7f);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1308 n = 2;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1309 do{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1310 x = (x<<7) | ((c = p[n++])&0x7f);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1311 }while( (c & 0x80)!=0 && n<9 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1312 *v = x;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1313 return n;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1314 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1315
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1316 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1317 ** Return the number of bytes that will be needed to store the given
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1318 ** 64-bit integer.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1319 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1320 int sqlite3VarintLen(u64 v){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1321 int i = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1322 do{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1323 i++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1324 v >>= 7;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1325 }while( v!=0 && i<9 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1326 return i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1327 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1328
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1329 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1330 || defined(SQLITE_TEST)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1331 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1332 ** Translate a single byte of Hex into an integer.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1333 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1334 static int hexToInt(int h){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1335 if( h>='0' && h<='9' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1336 return h - '0';
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1337 }else if( h>='a' && h<='f' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1338 return h - 'a' + 10;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1339 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1340 assert( h>='A' && h<='F' );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1341 return h - 'A' + 10;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1342 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1343 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1344 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC || SQLITE_TEST */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1345
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1346 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1347 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1348 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1349 ** value. Return a pointer to its binary value. Space to hold the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1350 ** binary value has been obtained from malloc and must be freed by
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1351 ** the calling routine.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1352 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1353 void *sqlite3HexToBlob(const char *z){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1354 char *zBlob;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1355 int i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1356 int n = strlen(z);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1357 if( n%2 ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1358
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1359 zBlob = (char *)sqliteMalloc(n/2);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1360 for(i=0; i<n; i+=2){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1361 zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1362 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1363 return zBlob;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1364 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1365 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1366
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1367 #if defined(SQLITE_TEST)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1368 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1369 ** Convert text generated by the "%p" conversion format back into
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1370 ** a pointer.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1371 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1372 void *sqlite3TextToPtr(const char *z){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1373 void *p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1374 u64 v;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1375 u32 v2;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1376 if( z[0]=='0' && z[1]=='x' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1377 z += 2;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1378 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1379 v = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1380 while( *z ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1381 v = (v<<4) + hexToInt(*z);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1382 z++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1383 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1384 if( sizeof(p)==sizeof(v) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1385 p = *(void**)&v;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1386 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1387 assert( sizeof(p)==sizeof(v2) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1388 v2 = (u32)v;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1389 p = *(void**)&v2;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1390 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1391 return p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1392 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1393 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1394
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1395 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1396 ** Return a pointer to the ThreadData associated with the calling thread.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1397 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1398 ThreadData *sqlite3ThreadData(){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1399 ThreadData *p = (ThreadData*)sqlite3OsThreadSpecificData(1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1400 if( !p ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1401 sqlite3FailedMalloc();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1402 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1403 return p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1404 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1405
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1406 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1407 ** Return a pointer to the ThreadData associated with the calling thread.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1408 ** If no ThreadData has been allocated to this thread yet, return a pointer
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1409 ** to a substitute ThreadData structure that is all zeros.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1410 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1411 const ThreadData *sqlite3ThreadDataReadOnly(){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1412 static const ThreadData zeroData = {0}; /* Initializer to silence warnings
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1413 ** from broken compilers */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1414 const ThreadData *pTd = sqlite3OsThreadSpecificData(0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1415 return pTd ? pTd : &zeroData;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1416 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1417
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1418 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1419 ** Check to see if the ThreadData for this thread is all zero. If it
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1420 ** is, then deallocate it.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1421 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1422 void sqlite3ReleaseThreadData(){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1423 sqlite3OsThreadSpecificData(-1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1424 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1425
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1426 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1427 ** This function must be called before exiting any API function (i.e.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1428 ** returning control to the user) that has called sqlite3Malloc or
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1429 ** sqlite3Realloc.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1430 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1431 ** The returned value is normally a copy of the second argument to this
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1432 ** function. However, if a malloc() failure has occured since the previous
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1433 ** invocation SQLITE_NOMEM is returned instead.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1434 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1435 ** If the first argument, db, is not NULL and a malloc() error has occured,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1436 ** then the connection error-code (the value returned by sqlite3_errcode())
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1437 ** is set to SQLITE_NOMEM.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1438 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1439 static int mallocHasFailed = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1440 int sqlite3ApiExit(sqlite3* db, int rc){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1441 if( sqlite3MallocFailed() ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1442 mallocHasFailed = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1443 sqlite3OsLeaveMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1444 sqlite3Error(db, SQLITE_NOMEM, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1445 rc = SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1446 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1447 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1448 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1449
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1450 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1451 ** Return true is a malloc has failed in this thread since the last call
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1452 ** to sqlite3ApiExit(), or false otherwise.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1453 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1454 int sqlite3MallocFailed(){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1455 return (mallocHasFailed && sqlite3OsInMutex(1));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1456 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1457
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1458 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1459 ** Set the "malloc has failed" condition to true for this thread.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1460 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1461 void sqlite3FailedMalloc(){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1462 sqlite3OsEnterMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1463 assert( mallocHasFailed==0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1464 mallocHasFailed = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1465 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1466
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1467 #ifdef SQLITE_MEMDEBUG
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1468 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1469 ** This function sets a flag in the thread-specific-data structure that will
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1470 ** cause an assert to fail if sqliteMalloc() or sqliteRealloc() is called.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1471 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1472 void sqlite3MallocDisallow(){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1473 assert( sqlite3_mallocDisallowed>=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1474 sqlite3_mallocDisallowed++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1475 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1476
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1477 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1478 ** This function clears the flag set in the thread-specific-data structure set
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1479 ** by sqlite3MallocDisallow().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1480 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1481 void sqlite3MallocAllow(){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1482 assert( sqlite3_mallocDisallowed>0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1483 sqlite3_mallocDisallowed--;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1484 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1485 #endif