OLD | NEW |
1 /* | 1 /* |
2 ** 2001 September 15 | 2 ** 2001 September 15 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 #endif | 123 #endif |
124 | 124 |
125 /* | 125 /* |
126 ** Set the soft heap-size limit for the library. Passing a zero or | 126 ** Set the soft heap-size limit for the library. Passing a zero or |
127 ** negative value indicates no limit. | 127 ** negative value indicates no limit. |
128 */ | 128 */ |
129 sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){ | 129 sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){ |
130 sqlite3_int64 priorLimit; | 130 sqlite3_int64 priorLimit; |
131 sqlite3_int64 excess; | 131 sqlite3_int64 excess; |
132 #ifndef SQLITE_OMIT_AUTOINIT | 132 #ifndef SQLITE_OMIT_AUTOINIT |
133 sqlite3_initialize(); | 133 int rc = sqlite3_initialize(); |
| 134 if( rc ) return -1; |
134 #endif | 135 #endif |
135 sqlite3_mutex_enter(mem0.mutex); | 136 sqlite3_mutex_enter(mem0.mutex); |
136 priorLimit = mem0.alarmThreshold; | 137 priorLimit = mem0.alarmThreshold; |
137 sqlite3_mutex_leave(mem0.mutex); | 138 sqlite3_mutex_leave(mem0.mutex); |
138 if( n<0 ) return priorLimit; | 139 if( n<0 ) return priorLimit; |
139 if( n>0 ){ | 140 if( n>0 ){ |
140 sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n); | 141 sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n); |
141 }else{ | 142 }else{ |
142 sqlite3MemoryAlarm(0, 0, 0); | 143 sqlite3MemoryAlarm(0, 0, 0); |
143 } | 144 } |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 ** lock is already held. | 260 ** lock is already held. |
260 */ | 261 */ |
261 static int mallocWithAlarm(int n, void **pp){ | 262 static int mallocWithAlarm(int n, void **pp){ |
262 int nFull; | 263 int nFull; |
263 void *p; | 264 void *p; |
264 assert( sqlite3_mutex_held(mem0.mutex) ); | 265 assert( sqlite3_mutex_held(mem0.mutex) ); |
265 nFull = sqlite3GlobalConfig.m.xRoundup(n); | 266 nFull = sqlite3GlobalConfig.m.xRoundup(n); |
266 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); | 267 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); |
267 if( mem0.alarmCallback!=0 ){ | 268 if( mem0.alarmCallback!=0 ){ |
268 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); | 269 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
269 if( nUsed+nFull >= mem0.alarmThreshold ){ | 270 if( nUsed >= mem0.alarmThreshold - nFull ){ |
270 mem0.nearlyFull = 1; | 271 mem0.nearlyFull = 1; |
271 sqlite3MallocAlarm(nFull); | 272 sqlite3MallocAlarm(nFull); |
272 }else{ | 273 }else{ |
273 mem0.nearlyFull = 0; | 274 mem0.nearlyFull = 0; |
274 } | 275 } |
275 } | 276 } |
276 p = sqlite3GlobalConfig.m.xMalloc(nFull); | 277 p = sqlite3GlobalConfig.m.xMalloc(nFull); |
277 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT | 278 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
278 if( p==0 && mem0.alarmCallback ){ | 279 if( p==0 && mem0.alarmCallback ){ |
279 sqlite3MallocAlarm(nFull); | 280 sqlite3MallocAlarm(nFull); |
280 p = sqlite3GlobalConfig.m.xMalloc(nFull); | 281 p = sqlite3GlobalConfig.m.xMalloc(nFull); |
281 } | 282 } |
282 #endif | 283 #endif |
283 if( p ){ | 284 if( p ){ |
284 nFull = sqlite3MallocSize(p); | 285 nFull = sqlite3MallocSize(p); |
285 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); | 286 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); |
286 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1); | 287 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1); |
287 } | 288 } |
288 *pp = p; | 289 *pp = p; |
289 return nFull; | 290 return nFull; |
290 } | 291 } |
291 | 292 |
292 /* | 293 /* |
293 ** Allocate memory. This routine is like sqlite3_malloc() except that it | 294 ** Allocate memory. This routine is like sqlite3_malloc() except that it |
294 ** assumes the memory subsystem has already been initialized. | 295 ** assumes the memory subsystem has already been initialized. |
295 */ | 296 */ |
296 void *sqlite3Malloc(int n){ | 297 void *sqlite3Malloc(u64 n){ |
297 void *p; | 298 void *p; |
298 if( n<=0 /* IMP: R-65312-04917 */ | 299 if( n==0 || n>=0x7fffff00 ){ |
299 || n>=0x7fffff00 | |
300 ){ | |
301 /* A memory allocation of a number of bytes which is near the maximum | 300 /* 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 | 301 ** signed integer value might cause an integer overflow inside of the |
303 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving | 302 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving |
304 ** 255 bytes of overhead. SQLite itself will never use anything near | 303 ** 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() */ | 304 ** this amount. The only way to reach the limit is with sqlite3_malloc() */ |
306 p = 0; | 305 p = 0; |
307 }else if( sqlite3GlobalConfig.bMemstat ){ | 306 }else if( sqlite3GlobalConfig.bMemstat ){ |
308 sqlite3_mutex_enter(mem0.mutex); | 307 sqlite3_mutex_enter(mem0.mutex); |
309 mallocWithAlarm(n, &p); | 308 mallocWithAlarm((int)n, &p); |
310 sqlite3_mutex_leave(mem0.mutex); | 309 sqlite3_mutex_leave(mem0.mutex); |
311 }else{ | 310 }else{ |
312 p = sqlite3GlobalConfig.m.xMalloc(n); | 311 p = sqlite3GlobalConfig.m.xMalloc((int)n); |
313 } | 312 } |
314 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-04675-44850 */ | 313 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */ |
315 return p; | 314 return p; |
316 } | 315 } |
317 | 316 |
318 /* | 317 /* |
319 ** This version of the memory allocation is for use by the application. | 318 ** This version of the memory allocation is for use by the application. |
320 ** First make sure the memory subsystem is initialized, then do the | 319 ** First make sure the memory subsystem is initialized, then do the |
321 ** allocation. | 320 ** allocation. |
322 */ | 321 */ |
323 void *sqlite3_malloc(int n){ | 322 void *sqlite3_malloc(int n){ |
324 #ifndef SQLITE_OMIT_AUTOINIT | 323 #ifndef SQLITE_OMIT_AUTOINIT |
325 if( sqlite3_initialize() ) return 0; | 324 if( sqlite3_initialize() ) return 0; |
326 #endif | 325 #endif |
| 326 return n<=0 ? 0 : sqlite3Malloc(n); |
| 327 } |
| 328 void *sqlite3_malloc64(sqlite3_uint64 n){ |
| 329 #ifndef SQLITE_OMIT_AUTOINIT |
| 330 if( sqlite3_initialize() ) return 0; |
| 331 #endif |
327 return sqlite3Malloc(n); | 332 return sqlite3Malloc(n); |
328 } | 333 } |
329 | 334 |
330 /* | 335 /* |
331 ** Each thread may only have a single outstanding allocation from | 336 ** Each thread may only have a single outstanding allocation from |
332 ** xScratchMalloc(). We verify this constraint in the single-threaded | 337 ** xScratchMalloc(). We verify this constraint in the single-threaded |
333 ** case by setting scratchAllocOut to 1 when an allocation | 338 ** case by setting scratchAllocOut to 1 when an allocation |
334 ** is outstanding clearing it when the allocation is freed. | 339 ** is outstanding clearing it when the allocation is freed. |
335 */ | 340 */ |
336 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) | 341 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
337 static int scratchAllocOut = 0; | 342 static int scratchAllocOut = 0; |
338 #endif | 343 #endif |
339 | 344 |
340 | 345 |
341 /* | 346 /* |
342 ** Allocate memory that is to be used and released right away. | 347 ** Allocate memory that is to be used and released right away. |
343 ** This routine is similar to alloca() in that it is not intended | 348 ** This routine is similar to alloca() in that it is not intended |
344 ** for situations where the memory might be held long-term. This | 349 ** for situations where the memory might be held long-term. This |
345 ** routine is intended to get memory to old large transient data | 350 ** routine is intended to get memory to old large transient data |
346 ** structures that would not normally fit on the stack of an | 351 ** structures that would not normally fit on the stack of an |
347 ** embedded processor. | 352 ** embedded processor. |
348 */ | 353 */ |
349 void *sqlite3ScratchMalloc(int n){ | 354 void *sqlite3ScratchMalloc(int n){ |
350 void *p; | 355 void *p; |
351 assert( n>0 ); | 356 assert( n>0 ); |
352 | 357 |
353 sqlite3_mutex_enter(mem0.mutex); | 358 sqlite3_mutex_enter(mem0.mutex); |
| 359 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); |
354 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){ | 360 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){ |
355 p = mem0.pScratchFree; | 361 p = mem0.pScratchFree; |
356 mem0.pScratchFree = mem0.pScratchFree->pNext; | 362 mem0.pScratchFree = mem0.pScratchFree->pNext; |
357 mem0.nScratchFree--; | 363 mem0.nScratchFree--; |
358 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); | 364 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); |
359 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); | |
360 sqlite3_mutex_leave(mem0.mutex); | 365 sqlite3_mutex_leave(mem0.mutex); |
361 }else{ | 366 }else{ |
362 if( sqlite3GlobalConfig.bMemstat ){ | 367 sqlite3_mutex_leave(mem0.mutex); |
363 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); | 368 p = sqlite3Malloc(n); |
364 n = mallocWithAlarm(n, &p); | 369 if( sqlite3GlobalConfig.bMemstat && p ){ |
365 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); | 370 sqlite3_mutex_enter(mem0.mutex); |
| 371 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p)); |
366 sqlite3_mutex_leave(mem0.mutex); | 372 sqlite3_mutex_leave(mem0.mutex); |
367 }else{ | |
368 sqlite3_mutex_leave(mem0.mutex); | |
369 p = sqlite3GlobalConfig.m.xMalloc(n); | |
370 } | 373 } |
371 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); | 374 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); |
372 } | 375 } |
373 assert( sqlite3_mutex_notheld(mem0.mutex) ); | 376 assert( sqlite3_mutex_notheld(mem0.mutex) ); |
374 | 377 |
375 | 378 |
376 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) | 379 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
377 /* Verify that no more than two scratch allocations per thread | 380 /* Verify that no more than two scratch allocations per thread |
378 ** are outstanding at one time. (This is only checked in the | 381 ** are outstanding at one time. (This is only checked in the |
379 ** single-threaded case since checking in the multi-threaded case | 382 ** single-threaded case since checking in the multi-threaded case |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 } | 428 } |
426 } | 429 } |
427 } | 430 } |
428 } | 431 } |
429 | 432 |
430 /* | 433 /* |
431 ** TRUE if p is a lookaside memory allocation from db | 434 ** TRUE if p is a lookaside memory allocation from db |
432 */ | 435 */ |
433 #ifndef SQLITE_OMIT_LOOKASIDE | 436 #ifndef SQLITE_OMIT_LOOKASIDE |
434 static int isLookaside(sqlite3 *db, void *p){ | 437 static int isLookaside(sqlite3 *db, void *p){ |
435 return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd; | 438 return p>=db->lookaside.pStart && p<db->lookaside.pEnd; |
436 } | 439 } |
437 #else | 440 #else |
438 #define isLookaside(A,B) 0 | 441 #define isLookaside(A,B) 0 |
439 #endif | 442 #endif |
440 | 443 |
441 /* | 444 /* |
442 ** Return the size of a memory allocation previously obtained from | 445 ** Return the size of a memory allocation previously obtained from |
443 ** sqlite3Malloc() or sqlite3_malloc(). | 446 ** sqlite3Malloc() or sqlite3_malloc(). |
444 */ | 447 */ |
445 int sqlite3MallocSize(void *p){ | 448 int sqlite3MallocSize(void *p){ |
446 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); | 449 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
447 assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); | |
448 return sqlite3GlobalConfig.m.xSize(p); | 450 return sqlite3GlobalConfig.m.xSize(p); |
449 } | 451 } |
450 int sqlite3DbMallocSize(sqlite3 *db, void *p){ | 452 int sqlite3DbMallocSize(sqlite3 *db, void *p){ |
451 assert( db==0 || sqlite3_mutex_held(db->mutex) ); | 453 if( db==0 ){ |
452 if( db && isLookaside(db, p) ){ | 454 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); |
453 return db->lookaside.sz; | 455 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 456 return sqlite3MallocSize(p); |
454 }else{ | 457 }else{ |
455 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); | 458 assert( sqlite3_mutex_held(db->mutex) ); |
456 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); | 459 if( isLookaside(db, p) ){ |
457 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); | 460 return db->lookaside.sz; |
458 return sqlite3GlobalConfig.m.xSize(p); | 461 }else{ |
| 462 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 463 assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 464 return sqlite3GlobalConfig.m.xSize(p); |
| 465 } |
459 } | 466 } |
460 } | 467 } |
| 468 sqlite3_uint64 sqlite3_msize(void *p){ |
| 469 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); |
| 470 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 471 return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p); |
| 472 } |
461 | 473 |
462 /* | 474 /* |
463 ** Free memory previously obtained from sqlite3Malloc(). | 475 ** Free memory previously obtained from sqlite3Malloc(). |
464 */ | 476 */ |
465 void sqlite3_free(void *p){ | 477 void sqlite3_free(void *p){ |
466 if( p==0 ) return; /* IMP: R-49053-54554 */ | 478 if( p==0 ) return; /* IMP: R-49053-54554 */ |
467 assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); | |
468 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); | 479 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 480 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); |
469 if( sqlite3GlobalConfig.bMemstat ){ | 481 if( sqlite3GlobalConfig.bMemstat ){ |
470 sqlite3_mutex_enter(mem0.mutex); | 482 sqlite3_mutex_enter(mem0.mutex); |
471 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); | 483 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); |
472 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1); | 484 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1); |
473 sqlite3GlobalConfig.m.xFree(p); | 485 sqlite3GlobalConfig.m.xFree(p); |
474 sqlite3_mutex_leave(mem0.mutex); | 486 sqlite3_mutex_leave(mem0.mutex); |
475 }else{ | 487 }else{ |
476 sqlite3GlobalConfig.m.xFree(p); | 488 sqlite3GlobalConfig.m.xFree(p); |
477 } | 489 } |
478 } | 490 } |
479 | 491 |
480 /* | 492 /* |
| 493 ** Add the size of memory allocation "p" to the count in |
| 494 ** *db->pnBytesFreed. |
| 495 */ |
| 496 static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){ |
| 497 *db->pnBytesFreed += sqlite3DbMallocSize(db,p); |
| 498 } |
| 499 |
| 500 /* |
481 ** Free memory that might be associated with a particular database | 501 ** Free memory that might be associated with a particular database |
482 ** connection. | 502 ** connection. |
483 */ | 503 */ |
484 void sqlite3DbFree(sqlite3 *db, void *p){ | 504 void sqlite3DbFree(sqlite3 *db, void *p){ |
485 assert( db==0 || sqlite3_mutex_held(db->mutex) ); | 505 assert( db==0 || sqlite3_mutex_held(db->mutex) ); |
| 506 if( p==0 ) return; |
486 if( db ){ | 507 if( db ){ |
487 if( db->pnBytesFreed ){ | 508 if( db->pnBytesFreed ){ |
488 *db->pnBytesFreed += sqlite3DbMallocSize(db, p); | 509 measureAllocationSize(db, p); |
489 return; | 510 return; |
490 } | 511 } |
491 if( isLookaside(db, p) ){ | 512 if( isLookaside(db, p) ){ |
492 LookasideSlot *pBuf = (LookasideSlot*)p; | 513 LookasideSlot *pBuf = (LookasideSlot*)p; |
| 514 #if SQLITE_DEBUG |
| 515 /* Trash all content in the buffer being freed */ |
| 516 memset(p, 0xaa, db->lookaside.sz); |
| 517 #endif |
493 pBuf->pNext = db->lookaside.pFree; | 518 pBuf->pNext = db->lookaside.pFree; |
494 db->lookaside.pFree = pBuf; | 519 db->lookaside.pFree = pBuf; |
495 db->lookaside.nOut--; | 520 db->lookaside.nOut--; |
496 return; | 521 return; |
497 } | 522 } |
498 } | 523 } |
499 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); | 524 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
500 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); | 525 assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
501 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); | 526 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); |
502 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); | 527 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
503 sqlite3_free(p); | 528 sqlite3_free(p); |
504 } | 529 } |
505 | 530 |
506 /* | 531 /* |
507 ** Change the size of an existing memory allocation | 532 ** Change the size of an existing memory allocation |
508 */ | 533 */ |
509 void *sqlite3Realloc(void *pOld, int nBytes){ | 534 void *sqlite3Realloc(void *pOld, u64 nBytes){ |
510 int nOld, nNew; | 535 int nOld, nNew, nDiff; |
511 void *pNew; | 536 void *pNew; |
| 537 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); |
| 538 assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) ); |
512 if( pOld==0 ){ | 539 if( pOld==0 ){ |
513 return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */ | 540 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ |
514 } | 541 } |
515 if( nBytes<=0 ){ | 542 if( nBytes==0 ){ |
516 sqlite3_free(pOld); /* IMP: R-31593-10574 */ | 543 sqlite3_free(pOld); /* IMP: R-26507-47431 */ |
517 return 0; | 544 return 0; |
518 } | 545 } |
519 if( nBytes>=0x7fffff00 ){ | 546 if( nBytes>=0x7fffff00 ){ |
520 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */ | 547 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */ |
521 return 0; | 548 return 0; |
522 } | 549 } |
523 nOld = sqlite3MallocSize(pOld); | 550 nOld = sqlite3MallocSize(pOld); |
524 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second | 551 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second |
525 ** argument to xRealloc is always a value returned by a prior call to | 552 ** argument to xRealloc is always a value returned by a prior call to |
526 ** xRoundup. */ | 553 ** xRoundup. */ |
527 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes); | 554 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes); |
528 if( nOld==nNew ){ | 555 if( nOld==nNew ){ |
529 pNew = pOld; | 556 pNew = pOld; |
530 }else if( sqlite3GlobalConfig.bMemstat ){ | 557 }else if( sqlite3GlobalConfig.bMemstat ){ |
531 sqlite3_mutex_enter(mem0.mutex); | 558 sqlite3_mutex_enter(mem0.mutex); |
532 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes); | 559 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes); |
533 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= | 560 nDiff = nNew - nOld; |
534 mem0.alarmThreshold ){ | 561 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= |
535 sqlite3MallocAlarm(nNew-nOld); | 562 mem0.alarmThreshold-nDiff ){ |
| 563 sqlite3MallocAlarm(nDiff); |
536 } | 564 } |
537 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); | |
538 assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) ); | |
539 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); | 565 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
540 if( pNew==0 && mem0.alarmCallback ){ | 566 if( pNew==0 && mem0.alarmCallback ){ |
541 sqlite3MallocAlarm(nBytes); | 567 sqlite3MallocAlarm((int)nBytes); |
542 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); | 568 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
543 } | 569 } |
544 if( pNew ){ | 570 if( pNew ){ |
545 nNew = sqlite3MallocSize(pNew); | 571 nNew = sqlite3MallocSize(pNew); |
546 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld); | 572 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld); |
547 } | 573 } |
548 sqlite3_mutex_leave(mem0.mutex); | 574 sqlite3_mutex_leave(mem0.mutex); |
549 }else{ | 575 }else{ |
550 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); | 576 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
551 } | 577 } |
552 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */ | 578 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */ |
553 return pNew; | 579 return pNew; |
554 } | 580 } |
555 | 581 |
556 /* | 582 /* |
557 ** The public interface to sqlite3Realloc. Make sure that the memory | 583 ** The public interface to sqlite3Realloc. Make sure that the memory |
558 ** subsystem is initialized prior to invoking sqliteRealloc. | 584 ** subsystem is initialized prior to invoking sqliteRealloc. |
559 */ | 585 */ |
560 void *sqlite3_realloc(void *pOld, int n){ | 586 void *sqlite3_realloc(void *pOld, int n){ |
561 #ifndef SQLITE_OMIT_AUTOINIT | 587 #ifndef SQLITE_OMIT_AUTOINIT |
562 if( sqlite3_initialize() ) return 0; | 588 if( sqlite3_initialize() ) return 0; |
563 #endif | 589 #endif |
| 590 if( n<0 ) n = 0; /* IMP: R-26507-47431 */ |
| 591 return sqlite3Realloc(pOld, n); |
| 592 } |
| 593 void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){ |
| 594 #ifndef SQLITE_OMIT_AUTOINIT |
| 595 if( sqlite3_initialize() ) return 0; |
| 596 #endif |
564 return sqlite3Realloc(pOld, n); | 597 return sqlite3Realloc(pOld, n); |
565 } | 598 } |
566 | 599 |
567 | 600 |
568 /* | 601 /* |
569 ** Allocate and zero memory. | 602 ** Allocate and zero memory. |
570 */ | 603 */ |
571 void *sqlite3MallocZero(int n){ | 604 void *sqlite3MallocZero(u64 n){ |
572 void *p = sqlite3Malloc(n); | 605 void *p = sqlite3Malloc(n); |
573 if( p ){ | 606 if( p ){ |
574 memset(p, 0, n); | 607 memset(p, 0, (size_t)n); |
575 } | 608 } |
576 return p; | 609 return p; |
577 } | 610 } |
578 | 611 |
579 /* | 612 /* |
580 ** Allocate and zero memory. If the allocation fails, make | 613 ** Allocate and zero memory. If the allocation fails, make |
581 ** the mallocFailed flag in the connection pointer. | 614 ** the mallocFailed flag in the connection pointer. |
582 */ | 615 */ |
583 void *sqlite3DbMallocZero(sqlite3 *db, int n){ | 616 void *sqlite3DbMallocZero(sqlite3 *db, u64 n){ |
584 void *p = sqlite3DbMallocRaw(db, n); | 617 void *p = sqlite3DbMallocRaw(db, n); |
585 if( p ){ | 618 if( p ){ |
586 memset(p, 0, n); | 619 memset(p, 0, (size_t)n); |
587 } | 620 } |
588 return p; | 621 return p; |
589 } | 622 } |
590 | 623 |
591 /* | 624 /* |
592 ** Allocate and zero memory. If the allocation fails, make | 625 ** Allocate and zero memory. If the allocation fails, make |
593 ** the mallocFailed flag in the connection pointer. | 626 ** the mallocFailed flag in the connection pointer. |
594 ** | 627 ** |
595 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc | 628 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc |
596 ** failure on the same database connection) then always return 0. | 629 ** failure on the same database connection) then always return 0. |
597 ** Hence for a particular database connection, once malloc starts | 630 ** Hence for a particular database connection, once malloc starts |
598 ** failing, it fails consistently until mallocFailed is reset. | 631 ** failing, it fails consistently until mallocFailed is reset. |
599 ** This is an important assumption. There are many places in the | 632 ** This is an important assumption. There are many places in the |
600 ** code that do things like this: | 633 ** code that do things like this: |
601 ** | 634 ** |
602 ** int *a = (int*)sqlite3DbMallocRaw(db, 100); | 635 ** int *a = (int*)sqlite3DbMallocRaw(db, 100); |
603 ** int *b = (int*)sqlite3DbMallocRaw(db, 200); | 636 ** int *b = (int*)sqlite3DbMallocRaw(db, 200); |
604 ** if( b ) a[10] = 9; | 637 ** if( b ) a[10] = 9; |
605 ** | 638 ** |
606 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed | 639 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed |
607 ** that all prior mallocs (ex: "a") worked too. | 640 ** that all prior mallocs (ex: "a") worked too. |
608 */ | 641 */ |
609 void *sqlite3DbMallocRaw(sqlite3 *db, int n){ | 642 void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){ |
610 void *p; | 643 void *p; |
611 assert( db==0 || sqlite3_mutex_held(db->mutex) ); | 644 assert( db==0 || sqlite3_mutex_held(db->mutex) ); |
612 assert( db==0 || db->pnBytesFreed==0 ); | 645 assert( db==0 || db->pnBytesFreed==0 ); |
613 #ifndef SQLITE_OMIT_LOOKASIDE | 646 #ifndef SQLITE_OMIT_LOOKASIDE |
614 if( db ){ | 647 if( db ){ |
615 LookasideSlot *pBuf; | 648 LookasideSlot *pBuf; |
616 if( db->mallocFailed ){ | 649 if( db->mallocFailed ){ |
617 return 0; | 650 return 0; |
618 } | 651 } |
619 if( db->lookaside.bEnabled ){ | 652 if( db->lookaside.bEnabled ){ |
(...skipping 14 matching lines...) Expand all Loading... |
634 } | 667 } |
635 #else | 668 #else |
636 if( db && db->mallocFailed ){ | 669 if( db && db->mallocFailed ){ |
637 return 0; | 670 return 0; |
638 } | 671 } |
639 #endif | 672 #endif |
640 p = sqlite3Malloc(n); | 673 p = sqlite3Malloc(n); |
641 if( !p && db ){ | 674 if( !p && db ){ |
642 db->mallocFailed = 1; | 675 db->mallocFailed = 1; |
643 } | 676 } |
644 sqlite3MemdebugSetType(p, MEMTYPE_DB | | 677 sqlite3MemdebugSetType(p, |
645 ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); | 678 (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP); |
646 return p; | 679 return p; |
647 } | 680 } |
648 | 681 |
649 /* | 682 /* |
650 ** Resize the block of memory pointed to by p to n bytes. If the | 683 ** 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. | 684 ** resize fails, set the mallocFailed flag in the connection object. |
652 */ | 685 */ |
653 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ | 686 void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){ |
654 void *pNew = 0; | 687 void *pNew = 0; |
655 assert( db!=0 ); | 688 assert( db!=0 ); |
656 assert( sqlite3_mutex_held(db->mutex) ); | 689 assert( sqlite3_mutex_held(db->mutex) ); |
657 if( db->mallocFailed==0 ){ | 690 if( db->mallocFailed==0 ){ |
658 if( p==0 ){ | 691 if( p==0 ){ |
659 return sqlite3DbMallocRaw(db, n); | 692 return sqlite3DbMallocRaw(db, n); |
660 } | 693 } |
661 if( isLookaside(db, p) ){ | 694 if( isLookaside(db, p) ){ |
662 if( n<=db->lookaside.sz ){ | 695 if( n<=db->lookaside.sz ){ |
663 return p; | 696 return p; |
664 } | 697 } |
665 pNew = sqlite3DbMallocRaw(db, n); | 698 pNew = sqlite3DbMallocRaw(db, n); |
666 if( pNew ){ | 699 if( pNew ){ |
667 memcpy(pNew, p, db->lookaside.sz); | 700 memcpy(pNew, p, db->lookaside.sz); |
668 sqlite3DbFree(db, p); | 701 sqlite3DbFree(db, p); |
669 } | 702 } |
670 }else{ | 703 }else{ |
671 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); | 704 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
672 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); | 705 assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
673 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); | 706 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
674 pNew = sqlite3_realloc(p, n); | 707 pNew = sqlite3_realloc64(p, n); |
675 if( !pNew ){ | 708 if( !pNew ){ |
676 sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP); | |
677 db->mallocFailed = 1; | 709 db->mallocFailed = 1; |
678 } | 710 } |
679 sqlite3MemdebugSetType(pNew, MEMTYPE_DB | | 711 sqlite3MemdebugSetType(pNew, |
680 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); | 712 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); |
681 } | 713 } |
682 } | 714 } |
683 return pNew; | 715 return pNew; |
684 } | 716 } |
685 | 717 |
686 /* | 718 /* |
687 ** Attempt to reallocate p. If the reallocation fails, then free p | 719 ** Attempt to reallocate p. If the reallocation fails, then free p |
688 ** and set the mallocFailed flag in the database connection. | 720 ** and set the mallocFailed flag in the database connection. |
689 */ | 721 */ |
690 void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ | 722 void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){ |
691 void *pNew; | 723 void *pNew; |
692 pNew = sqlite3DbRealloc(db, p, n); | 724 pNew = sqlite3DbRealloc(db, p, n); |
693 if( !pNew ){ | 725 if( !pNew ){ |
694 sqlite3DbFree(db, p); | 726 sqlite3DbFree(db, p); |
695 } | 727 } |
696 return pNew; | 728 return pNew; |
697 } | 729 } |
698 | 730 |
699 /* | 731 /* |
700 ** Make a copy of a string in memory obtained from sqliteMalloc(). These | 732 ** Make a copy of a string in memory obtained from sqliteMalloc(). These |
701 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This | 733 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This |
702 ** is because when memory debugging is turned on, these two functions are | 734 ** 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 | 735 ** called via macros that record the current file and line number in the |
704 ** ThreadData structure. | 736 ** ThreadData structure. |
705 */ | 737 */ |
706 char *sqlite3DbStrDup(sqlite3 *db, const char *z){ | 738 char *sqlite3DbStrDup(sqlite3 *db, const char *z){ |
707 char *zNew; | 739 char *zNew; |
708 size_t n; | 740 size_t n; |
709 if( z==0 ){ | 741 if( z==0 ){ |
710 return 0; | 742 return 0; |
711 } | 743 } |
712 n = sqlite3Strlen30(z) + 1; | 744 n = sqlite3Strlen30(z) + 1; |
713 assert( (n&0x7fffffff)==n ); | 745 assert( (n&0x7fffffff)==n ); |
714 zNew = sqlite3DbMallocRaw(db, (int)n); | 746 zNew = sqlite3DbMallocRaw(db, (int)n); |
715 if( zNew ){ | 747 if( zNew ){ |
716 memcpy(zNew, z, n); | 748 memcpy(zNew, z, n); |
717 } | 749 } |
718 return zNew; | 750 return zNew; |
719 } | 751 } |
720 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ | 752 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){ |
721 char *zNew; | 753 char *zNew; |
722 if( z==0 ){ | 754 if( z==0 ){ |
723 return 0; | 755 return 0; |
724 } | 756 } |
725 assert( (n&0x7fffffff)==n ); | 757 assert( (n&0x7fffffff)==n ); |
726 zNew = sqlite3DbMallocRaw(db, n+1); | 758 zNew = sqlite3DbMallocRaw(db, n+1); |
727 if( zNew ){ | 759 if( zNew ){ |
728 memcpy(zNew, z, n); | 760 memcpy(zNew, z, (size_t)n); |
729 zNew[n] = 0; | 761 zNew[n] = 0; |
730 } | 762 } |
731 return zNew; | 763 return zNew; |
732 } | 764 } |
733 | 765 |
734 /* | 766 /* |
735 ** Create a string from the zFromat argument and the va_list that follows. | 767 ** 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 | 768 ** Store the string in memory obtained from sqliteMalloc() and make *pz |
737 ** point to that string. | 769 ** point to that string. |
738 */ | 770 */ |
739 void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){ | 771 void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){ |
740 va_list ap; | 772 va_list ap; |
741 char *z; | 773 char *z; |
742 | 774 |
743 va_start(ap, zFormat); | 775 va_start(ap, zFormat); |
744 z = sqlite3VMPrintf(db, zFormat, ap); | 776 z = sqlite3VMPrintf(db, zFormat, ap); |
745 va_end(ap); | 777 va_end(ap); |
746 sqlite3DbFree(db, *pz); | 778 sqlite3DbFree(db, *pz); |
747 *pz = z; | 779 *pz = z; |
748 } | 780 } |
749 | 781 |
| 782 /* |
| 783 ** Take actions at the end of an API call to indicate an OOM error |
| 784 */ |
| 785 static SQLITE_NOINLINE int apiOomError(sqlite3 *db){ |
| 786 db->mallocFailed = 0; |
| 787 sqlite3Error(db, SQLITE_NOMEM); |
| 788 return SQLITE_NOMEM; |
| 789 } |
750 | 790 |
751 /* | 791 /* |
752 ** This function must be called before exiting any API function (i.e. | 792 ** This function must be called before exiting any API function (i.e. |
753 ** returning control to the user) that has called sqlite3_malloc or | 793 ** returning control to the user) that has called sqlite3_malloc or |
754 ** sqlite3_realloc. | 794 ** sqlite3_realloc. |
755 ** | 795 ** |
756 ** The returned value is normally a copy of the second argument to this | 796 ** 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 | 797 ** function. However, if a malloc() failure has occurred since the previous |
758 ** invocation SQLITE_NOMEM is returned instead. | 798 ** invocation SQLITE_NOMEM is returned instead. |
759 ** | 799 ** |
760 ** If the first argument, db, is not NULL and a malloc() error has occurred, | 800 ** 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()) | 801 ** then the connection error-code (the value returned by sqlite3_errcode()) |
762 ** is set to SQLITE_NOMEM. | 802 ** is set to SQLITE_NOMEM. |
763 */ | 803 */ |
764 int sqlite3ApiExit(sqlite3* db, int rc){ | 804 int sqlite3ApiExit(sqlite3* db, int rc){ |
765 /* If the db handle is not NULL, then we must hold the connection handle | 805 /* 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 | 806 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed |
767 ** is unsafe, as is the call to sqlite3Error(). | 807 ** is unsafe, as is the call to sqlite3Error(). |
768 */ | 808 */ |
769 assert( !db || sqlite3_mutex_held(db->mutex) ); | 809 assert( !db || sqlite3_mutex_held(db->mutex) ); |
770 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){ | 810 if( db==0 ) return rc & 0xff; |
771 sqlite3Error(db, SQLITE_NOMEM, 0); | 811 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){ |
772 db->mallocFailed = 0; | 812 return apiOomError(db); |
773 rc = SQLITE_NOMEM; | |
774 } | 813 } |
775 return rc & (db ? db->errMask : 0xff); | 814 return rc & db->errMask; |
776 } | 815 } |
OLD | NEW |