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