xref: /sqlite-3.40.0/src/malloc.c (revision c9099d2d)
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 */
sqlite3_release_memory(int n)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 ** Default value of the hard heap limit.  0 means "no limit".
37 */
38 #ifndef SQLITE_MAX_MEMORY
39 # define SQLITE_MAX_MEMORY 0
40 #endif
41 
42 /*
43 ** State information local to the memory allocation subsystem.
44 */
45 static SQLITE_WSD struct Mem0Global {
46   sqlite3_mutex *mutex;         /* Mutex to serialize access */
47   sqlite3_int64 alarmThreshold; /* The soft heap limit */
48   sqlite3_int64 hardLimit;      /* The hard upper bound on memory */
49 
50   /*
51   ** True if heap is nearly "full" where "full" is defined by the
52   ** sqlite3_soft_heap_limit() setting.
53   */
54   int nearlyFull;
55 } mem0 = { 0, SQLITE_MAX_MEMORY, SQLITE_MAX_MEMORY, 0 };
56 
57 #define mem0 GLOBAL(struct Mem0Global, mem0)
58 
59 /*
60 ** Return the memory allocator mutex. sqlite3_status() needs it.
61 */
sqlite3MallocMutex(void)62 sqlite3_mutex *sqlite3MallocMutex(void){
63   return mem0.mutex;
64 }
65 
66 #ifndef SQLITE_OMIT_DEPRECATED
67 /*
68 ** Deprecated external interface.  It used to set an alarm callback
69 ** that was invoked when memory usage grew too large.  Now it is a
70 ** no-op.
71 */
sqlite3_memory_alarm(void (* xCallback)(void * pArg,sqlite3_int64 used,int N),void * pArg,sqlite3_int64 iThreshold)72 int sqlite3_memory_alarm(
73   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
74   void *pArg,
75   sqlite3_int64 iThreshold
76 ){
77   (void)xCallback;
78   (void)pArg;
79   (void)iThreshold;
80   return SQLITE_OK;
81 }
82 #endif
83 
84 /*
85 ** Set the soft heap-size limit for the library.  An argument of
86 ** zero disables the limit.  A negative argument is a no-op used to
87 ** obtain the return value.
88 **
89 ** The return value is the value of the heap limit just before this
90 ** interface was called.
91 **
92 ** If the hard heap limit is enabled, then the soft heap limit cannot
93 ** be disabled nor raised above the hard heap limit.
94 */
sqlite3_soft_heap_limit64(sqlite3_int64 n)95 sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
96   sqlite3_int64 priorLimit;
97   sqlite3_int64 excess;
98   sqlite3_int64 nUsed;
99 #ifndef SQLITE_OMIT_AUTOINIT
100   int rc = sqlite3_initialize();
101   if( rc ) return -1;
102 #endif
103   sqlite3_mutex_enter(mem0.mutex);
104   priorLimit = mem0.alarmThreshold;
105   if( n<0 ){
106     sqlite3_mutex_leave(mem0.mutex);
107     return priorLimit;
108   }
109   if( mem0.hardLimit>0 && (n>mem0.hardLimit || n==0) ){
110     n = mem0.hardLimit;
111   }
112   mem0.alarmThreshold = n;
113   nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
114   AtomicStore(&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 }
sqlite3_soft_heap_limit(int n)120 void sqlite3_soft_heap_limit(int n){
121   if( n<0 ) n = 0;
122   sqlite3_soft_heap_limit64(n);
123 }
124 
125 /*
126 ** Set the hard heap-size limit for the library. An argument of zero
127 ** disables the hard heap limit.  A negative argument is a no-op used
128 ** to obtain the return value without affecting the hard heap limit.
129 **
130 ** The return value is the value of the hard heap limit just prior to
131 ** calling this interface.
132 **
133 ** Setting the hard heap limit will also activate the soft heap limit
134 ** and constrain the soft heap limit to be no more than the hard heap
135 ** limit.
136 */
sqlite3_hard_heap_limit64(sqlite3_int64 n)137 sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 n){
138   sqlite3_int64 priorLimit;
139 #ifndef SQLITE_OMIT_AUTOINIT
140   int rc = sqlite3_initialize();
141   if( rc ) return -1;
142 #endif
143   sqlite3_mutex_enter(mem0.mutex);
144   priorLimit = mem0.hardLimit;
145   if( n>=0 ){
146     mem0.hardLimit = n;
147     if( n<mem0.alarmThreshold || mem0.alarmThreshold==0 ){
148       mem0.alarmThreshold = n;
149     }
150   }
151   sqlite3_mutex_leave(mem0.mutex);
152   return priorLimit;
153 }
154 
155 
156 /*
157 ** Initialize the memory allocation subsystem.
158 */
sqlite3MallocInit(void)159 int sqlite3MallocInit(void){
160   int rc;
161   if( sqlite3GlobalConfig.m.xMalloc==0 ){
162     sqlite3MemSetDefault();
163   }
164   mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
165   if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
166       || sqlite3GlobalConfig.nPage<=0 ){
167     sqlite3GlobalConfig.pPage = 0;
168     sqlite3GlobalConfig.szPage = 0;
169   }
170   rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
171   if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
172   return rc;
173 }
174 
175 /*
176 ** Return true if the heap is currently under memory pressure - in other
177 ** words if the amount of heap used is close to the limit set by
178 ** sqlite3_soft_heap_limit().
179 */
sqlite3HeapNearlyFull(void)180 int sqlite3HeapNearlyFull(void){
181   return AtomicLoad(&mem0.nearlyFull);
182 }
183 
184 /*
185 ** Deinitialize the memory allocation subsystem.
186 */
sqlite3MallocEnd(void)187 void sqlite3MallocEnd(void){
188   if( sqlite3GlobalConfig.m.xShutdown ){
189     sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
190   }
191   memset(&mem0, 0, sizeof(mem0));
192 }
193 
194 /*
195 ** Return the amount of memory currently checked out.
196 */
sqlite3_memory_used(void)197 sqlite3_int64 sqlite3_memory_used(void){
198   sqlite3_int64 res, mx;
199   sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
200   return res;
201 }
202 
203 /*
204 ** Return the maximum amount of memory that has ever been
205 ** checked out since either the beginning of this process
206 ** or since the most recent reset.
207 */
sqlite3_memory_highwater(int resetFlag)208 sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
209   sqlite3_int64 res, mx;
210   sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
211   return mx;
212 }
213 
214 /*
215 ** Trigger the alarm
216 */
sqlite3MallocAlarm(int nByte)217 static void sqlite3MallocAlarm(int nByte){
218   if( mem0.alarmThreshold<=0 ) return;
219   sqlite3_mutex_leave(mem0.mutex);
220   sqlite3_release_memory(nByte);
221   sqlite3_mutex_enter(mem0.mutex);
222 }
223 
224 /*
225 ** Do a memory allocation with statistics and alarms.  Assume the
226 ** lock is already held.
227 */
mallocWithAlarm(int n,void ** pp)228 static void mallocWithAlarm(int n, void **pp){
229   void *p;
230   int nFull;
231   assert( sqlite3_mutex_held(mem0.mutex) );
232   assert( n>0 );
233 
234   /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
235   ** implementation of malloc_good_size(), which must be called in debug
236   ** mode and specifically when the DMD "Dark Matter Detector" is enabled
237   ** or else a crash results.  Hence, do not attempt to optimize out the
238   ** following xRoundup() call. */
239   nFull = sqlite3GlobalConfig.m.xRoundup(n);
240 
241   sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
242   if( mem0.alarmThreshold>0 ){
243     sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
244     if( nUsed >= mem0.alarmThreshold - nFull ){
245       AtomicStore(&mem0.nearlyFull, 1);
246       sqlite3MallocAlarm(nFull);
247       if( mem0.hardLimit ){
248         nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
249         if( nUsed >= mem0.hardLimit - nFull ){
250           *pp = 0;
251           return;
252         }
253       }
254     }else{
255       AtomicStore(&mem0.nearlyFull, 0);
256     }
257   }
258   p = sqlite3GlobalConfig.m.xMalloc(nFull);
259 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
260   if( p==0 && mem0.alarmThreshold>0 ){
261     sqlite3MallocAlarm(nFull);
262     p = sqlite3GlobalConfig.m.xMalloc(nFull);
263   }
264 #endif
265   if( p ){
266     nFull = sqlite3MallocSize(p);
267     sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
268     sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
269   }
270   *pp = p;
271 }
272 
273 /*
274 ** Maximum size of any single memory allocation.
275 **
276 ** This is not a limit on the total amount of memory used.  This is
277 ** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc().
278 **
279 ** The upper bound is slightly less than 2GiB:  0x7ffffeff == 2,147,483,391
280 ** This provides a 256-byte safety margin for defense against 32-bit
281 ** signed integer overflow bugs when computing memory allocation sizes.
282 ** Parnoid applications might want to reduce the maximum allocation size
283 ** further for an even larger safety margin.  0x3fffffff or 0x0fffffff
284 ** or even smaller would be reasonable upper bounds on the size of a memory
285 ** allocations for most applications.
286 */
287 #ifndef SQLITE_MAX_ALLOCATION_SIZE
288 # define SQLITE_MAX_ALLOCATION_SIZE  2147483391
289 #endif
290 #if SQLITE_MAX_ALLOCATION_SIZE>2147483391
291 # error Maximum size for SQLITE_MAX_ALLOCATION_SIZE is 2147483391
292 #endif
293 
294 /*
295 ** Allocate memory.  This routine is like sqlite3_malloc() except that it
296 ** assumes the memory subsystem has already been initialized.
297 */
sqlite3Malloc(u64 n)298 void *sqlite3Malloc(u64 n){
299   void *p;
300   if( n==0 || n>SQLITE_MAX_ALLOCATION_SIZE ){
301     p = 0;
302   }else if( sqlite3GlobalConfig.bMemstat ){
303     sqlite3_mutex_enter(mem0.mutex);
304     mallocWithAlarm((int)n, &p);
305     sqlite3_mutex_leave(mem0.mutex);
306   }else{
307     p = sqlite3GlobalConfig.m.xMalloc((int)n);
308   }
309   assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-11148-40995 */
310   return p;
311 }
312 
313 /*
314 ** This version of the memory allocation is for use by the application.
315 ** First make sure the memory subsystem is initialized, then do the
316 ** allocation.
317 */
sqlite3_malloc(int n)318 void *sqlite3_malloc(int n){
319 #ifndef SQLITE_OMIT_AUTOINIT
320   if( sqlite3_initialize() ) return 0;
321 #endif
322   return n<=0 ? 0 : sqlite3Malloc(n);
323 }
sqlite3_malloc64(sqlite3_uint64 n)324 void *sqlite3_malloc64(sqlite3_uint64 n){
325 #ifndef SQLITE_OMIT_AUTOINIT
326   if( sqlite3_initialize() ) return 0;
327 #endif
328   return sqlite3Malloc(n);
329 }
330 
331 /*
332 ** TRUE if p is a lookaside memory allocation from db
333 */
334 #ifndef SQLITE_OMIT_LOOKASIDE
isLookaside(sqlite3 * db,const void * p)335 static int isLookaside(sqlite3 *db, const void *p){
336   return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd);
337 }
338 #else
339 #define isLookaside(A,B) 0
340 #endif
341 
342 /*
343 ** Return the size of a memory allocation previously obtained from
344 ** sqlite3Malloc() or sqlite3_malloc().
345 */
sqlite3MallocSize(const void * p)346 int sqlite3MallocSize(const void *p){
347   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
348   return sqlite3GlobalConfig.m.xSize((void*)p);
349 }
lookasideMallocSize(sqlite3 * db,const void * p)350 static int lookasideMallocSize(sqlite3 *db, const void *p){
351 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
352   return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL;
353 #else
354   return db->lookaside.szTrue;
355 #endif
356 }
sqlite3DbMallocSize(sqlite3 * db,const void * p)357 int sqlite3DbMallocSize(sqlite3 *db, const void *p){
358   assert( p!=0 );
359 #ifdef SQLITE_DEBUG
360   if( db==0 ){
361     assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
362     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
363   }else if( !isLookaside(db,p) ){
364     assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
365     assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
366   }
367 #endif
368   if( db ){
369     if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){
370 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
371       if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
372         assert( sqlite3_mutex_held(db->mutex) );
373         return LOOKASIDE_SMALL;
374       }
375 #endif
376       if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
377         assert( sqlite3_mutex_held(db->mutex) );
378         return db->lookaside.szTrue;
379       }
380     }
381   }
382   return sqlite3GlobalConfig.m.xSize((void*)p);
383 }
sqlite3_msize(void * p)384 sqlite3_uint64 sqlite3_msize(void *p){
385   assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
386   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
387   return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
388 }
389 
390 /*
391 ** Free memory previously obtained from sqlite3Malloc().
392 */
sqlite3_free(void * p)393 void sqlite3_free(void *p){
394   if( p==0 ) return;  /* IMP: R-49053-54554 */
395   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
396   assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
397   if( sqlite3GlobalConfig.bMemstat ){
398     sqlite3_mutex_enter(mem0.mutex);
399     sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
400     sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
401     sqlite3GlobalConfig.m.xFree(p);
402     sqlite3_mutex_leave(mem0.mutex);
403   }else{
404     sqlite3GlobalConfig.m.xFree(p);
405   }
406 }
407 
408 /*
409 ** Add the size of memory allocation "p" to the count in
410 ** *db->pnBytesFreed.
411 */
measureAllocationSize(sqlite3 * db,void * p)412 static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
413   *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
414 }
415 
416 /*
417 ** Free memory that might be associated with a particular database
418 ** connection.  Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
419 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
420 */
sqlite3DbFreeNN(sqlite3 * db,void * p)421 void sqlite3DbFreeNN(sqlite3 *db, void *p){
422   assert( db==0 || sqlite3_mutex_held(db->mutex) );
423   assert( p!=0 );
424   if( db ){
425     if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
426 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
427       if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
428         LookasideSlot *pBuf = (LookasideSlot*)p;
429         assert( db->pnBytesFreed==0 );
430 #ifdef SQLITE_DEBUG
431         memset(p, 0xaa, LOOKASIDE_SMALL);  /* Trash freed content */
432 #endif
433         pBuf->pNext = db->lookaside.pSmallFree;
434         db->lookaside.pSmallFree = pBuf;
435         return;
436       }
437 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
438       if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
439         LookasideSlot *pBuf = (LookasideSlot*)p;
440         assert( db->pnBytesFreed==0 );
441 #ifdef SQLITE_DEBUG
442         memset(p, 0xaa, db->lookaside.szTrue);  /* Trash freed content */
443 #endif
444         pBuf->pNext = db->lookaside.pFree;
445         db->lookaside.pFree = pBuf;
446         return;
447       }
448     }
449     if( db->pnBytesFreed ){
450       measureAllocationSize(db, p);
451       return;
452     }
453   }
454   assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
455   assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
456   assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
457   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
458   sqlite3_free(p);
459 }
sqlite3DbNNFreeNN(sqlite3 * db,void * p)460 void sqlite3DbNNFreeNN(sqlite3 *db, void *p){
461   assert( db!=0 );
462   assert( sqlite3_mutex_held(db->mutex) );
463   assert( p!=0 );
464   if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
465 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
466     if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
467       LookasideSlot *pBuf = (LookasideSlot*)p;
468       assert( db->pnBytesFreed==0 );
469 #ifdef SQLITE_DEBUG
470       memset(p, 0xaa, LOOKASIDE_SMALL);  /* Trash freed content */
471 #endif
472       pBuf->pNext = db->lookaside.pSmallFree;
473       db->lookaside.pSmallFree = pBuf;
474       return;
475     }
476 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
477     if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
478       LookasideSlot *pBuf = (LookasideSlot*)p;
479       assert( db->pnBytesFreed==0 );
480 #ifdef SQLITE_DEBUG
481       memset(p, 0xaa, db->lookaside.szTrue);  /* Trash freed content */
482 #endif
483       pBuf->pNext = db->lookaside.pFree;
484       db->lookaside.pFree = pBuf;
485       return;
486     }
487   }
488   if( db->pnBytesFreed ){
489     measureAllocationSize(db, p);
490     return;
491   }
492   assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
493   assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
494   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
495   sqlite3_free(p);
496 }
sqlite3DbFree(sqlite3 * db,void * p)497 void sqlite3DbFree(sqlite3 *db, void *p){
498   assert( db==0 || sqlite3_mutex_held(db->mutex) );
499   if( p ) sqlite3DbFreeNN(db, p);
500 }
501 
502 /*
503 ** Change the size of an existing memory allocation
504 */
sqlite3Realloc(void * pOld,u64 nBytes)505 void *sqlite3Realloc(void *pOld, u64 nBytes){
506   int nOld, nNew, nDiff;
507   void *pNew;
508   assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
509   assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
510   if( pOld==0 ){
511     return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
512   }
513   if( nBytes==0 ){
514     sqlite3_free(pOld); /* IMP: R-26507-47431 */
515     return 0;
516   }
517   if( nBytes>=0x7fffff00 ){
518     /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
519     return 0;
520   }
521   nOld = sqlite3MallocSize(pOld);
522   /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
523   ** argument to xRealloc is always a value returned by a prior call to
524   ** xRoundup. */
525   nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
526   if( nOld==nNew ){
527     pNew = pOld;
528   }else if( sqlite3GlobalConfig.bMemstat ){
529     sqlite3_int64 nUsed;
530     sqlite3_mutex_enter(mem0.mutex);
531     sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
532     nDiff = nNew - nOld;
533     if( nDiff>0 && (nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)) >=
534           mem0.alarmThreshold-nDiff ){
535       sqlite3MallocAlarm(nDiff);
536       if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){
537         sqlite3_mutex_leave(mem0.mutex);
538         return 0;
539       }
540     }
541     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
542 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
543     if( pNew==0 && mem0.alarmThreshold>0 ){
544       sqlite3MallocAlarm((int)nBytes);
545       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
546     }
547 #endif
548     if( pNew ){
549       nNew = sqlite3MallocSize(pNew);
550       sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
551     }
552     sqlite3_mutex_leave(mem0.mutex);
553   }else{
554     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
555   }
556   assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
557   return pNew;
558 }
559 
560 /*
561 ** The public interface to sqlite3Realloc.  Make sure that the memory
562 ** subsystem is initialized prior to invoking sqliteRealloc.
563 */
sqlite3_realloc(void * pOld,int n)564 void *sqlite3_realloc(void *pOld, int n){
565 #ifndef SQLITE_OMIT_AUTOINIT
566   if( sqlite3_initialize() ) return 0;
567 #endif
568   if( n<0 ) n = 0;  /* IMP: R-26507-47431 */
569   return sqlite3Realloc(pOld, n);
570 }
sqlite3_realloc64(void * pOld,sqlite3_uint64 n)571 void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
572 #ifndef SQLITE_OMIT_AUTOINIT
573   if( sqlite3_initialize() ) return 0;
574 #endif
575   return sqlite3Realloc(pOld, n);
576 }
577 
578 
579 /*
580 ** Allocate and zero memory.
581 */
sqlite3MallocZero(u64 n)582 void *sqlite3MallocZero(u64 n){
583   void *p = sqlite3Malloc(n);
584   if( p ){
585     memset(p, 0, (size_t)n);
586   }
587   return p;
588 }
589 
590 /*
591 ** Allocate and zero memory.  If the allocation fails, make
592 ** the mallocFailed flag in the connection pointer.
593 */
sqlite3DbMallocZero(sqlite3 * db,u64 n)594 void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
595   void *p;
596   testcase( db==0 );
597   p = sqlite3DbMallocRaw(db, n);
598   if( p ) memset(p, 0, (size_t)n);
599   return p;
600 }
601 
602 
603 /* Finish the work of sqlite3DbMallocRawNN for the unusual and
604 ** slower case when the allocation cannot be fulfilled using lookaside.
605 */
dbMallocRawFinish(sqlite3 * db,u64 n)606 static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
607   void *p;
608   assert( db!=0 );
609   p = sqlite3Malloc(n);
610   if( !p ) sqlite3OomFault(db);
611   sqlite3MemdebugSetType(p,
612          (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
613   return p;
614 }
615 
616 /*
617 ** Allocate memory, either lookaside (if possible) or heap.
618 ** If the allocation fails, set the mallocFailed flag in
619 ** the connection pointer.
620 **
621 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
622 ** failure on the same database connection) then always return 0.
623 ** Hence for a particular database connection, once malloc starts
624 ** failing, it fails consistently until mallocFailed is reset.
625 ** This is an important assumption.  There are many places in the
626 ** code that do things like this:
627 **
628 **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
629 **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
630 **         if( b ) a[10] = 9;
631 **
632 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
633 ** that all prior mallocs (ex: "a") worked too.
634 **
635 ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
636 ** not a NULL pointer.
637 */
sqlite3DbMallocRaw(sqlite3 * db,u64 n)638 void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
639   void *p;
640   if( db ) return sqlite3DbMallocRawNN(db, n);
641   p = sqlite3Malloc(n);
642   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
643   return p;
644 }
sqlite3DbMallocRawNN(sqlite3 * db,u64 n)645 void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
646 #ifndef SQLITE_OMIT_LOOKASIDE
647   LookasideSlot *pBuf;
648   assert( db!=0 );
649   assert( sqlite3_mutex_held(db->mutex) );
650   assert( db->pnBytesFreed==0 );
651   if( n>db->lookaside.sz ){
652     if( !db->lookaside.bDisable ){
653       db->lookaside.anStat[1]++;
654     }else if( db->mallocFailed ){
655       return 0;
656     }
657     return dbMallocRawFinish(db, n);
658   }
659 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
660   if( n<=LOOKASIDE_SMALL ){
661     if( (pBuf = db->lookaside.pSmallFree)!=0 ){
662       db->lookaside.pSmallFree = pBuf->pNext;
663       db->lookaside.anStat[0]++;
664       return (void*)pBuf;
665     }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
666       db->lookaside.pSmallInit = pBuf->pNext;
667       db->lookaside.anStat[0]++;
668       return (void*)pBuf;
669     }
670   }
671 #endif
672   if( (pBuf = db->lookaside.pFree)!=0 ){
673     db->lookaside.pFree = pBuf->pNext;
674     db->lookaside.anStat[0]++;
675     return (void*)pBuf;
676   }else if( (pBuf = db->lookaside.pInit)!=0 ){
677     db->lookaside.pInit = pBuf->pNext;
678     db->lookaside.anStat[0]++;
679     return (void*)pBuf;
680   }else{
681     db->lookaside.anStat[2]++;
682   }
683 #else
684   assert( db!=0 );
685   assert( sqlite3_mutex_held(db->mutex) );
686   assert( db->pnBytesFreed==0 );
687   if( db->mallocFailed ){
688     return 0;
689   }
690 #endif
691   return dbMallocRawFinish(db, n);
692 }
693 
694 /* Forward declaration */
695 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
696 
697 /*
698 ** Resize the block of memory pointed to by p to n bytes. If the
699 ** resize fails, set the mallocFailed flag in the connection object.
700 */
sqlite3DbRealloc(sqlite3 * db,void * p,u64 n)701 void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
702   assert( db!=0 );
703   if( p==0 ) return sqlite3DbMallocRawNN(db, n);
704   assert( sqlite3_mutex_held(db->mutex) );
705   if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
706 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
707     if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
708       if( n<=LOOKASIDE_SMALL ) return p;
709     }else
710 #endif
711     if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
712       if( n<=db->lookaside.szTrue ) return p;
713     }
714   }
715   return dbReallocFinish(db, p, n);
716 }
dbReallocFinish(sqlite3 * db,void * p,u64 n)717 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
718   void *pNew = 0;
719   assert( db!=0 );
720   assert( p!=0 );
721   if( db->mallocFailed==0 ){
722     if( isLookaside(db, p) ){
723       pNew = sqlite3DbMallocRawNN(db, n);
724       if( pNew ){
725         memcpy(pNew, p, lookasideMallocSize(db, p));
726         sqlite3DbFree(db, p);
727       }
728     }else{
729       assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
730       assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
731       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
732       pNew = sqlite3Realloc(p, n);
733       if( !pNew ){
734         sqlite3OomFault(db);
735       }
736       sqlite3MemdebugSetType(pNew,
737             (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
738     }
739   }
740   return pNew;
741 }
742 
743 /*
744 ** Attempt to reallocate p.  If the reallocation fails, then free p
745 ** and set the mallocFailed flag in the database connection.
746 */
sqlite3DbReallocOrFree(sqlite3 * db,void * p,u64 n)747 void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
748   void *pNew;
749   pNew = sqlite3DbRealloc(db, p, n);
750   if( !pNew ){
751     sqlite3DbFree(db, p);
752   }
753   return pNew;
754 }
755 
756 /*
757 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
758 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
759 ** is because when memory debugging is turned on, these two functions are
760 ** called via macros that record the current file and line number in the
761 ** ThreadData structure.
762 */
sqlite3DbStrDup(sqlite3 * db,const char * z)763 char *sqlite3DbStrDup(sqlite3 *db, const char *z){
764   char *zNew;
765   size_t n;
766   if( z==0 ){
767     return 0;
768   }
769   n = strlen(z) + 1;
770   zNew = sqlite3DbMallocRaw(db, n);
771   if( zNew ){
772     memcpy(zNew, z, n);
773   }
774   return zNew;
775 }
sqlite3DbStrNDup(sqlite3 * db,const char * z,u64 n)776 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
777   char *zNew;
778   assert( db!=0 );
779   assert( z!=0 || n==0 );
780   assert( (n&0x7fffffff)==n );
781   zNew = z ? sqlite3DbMallocRawNN(db, n+1) : 0;
782   if( zNew ){
783     memcpy(zNew, z, (size_t)n);
784     zNew[n] = 0;
785   }
786   return zNew;
787 }
788 
789 /*
790 ** The text between zStart and zEnd represents a phrase within a larger
791 ** SQL statement.  Make a copy of this phrase in space obtained form
792 ** sqlite3DbMalloc().  Omit leading and trailing whitespace.
793 */
sqlite3DbSpanDup(sqlite3 * db,const char * zStart,const char * zEnd)794 char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
795   int n;
796   while( sqlite3Isspace(zStart[0]) ) zStart++;
797   n = (int)(zEnd - zStart);
798   while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
799   return sqlite3DbStrNDup(db, zStart, n);
800 }
801 
802 /*
803 ** Free any prior content in *pz and replace it with a copy of zNew.
804 */
sqlite3SetString(char ** pz,sqlite3 * db,const char * zNew)805 void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
806   char *z = sqlite3DbStrDup(db, zNew);
807   sqlite3DbFree(db, *pz);
808   *pz = z;
809 }
810 
811 /*
812 ** Call this routine to record the fact that an OOM (out-of-memory) error
813 ** has happened.  This routine will set db->mallocFailed, and also
814 ** temporarily disable the lookaside memory allocator and interrupt
815 ** any running VDBEs.
816 **
817 ** Always return a NULL pointer so that this routine can be invoked using
818 **
819 **      return sqlite3OomFault(db);
820 **
821 ** and thereby avoid unnecessary stack frame allocations for the overwhelmingly
822 ** common case where no OOM occurs.
823 */
sqlite3OomFault(sqlite3 * db)824 void *sqlite3OomFault(sqlite3 *db){
825   if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
826     db->mallocFailed = 1;
827     if( db->nVdbeExec>0 ){
828       AtomicStore(&db->u1.isInterrupted, 1);
829     }
830     DisableLookaside;
831     if( db->pParse ){
832       Parse *pParse;
833       sqlite3ErrorMsg(db->pParse, "out of memory");
834       db->pParse->rc = SQLITE_NOMEM_BKPT;
835       for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){
836         pParse->nErr++;
837         pParse->rc = SQLITE_NOMEM;
838       }
839     }
840   }
841   return 0;
842 }
843 
844 /*
845 ** This routine reactivates the memory allocator and clears the
846 ** db->mallocFailed flag as necessary.
847 **
848 ** The memory allocator is not restarted if there are running
849 ** VDBEs.
850 */
sqlite3OomClear(sqlite3 * db)851 void sqlite3OomClear(sqlite3 *db){
852   if( db->mallocFailed && db->nVdbeExec==0 ){
853     db->mallocFailed = 0;
854     AtomicStore(&db->u1.isInterrupted, 0);
855     assert( db->lookaside.bDisable>0 );
856     EnableLookaside;
857   }
858 }
859 
860 /*
861 ** Take actions at the end of an API call to deal with error codes.
862 */
apiHandleError(sqlite3 * db,int rc)863 static SQLITE_NOINLINE int apiHandleError(sqlite3 *db, int rc){
864   if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
865     sqlite3OomClear(db);
866     sqlite3Error(db, SQLITE_NOMEM);
867     return SQLITE_NOMEM_BKPT;
868   }
869   return rc & db->errMask;
870 }
871 
872 /*
873 ** This function must be called before exiting any API function (i.e.
874 ** returning control to the user) that has called sqlite3_malloc or
875 ** sqlite3_realloc.
876 **
877 ** The returned value is normally a copy of the second argument to this
878 ** function. However, if a malloc() failure has occurred since the previous
879 ** invocation SQLITE_NOMEM is returned instead.
880 **
881 ** If an OOM as occurred, then the connection error-code (the value
882 ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
883 */
sqlite3ApiExit(sqlite3 * db,int rc)884 int sqlite3ApiExit(sqlite3* db, int rc){
885   /* If the db handle must hold the connection handle mutex here.
886   ** Otherwise the read (and possible write) of db->mallocFailed
887   ** is unsafe, as is the call to sqlite3Error().
888   */
889   assert( db!=0 );
890   assert( sqlite3_mutex_held(db->mutex) );
891   if( db->mallocFailed || rc ){
892     return apiHandleError(db, rc);
893   }
894   return rc & db->errMask;
895 }
896