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