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