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