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