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