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