persistentstorage/sql/SQLite364/malloc.c
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     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 **
       
    13 ** Memory allocation functions used throughout sqlite.
       
    14 **
       
    15 ** $Id: malloc.c,v 1.45 2008/10/12 00:27:53 shane Exp $
       
    16 */
       
    17 #include "sqliteInt.h"
       
    18 #include <stdarg.h>
       
    19 #include <ctype.h>
       
    20 
       
    21 /*
       
    22 ** This routine runs when the memory allocator sees that the
       
    23 ** total memory allocation is about to exceed the soft heap
       
    24 ** limit.
       
    25 */
       
    26 static void softHeapLimitEnforcer(
       
    27   void *NotUsed, 
       
    28   sqlite3_int64 inUse,
       
    29   int allocSize
       
    30 ){
       
    31   sqlite3_release_memory(allocSize);
       
    32 }
       
    33 
       
    34 /*
       
    35 ** Set the soft heap-size limit for the library. Passing a zero or 
       
    36 ** negative value indicates no limit.
       
    37 */
       
    38 void sqlite3_soft_heap_limit(int n){
       
    39   sqlite3_uint64 iLimit;
       
    40   int overage;
       
    41   if( n<0 ){
       
    42     iLimit = 0;
       
    43   }else{
       
    44     iLimit = n;
       
    45   }
       
    46   sqlite3_initialize();
       
    47   if( iLimit>0 ){
       
    48     sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit);
       
    49   }else{
       
    50     sqlite3MemoryAlarm(0, 0, 0);
       
    51   }
       
    52   overage = sqlite3_memory_used() - n;
       
    53   if( overage>0 ){
       
    54     sqlite3_release_memory(overage);
       
    55   }
       
    56 }
       
    57 
       
    58 /*
       
    59 ** Attempt to release up to n bytes of non-essential memory currently
       
    60 ** held by SQLite. An example of non-essential memory is memory used to
       
    61 ** cache database pages that are not currently in use.
       
    62 */
       
    63 int sqlite3_release_memory(int n){
       
    64 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
       
    65   int nRet = 0;
       
    66 #if 0
       
    67   nRet += sqlite3VdbeReleaseMemory(n);
       
    68 #endif
       
    69   nRet += sqlite3PcacheReleaseMemory(n-nRet);
       
    70   return nRet;
       
    71 #else
       
    72   return SQLITE_OK;
       
    73 #endif
       
    74 }
       
    75 
       
    76 /*
       
    77 ** State information local to the memory allocation subsystem.
       
    78 */
       
    79 static SQLITE_WSD struct Mem0Global {
       
    80   /* Number of free pages for scratch and page-cache memory */
       
    81   u32 nScratchFree;
       
    82   u32 nPageFree;
       
    83 
       
    84   sqlite3_mutex *mutex;         /* Mutex to serialize access */
       
    85 
       
    86   /*
       
    87   ** The alarm callback and its arguments.  The mem0.mutex lock will
       
    88   ** be held while the callback is running.  Recursive calls into
       
    89   ** the memory subsystem are allowed, but no new callbacks will be
       
    90   ** issued.  The alarmBusy variable is set to prevent recursive
       
    91   ** callbacks.
       
    92   */
       
    93   sqlite3_int64 alarmThreshold;
       
    94   void (*alarmCallback)(void*, sqlite3_int64,int);
       
    95   void *alarmArg;
       
    96   int alarmBusy;
       
    97 
       
    98   /*
       
    99   ** Pointers to the end of sqlite3GlobalConfig.pScratch and
       
   100   ** sqlite3GlobalConfig.pPage to a block of memory that records
       
   101   ** which pages are available.
       
   102   */
       
   103   u32 *aScratchFree;
       
   104   u32 *aPageFree;
       
   105 } mem0 = { 62560955 };
       
   106 
       
   107 #define mem0 GLOBAL(struct Mem0Global, mem0)
       
   108 
       
   109 /*
       
   110 ** Initialize the memory allocation subsystem.
       
   111 */
       
   112 int sqlite3MallocInit(void){
       
   113   if( sqlite3GlobalConfig.m.xMalloc==0 ){
       
   114     sqlite3MemSetDefault();
       
   115   }
       
   116   memset(&mem0, 0, sizeof(mem0));
       
   117   if( sqlite3GlobalConfig.bCoreMutex ){
       
   118     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
       
   119   }
       
   120   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
       
   121       && sqlite3GlobalConfig.nScratch>=0 ){
       
   122     int i;
       
   123     sqlite3GlobalConfig.szScratch -= 4;
       
   124     mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch)
       
   125                   [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch];
       
   126     for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; }
       
   127     mem0.nScratchFree = sqlite3GlobalConfig.nScratch;
       
   128   }else{
       
   129     sqlite3GlobalConfig.pScratch = 0;
       
   130     sqlite3GlobalConfig.szScratch = 0;
       
   131   }
       
   132   if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512
       
   133       && sqlite3GlobalConfig.nPage>=1 ){
       
   134     int i;
       
   135     int overhead;
       
   136     int sz = sqlite3GlobalConfig.szPage;
       
   137     int n = sqlite3GlobalConfig.nPage;
       
   138     overhead = (4*n + sz - 1)/sz;
       
   139     sqlite3GlobalConfig.nPage -= overhead;
       
   140     mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage)
       
   141                   [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage];
       
   142     for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; }
       
   143     mem0.nPageFree = sqlite3GlobalConfig.nPage;
       
   144   }else{
       
   145     sqlite3GlobalConfig.pPage = 0;
       
   146     sqlite3GlobalConfig.szPage = 0;
       
   147   }
       
   148   return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
       
   149 }
       
   150 
       
   151 /*
       
   152 ** Deinitialize the memory allocation subsystem.
       
   153 */
       
   154 void sqlite3MallocEnd(void){
       
   155   if( sqlite3GlobalConfig.m.xShutdown ){
       
   156   sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
       
   157   }
       
   158   memset(&mem0, 0, sizeof(mem0));
       
   159 }
       
   160 
       
   161 /*
       
   162 ** Return the amount of memory currently checked out.
       
   163 */
       
   164 sqlite3_int64 sqlite3_memory_used(void){
       
   165   int n, mx;
       
   166   sqlite3_int64 res;
       
   167   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
       
   168   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
       
   169   return res;
       
   170 }
       
   171 
       
   172 /*
       
   173 ** Return the maximum amount of memory that has ever been
       
   174 ** checked out since either the beginning of this process
       
   175 ** or since the most recent reset.
       
   176 */
       
   177 sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
       
   178   int n, mx;
       
   179   sqlite3_int64 res;
       
   180   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
       
   181   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
       
   182   return res;
       
   183 }
       
   184 
       
   185 /*
       
   186 ** Change the alarm callback
       
   187 */
       
   188 int sqlite3MemoryAlarm(
       
   189   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
       
   190   void *pArg,
       
   191   sqlite3_int64 iThreshold
       
   192 ){
       
   193   sqlite3_mutex_enter(mem0.mutex);
       
   194   mem0.alarmCallback = xCallback;
       
   195   mem0.alarmArg = pArg;
       
   196   mem0.alarmThreshold = iThreshold;
       
   197   sqlite3_mutex_leave(mem0.mutex);
       
   198   return SQLITE_OK;
       
   199 }
       
   200 
       
   201 #ifndef SQLITE_OMIT_DEPRECATED
       
   202 /*
       
   203 ** Deprecated external interface.  Internal/core SQLite code
       
   204 ** should call sqlite3MemoryAlarm.
       
   205 */
       
   206 int sqlite3_memory_alarm(
       
   207   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
       
   208   void *pArg,
       
   209   sqlite3_int64 iThreshold
       
   210 ){
       
   211   return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
       
   212 }
       
   213 #endif
       
   214 
       
   215 /*
       
   216 ** Trigger the alarm 
       
   217 */
       
   218 static void sqlite3MallocAlarm(int nByte){
       
   219   void (*xCallback)(void*,sqlite3_int64,int);
       
   220   sqlite3_int64 nowUsed;
       
   221   void *pArg;
       
   222   if( mem0.alarmCallback==0 || mem0.alarmBusy  ) return;
       
   223   mem0.alarmBusy = 1;
       
   224   xCallback = mem0.alarmCallback;
       
   225   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
       
   226   pArg = mem0.alarmArg;
       
   227   sqlite3_mutex_leave(mem0.mutex);
       
   228   xCallback(pArg, nowUsed, nByte);
       
   229   sqlite3_mutex_enter(mem0.mutex);
       
   230   mem0.alarmBusy = 0;
       
   231 }
       
   232 
       
   233 /*
       
   234 ** Do a memory allocation with statistics and alarms.  Assume the
       
   235 ** lock is already held.
       
   236 */
       
   237 static int mallocWithAlarm(int n, void **pp){
       
   238   int nFull;
       
   239   void *p;
       
   240   assert( sqlite3_mutex_held(mem0.mutex) );
       
   241   nFull = sqlite3GlobalConfig.m.xRoundup(n);
       
   242   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
       
   243   if( mem0.alarmCallback!=0 ){
       
   244     int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
       
   245     if( nUsed+nFull >= mem0.alarmThreshold ){
       
   246       sqlite3MallocAlarm(nFull);
       
   247     }
       
   248   }
       
   249   p = sqlite3GlobalConfig.m.xMalloc(nFull);
       
   250   if( p==0 && mem0.alarmCallback ){
       
   251     sqlite3MallocAlarm(nFull);
       
   252     p = sqlite3GlobalConfig.m.xMalloc(nFull);
       
   253   }
       
   254   if( p ){
       
   255     nFull = sqlite3MallocSize(p);
       
   256     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
       
   257   }
       
   258   *pp = p;
       
   259   return nFull;
       
   260 }
       
   261 
       
   262 /*
       
   263 ** Allocate memory.  This routine is like sqlite3_malloc() except that it
       
   264 ** assumes the memory subsystem has already been initialized.
       
   265 */
       
   266 void *sqlite3Malloc(int n){
       
   267   void *p;
       
   268   if( n<=0 ){
       
   269     p = 0;
       
   270   }else if( sqlite3GlobalConfig.bMemstat ){
       
   271     sqlite3_mutex_enter(mem0.mutex);
       
   272     mallocWithAlarm(n, &p);
       
   273     sqlite3_mutex_leave(mem0.mutex);
       
   274   }else{
       
   275     p = sqlite3GlobalConfig.m.xMalloc(n);
       
   276   }
       
   277   return p;
       
   278 }
       
   279 
       
   280 /*
       
   281 ** This version of the memory allocation is for use by the application.
       
   282 ** First make sure the memory subsystem is initialized, then do the
       
   283 ** allocation.
       
   284 */
       
   285 void *sqlite3_malloc(int n){
       
   286 #ifndef SQLITE_OMIT_AUTOINIT
       
   287   if( sqlite3_initialize() ) return 0;
       
   288 #endif
       
   289   return sqlite3Malloc(n);
       
   290 }
       
   291 
       
   292 /*
       
   293 ** Each thread may only have a single outstanding allocation from
       
   294 ** xScratchMalloc().  We verify this constraint in the single-threaded
       
   295 ** case by setting scratchAllocOut to 1 when an allocation
       
   296 ** is outstanding clearing it when the allocation is freed.
       
   297 */
       
   298 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
       
   299 static int scratchAllocOut = 0;
       
   300 #endif
       
   301 
       
   302 
       
   303 /*
       
   304 ** Allocate memory that is to be used and released right away.
       
   305 ** This routine is similar to alloca() in that it is not intended
       
   306 ** for situations where the memory might be held long-term.  This
       
   307 ** routine is intended to get memory to old large transient data
       
   308 ** structures that would not normally fit on the stack of an
       
   309 ** embedded processor.
       
   310 */
       
   311 void *sqlite3ScratchMalloc(int n){
       
   312   void *p;
       
   313   assert( n>0 );
       
   314 
       
   315 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
       
   316   /* Verify that no more than one scratch allocation per thread
       
   317   ** is outstanding at one time.  (This is only checked in the
       
   318   ** single-threaded case since checking in the multi-threaded case
       
   319   ** would be much more complicated.) */
       
   320   assert( scratchAllocOut==0 );
       
   321 #endif
       
   322 
       
   323   if( sqlite3GlobalConfig.szScratch<n ){
       
   324     goto scratch_overflow;
       
   325   }else{  
       
   326     sqlite3_mutex_enter(mem0.mutex);
       
   327     if( mem0.nScratchFree==0 ){
       
   328       sqlite3_mutex_leave(mem0.mutex);
       
   329       goto scratch_overflow;
       
   330     }else{
       
   331       int i;
       
   332       i = mem0.aScratchFree[--mem0.nScratchFree];
       
   333       i *= sqlite3GlobalConfig.szScratch;
       
   334       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
       
   335       sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
       
   336       sqlite3_mutex_leave(mem0.mutex);
       
   337       p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i];
       
   338     }
       
   339   }
       
   340 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
       
   341   scratchAllocOut = p!=0;
       
   342 #endif
       
   343 
       
   344   return p;
       
   345 
       
   346 scratch_overflow:
       
   347   if( sqlite3GlobalConfig.bMemstat ){
       
   348     sqlite3_mutex_enter(mem0.mutex);
       
   349     sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
       
   350     n = mallocWithAlarm(n, &p);
       
   351     if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
       
   352     sqlite3_mutex_leave(mem0.mutex);
       
   353   }else{
       
   354     p = sqlite3GlobalConfig.m.xMalloc(n);
       
   355   }
       
   356 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
       
   357   scratchAllocOut = p!=0;
       
   358 #endif
       
   359   return p;    
       
   360 }
       
   361 void sqlite3ScratchFree(void *p){
       
   362   if( p ){
       
   363 
       
   364 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
       
   365     /* Verify that no more than one scratch allocation per thread
       
   366     ** is outstanding at one time.  (This is only checked in the
       
   367     ** single-threaded case since checking in the multi-threaded case
       
   368     ** would be much more complicated.) */
       
   369     assert( scratchAllocOut==1 );
       
   370     scratchAllocOut = 0;
       
   371 #endif
       
   372 
       
   373     if( sqlite3GlobalConfig.pScratch==0
       
   374            || p<sqlite3GlobalConfig.pScratch
       
   375            || p>=(void*)mem0.aScratchFree ){
       
   376       if( sqlite3GlobalConfig.bMemstat ){
       
   377         int iSize = sqlite3MallocSize(p);
       
   378         sqlite3_mutex_enter(mem0.mutex);
       
   379         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
       
   380         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
       
   381         sqlite3GlobalConfig.m.xFree(p);
       
   382         sqlite3_mutex_leave(mem0.mutex);
       
   383       }else{
       
   384         sqlite3GlobalConfig.m.xFree(p);
       
   385       }
       
   386     }else{
       
   387       int i;
       
   388       i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pScratch;
       
   389       i /= sqlite3GlobalConfig.szScratch;
       
   390       assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
       
   391       sqlite3_mutex_enter(mem0.mutex);
       
   392       assert( mem0.nScratchFree<sqlite3GlobalConfig.nScratch );
       
   393       mem0.aScratchFree[mem0.nScratchFree++] = i;
       
   394       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
       
   395       sqlite3_mutex_leave(mem0.mutex);
       
   396     }
       
   397   }
       
   398 }
       
   399 
       
   400 /*
       
   401 ** Allocate memory to be used by the page cache.  Make use of the
       
   402 ** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
       
   403 ** and that memory is of the right size and is not completely
       
   404 ** consumed.  Otherwise, failover to sqlite3Malloc().
       
   405 */
       
   406 #if 0
       
   407 void *sqlite3PageMalloc(int n){
       
   408   void *p;
       
   409   assert( n>0 );
       
   410   assert( (n & (n-1))==0 );
       
   411   assert( n>=512 && n<=32768 );
       
   412 
       
   413   if( sqlite3GlobalConfig.szPage<n ){
       
   414     goto page_overflow;
       
   415   }else{  
       
   416     sqlite3_mutex_enter(mem0.mutex);
       
   417     if( mem0.nPageFree==0 ){
       
   418       sqlite3_mutex_leave(mem0.mutex);
       
   419       goto page_overflow;
       
   420     }else{
       
   421       int i;
       
   422       i = mem0.aPageFree[--mem0.nPageFree];
       
   423       sqlite3_mutex_leave(mem0.mutex);
       
   424       i *= sqlite3GlobalConfig.szPage;
       
   425       sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
       
   426       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
       
   427       p = (void*)&((char*)sqlite3GlobalConfig.pPage)[i];
       
   428     }
       
   429   }
       
   430   return p;
       
   431 
       
   432 page_overflow:
       
   433   if( sqlite3GlobalConfig.bMemstat ){
       
   434     sqlite3_mutex_enter(mem0.mutex);
       
   435     sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
       
   436     n = mallocWithAlarm(n, &p);
       
   437     if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
       
   438     sqlite3_mutex_leave(mem0.mutex);
       
   439   }else{
       
   440     p = sqlite3GlobalConfig.m.xMalloc(n);
       
   441   }
       
   442   return p;    
       
   443 }
       
   444 void sqlite3PageFree(void *p){
       
   445   if( p ){
       
   446     if( sqlite3GlobalConfig.pPage==0
       
   447            || p<sqlite3GlobalConfig.pPage
       
   448            || p>=(void*)mem0.aPageFree ){
       
   449       /* In this case, the page allocation was obtained from a regular 
       
   450       ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory 
       
   451       ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
       
   452       */
       
   453       if( sqlite3GlobalConfig.bMemstat ){
       
   454         int iSize = sqlite3MallocSize(p);
       
   455         sqlite3_mutex_enter(mem0.mutex);
       
   456         sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
       
   457         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
       
   458         sqlite3GlobalConfig.m.xFree(p);
       
   459         sqlite3_mutex_leave(mem0.mutex);
       
   460       }else{
       
   461         sqlite3GlobalConfig.m.xFree(p);
       
   462       }
       
   463     }else{
       
   464       /* The page allocation was allocated from the sqlite3GlobalConfig.pPage
       
   465       ** buffer. In this case all that is add the index of the page in
       
   466       ** the sqlite3GlobalConfig.pPage array to the set of free indexes stored
       
   467       ** in the mem0.aPageFree[] array.
       
   468       */
       
   469       int i;
       
   470       i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pPage;
       
   471       i /= sqlite3GlobalConfig.szPage;
       
   472       assert( i>=0 && i<sqlite3GlobalConfig.nPage );
       
   473       sqlite3_mutex_enter(mem0.mutex);
       
   474       assert( mem0.nPageFree<sqlite3GlobalConfig.nPage );
       
   475       mem0.aPageFree[mem0.nPageFree++] = i;
       
   476       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
       
   477       sqlite3_mutex_leave(mem0.mutex);
       
   478 #if !defined(NDEBUG) && 0
       
   479       /* Assert that a duplicate was not just inserted into aPageFree[]. */
       
   480       for(i=0; i<mem0.nPageFree-1; i++){
       
   481         assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
       
   482       }
       
   483 #endif
       
   484     }
       
   485   }
       
   486 }
       
   487 #endif
       
   488 
       
   489 /*
       
   490 ** TRUE if p is a lookaside memory allocation from db
       
   491 */
       
   492 #ifndef SQLITE_OMIT_LOOKASIDE
       
   493 static int isLookaside(sqlite3 *db, void *p){
       
   494   return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
       
   495 }
       
   496 #else
       
   497 #define isLookaside(A,B) 0
       
   498 #endif
       
   499 
       
   500 /*
       
   501 ** Return the size of a memory allocation previously obtained from
       
   502 ** sqlite3Malloc() or sqlite3_malloc().
       
   503 */
       
   504 int sqlite3MallocSize(void *p){
       
   505   return sqlite3GlobalConfig.m.xSize(p);
       
   506 }
       
   507 int sqlite3DbMallocSize(sqlite3 *db, void *p){
       
   508   if( isLookaside(db, p) ){
       
   509     return db->lookaside.sz;
       
   510   }else{
       
   511     return sqlite3GlobalConfig.m.xSize(p);
       
   512   }
       
   513 }
       
   514 
       
   515 /*
       
   516 ** Free memory previously obtained from sqlite3Malloc().
       
   517 */
       
   518 void sqlite3_free(void *p){
       
   519   if( p==0 ) return;
       
   520   if( sqlite3GlobalConfig.bMemstat ){
       
   521     sqlite3_mutex_enter(mem0.mutex);
       
   522     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
       
   523     sqlite3GlobalConfig.m.xFree(p);
       
   524     sqlite3_mutex_leave(mem0.mutex);
       
   525   }else{
       
   526     sqlite3GlobalConfig.m.xFree(p);
       
   527   }
       
   528 }
       
   529 
       
   530 /*
       
   531 ** Free memory that might be associated with a particular database
       
   532 ** connection.
       
   533 */
       
   534 void sqlite3DbFree(sqlite3 *db, void *p){
       
   535   if( isLookaside(db, p) ){
       
   536     LookasideSlot *pBuf = (LookasideSlot*)p;
       
   537     pBuf->pNext = db->lookaside.pFree;
       
   538     db->lookaside.pFree = pBuf;
       
   539     db->lookaside.nOut--;
       
   540   }else{
       
   541     sqlite3_free(p);
       
   542   }
       
   543 }
       
   544 
       
   545 /*
       
   546 ** Change the size of an existing memory allocation
       
   547 */
       
   548 void *sqlite3Realloc(void *pOld, int nBytes){
       
   549   int nOld, nNew;
       
   550   void *pNew;
       
   551   if( pOld==0 ){
       
   552     return sqlite3Malloc(nBytes);
       
   553   }
       
   554   if( nBytes<=0 ){
       
   555     sqlite3_free(pOld);
       
   556     return 0;
       
   557   }
       
   558   nOld = sqlite3MallocSize(pOld);
       
   559   if( sqlite3GlobalConfig.bMemstat ){
       
   560     sqlite3_mutex_enter(mem0.mutex);
       
   561     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
       
   562     nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
       
   563     if( nOld==nNew ){
       
   564       pNew = pOld;
       
   565     }else{
       
   566       if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= 
       
   567             mem0.alarmThreshold ){
       
   568         sqlite3MallocAlarm(nNew-nOld);
       
   569       }
       
   570       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
       
   571       if( pNew==0 && mem0.alarmCallback ){
       
   572         sqlite3MallocAlarm(nBytes);
       
   573         pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
       
   574       }
       
   575       if( pNew ){
       
   576         nNew = sqlite3MallocSize(pNew);
       
   577         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
       
   578       }
       
   579     }
       
   580     sqlite3_mutex_leave(mem0.mutex);
       
   581   }else{
       
   582     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nBytes);
       
   583   }
       
   584   return pNew;
       
   585 }
       
   586 
       
   587 /*
       
   588 ** The public interface to sqlite3Realloc.  Make sure that the memory
       
   589 ** subsystem is initialized prior to invoking sqliteRealloc.
       
   590 */
       
   591 void *sqlite3_realloc(void *pOld, int n){
       
   592 #ifndef SQLITE_OMIT_AUTOINIT
       
   593   if( sqlite3_initialize() ) return 0;
       
   594 #endif
       
   595   return sqlite3Realloc(pOld, n);
       
   596 }
       
   597 
       
   598 
       
   599 /*
       
   600 ** Allocate and zero memory.
       
   601 */ 
       
   602 void *sqlite3MallocZero(int n){
       
   603   void *p = sqlite3Malloc(n);
       
   604   if( p ){
       
   605     memset(p, 0, n);
       
   606   }
       
   607   return p;
       
   608 }
       
   609 
       
   610 /*
       
   611 ** Allocate and zero memory.  If the allocation fails, make
       
   612 ** the mallocFailed flag in the connection pointer.
       
   613 */
       
   614 void *sqlite3DbMallocZero(sqlite3 *db, int n){
       
   615   void *p = sqlite3DbMallocRaw(db, n);
       
   616   if( p ){
       
   617     memset(p, 0, n);
       
   618   }
       
   619   return p;
       
   620 }
       
   621 
       
   622 /*
       
   623 ** Allocate and zero memory.  If the allocation fails, make
       
   624 ** the mallocFailed flag in the connection pointer.
       
   625 **
       
   626 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
       
   627 ** failure on the same database connection) then always return 0.
       
   628 ** Hence for a particular database connection, once malloc starts
       
   629 ** failing, it fails consistently until mallocFailed is reset.
       
   630 ** This is an important assumption.  There are many places in the
       
   631 ** code that do things like this:
       
   632 **
       
   633 **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
       
   634 **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
       
   635 **         if( b ) a[10] = 9;
       
   636 **
       
   637 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
       
   638 ** that all prior mallocs (ex: "a") worked too.
       
   639 */
       
   640 void *sqlite3DbMallocRaw(sqlite3 *db, int n){
       
   641   void *p;
       
   642 #ifndef SQLITE_OMIT_LOOKASIDE
       
   643   if( db ){
       
   644     LookasideSlot *pBuf;
       
   645     if( db->mallocFailed ){
       
   646       return 0;
       
   647     }
       
   648     if( db->lookaside.bEnabled && n<=db->lookaside.sz
       
   649          && (pBuf = db->lookaside.pFree)!=0 ){
       
   650       db->lookaside.pFree = pBuf->pNext;
       
   651       db->lookaside.nOut++;
       
   652       if( db->lookaside.nOut>db->lookaside.mxOut ){
       
   653         db->lookaside.mxOut = db->lookaside.nOut;
       
   654       }
       
   655       return (void*)pBuf;
       
   656     }
       
   657   }
       
   658 #else
       
   659   if( db && db->mallocFailed ){
       
   660     return 0;
       
   661   }
       
   662 #endif
       
   663   p = sqlite3Malloc(n);
       
   664   if( !p && db ){
       
   665     db->mallocFailed = 1;
       
   666   }
       
   667   return p;
       
   668 }
       
   669 
       
   670 /*
       
   671 ** Resize the block of memory pointed to by p to n bytes. If the
       
   672 ** resize fails, set the mallocFailed flag in the connection object.
       
   673 */
       
   674 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
       
   675   void *pNew = 0;
       
   676   if( db->mallocFailed==0 ){
       
   677     if( p==0 ){
       
   678       return sqlite3DbMallocRaw(db, n);
       
   679     }
       
   680     if( isLookaside(db, p) ){
       
   681       if( n<=db->lookaside.sz ){
       
   682         return p;
       
   683       }
       
   684       pNew = sqlite3DbMallocRaw(db, n);
       
   685       if( pNew ){
       
   686         memcpy(pNew, p, db->lookaside.sz);
       
   687         sqlite3DbFree(db, p);
       
   688       }
       
   689     }else{
       
   690       pNew = sqlite3_realloc(p, n);
       
   691       if( !pNew ){
       
   692         db->mallocFailed = 1;
       
   693       }
       
   694     }
       
   695   }
       
   696   return pNew;
       
   697 }
       
   698 
       
   699 /*
       
   700 ** Attempt to reallocate p.  If the reallocation fails, then free p
       
   701 ** and set the mallocFailed flag in the database connection.
       
   702 */
       
   703 void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
       
   704   void *pNew;
       
   705   pNew = sqlite3DbRealloc(db, p, n);
       
   706   if( !pNew ){
       
   707     sqlite3DbFree(db, p);
       
   708   }
       
   709   return pNew;
       
   710 }
       
   711 
       
   712 /*
       
   713 ** Make a copy of a string in memory obtained from sqliteMalloc(). These 
       
   714 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
       
   715 ** is because when memory debugging is turned on, these two functions are 
       
   716 ** called via macros that record the current file and line number in the
       
   717 ** ThreadData structure.
       
   718 */
       
   719 char *sqlite3DbStrDup(sqlite3 *db, const char *z){
       
   720   char *zNew;
       
   721   size_t n;
       
   722   if( z==0 ){
       
   723     return 0;
       
   724   }
       
   725   n = strlen(z)+1;
       
   726   assert( (n&0x7fffffff)==n );
       
   727   zNew = sqlite3DbMallocRaw(db, (int)n);
       
   728   if( zNew ){
       
   729     memcpy(zNew, z, n);
       
   730   }
       
   731   return zNew;
       
   732 }
       
   733 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
       
   734   char *zNew;
       
   735   if( z==0 ){
       
   736     return 0;
       
   737   }
       
   738   assert( (n&0x7fffffff)==n );
       
   739   zNew = sqlite3DbMallocRaw(db, n+1);
       
   740   if( zNew ){
       
   741     memcpy(zNew, z, n);
       
   742     zNew[n] = 0;
       
   743   }
       
   744   return zNew;
       
   745 }
       
   746 
       
   747 /*
       
   748 ** Create a string from the zFromat argument and the va_list that follows.
       
   749 ** Store the string in memory obtained from sqliteMalloc() and make *pz
       
   750 ** point to that string.
       
   751 */
       
   752 void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
       
   753   va_list ap;
       
   754   char *z;
       
   755 
       
   756   va_start(ap, zFormat);
       
   757   z = sqlite3VMPrintf(db, zFormat, ap);
       
   758   va_end(ap);
       
   759   sqlite3DbFree(db, *pz);
       
   760   *pz = z;
       
   761 }
       
   762 
       
   763 
       
   764 /*
       
   765 ** This function must be called before exiting any API function (i.e. 
       
   766 ** returning control to the user) that has called sqlite3_malloc or
       
   767 ** sqlite3_realloc.
       
   768 **
       
   769 ** The returned value is normally a copy of the second argument to this
       
   770 ** function. However, if a malloc() failure has occured since the previous
       
   771 ** invocation SQLITE_NOMEM is returned instead. 
       
   772 **
       
   773 ** If the first argument, db, is not NULL and a malloc() error has occured,
       
   774 ** then the connection error-code (the value returned by sqlite3_errcode())
       
   775 ** is set to SQLITE_NOMEM.
       
   776 */
       
   777 int sqlite3ApiExit(sqlite3* db, int rc){
       
   778   /* If the db handle is not NULL, then we must hold the connection handle
       
   779   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 
       
   780   ** is unsafe, as is the call to sqlite3Error().
       
   781   */
       
   782   assert( !db || sqlite3_mutex_held(db->mutex) );
       
   783   if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
       
   784     sqlite3Error(db, SQLITE_NOMEM, 0);
       
   785     db->mallocFailed = 0;
       
   786     rc = SQLITE_NOMEM;
       
   787   }
       
   788   return rc & (db ? db->errMask : 0xff);
       
   789 }