malloc.c 23 KB

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