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 memset(&mem0, 0, sizeof(mem0));
165 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
166 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
167 || sqlite3GlobalConfig.nPage<=0 ){
168 sqlite3GlobalConfig.pPage = 0;
169 sqlite3GlobalConfig.szPage = 0;
170 }
171 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
172 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
173 return rc;
174 }
175
176 /*
177 ** Return true if the heap is currently under memory pressure - in other
178 ** words if the amount of heap used is close to the limit set by
179 ** sqlite3_soft_heap_limit().
180 */
sqlite3HeapNearlyFull(void)181 int sqlite3HeapNearlyFull(void){
182 return AtomicLoad(&mem0.nearlyFull);
183 }
184
185 /*
186 ** Deinitialize the memory allocation subsystem.
187 */
sqlite3MallocEnd(void)188 void sqlite3MallocEnd(void){
189 if( sqlite3GlobalConfig.m.xShutdown ){
190 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
191 }
192 memset(&mem0, 0, sizeof(mem0));
193 }
194
195 /*
196 ** Return the amount of memory currently checked out.
197 */
sqlite3_memory_used(void)198 sqlite3_int64 sqlite3_memory_used(void){
199 sqlite3_int64 res, mx;
200 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
201 return res;
202 }
203
204 /*
205 ** Return the maximum amount of memory that has ever been
206 ** checked out since either the beginning of this process
207 ** or since the most recent reset.
208 */
sqlite3_memory_highwater(int resetFlag)209 sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
210 sqlite3_int64 res, mx;
211 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
212 return mx;
213 }
214
215 /*
216 ** Trigger the alarm
217 */
sqlite3MallocAlarm(int nByte)218 static void sqlite3MallocAlarm(int nByte){
219 if( mem0.alarmThreshold<=0 ) return;
220 sqlite3_mutex_leave(mem0.mutex);
221 sqlite3_release_memory(nByte);
222 sqlite3_mutex_enter(mem0.mutex);
223 }
224
225 /*
226 ** Do a memory allocation with statistics and alarms. Assume the
227 ** lock is already held.
228 */
mallocWithAlarm(int n,void ** pp)229 static void mallocWithAlarm(int n, void **pp){
230 void *p;
231 int nFull;
232 assert( sqlite3_mutex_held(mem0.mutex) );
233 assert( n>0 );
234
235 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
236 ** implementation of malloc_good_size(), which must be called in debug
237 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
238 ** or else a crash results. Hence, do not attempt to optimize out the
239 ** following xRoundup() call. */
240 nFull = sqlite3GlobalConfig.m.xRoundup(n);
241
242 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
243 if( mem0.alarmThreshold>0 ){
244 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
245 if( nUsed >= mem0.alarmThreshold - nFull ){
246 AtomicStore(&mem0.nearlyFull, 1);
247 sqlite3MallocAlarm(nFull);
248 if( mem0.hardLimit ){
249 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
250 if( nUsed >= mem0.hardLimit - nFull ){
251 *pp = 0;
252 return;
253 }
254 }
255 }else{
256 AtomicStore(&mem0.nearlyFull, 0);
257 }
258 }
259 p = sqlite3GlobalConfig.m.xMalloc(nFull);
260 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
261 if( p==0 && mem0.alarmThreshold>0 ){
262 sqlite3MallocAlarm(nFull);
263 p = sqlite3GlobalConfig.m.xMalloc(nFull);
264 }
265 #endif
266 if( p ){
267 nFull = sqlite3MallocSize(p);
268 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
269 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
270 }
271 *pp = p;
272 }
273
274 /*
275 ** Allocate memory. This routine is like sqlite3_malloc() except that it
276 ** assumes the memory subsystem has already been initialized.
277 */
sqlite3Malloc(u64 n)278 void *sqlite3Malloc(u64 n){
279 void *p;
280 if( n==0 || n>=0x7fffff00 ){
281 /* A memory allocation of a number of bytes which is near the maximum
282 ** signed integer value might cause an integer overflow inside of the
283 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
284 ** 255 bytes of overhead. SQLite itself will never use anything near
285 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
286 p = 0;
287 }else if( sqlite3GlobalConfig.bMemstat ){
288 sqlite3_mutex_enter(mem0.mutex);
289 mallocWithAlarm((int)n, &p);
290 sqlite3_mutex_leave(mem0.mutex);
291 }else{
292 p = sqlite3GlobalConfig.m.xMalloc((int)n);
293 }
294 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
295 return p;
296 }
297
298 /*
299 ** This version of the memory allocation is for use by the application.
300 ** First make sure the memory subsystem is initialized, then do the
301 ** allocation.
302 */
sqlite3_malloc(int n)303 void *sqlite3_malloc(int n){
304 #ifndef SQLITE_OMIT_AUTOINIT
305 if( sqlite3_initialize() ) return 0;
306 #endif
307 return n<=0 ? 0 : sqlite3Malloc(n);
308 }
sqlite3_malloc64(sqlite3_uint64 n)309 void *sqlite3_malloc64(sqlite3_uint64 n){
310 #ifndef SQLITE_OMIT_AUTOINIT
311 if( sqlite3_initialize() ) return 0;
312 #endif
313 return sqlite3Malloc(n);
314 }
315
316 /*
317 ** TRUE if p is a lookaside memory allocation from db
318 */
319 #ifndef SQLITE_OMIT_LOOKASIDE
isLookaside(sqlite3 * db,void * p)320 static int isLookaside(sqlite3 *db, void *p){
321 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
322 }
323 #else
324 #define isLookaside(A,B) 0
325 #endif
326
327 /*
328 ** Return the size of a memory allocation previously obtained from
329 ** sqlite3Malloc() or sqlite3_malloc().
330 */
sqlite3MallocSize(void * p)331 int sqlite3MallocSize(void *p){
332 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
333 return sqlite3GlobalConfig.m.xSize(p);
334 }
lookasideMallocSize(sqlite3 * db,void * p)335 static int lookasideMallocSize(sqlite3 *db, void *p){
336 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
337 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL;
338 #else
339 return db->lookaside.szTrue;
340 #endif
341 }
sqlite3DbMallocSize(sqlite3 * db,void * p)342 int sqlite3DbMallocSize(sqlite3 *db, void *p){
343 assert( p!=0 );
344 #ifdef SQLITE_DEBUG
345 if( db==0 || !isLookaside(db,p) ){
346 if( db==0 ){
347 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
348 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
349 }else{
350 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
351 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
352 }
353 }
354 #endif
355 if( db ){
356 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
357 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
358 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
359 assert( sqlite3_mutex_held(db->mutex) );
360 return LOOKASIDE_SMALL;
361 }
362 #endif
363 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
364 assert( sqlite3_mutex_held(db->mutex) );
365 return db->lookaside.szTrue;
366 }
367 }
368 }
369 return sqlite3GlobalConfig.m.xSize(p);
370 }
sqlite3_msize(void * p)371 sqlite3_uint64 sqlite3_msize(void *p){
372 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
373 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
374 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
375 }
376
377 /*
378 ** Free memory previously obtained from sqlite3Malloc().
379 */
sqlite3_free(void * p)380 void sqlite3_free(void *p){
381 if( p==0 ) return; /* IMP: R-49053-54554 */
382 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
383 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
384 if( sqlite3GlobalConfig.bMemstat ){
385 sqlite3_mutex_enter(mem0.mutex);
386 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
387 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
388 sqlite3GlobalConfig.m.xFree(p);
389 sqlite3_mutex_leave(mem0.mutex);
390 }else{
391 sqlite3GlobalConfig.m.xFree(p);
392 }
393 }
394
395 /*
396 ** Add the size of memory allocation "p" to the count in
397 ** *db->pnBytesFreed.
398 */
measureAllocationSize(sqlite3 * db,void * p)399 static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
400 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
401 }
402
403 /*
404 ** Free memory that might be associated with a particular database
405 ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
406 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
407 */
sqlite3DbFreeNN(sqlite3 * db,void * p)408 void sqlite3DbFreeNN(sqlite3 *db, void *p){
409 assert( db==0 || sqlite3_mutex_held(db->mutex) );
410 assert( p!=0 );
411 if( db ){
412 if( db->pnBytesFreed ){
413 measureAllocationSize(db, p);
414 return;
415 }
416 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
417 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
418 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
419 LookasideSlot *pBuf = (LookasideSlot*)p;
420 #ifdef SQLITE_DEBUG
421 memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
422 #endif
423 pBuf->pNext = db->lookaside.pSmallFree;
424 db->lookaside.pSmallFree = pBuf;
425 return;
426 }
427 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
428 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
429 LookasideSlot *pBuf = (LookasideSlot*)p;
430 #ifdef SQLITE_DEBUG
431 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
432 #endif
433 pBuf->pNext = db->lookaside.pFree;
434 db->lookaside.pFree = pBuf;
435 return;
436 }
437 }
438 }
439 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
440 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
441 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
442 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
443 sqlite3_free(p);
444 }
sqlite3DbFree(sqlite3 * db,void * p)445 void sqlite3DbFree(sqlite3 *db, void *p){
446 assert( db==0 || sqlite3_mutex_held(db->mutex) );
447 if( p ) sqlite3DbFreeNN(db, p);
448 }
449
450 /*
451 ** Change the size of an existing memory allocation
452 */
sqlite3Realloc(void * pOld,u64 nBytes)453 void *sqlite3Realloc(void *pOld, u64 nBytes){
454 int nOld, nNew, nDiff;
455 void *pNew;
456 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
457 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
458 if( pOld==0 ){
459 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
460 }
461 if( nBytes==0 ){
462 sqlite3_free(pOld); /* IMP: R-26507-47431 */
463 return 0;
464 }
465 if( nBytes>=0x7fffff00 ){
466 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
467 return 0;
468 }
469 nOld = sqlite3MallocSize(pOld);
470 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
471 ** argument to xRealloc is always a value returned by a prior call to
472 ** xRoundup. */
473 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
474 if( nOld==nNew ){
475 pNew = pOld;
476 }else if( sqlite3GlobalConfig.bMemstat ){
477 sqlite3_int64 nUsed;
478 sqlite3_mutex_enter(mem0.mutex);
479 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
480 nDiff = nNew - nOld;
481 if( nDiff>0 && (nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)) >=
482 mem0.alarmThreshold-nDiff ){
483 sqlite3MallocAlarm(nDiff);
484 if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){
485 sqlite3_mutex_leave(mem0.mutex);
486 return 0;
487 }
488 }
489 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
490 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
491 if( pNew==0 && mem0.alarmThreshold>0 ){
492 sqlite3MallocAlarm((int)nBytes);
493 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
494 }
495 #endif
496 if( pNew ){
497 nNew = sqlite3MallocSize(pNew);
498 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
499 }
500 sqlite3_mutex_leave(mem0.mutex);
501 }else{
502 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
503 }
504 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
505 return pNew;
506 }
507
508 /*
509 ** The public interface to sqlite3Realloc. Make sure that the memory
510 ** subsystem is initialized prior to invoking sqliteRealloc.
511 */
sqlite3_realloc(void * pOld,int n)512 void *sqlite3_realloc(void *pOld, int n){
513 #ifndef SQLITE_OMIT_AUTOINIT
514 if( sqlite3_initialize() ) return 0;
515 #endif
516 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
517 return sqlite3Realloc(pOld, n);
518 }
sqlite3_realloc64(void * pOld,sqlite3_uint64 n)519 void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
520 #ifndef SQLITE_OMIT_AUTOINIT
521 if( sqlite3_initialize() ) return 0;
522 #endif
523 return sqlite3Realloc(pOld, n);
524 }
525
526
527 /*
528 ** Allocate and zero memory.
529 */
sqlite3MallocZero(u64 n)530 void *sqlite3MallocZero(u64 n){
531 void *p = sqlite3Malloc(n);
532 if( p ){
533 memset(p, 0, (size_t)n);
534 }
535 return p;
536 }
537
538 /*
539 ** Allocate and zero memory. If the allocation fails, make
540 ** the mallocFailed flag in the connection pointer.
541 */
sqlite3DbMallocZero(sqlite3 * db,u64 n)542 void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
543 void *p;
544 testcase( db==0 );
545 p = sqlite3DbMallocRaw(db, n);
546 if( p ) memset(p, 0, (size_t)n);
547 return p;
548 }
549
550
551 /* Finish the work of sqlite3DbMallocRawNN for the unusual and
552 ** slower case when the allocation cannot be fulfilled using lookaside.
553 */
dbMallocRawFinish(sqlite3 * db,u64 n)554 static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
555 void *p;
556 assert( db!=0 );
557 p = sqlite3Malloc(n);
558 if( !p ) sqlite3OomFault(db);
559 sqlite3MemdebugSetType(p,
560 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
561 return p;
562 }
563
564 /*
565 ** Allocate memory, either lookaside (if possible) or heap.
566 ** If the allocation fails, set the mallocFailed flag in
567 ** the connection pointer.
568 **
569 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
570 ** failure on the same database connection) then always return 0.
571 ** Hence for a particular database connection, once malloc starts
572 ** failing, it fails consistently until mallocFailed is reset.
573 ** This is an important assumption. There are many places in the
574 ** code that do things like this:
575 **
576 ** int *a = (int*)sqlite3DbMallocRaw(db, 100);
577 ** int *b = (int*)sqlite3DbMallocRaw(db, 200);
578 ** if( b ) a[10] = 9;
579 **
580 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
581 ** that all prior mallocs (ex: "a") worked too.
582 **
583 ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
584 ** not a NULL pointer.
585 */
sqlite3DbMallocRaw(sqlite3 * db,u64 n)586 void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
587 void *p;
588 if( db ) return sqlite3DbMallocRawNN(db, n);
589 p = sqlite3Malloc(n);
590 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
591 return p;
592 }
sqlite3DbMallocRawNN(sqlite3 * db,u64 n)593 void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
594 #ifndef SQLITE_OMIT_LOOKASIDE
595 LookasideSlot *pBuf;
596 assert( db!=0 );
597 assert( sqlite3_mutex_held(db->mutex) );
598 assert( db->pnBytesFreed==0 );
599 if( n>db->lookaside.sz ){
600 if( !db->lookaside.bDisable ){
601 db->lookaside.anStat[1]++;
602 }else if( db->mallocFailed ){
603 return 0;
604 }
605 return dbMallocRawFinish(db, n);
606 }
607 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
608 if( n<=LOOKASIDE_SMALL ){
609 if( (pBuf = db->lookaside.pSmallFree)!=0 ){
610 db->lookaside.pSmallFree = pBuf->pNext;
611 db->lookaside.anStat[0]++;
612 return (void*)pBuf;
613 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
614 db->lookaside.pSmallInit = pBuf->pNext;
615 db->lookaside.anStat[0]++;
616 return (void*)pBuf;
617 }
618 }
619 #endif
620 if( (pBuf = db->lookaside.pFree)!=0 ){
621 db->lookaside.pFree = pBuf->pNext;
622 db->lookaside.anStat[0]++;
623 return (void*)pBuf;
624 }else if( (pBuf = db->lookaside.pInit)!=0 ){
625 db->lookaside.pInit = pBuf->pNext;
626 db->lookaside.anStat[0]++;
627 return (void*)pBuf;
628 }else{
629 db->lookaside.anStat[2]++;
630 }
631 #else
632 assert( db!=0 );
633 assert( sqlite3_mutex_held(db->mutex) );
634 assert( db->pnBytesFreed==0 );
635 if( db->mallocFailed ){
636 return 0;
637 }
638 #endif
639 return dbMallocRawFinish(db, n);
640 }
641
642 /* Forward declaration */
643 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
644
645 /*
646 ** Resize the block of memory pointed to by p to n bytes. If the
647 ** resize fails, set the mallocFailed flag in the connection object.
648 */
sqlite3DbRealloc(sqlite3 * db,void * p,u64 n)649 void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
650 assert( db!=0 );
651 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
652 assert( sqlite3_mutex_held(db->mutex) );
653 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
654 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
655 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
656 if( n<=LOOKASIDE_SMALL ) return p;
657 }else
658 #endif
659 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
660 if( n<=db->lookaside.szTrue ) return p;
661 }
662 }
663 return dbReallocFinish(db, p, n);
664 }
dbReallocFinish(sqlite3 * db,void * p,u64 n)665 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
666 void *pNew = 0;
667 assert( db!=0 );
668 assert( p!=0 );
669 if( db->mallocFailed==0 ){
670 if( isLookaside(db, p) ){
671 pNew = sqlite3DbMallocRawNN(db, n);
672 if( pNew ){
673 memcpy(pNew, p, lookasideMallocSize(db, p));
674 sqlite3DbFree(db, p);
675 }
676 }else{
677 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
678 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
679 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
680 pNew = sqlite3Realloc(p, n);
681 if( !pNew ){
682 sqlite3OomFault(db);
683 }
684 sqlite3MemdebugSetType(pNew,
685 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
686 }
687 }
688 return pNew;
689 }
690
691 /*
692 ** Attempt to reallocate p. If the reallocation fails, then free p
693 ** and set the mallocFailed flag in the database connection.
694 */
sqlite3DbReallocOrFree(sqlite3 * db,void * p,u64 n)695 void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
696 void *pNew;
697 pNew = sqlite3DbRealloc(db, p, n);
698 if( !pNew ){
699 sqlite3DbFree(db, p);
700 }
701 return pNew;
702 }
703
704 /*
705 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
706 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
707 ** is because when memory debugging is turned on, these two functions are
708 ** called via macros that record the current file and line number in the
709 ** ThreadData structure.
710 */
sqlite3DbStrDup(sqlite3 * db,const char * z)711 char *sqlite3DbStrDup(sqlite3 *db, const char *z){
712 char *zNew;
713 size_t n;
714 if( z==0 ){
715 return 0;
716 }
717 n = strlen(z) + 1;
718 zNew = sqlite3DbMallocRaw(db, n);
719 if( zNew ){
720 memcpy(zNew, z, n);
721 }
722 return zNew;
723 }
sqlite3DbStrNDup(sqlite3 * db,const char * z,u64 n)724 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
725 char *zNew;
726 assert( db!=0 );
727 assert( z!=0 || n==0 );
728 assert( (n&0x7fffffff)==n );
729 zNew = z ? sqlite3DbMallocRawNN(db, n+1) : 0;
730 if( zNew ){
731 memcpy(zNew, z, (size_t)n);
732 zNew[n] = 0;
733 }
734 return zNew;
735 }
736
737 /*
738 ** The text between zStart and zEnd represents a phrase within a larger
739 ** SQL statement. Make a copy of this phrase in space obtained form
740 ** sqlite3DbMalloc(). Omit leading and trailing whitespace.
741 */
sqlite3DbSpanDup(sqlite3 * db,const char * zStart,const char * zEnd)742 char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
743 int n;
744 while( sqlite3Isspace(zStart[0]) ) zStart++;
745 n = (int)(zEnd - zStart);
746 while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
747 return sqlite3DbStrNDup(db, zStart, n);
748 }
749
750 /*
751 ** Free any prior content in *pz and replace it with a copy of zNew.
752 */
sqlite3SetString(char ** pz,sqlite3 * db,const char * zNew)753 void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
754 sqlite3DbFree(db, *pz);
755 *pz = sqlite3DbStrDup(db, zNew);
756 }
757
758 /*
759 ** Call this routine to record the fact that an OOM (out-of-memory) error
760 ** has happened. This routine will set db->mallocFailed, and also
761 ** temporarily disable the lookaside memory allocator and interrupt
762 ** any running VDBEs.
763 */
sqlite3OomFault(sqlite3 * db)764 void sqlite3OomFault(sqlite3 *db){
765 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
766 db->mallocFailed = 1;
767 if( db->nVdbeExec>0 ){
768 AtomicStore(&db->u1.isInterrupted, 1);
769 }
770 DisableLookaside;
771 if( db->pParse ){
772 db->pParse->rc = SQLITE_NOMEM_BKPT;
773 }
774 }
775 }
776
777 /*
778 ** This routine reactivates the memory allocator and clears the
779 ** db->mallocFailed flag as necessary.
780 **
781 ** The memory allocator is not restarted if there are running
782 ** VDBEs.
783 */
sqlite3OomClear(sqlite3 * db)784 void sqlite3OomClear(sqlite3 *db){
785 if( db->mallocFailed && db->nVdbeExec==0 ){
786 db->mallocFailed = 0;
787 AtomicStore(&db->u1.isInterrupted, 0);
788 assert( db->lookaside.bDisable>0 );
789 EnableLookaside;
790 }
791 }
792
793 /*
794 ** Take actions at the end of an API call to deal with error codes.
795 */
apiHandleError(sqlite3 * db,int rc)796 static SQLITE_NOINLINE int apiHandleError(sqlite3 *db, int rc){
797 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
798 sqlite3OomClear(db);
799 sqlite3Error(db, SQLITE_NOMEM);
800 return SQLITE_NOMEM_BKPT;
801 }
802 return rc & db->errMask;
803 }
804
805 /*
806 ** This function must be called before exiting any API function (i.e.
807 ** returning control to the user) that has called sqlite3_malloc or
808 ** sqlite3_realloc.
809 **
810 ** The returned value is normally a copy of the second argument to this
811 ** function. However, if a malloc() failure has occurred since the previous
812 ** invocation SQLITE_NOMEM is returned instead.
813 **
814 ** If an OOM as occurred, then the connection error-code (the value
815 ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
816 */
sqlite3ApiExit(sqlite3 * db,int rc)817 int sqlite3ApiExit(sqlite3* db, int rc){
818 /* If the db handle must hold the connection handle mutex here.
819 ** Otherwise the read (and possible write) of db->mallocFailed
820 ** is unsafe, as is the call to sqlite3Error().
821 */
822 assert( db!=0 );
823 assert( sqlite3_mutex_held(db->mutex) );
824 if( db->mallocFailed || rc ){
825 return apiHandleError(db, rc);
826 }
827 return rc & db->errMask;
828 }
829