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 | ** |
danielk1977 | bc73971 | 2009-03-23 04:33:32 +0000 | [diff] [blame^] | 15 | ** $Id: malloc.c,v 1.59 2009/03/23 04:33:33 danielk1977 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 |
| 91 | ** issued. The alarmBusy variable is set to prevent recursive |
| 92 | ** callbacks. |
| 93 | */ |
| 94 | sqlite3_int64 alarmThreshold; |
| 95 | void (*alarmCallback)(void*, sqlite3_int64,int); |
| 96 | void *alarmArg; |
| 97 | int alarmBusy; |
| 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; |
danielk1977 | cdcfe95 | 2008-11-18 07:27:24 +0000 | [diff] [blame] | 106 | } mem0 = { 62560955, 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; |
| 223 | if( mem0.alarmCallback==0 || mem0.alarmBusy ) return; |
| 224 | mem0.alarmBusy = 1; |
| 225 | xCallback = mem0.alarmCallback; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 226 | nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 227 | pArg = mem0.alarmArg; |
| 228 | sqlite3_mutex_leave(mem0.mutex); |
| 229 | xCallback(pArg, nowUsed, nByte); |
| 230 | sqlite3_mutex_enter(mem0.mutex); |
| 231 | mem0.alarmBusy = 0; |
| 232 | } |
| 233 | |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 234 | /* |
| 235 | ** Do a memory allocation with statistics and alarms. Assume the |
| 236 | ** lock is already held. |
| 237 | */ |
| 238 | static int mallocWithAlarm(int n, void **pp){ |
| 239 | int nFull; |
| 240 | void *p; |
| 241 | assert( sqlite3_mutex_held(mem0.mutex) ); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 242 | nFull = sqlite3GlobalConfig.m.xRoundup(n); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 243 | sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); |
| 244 | if( mem0.alarmCallback!=0 ){ |
| 245 | int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 246 | if( nUsed+nFull >= mem0.alarmThreshold ){ |
| 247 | sqlite3MallocAlarm(nFull); |
| 248 | } |
| 249 | } |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 250 | p = sqlite3GlobalConfig.m.xMalloc(nFull); |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 251 | if( p==0 && mem0.alarmCallback ){ |
| 252 | sqlite3MallocAlarm(nFull); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 253 | p = sqlite3GlobalConfig.m.xMalloc(nFull); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 254 | } |
drh | c702c7c | 2008-07-18 18:56:16 +0000 | [diff] [blame] | 255 | if( p ){ |
| 256 | nFull = sqlite3MallocSize(p); |
| 257 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); |
| 258 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 259 | *pp = p; |
| 260 | return nFull; |
| 261 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 262 | |
| 263 | /* |
| 264 | ** Allocate memory. This routine is like sqlite3_malloc() except that it |
| 265 | ** assumes the memory subsystem has already been initialized. |
| 266 | */ |
| 267 | void *sqlite3Malloc(int n){ |
| 268 | void *p; |
drh | 50b6568 | 2009-02-17 18:37:28 +0000 | [diff] [blame] | 269 | if( n<=0 || NEVER(n>=0x7fffff00) ){ |
| 270 | /* The NEVER(n>=0x7fffff00) term is added out of paranoia. We want to make |
| 271 | ** absolutely sure that there is nothing within SQLite that can cause a |
| 272 | ** memory allocation of a number of bytes which is near the maximum signed |
| 273 | ** integer value and thus cause an integer overflow inside of the xMalloc() |
| 274 | ** implementation. The n>=0x7fffff00 gives us 255 bytes of headroom. The |
| 275 | ** test should never be true because SQLITE_MAX_LENGTH should be much |
| 276 | ** less than 0x7fffff00 and it should catch large memory allocations |
| 277 | ** before they reach this point. */ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 278 | p = 0; |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 279 | }else if( sqlite3GlobalConfig.bMemstat ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 280 | sqlite3_mutex_enter(mem0.mutex); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 281 | mallocWithAlarm(n, &p); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 282 | sqlite3_mutex_leave(mem0.mutex); |
| 283 | }else{ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 284 | p = sqlite3GlobalConfig.m.xMalloc(n); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 285 | } |
| 286 | return p; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | ** This version of the memory allocation is for use by the application. |
| 291 | ** First make sure the memory subsystem is initialized, then do the |
| 292 | ** allocation. |
| 293 | */ |
| 294 | void *sqlite3_malloc(int n){ |
| 295 | #ifndef SQLITE_OMIT_AUTOINIT |
| 296 | if( sqlite3_initialize() ) return 0; |
| 297 | #endif |
| 298 | return sqlite3Malloc(n); |
| 299 | } |
| 300 | |
| 301 | /* |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 302 | ** Each thread may only have a single outstanding allocation from |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 303 | ** xScratchMalloc(). We verify this constraint in the single-threaded |
| 304 | ** case by setting scratchAllocOut to 1 when an allocation |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 305 | ** is outstanding clearing it when the allocation is freed. |
| 306 | */ |
| 307 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 308 | static int scratchAllocOut = 0; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 309 | #endif |
| 310 | |
| 311 | |
| 312 | /* |
| 313 | ** Allocate memory that is to be used and released right away. |
| 314 | ** This routine is similar to alloca() in that it is not intended |
| 315 | ** for situations where the memory might be held long-term. This |
| 316 | ** routine is intended to get memory to old large transient data |
| 317 | ** structures that would not normally fit on the stack of an |
| 318 | ** embedded processor. |
| 319 | */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 320 | void *sqlite3ScratchMalloc(int n){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 321 | void *p; |
| 322 | assert( n>0 ); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 323 | |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 324 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 325 | /* Verify that no more than one scratch allocation per thread |
| 326 | ** is outstanding at one time. (This is only checked in the |
| 327 | ** single-threaded case since checking in the multi-threaded case |
| 328 | ** would be much more complicated.) */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 329 | assert( scratchAllocOut==0 ); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 330 | #endif |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 331 | |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 332 | if( sqlite3GlobalConfig.szScratch<n ){ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 333 | goto scratch_overflow; |
| 334 | }else{ |
| 335 | sqlite3_mutex_enter(mem0.mutex); |
| 336 | if( mem0.nScratchFree==0 ){ |
| 337 | sqlite3_mutex_leave(mem0.mutex); |
| 338 | goto scratch_overflow; |
| 339 | }else{ |
| 340 | int i; |
| 341 | i = mem0.aScratchFree[--mem0.nScratchFree]; |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 342 | i *= sqlite3GlobalConfig.szScratch; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 343 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 344 | sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); |
danielk1977 | 8183e33 | 2008-08-29 17:56:12 +0000 | [diff] [blame] | 345 | sqlite3_mutex_leave(mem0.mutex); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 346 | p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i]; |
shane | 1530159 | 2008-12-16 17:20:38 +0000 | [diff] [blame] | 347 | assert( (((u8*)p - (u8*)0) & 7)==0 ); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 348 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 349 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 350 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 351 | scratchAllocOut = p!=0; |
| 352 | #endif |
| 353 | |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 354 | return p; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 355 | |
| 356 | scratch_overflow: |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 357 | if( sqlite3GlobalConfig.bMemstat ){ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 358 | sqlite3_mutex_enter(mem0.mutex); |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 359 | sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 360 | n = mallocWithAlarm(n, &p); |
| 361 | if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); |
| 362 | sqlite3_mutex_leave(mem0.mutex); |
| 363 | }else{ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 364 | p = sqlite3GlobalConfig.m.xMalloc(n); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 365 | } |
| 366 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 367 | scratchAllocOut = p!=0; |
| 368 | #endif |
| 369 | return p; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 370 | } |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 371 | void sqlite3ScratchFree(void *p){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 372 | if( p ){ |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 373 | |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 374 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 375 | /* Verify that no more than one scratch allocation per thread |
| 376 | ** is outstanding at one time. (This is only checked in the |
| 377 | ** single-threaded case since checking in the multi-threaded case |
| 378 | ** would be much more complicated.) */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 379 | assert( scratchAllocOut==1 ); |
| 380 | scratchAllocOut = 0; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 381 | #endif |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 382 | |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 383 | if( sqlite3GlobalConfig.pScratch==0 |
| 384 | || p<sqlite3GlobalConfig.pScratch |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 385 | || p>=(void*)mem0.aScratchFree ){ |
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); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 391 | sqlite3GlobalConfig.m.xFree(p); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 392 | sqlite3_mutex_leave(mem0.mutex); |
| 393 | }else{ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 394 | sqlite3GlobalConfig.m.xFree(p); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 395 | } |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 396 | }else{ |
| 397 | int i; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 398 | i = (int)((u8*)p - (u8*)sqlite3GlobalConfig.pScratch); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 399 | i /= sqlite3GlobalConfig.szScratch; |
| 400 | assert( i>=0 && i<sqlite3GlobalConfig.nScratch ); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 401 | sqlite3_mutex_enter(mem0.mutex); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 402 | assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch ); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 403 | mem0.aScratchFree[mem0.nScratchFree++] = i; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 404 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 405 | sqlite3_mutex_leave(mem0.mutex); |
| 406 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
| 410 | /* |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 411 | ** TRUE if p is a lookaside memory allocation from db |
| 412 | */ |
drh | 4150ebf | 2008-10-11 15:38:29 +0000 | [diff] [blame] | 413 | #ifndef SQLITE_OMIT_LOOKASIDE |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 414 | static int isLookaside(sqlite3 *db, void *p){ |
| 415 | return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd; |
| 416 | } |
drh | 4150ebf | 2008-10-11 15:38:29 +0000 | [diff] [blame] | 417 | #else |
| 418 | #define isLookaside(A,B) 0 |
| 419 | #endif |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 420 | |
| 421 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 422 | ** Return the size of a memory allocation previously obtained from |
| 423 | ** sqlite3Malloc() or sqlite3_malloc(). |
| 424 | */ |
| 425 | int sqlite3MallocSize(void *p){ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 426 | return sqlite3GlobalConfig.m.xSize(p); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 427 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 428 | int sqlite3DbMallocSize(sqlite3 *db, void *p){ |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 429 | if( p==0 ){ |
| 430 | return 0; |
| 431 | }else if( isLookaside(db, p) ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 432 | return db->lookaside.sz; |
| 433 | }else{ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 434 | return sqlite3GlobalConfig.m.xSize(p); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 437 | |
| 438 | /* |
| 439 | ** Free memory previously obtained from sqlite3Malloc(). |
| 440 | */ |
| 441 | void sqlite3_free(void *p){ |
| 442 | if( p==0 ) return; |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 443 | if( sqlite3GlobalConfig.bMemstat ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 444 | sqlite3_mutex_enter(mem0.mutex); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 445 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); |
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 | sqlite3_mutex_leave(mem0.mutex); |
| 448 | }else{ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 449 | sqlite3GlobalConfig.m.xFree(p); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
| 453 | /* |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 454 | ** Free memory that might be associated with a particular database |
| 455 | ** connection. |
| 456 | */ |
| 457 | void sqlite3DbFree(sqlite3 *db, void *p){ |
| 458 | if( isLookaside(db, p) ){ |
| 459 | LookasideSlot *pBuf = (LookasideSlot*)p; |
| 460 | pBuf->pNext = db->lookaside.pFree; |
| 461 | db->lookaside.pFree = pBuf; |
| 462 | db->lookaside.nOut--; |
| 463 | }else{ |
| 464 | sqlite3_free(p); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 469 | ** Change the size of an existing memory allocation |
| 470 | */ |
| 471 | void *sqlite3Realloc(void *pOld, int nBytes){ |
| 472 | int nOld, nNew; |
| 473 | void *pNew; |
| 474 | if( pOld==0 ){ |
| 475 | return sqlite3Malloc(nBytes); |
| 476 | } |
drh | 50b6568 | 2009-02-17 18:37:28 +0000 | [diff] [blame] | 477 | if( nBytes<=0 || NEVER(nBytes>=0x7fffff00) ){ |
| 478 | /* The NEVER(...) term is explained in comments on sqlite3Malloc() */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 479 | sqlite3_free(pOld); |
| 480 | return 0; |
| 481 | } |
| 482 | nOld = sqlite3MallocSize(pOld); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 483 | if( sqlite3GlobalConfig.bMemstat ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 484 | sqlite3_mutex_enter(mem0.mutex); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 485 | sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 486 | nNew = sqlite3GlobalConfig.m.xRoundup(nBytes); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 487 | if( nOld==nNew ){ |
| 488 | pNew = pOld; |
| 489 | }else{ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 490 | if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= |
| 491 | mem0.alarmThreshold ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 492 | sqlite3MallocAlarm(nNew-nOld); |
| 493 | } |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 494 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 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 | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 498 | } |
| 499 | if( pNew ){ |
drh | c702c7c | 2008-07-18 18:56:16 +0000 | [diff] [blame] | 500 | nNew = sqlite3MallocSize(pNew); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 501 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | sqlite3_mutex_leave(mem0.mutex); |
| 505 | }else{ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 506 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nBytes); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 507 | } |
| 508 | return pNew; |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | ** The public interface to sqlite3Realloc. Make sure that the memory |
| 513 | ** subsystem is initialized prior to invoking sqliteRealloc. |
| 514 | */ |
| 515 | void *sqlite3_realloc(void *pOld, int n){ |
| 516 | #ifndef SQLITE_OMIT_AUTOINIT |
| 517 | if( sqlite3_initialize() ) return 0; |
| 518 | #endif |
| 519 | return sqlite3Realloc(pOld, n); |
| 520 | } |
| 521 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 522 | |
| 523 | /* |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 524 | ** Allocate and zero memory. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 525 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 526 | void *sqlite3MallocZero(int n){ |
| 527 | void *p = sqlite3Malloc(n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 528 | if( p ){ |
| 529 | memset(p, 0, n); |
| 530 | } |
| 531 | return p; |
| 532 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 533 | |
| 534 | /* |
| 535 | ** Allocate and zero memory. If the allocation fails, make |
| 536 | ** the mallocFailed flag in the connection pointer. |
| 537 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 538 | void *sqlite3DbMallocZero(sqlite3 *db, int n){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 539 | void *p = sqlite3DbMallocRaw(db, n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 540 | if( p ){ |
| 541 | memset(p, 0, n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 542 | } |
| 543 | return p; |
| 544 | } |
| 545 | |
| 546 | /* |
| 547 | ** Allocate and zero memory. If the allocation fails, make |
| 548 | ** the mallocFailed flag in the connection pointer. |
drh | ddecae7 | 2008-10-11 17:35:16 +0000 | [diff] [blame] | 549 | ** |
| 550 | ** If db!=0 and db->mallocFailed is true (indicating a prior malloc |
| 551 | ** failure on the same database connection) then always return 0. |
| 552 | ** Hence for a particular database connection, once malloc starts |
| 553 | ** failing, it fails consistently until mallocFailed is reset. |
| 554 | ** This is an important assumption. There are many places in the |
| 555 | ** code that do things like this: |
| 556 | ** |
| 557 | ** int *a = (int*)sqlite3DbMallocRaw(db, 100); |
| 558 | ** int *b = (int*)sqlite3DbMallocRaw(db, 200); |
| 559 | ** if( b ) a[10] = 9; |
| 560 | ** |
| 561 | ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed |
| 562 | ** that all prior mallocs (ex: "a") worked too. |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 563 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 564 | void *sqlite3DbMallocRaw(sqlite3 *db, int n){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 565 | void *p; |
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; |
| 600 | if( db->mallocFailed==0 ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 601 | if( p==0 ){ |
| 602 | return sqlite3DbMallocRaw(db, n); |
| 603 | } |
| 604 | if( isLookaside(db, p) ){ |
| 605 | if( n<=db->lookaside.sz ){ |
| 606 | return p; |
| 607 | } |
| 608 | pNew = sqlite3DbMallocRaw(db, n); |
| 609 | if( pNew ){ |
| 610 | memcpy(pNew, p, db->lookaside.sz); |
| 611 | sqlite3DbFree(db, p); |
| 612 | } |
| 613 | }else{ |
| 614 | pNew = sqlite3_realloc(p, n); |
| 615 | if( !pNew ){ |
| 616 | db->mallocFailed = 1; |
| 617 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | return pNew; |
| 621 | } |
| 622 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 623 | /* |
| 624 | ** Attempt to reallocate p. If the reallocation fails, then free p |
| 625 | ** and set the mallocFailed flag in the database connection. |
| 626 | */ |
| 627 | void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 628 | void *pNew; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 629 | pNew = sqlite3DbRealloc(db, p, n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 630 | if( !pNew ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 631 | sqlite3DbFree(db, p); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 632 | } |
| 633 | return pNew; |
| 634 | } |
| 635 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 636 | /* |
| 637 | ** Make a copy of a string in memory obtained from sqliteMalloc(). These |
| 638 | ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This |
| 639 | ** is because when memory debugging is turned on, these two functions are |
| 640 | ** called via macros that record the current file and line number in the |
| 641 | ** ThreadData structure. |
| 642 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 643 | char *sqlite3DbStrDup(sqlite3 *db, const char *z){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 644 | char *zNew; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 645 | size_t n; |
| 646 | if( z==0 ){ |
| 647 | return 0; |
| 648 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 649 | n = (db ? sqlite3Strlen(db, z) : sqlite3Strlen30(z))+1; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 650 | assert( (n&0x7fffffff)==n ); |
| 651 | zNew = sqlite3DbMallocRaw(db, (int)n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 652 | if( zNew ){ |
| 653 | memcpy(zNew, z, n); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 654 | } |
| 655 | return zNew; |
| 656 | } |
| 657 | char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 658 | char *zNew; |
| 659 | if( z==0 ){ |
| 660 | return 0; |
| 661 | } |
| 662 | assert( (n&0x7fffffff)==n ); |
| 663 | zNew = sqlite3DbMallocRaw(db, n+1); |
| 664 | if( zNew ){ |
| 665 | memcpy(zNew, z, n); |
| 666 | zNew[n] = 0; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 667 | } |
| 668 | return zNew; |
| 669 | } |
| 670 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 671 | /* |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 672 | ** Create a string from the zFromat argument and the va_list that follows. |
| 673 | ** Store the string in memory obtained from sqliteMalloc() and make *pz |
| 674 | ** point to that string. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 675 | */ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 676 | void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 677 | va_list ap; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 678 | char *z; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 679 | |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 680 | va_start(ap, zFormat); |
| 681 | z = sqlite3VMPrintf(db, zFormat, ap); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 682 | va_end(ap); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 683 | sqlite3DbFree(db, *pz); |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 684 | *pz = z; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | |
| 688 | /* |
| 689 | ** This function must be called before exiting any API function (i.e. |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 690 | ** returning control to the user) that has called sqlite3_malloc or |
| 691 | ** sqlite3_realloc. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 692 | ** |
| 693 | ** The returned value is normally a copy of the second argument to this |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 694 | ** function. However, if a malloc() failure has occurred since the previous |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 695 | ** invocation SQLITE_NOMEM is returned instead. |
| 696 | ** |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 697 | ** 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] | 698 | ** then the connection error-code (the value returned by sqlite3_errcode()) |
| 699 | ** is set to SQLITE_NOMEM. |
| 700 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 701 | int sqlite3ApiExit(sqlite3* db, int rc){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 702 | /* If the db handle is not NULL, then we must hold the connection handle |
| 703 | ** mutex here. Otherwise the read (and possible write) of db->mallocFailed |
| 704 | ** is unsafe, as is the call to sqlite3Error(). |
| 705 | */ |
| 706 | assert( !db || sqlite3_mutex_held(db->mutex) ); |
danielk1977 | 98c2190 | 2008-09-23 16:41:29 +0000 | [diff] [blame] | 707 | if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 708 | sqlite3Error(db, SQLITE_NOMEM, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 709 | db->mallocFailed = 0; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 710 | rc = SQLITE_NOMEM; |
| 711 | } |
| 712 | return rc & (db ? db->errMask : 0xff); |
| 713 | } |