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