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