extensions/sqlite/sqlite-source/malloc.c
changeset 1645 27307df4d711
parent 1203 633c25ce6405
     1.1 --- a/extensions/sqlite/sqlite-source/malloc.c	Sun Oct 28 05:06:12 2007 +0000
     1.2 +++ b/extensions/sqlite/sqlite-source/malloc.c	Sun Oct 28 05:48:06 2007 +0000
     1.3 @@ -15,705 +15,122 @@
     1.4  ** $Id$
     1.5  */
     1.6  #include "sqliteInt.h"
     1.7 -#include "os.h"
     1.8  #include <stdarg.h>
     1.9  #include <ctype.h>
    1.10  
    1.11  /*
    1.12 -** MALLOC WRAPPER ARCHITECTURE
    1.13 -**
    1.14 -** The sqlite code accesses dynamic memory allocation/deallocation by invoking
    1.15 -** the following six APIs (which may be implemented as macros).
    1.16 -**
    1.17 -**     sqlite3Malloc()
    1.18 -**     sqlite3MallocRaw()
    1.19 -**     sqlite3Realloc()
    1.20 -**     sqlite3ReallocOrFree()
    1.21 -**     sqlite3Free()
    1.22 -**     sqlite3AllocSize()
    1.23 -**
    1.24 -** The function sqlite3FreeX performs the same task as sqlite3Free and is
    1.25 -** guaranteed to be a real function. The same holds for sqlite3MallocX
    1.26 -**
    1.27 -** The above APIs are implemented in terms of the functions provided in the
    1.28 -** operating-system interface. The OS interface is never accessed directly
    1.29 -** by code outside of this file.
    1.30 -**
    1.31 -**     sqlite3OsMalloc()
    1.32 -**     sqlite3OsRealloc()
    1.33 -**     sqlite3OsFree()
    1.34 -**     sqlite3OsAllocationSize()
    1.35 -**
    1.36 -** Functions sqlite3MallocRaw() and sqlite3Realloc() may invoke 
    1.37 -** sqlite3_release_memory() if a call to sqlite3OsMalloc() or
    1.38 -** sqlite3OsRealloc() fails (or if the soft-heap-limit for the thread is
    1.39 -** exceeded). Function sqlite3Malloc() usually invokes
    1.40 -** sqlite3MallocRaw().
    1.41 -**
    1.42 -** MALLOC TEST WRAPPER ARCHITECTURE
    1.43 -**
    1.44 -** The test wrapper provides extra test facilities to ensure the library 
    1.45 -** does not leak memory and handles the failure of the underlying OS level
    1.46 -** allocation system correctly. It is only present if the library is 
    1.47 -** compiled with the SQLITE_MEMDEBUG macro set.
    1.48 -**
    1.49 -**     * Guardposts to detect overwrites.
    1.50 -**     * Ability to cause a specific Malloc() or Realloc() to fail.
    1.51 -**     * Audit outstanding memory allocations (i.e check for leaks).
    1.52 +** This routine runs when the memory allocator sees that the
    1.53 +** total memory allocation is about to exceed the soft heap
    1.54 +** limit.
    1.55  */
    1.56 -
    1.57 -#define MAX(x,y) ((x)>(y)?(x):(y))
    1.58 +static void softHeapLimitEnforcer(
    1.59 +  void *NotUsed, 
    1.60 +  sqlite3_int64 inUse,
    1.61 +  int allocSize
    1.62 +){
    1.63 +  sqlite3_release_memory(allocSize);
    1.64 +}
    1.65  
    1.66 -#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
    1.67  /*
    1.68 -** Set the soft heap-size limit for the current thread. Passing a negative
    1.69 -** value indicates no limit.
    1.70 +** Set the soft heap-size limit for the current thread. Passing a
    1.71 +** zero or negative value indicates no limit.
    1.72  */
    1.73  void sqlite3_soft_heap_limit(int n){
    1.74 -  ThreadData *pTd = sqlite3ThreadData();
    1.75 -  if( pTd ){
    1.76 -    pTd->nSoftHeapLimit = n;
    1.77 +  sqlite3_uint64 iLimit;
    1.78 +  int overage;
    1.79 +  if( n<0 ){
    1.80 +    iLimit = 0;
    1.81 +  }else{
    1.82 +    iLimit = n;
    1.83    }
    1.84 -  sqlite3ReleaseThreadData();
    1.85 +  if( iLimit>0 ){
    1.86 +    sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit);
    1.87 +  }else{
    1.88 +    sqlite3_memory_alarm(0, 0, 0);
    1.89 +  }
    1.90 +  overage = sqlite3_memory_used() - n;
    1.91 +  if( overage>0 ){
    1.92 +    sqlite3_release_memory(overage);
    1.93 +  }
    1.94  }
    1.95  
    1.96  /*
    1.97  ** Release memory held by SQLite instances created by the current thread.
    1.98  */
    1.99  int sqlite3_release_memory(int n){
   1.100 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   1.101    return sqlite3PagerReleaseMemory(n);
   1.102 -}
   1.103  #else
   1.104 -/* If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, then define a version
   1.105 -** of sqlite3_release_memory() to be used by other code in this file.
   1.106 -** This is done for no better reason than to reduce the number of 
   1.107 -** pre-processor #ifndef statements.
   1.108 -*/
   1.109 -#define sqlite3_release_memory(x) 0    /* 0 == no memory freed */
   1.110 -#endif
   1.111 -
   1.112 -#ifdef SQLITE_MEMDEBUG
   1.113 -/*--------------------------------------------------------------------------
   1.114 -** Begin code for memory allocation system test layer.
   1.115 -**
   1.116 -** Memory debugging is turned on by defining the SQLITE_MEMDEBUG macro.
   1.117 -**
   1.118 -** SQLITE_MEMDEBUG==1    -> Fence-posting only (thread safe) 
   1.119 -** SQLITE_MEMDEBUG==2    -> Fence-posting + linked list of allocations (not ts)
   1.120 -** SQLITE_MEMDEBUG==3    -> Above + backtraces (not thread safe, req. glibc)
   1.121 -*/
   1.122 -
   1.123 -/* Figure out whether or not to store backtrace() information for each malloc.
   1.124 -** The backtrace() function is only used if SQLITE_MEMDEBUG is set to 2 or 
   1.125 -** greater and glibc is in use. If we don't want to use backtrace(), then just
   1.126 -** define it as an empty macro and set the amount of space reserved to 0.
   1.127 -*/
   1.128 -#if defined(__GLIBC__) && SQLITE_MEMDEBUG>2
   1.129 -  extern int backtrace(void **, int);
   1.130 -  #define TESTALLOC_STACKSIZE 128
   1.131 -  #define TESTALLOC_STACKFRAMES ((TESTALLOC_STACKSIZE-8)/sizeof(void*))
   1.132 -#else
   1.133 -  #define backtrace(x, y)
   1.134 -  #define TESTALLOC_STACKSIZE 0
   1.135 -  #define TESTALLOC_STACKFRAMES 0
   1.136 -#endif
   1.137 -
   1.138 -/*
   1.139 -** Number of 32-bit guard words.  This should probably be a multiple of
   1.140 -** 2 since on 64-bit machines we want the value returned by sqliteMalloc()
   1.141 -** to be 8-byte aligned.
   1.142 -*/
   1.143 -#ifndef TESTALLOC_NGUARD
   1.144 -# define TESTALLOC_NGUARD 2
   1.145 +  return SQLITE_OK;
   1.146  #endif
   1.147 -
   1.148 -/*
   1.149 -** Size reserved for storing file-name along with each malloc()ed blob.
   1.150 -*/
   1.151 -#define TESTALLOC_FILESIZE 64
   1.152 -
   1.153 -/*
   1.154 -** Size reserved for storing the user string. Each time a Malloc() or Realloc()
   1.155 -** call succeeds, up to TESTALLOC_USERSIZE bytes of the string pointed to by
   1.156 -** sqlite3_malloc_id are stored along with the other test system metadata.
   1.157 -*/
   1.158 -#define TESTALLOC_USERSIZE 64
   1.159 -const char *sqlite3_malloc_id = 0;
   1.160 -
   1.161 -/*
   1.162 -** Blocks used by the test layer have the following format:
   1.163 -**
   1.164 -**        <sizeof(void *) pNext pointer>
   1.165 -**        <sizeof(void *) pPrev pointer>
   1.166 -**        <TESTALLOC_NGUARD 32-bit guard words>
   1.167 -**            <The application level allocation>
   1.168 -**        <TESTALLOC_NGUARD 32-bit guard words>
   1.169 -**        <32-bit line number>
   1.170 -**        <TESTALLOC_FILESIZE bytes containing null-terminated file name>
   1.171 -**        <TESTALLOC_STACKSIZE bytes of backtrace() output>
   1.172 -*/ 
   1.173 -
   1.174 -#define TESTALLOC_OFFSET_GUARD1(p)    (sizeof(void *) * 2)
   1.175 -#define TESTALLOC_OFFSET_DATA(p) ( \
   1.176 -  TESTALLOC_OFFSET_GUARD1(p) + sizeof(u32) * TESTALLOC_NGUARD \
   1.177 -)
   1.178 -#define TESTALLOC_OFFSET_GUARD2(p) ( \
   1.179 -  TESTALLOC_OFFSET_DATA(p) + sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD \
   1.180 -)
   1.181 -#define TESTALLOC_OFFSET_LINENUMBER(p) ( \
   1.182 -  TESTALLOC_OFFSET_GUARD2(p) + sizeof(u32) * TESTALLOC_NGUARD \
   1.183 -)
   1.184 -#define TESTALLOC_OFFSET_FILENAME(p) ( \
   1.185 -  TESTALLOC_OFFSET_LINENUMBER(p) + sizeof(u32) \
   1.186 -)
   1.187 -#define TESTALLOC_OFFSET_USER(p) ( \
   1.188 -  TESTALLOC_OFFSET_FILENAME(p) + TESTALLOC_FILESIZE \
   1.189 -)
   1.190 -#define TESTALLOC_OFFSET_STACK(p) ( \
   1.191 -  TESTALLOC_OFFSET_USER(p) + TESTALLOC_USERSIZE + 8 - \
   1.192 -  (TESTALLOC_OFFSET_USER(p) % 8) \
   1.193 -)
   1.194 -
   1.195 -#define TESTALLOC_OVERHEAD ( \
   1.196 -  sizeof(void *)*2 +                   /* pPrev and pNext pointers */   \
   1.197 -  TESTALLOC_NGUARD*sizeof(u32)*2 +              /* Guard words */       \
   1.198 -  sizeof(u32) + TESTALLOC_FILESIZE +   /* File and line number */       \
   1.199 -  TESTALLOC_USERSIZE +                 /* User string */                \
   1.200 -  TESTALLOC_STACKSIZE                  /* backtrace() stack */          \
   1.201 -)
   1.202 +}
   1.203  
   1.204  
   1.205  /*
   1.206 -** For keeping track of the number of mallocs and frees.   This
   1.207 -** is used to check for memory leaks.  The iMallocFail and iMallocReset
   1.208 -** values are used to simulate malloc() failures during testing in 
   1.209 -** order to verify that the library correctly handles an out-of-memory
   1.210 -** condition.
   1.211 -*/
   1.212 -int sqlite3_nMalloc;         /* Number of sqliteMalloc() calls */
   1.213 -int sqlite3_nFree;           /* Number of sqliteFree() calls */
   1.214 -int sqlite3_memUsed;         /* TODO Total memory obtained from malloc */
   1.215 -int sqlite3_memMax;          /* TODO Mem usage high-water mark */
   1.216 -int sqlite3_iMallocFail;     /* Fail sqliteMalloc() after this many calls */
   1.217 -int sqlite3_iMallocReset = -1; /* When iMallocFail reaches 0, set to this */
   1.218 -
   1.219 -void *sqlite3_pFirst = 0;         /* Pointer to linked list of allocations */
   1.220 -int sqlite3_nMaxAlloc = 0;        /* High water mark of ThreadData.nAlloc */
   1.221 -int sqlite3_mallocDisallowed = 0; /* assert() in sqlite3Malloc() if set */
   1.222 -int sqlite3_isFail = 0;           /* True if all malloc calls should fail */
   1.223 -const char *sqlite3_zFile = 0;    /* Filename to associate debug info with */
   1.224 -int sqlite3_iLine = 0;            /* Line number for debug info */
   1.225 -int sqlite3_mallocfail_trace = 0; /* Print a msg on malloc fail if true */
   1.226 -
   1.227 -/*
   1.228 -** Check for a simulated memory allocation failure.  Return true if
   1.229 -** the failure should be simulated.  Return false to proceed as normal.
   1.230 -*/
   1.231 -int sqlite3TestMallocFail(){
   1.232 -  if( sqlite3_isFail ){
   1.233 -    return 1;
   1.234 -  }
   1.235 -  if( sqlite3_iMallocFail>=0 ){
   1.236 -    sqlite3_iMallocFail--;
   1.237 -    if( sqlite3_iMallocFail==0 ){
   1.238 -      sqlite3_iMallocFail = sqlite3_iMallocReset;
   1.239 -      sqlite3_isFail = 1;
   1.240 -      if( sqlite3_mallocfail_trace ){
   1.241 -         sqlite3DebugPrintf("###_malloc_fails_###\n");
   1.242 -      }
   1.243 -      return 1;
   1.244 -    }
   1.245 -  }
   1.246 -  return 0;
   1.247 -}
   1.248 -
   1.249 -/*
   1.250 -** The argument is a pointer returned by sqlite3OsMalloc() or xRealloc().
   1.251 -** assert() that the first and last (TESTALLOC_NGUARD*4) bytes are set to the
   1.252 -** values set by the applyGuards() function.
   1.253 -*/
   1.254 -static void checkGuards(u32 *p)
   1.255 -{
   1.256 -  int i;
   1.257 -  char *zAlloc = (char *)p;
   1.258 -  char *z;
   1.259 -
   1.260 -  /* First set of guard words */
   1.261 -  z = &zAlloc[TESTALLOC_OFFSET_GUARD1(p)];
   1.262 -  for(i=0; i<TESTALLOC_NGUARD; i++){
   1.263 -    assert(((u32 *)z)[i]==0xdead1122);
   1.264 -  }
   1.265 -
   1.266 -  /* Second set of guard words */
   1.267 -  z = &zAlloc[TESTALLOC_OFFSET_GUARD2(p)];
   1.268 -  for(i=0; i<TESTALLOC_NGUARD; i++){
   1.269 -    u32 guard = 0;
   1.270 -    memcpy(&guard, &z[i*sizeof(u32)], sizeof(u32));
   1.271 -    assert(guard==0xdead3344);
   1.272 +** Allocate and zero memory.
   1.273 +*/ 
   1.274 +void *sqlite3MallocZero(unsigned n){
   1.275 +  void *p = sqlite3_malloc(n);
   1.276 +  if( p ){
   1.277 +    memset(p, 0, n);
   1.278    }
   1.279 -}
   1.280 -
   1.281 -/*
   1.282 -** The argument is a pointer returned by sqlite3OsMalloc() or Realloc(). The
   1.283 -** first and last (TESTALLOC_NGUARD*4) bytes are set to known values for use as 
   1.284 -** guard-posts.
   1.285 -*/
   1.286 -static void applyGuards(u32 *p)
   1.287 -{
   1.288 -  int i;
   1.289 -  char *z;
   1.290 -  char *zAlloc = (char *)p;
   1.291 -
   1.292 -  /* First set of guard words */
   1.293 -  z = &zAlloc[TESTALLOC_OFFSET_GUARD1(p)];
   1.294 -  for(i=0; i<TESTALLOC_NGUARD; i++){
   1.295 -    ((u32 *)z)[i] = 0xdead1122;
   1.296 -  }
   1.297 -
   1.298 -  /* Second set of guard words */
   1.299 -  z = &zAlloc[TESTALLOC_OFFSET_GUARD2(p)];
   1.300 -  for(i=0; i<TESTALLOC_NGUARD; i++){
   1.301 -    static const int guard = 0xdead3344;
   1.302 -    memcpy(&z[i*sizeof(u32)], &guard, sizeof(u32));
   1.303 -  }
   1.304 -
   1.305 -  /* Line number */
   1.306 -  z = &((char *)z)[TESTALLOC_NGUARD*sizeof(u32)];             /* Guard words */
   1.307 -  z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)];
   1.308 -  memcpy(z, &sqlite3_iLine, sizeof(u32));
   1.309 -
   1.310 -  /* File name */
   1.311 -  z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)];
   1.312 -  strncpy(z, sqlite3_zFile, TESTALLOC_FILESIZE);
   1.313 -  z[TESTALLOC_FILESIZE - 1] = '\0';
   1.314 -
   1.315 -  /* User string */
   1.316 -  z = &zAlloc[TESTALLOC_OFFSET_USER(p)];
   1.317 -  z[0] = 0;
   1.318 -  if( sqlite3_malloc_id ){
   1.319 -    strncpy(z, sqlite3_malloc_id, TESTALLOC_USERSIZE);
   1.320 -    z[TESTALLOC_USERSIZE-1] = 0;
   1.321 -  }
   1.322 -
   1.323 -  /* backtrace() stack */
   1.324 -  z = &zAlloc[TESTALLOC_OFFSET_STACK(p)];
   1.325 -  backtrace((void **)z, TESTALLOC_STACKFRAMES);
   1.326 -
   1.327 -  /* Sanity check to make sure checkGuards() is working */
   1.328 -  checkGuards(p);
   1.329 -}
   1.330 -
   1.331 -/*
   1.332 -** The argument is a malloc()ed pointer as returned by the test-wrapper.
   1.333 -** Return a pointer to the Os level allocation.
   1.334 -*/
   1.335 -static void *getOsPointer(void *p)
   1.336 -{
   1.337 -  char *z = (char *)p;
   1.338 -  return (void *)(&z[-1 * TESTALLOC_OFFSET_DATA(p)]);
   1.339 -}
   1.340 -
   1.341 -
   1.342 -#if SQLITE_MEMDEBUG>1
   1.343 -/*
   1.344 -** The argument points to an Os level allocation. Link it into the threads list
   1.345 -** of allocations.
   1.346 -*/
   1.347 -static void linkAlloc(void *p){
   1.348 -  void **pp = (void **)p;
   1.349 -  pp[0] = 0;
   1.350 -  pp[1] = sqlite3_pFirst;
   1.351 -  if( sqlite3_pFirst ){
   1.352 -    ((void **)sqlite3_pFirst)[0] = p;
   1.353 -  }
   1.354 -  sqlite3_pFirst = p;
   1.355 +  return p;
   1.356  }
   1.357  
   1.358  /*
   1.359 -** The argument points to an Os level allocation. Unlinke it from the threads
   1.360 -** list of allocations.
   1.361 +** Allocate and zero memory.  If the allocation fails, make
   1.362 +** the mallocFailed flag in the connection pointer.
   1.363  */
   1.364 -static void unlinkAlloc(void *p)
   1.365 -{
   1.366 -  void **pp = (void **)p;
   1.367 -  if( p==sqlite3_pFirst ){
   1.368 -    assert(!pp[0]);
   1.369 -    assert(!pp[1] || ((void **)(pp[1]))[0]==p);
   1.370 -    sqlite3_pFirst = pp[1];
   1.371 -    if( sqlite3_pFirst ){
   1.372 -      ((void **)sqlite3_pFirst)[0] = 0;
   1.373 -    }
   1.374 -  }else{
   1.375 -    void **pprev = pp[0];
   1.376 -    void **pnext = pp[1];
   1.377 -    assert(pprev);
   1.378 -    assert(pprev[1]==p);
   1.379 -    pprev[1] = (void *)pnext;
   1.380 -    if( pnext ){
   1.381 -      assert(pnext[0]==p);
   1.382 -      pnext[0] = (void *)pprev;
   1.383 -    }
   1.384 +void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){
   1.385 +  void *p = sqlite3DbMallocRaw(db, n);
   1.386 +  if( p ){
   1.387 +    memset(p, 0, n);
   1.388    }
   1.389 +  return p;
   1.390  }
   1.391  
   1.392  /*
   1.393 -** Pointer p is a pointer to an OS level allocation that has just been
   1.394 -** realloc()ed. Set the list pointers that point to this entry to it's new
   1.395 -** location.
   1.396 -*/
   1.397 -static void relinkAlloc(void *p)
   1.398 -{
   1.399 -  void **pp = (void **)p;
   1.400 -  if( pp[0] ){
   1.401 -    ((void **)(pp[0]))[1] = p;
   1.402 -  }else{
   1.403 -    sqlite3_pFirst = p;
   1.404 -  }
   1.405 -  if( pp[1] ){
   1.406 -    ((void **)(pp[1]))[0] = p;
   1.407 -  }
   1.408 -}
   1.409 -#else
   1.410 -#define linkAlloc(x)
   1.411 -#define relinkAlloc(x)
   1.412 -#define unlinkAlloc(x)
   1.413 -#endif
   1.414 -
   1.415 -/*
   1.416 -** This function sets the result of the Tcl interpreter passed as an argument
   1.417 -** to a list containing an entry for each currently outstanding call made to 
   1.418 -** sqliteMalloc and friends by the current thread. Each list entry is itself a
   1.419 -** list, consisting of the following (in order):
   1.420 -**
   1.421 -**     * The number of bytes allocated
   1.422 -**     * The __FILE__ macro at the time of the sqliteMalloc() call.
   1.423 -**     * The __LINE__ macro ...
   1.424 -**     * The value of the sqlite3_malloc_id variable ...
   1.425 -**     * The output of backtrace() (if available) ...
   1.426 -**
   1.427 -** Todo: We could have a version of this function that outputs to stdout, 
   1.428 -** to debug memory leaks when Tcl is not available.
   1.429 -*/
   1.430 -#if defined(TCLSH) && defined(SQLITE_DEBUG) && SQLITE_MEMDEBUG>1
   1.431 -#include <tcl.h>
   1.432 -int sqlite3OutstandingMallocs(Tcl_Interp *interp){
   1.433 -  void *p;
   1.434 -  Tcl_Obj *pRes = Tcl_NewObj();
   1.435 -  Tcl_IncrRefCount(pRes);
   1.436 -
   1.437 -
   1.438 -  for(p=sqlite3_pFirst; p; p=((void **)p)[1]){
   1.439 -    Tcl_Obj *pEntry = Tcl_NewObj();
   1.440 -    Tcl_Obj *pStack = Tcl_NewObj();
   1.441 -    char *z;
   1.442 -    u32 iLine;
   1.443 -    int nBytes = sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD;
   1.444 -    char *zAlloc = (char *)p;
   1.445 -    int i;
   1.446 -
   1.447 -    Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(nBytes));
   1.448 -
   1.449 -    z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)];
   1.450 -    Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1));
   1.451 -
   1.452 -    z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)];
   1.453 -    memcpy(&iLine, z, sizeof(u32));
   1.454 -    Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(iLine));
   1.455 -
   1.456 -    z = &zAlloc[TESTALLOC_OFFSET_USER(p)];
   1.457 -    Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1));
   1.458 -
   1.459 -    z = &zAlloc[TESTALLOC_OFFSET_STACK(p)];
   1.460 -    for(i=0; i<TESTALLOC_STACKFRAMES; i++){
   1.461 -      char zHex[128];
   1.462 -      sqlite3_snprintf(sizeof(zHex), zHex, "%p", ((void **)z)[i]);
   1.463 -      Tcl_ListObjAppendElement(0, pStack, Tcl_NewStringObj(zHex, -1));
   1.464 -    }
   1.465 -
   1.466 -    Tcl_ListObjAppendElement(0, pEntry, pStack);
   1.467 -    Tcl_ListObjAppendElement(0, pRes, pEntry);
   1.468 -  }
   1.469 -
   1.470 -  Tcl_ResetResult(interp);
   1.471 -  Tcl_SetObjResult(interp, pRes);
   1.472 -  Tcl_DecrRefCount(pRes);
   1.473 -  return TCL_OK;
   1.474 -}
   1.475 -#endif
   1.476 -
   1.477 -/*
   1.478 -** This is the test layer's wrapper around sqlite3OsMalloc().
   1.479 +** Allocate and zero memory.  If the allocation fails, make
   1.480 +** the mallocFailed flag in the connection pointer.
   1.481  */
   1.482 -static void * OSMALLOC(int n){
   1.483 -  sqlite3OsEnterMutex();
   1.484 -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   1.485 -  sqlite3_nMaxAlloc = 
   1.486 -      MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc);
   1.487 -#endif
   1.488 -  assert( !sqlite3_mallocDisallowed );
   1.489 -  if( !sqlite3TestMallocFail() ){
   1.490 -    u32 *p;
   1.491 -    p = (u32 *)sqlite3OsMalloc(n + TESTALLOC_OVERHEAD);
   1.492 -    assert(p);
   1.493 -    sqlite3_nMalloc++;
   1.494 -    applyGuards(p);
   1.495 -    linkAlloc(p);
   1.496 -    sqlite3OsLeaveMutex();
   1.497 -    return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]);
   1.498 -  }
   1.499 -  sqlite3OsLeaveMutex();
   1.500 -  return 0;
   1.501 -}
   1.502 -
   1.503 -static int OSSIZEOF(void *p){
   1.504 -  if( p ){
   1.505 -    u32 *pOs = (u32 *)getOsPointer(p);
   1.506 -    return sqlite3OsAllocationSize(pOs) - TESTALLOC_OVERHEAD;
   1.507 -  }
   1.508 -  return 0;
   1.509 -}
   1.510 -
   1.511 -/*
   1.512 -** This is the test layer's wrapper around sqlite3OsFree(). The argument is a
   1.513 -** pointer to the space allocated for the application to use.
   1.514 -*/
   1.515 -static void OSFREE(void *pFree){
   1.516 -  u32 *p;         /* Pointer to the OS-layer allocation */
   1.517 -  sqlite3OsEnterMutex();
   1.518 -  p = (u32 *)getOsPointer(pFree);
   1.519 -  checkGuards(p);
   1.520 -  unlinkAlloc(p);
   1.521 -  memset(pFree, 0x55, OSSIZEOF(pFree));
   1.522 -  sqlite3OsFree(p);
   1.523 -  sqlite3_nFree++;
   1.524 -  sqlite3OsLeaveMutex();
   1.525 -}
   1.526 -
   1.527 -/*
   1.528 -** This is the test layer's wrapper around sqlite3OsRealloc().
   1.529 -*/
   1.530 -static void * OSREALLOC(void *pRealloc, int n){
   1.531 -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   1.532 -  sqlite3_nMaxAlloc = 
   1.533 -      MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc);
   1.534 -#endif
   1.535 -  assert( !sqlite3_mallocDisallowed );
   1.536 -  if( !sqlite3TestMallocFail() ){
   1.537 -    u32 *p = (u32 *)getOsPointer(pRealloc);
   1.538 -    checkGuards(p);
   1.539 -    p = sqlite3OsRealloc(p, n + TESTALLOC_OVERHEAD);
   1.540 -    applyGuards(p);
   1.541 -    relinkAlloc(p);
   1.542 -    return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]);
   1.543 -  }
   1.544 -  return 0;
   1.545 -}
   1.546 -
   1.547 -static void OSMALLOC_FAILED(){
   1.548 -  sqlite3_isFail = 0;
   1.549 -}
   1.550 -
   1.551 -#else
   1.552 -/* Define macros to call the sqlite3OsXXX interface directly if 
   1.553 -** the SQLITE_MEMDEBUG macro is not defined.
   1.554 -*/
   1.555 -#define OSMALLOC(x)        sqlite3OsMalloc(x)
   1.556 -#define OSREALLOC(x,y)     sqlite3OsRealloc(x,y)
   1.557 -#define OSFREE(x)          sqlite3OsFree(x)
   1.558 -#define OSSIZEOF(x)        sqlite3OsAllocationSize(x)
   1.559 -#define OSMALLOC_FAILED()
   1.560 -
   1.561 -#endif  /* SQLITE_MEMDEBUG */
   1.562 -/*
   1.563 -** End code for memory allocation system test layer.
   1.564 -**--------------------------------------------------------------------------*/
   1.565 -
   1.566 -/*
   1.567 -** This routine is called when we are about to allocate n additional bytes
   1.568 -** of memory.  If the new allocation will put is over the soft allocation
   1.569 -** limit, then invoke sqlite3_release_memory() to try to release some
   1.570 -** memory before continuing with the allocation.
   1.571 -**
   1.572 -** This routine also makes sure that the thread-specific-data (TSD) has
   1.573 -** be allocated.  If it has not and can not be allocated, then return
   1.574 -** false.  The updateMemoryUsedCount() routine below will deallocate
   1.575 -** the TSD if it ought to be.
   1.576 -**
   1.577 -** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is
   1.578 -** a no-op
   1.579 -*/ 
   1.580 -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   1.581 -static int enforceSoftLimit(int n){
   1.582 -  ThreadData *pTsd = sqlite3ThreadData();
   1.583 -  if( pTsd==0 ){
   1.584 -    return 0;
   1.585 -  }
   1.586 -  assert( pTsd->nAlloc>=0 );
   1.587 -  if( n>0 && pTsd->nSoftHeapLimit>0 ){
   1.588 -    while( pTsd->nAlloc+n>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) ){}
   1.589 -  }
   1.590 -  return 1;
   1.591 -}
   1.592 -#else
   1.593 -# define enforceSoftLimit(X)  1
   1.594 -#endif
   1.595 -
   1.596 -/*
   1.597 -** Update the count of total outstanding memory that is held in
   1.598 -** thread-specific-data (TSD).  If after this update the TSD is
   1.599 -** no longer being used, then deallocate it.
   1.600 -**
   1.601 -** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is
   1.602 -** a no-op
   1.603 -*/
   1.604 -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   1.605 -static void updateMemoryUsedCount(int n){
   1.606 -  ThreadData *pTsd = sqlite3ThreadData();
   1.607 -  if( pTsd ){
   1.608 -    pTsd->nAlloc += n;
   1.609 -    assert( pTsd->nAlloc>=0 );
   1.610 -    if( pTsd->nAlloc==0 && pTsd->nSoftHeapLimit==0 ){
   1.611 -      sqlite3ReleaseThreadData();
   1.612 -    }
   1.613 -  }
   1.614 -}
   1.615 -#else
   1.616 -#define updateMemoryUsedCount(x)  /* no-op */
   1.617 -#endif
   1.618 -
   1.619 -/*
   1.620 -** Allocate and return N bytes of uninitialised memory by calling
   1.621 -** sqlite3OsMalloc(). If the Malloc() call fails, attempt to free memory 
   1.622 -** by calling sqlite3_release_memory().
   1.623 -*/
   1.624 -void *sqlite3MallocRaw(int n, int doMemManage){
   1.625 +void *sqlite3DbMallocRaw(sqlite3 *db, unsigned n){
   1.626    void *p = 0;
   1.627 -  if( n>0 && !sqlite3MallocFailed() && (!doMemManage || enforceSoftLimit(n)) ){
   1.628 -    while( (p = OSMALLOC(n))==0 && sqlite3_release_memory(n) ){}
   1.629 -    if( !p ){
   1.630 -      sqlite3FailedMalloc();
   1.631 -      OSMALLOC_FAILED();
   1.632 -    }else if( doMemManage ){
   1.633 -      updateMemoryUsedCount(OSSIZEOF(p));
   1.634 +  if( !db || db->mallocFailed==0 ){
   1.635 +    p = sqlite3_malloc(n);
   1.636 +    if( !p && db ){
   1.637 +      db->mallocFailed = 1;
   1.638      }
   1.639    }
   1.640    return p;
   1.641  }
   1.642  
   1.643  /*
   1.644 -** Resize the allocation at p to n bytes by calling sqlite3OsRealloc(). The
   1.645 -** pointer to the new allocation is returned.  If the Realloc() call fails,
   1.646 -** attempt to free memory by calling sqlite3_release_memory().
   1.647 -*/
   1.648 -void *sqlite3Realloc(void *p, int n){
   1.649 -  if( sqlite3MallocFailed() ){
   1.650 -    return 0;
   1.651 -  }
   1.652 -
   1.653 -  if( !p ){
   1.654 -    return sqlite3Malloc(n, 1);
   1.655 -  }else{
   1.656 -    void *np = 0;
   1.657 -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   1.658 -    int origSize = OSSIZEOF(p);
   1.659 -#endif
   1.660 -    if( enforceSoftLimit(n - origSize) ){
   1.661 -      while( (np = OSREALLOC(p, n))==0 && sqlite3_release_memory(n) ){}
   1.662 -      if( !np ){
   1.663 -        sqlite3FailedMalloc();
   1.664 -        OSMALLOC_FAILED();
   1.665 -      }else{
   1.666 -        updateMemoryUsedCount(OSSIZEOF(np) - origSize);
   1.667 -      }
   1.668 -    }
   1.669 -    return np;
   1.670 -  }
   1.671 -}
   1.672 -
   1.673 -/*
   1.674 -** Free the memory pointed to by p. p must be either a NULL pointer or a 
   1.675 -** value returned by a previous call to sqlite3Malloc() or sqlite3Realloc().
   1.676 +** Resize the block of memory pointed to by p to n bytes. If the
   1.677 +** resize fails, set the mallocFailed flag inthe connection object.
   1.678  */
   1.679 -void sqlite3FreeX(void *p){
   1.680 -  if( p ){
   1.681 -    updateMemoryUsedCount(0 - OSSIZEOF(p));
   1.682 -    OSFREE(p);
   1.683 -  }
   1.684 -}
   1.685 -
   1.686 -/*
   1.687 -** A version of sqliteMalloc() that is always a function, not a macro.
   1.688 -** Currently, this is used only to alloc to allocate the parser engine.
   1.689 -*/
   1.690 -void *sqlite3MallocX(int n){
   1.691 -  return sqliteMalloc(n);
   1.692 -}
   1.693 -
   1.694 -/*
   1.695 -** sqlite3Malloc
   1.696 -** sqlite3ReallocOrFree
   1.697 -**
   1.698 -** These two are implemented as wrappers around sqlite3MallocRaw(), 
   1.699 -** sqlite3Realloc() and sqlite3Free().
   1.700 -*/ 
   1.701 -void *sqlite3Malloc(int n, int doMemManage){
   1.702 -  void *p = sqlite3MallocRaw(n, doMemManage);
   1.703 -  if( p ){
   1.704 -    memset(p, 0, n);
   1.705 -  }
   1.706 -  return p;
   1.707 -}
   1.708 -void *sqlite3ReallocOrFree(void *p, int n){
   1.709 -  void *pNew;
   1.710 -  pNew = sqlite3Realloc(p, n);
   1.711 -  if( !pNew ){
   1.712 -    sqlite3FreeX(p);
   1.713 +void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
   1.714 +  void *pNew = 0;
   1.715 +  if( db->mallocFailed==0 ){
   1.716 +    pNew = sqlite3_realloc(p, n);
   1.717 +    if( !pNew ){
   1.718 +      db->mallocFailed = 1;
   1.719 +    }
   1.720    }
   1.721    return pNew;
   1.722  }
   1.723  
   1.724  /*
   1.725 -** sqlite3ThreadSafeMalloc() and sqlite3ThreadSafeFree() are used in those
   1.726 -** rare scenarios where sqlite may allocate memory in one thread and free
   1.727 -** it in another. They are exactly the same as sqlite3Malloc() and 
   1.728 -** sqlite3Free() except that:
   1.729 -**
   1.730 -**   * The allocated memory is not included in any calculations with 
   1.731 -**     respect to the soft-heap-limit, and
   1.732 -**
   1.733 -**   * sqlite3ThreadSafeMalloc() must be matched with ThreadSafeFree(),
   1.734 -**     not sqlite3Free(). Calling sqlite3Free() on memory obtained from
   1.735 -**     ThreadSafeMalloc() will cause an error somewhere down the line.
   1.736 +** Attempt to reallocate p.  If the reallocation fails, then free p
   1.737 +** and set the mallocFailed flag in the database connection.
   1.738  */
   1.739 -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   1.740 -void *sqlite3ThreadSafeMalloc(int n){
   1.741 -  (void)ENTER_MALLOC;
   1.742 -  return sqlite3Malloc(n, 0);
   1.743 +void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
   1.744 +  void *pNew;
   1.745 +  pNew = sqlite3DbRealloc(db, p, n);
   1.746 +  if( !pNew ){
   1.747 +    sqlite3_free(p);
   1.748 +  }
   1.749 +  return pNew;
   1.750  }
   1.751 -void sqlite3ThreadSafeFree(void *p){
   1.752 -  (void)ENTER_MALLOC;
   1.753 -  if( p ){
   1.754 -    OSFREE(p);
   1.755 -  }
   1.756 -}
   1.757 -#endif
   1.758 -
   1.759 -
   1.760 -/*
   1.761 -** Return the number of bytes allocated at location p. p must be either 
   1.762 -** a NULL pointer (in which case 0 is returned) or a pointer returned by 
   1.763 -** sqlite3Malloc(), sqlite3Realloc() or sqlite3ReallocOrFree().
   1.764 -**
   1.765 -** The number of bytes allocated does not include any overhead inserted by 
   1.766 -** any malloc() wrapper functions that may be called. So the value returned
   1.767 -** is the number of bytes that were available to SQLite using pointer p, 
   1.768 -** regardless of how much memory was actually allocated.
   1.769 -*/
   1.770 -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   1.771 -int sqlite3AllocSize(void *p){
   1.772 -  return OSSIZEOF(p);
   1.773 -}
   1.774 -#endif
   1.775  
   1.776  /*
   1.777  ** Make a copy of a string in memory obtained from sqliteMalloc(). These 
   1.778 @@ -727,14 +144,14 @@
   1.779    int n;
   1.780    if( z==0 ) return 0;
   1.781    n = strlen(z)+1;
   1.782 -  zNew = sqlite3MallocRaw(n, 1);
   1.783 +  zNew = sqlite3_malloc(n);
   1.784    if( zNew ) memcpy(zNew, z, n);
   1.785    return zNew;
   1.786  }
   1.787  char *sqlite3StrNDup(const char *z, int n){
   1.788    char *zNew;
   1.789    if( z==0 ) return 0;
   1.790 -  zNew = sqlite3MallocRaw(n+1, 1);
   1.791 +  zNew = sqlite3_malloc(n+1);
   1.792    if( zNew ){
   1.793      memcpy(zNew, z, n);
   1.794      zNew[n] = 0;
   1.795 @@ -742,6 +159,21 @@
   1.796    return zNew;
   1.797  }
   1.798  
   1.799 +char *sqlite3DbStrDup(sqlite3 *db, const char *z){
   1.800 +  char *zNew = sqlite3StrDup(z);
   1.801 +  if( z && !zNew ){
   1.802 +    db->mallocFailed = 1;
   1.803 +  }
   1.804 +  return zNew;
   1.805 +}
   1.806 +char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
   1.807 +  char *zNew = sqlite3StrNDup(z, n);
   1.808 +  if( z && !zNew ){
   1.809 +    db->mallocFailed = 1;
   1.810 +  }
   1.811 +  return zNew;
   1.812 +}
   1.813 +
   1.814  /*
   1.815  ** Create a string from the 2nd and subsequent arguments (up to the
   1.816  ** first NULL argument), store the string in memory obtained from
   1.817 @@ -762,8 +194,8 @@
   1.818      nByte += strlen(z);
   1.819    }
   1.820    va_end(ap);
   1.821 -  sqliteFree(*pz);
   1.822 -  *pz = zResult = sqliteMallocRaw( nByte );
   1.823 +  sqlite3_free(*pz);
   1.824 +  *pz = zResult = sqlite3_malloc(nByte);
   1.825    if( zResult==0 ){
   1.826      return;
   1.827    }
   1.828 @@ -781,8 +213,8 @@
   1.829  
   1.830  /*
   1.831  ** This function must be called before exiting any API function (i.e. 
   1.832 -** returning control to the user) that has called sqlite3Malloc or
   1.833 -** sqlite3Realloc.
   1.834 +** returning control to the user) that has called sqlite3_malloc or
   1.835 +** sqlite3_realloc.
   1.836  **
   1.837  ** The returned value is normally a copy of the second argument to this
   1.838  ** function. However, if a malloc() failure has occured since the previous
   1.839 @@ -792,44 +224,17 @@
   1.840  ** then the connection error-code (the value returned by sqlite3_errcode())
   1.841  ** is set to SQLITE_NOMEM.
   1.842  */
   1.843 -int sqlite3_mallocHasFailed = 0;
   1.844  int sqlite3ApiExit(sqlite3* db, int rc){
   1.845 -  if( sqlite3MallocFailed() ){
   1.846 -    sqlite3_mallocHasFailed = 0;
   1.847 -    sqlite3OsLeaveMutex();
   1.848 +  /* If the db handle is not NULL, then we must hold the connection handle
   1.849 +  ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 
   1.850 +  ** is unsafe, as is the call to sqlite3Error().
   1.851 +  */
   1.852 +  assert( !db || sqlite3_mutex_held(db->mutex) );
   1.853 +  if( db && db->mallocFailed ){
   1.854      sqlite3Error(db, SQLITE_NOMEM, 0);
   1.855 +    db->mallocFailed = 0;
   1.856      rc = SQLITE_NOMEM;
   1.857    }
   1.858    return rc & (db ? db->errMask : 0xff);
   1.859  }
   1.860 -
   1.861 -/* 
   1.862 -** Set the "malloc has failed" condition to true for this thread.
   1.863 -*/
   1.864 -void sqlite3FailedMalloc(){
   1.865 -  if( !sqlite3MallocFailed() ){
   1.866 -    sqlite3OsEnterMutex();
   1.867 -    assert( sqlite3_mallocHasFailed==0 );
   1.868 -    sqlite3_mallocHasFailed = 1;
   1.869 -  }
   1.870 -}
   1.871 -
   1.872 -#ifdef SQLITE_MEMDEBUG
   1.873 -/*
   1.874 -** This function sets a flag in the thread-specific-data structure that will
   1.875 -** cause an assert to fail if sqliteMalloc() or sqliteRealloc() is called.
   1.876 -*/
   1.877 -void sqlite3MallocDisallow(){
   1.878 -  assert( sqlite3_mallocDisallowed>=0 );
   1.879 -  sqlite3_mallocDisallowed++;
   1.880 -}
   1.881 -
   1.882 -/*
   1.883 -** This function clears the flag set in the thread-specific-data structure set
   1.884 -** by sqlite3MallocDisallow().
   1.885 -*/
   1.886 -void sqlite3MallocAllow(){
   1.887 -  assert( sqlite3_mallocDisallowed>0 );
   1.888 -  sqlite3_mallocDisallowed--;
   1.889 -}
   1.890 -#endif
   1.891 +