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 | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 15 | ** $Id: malloc.c,v 1.21 2008/06/19 00:16:08 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, |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 28 | sqlite3_int64 inUse, |
| 29 | int allocSize |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 30 | ){ |
| 31 | sqlite3_release_memory(allocSize); |
| 32 | } |
| 33 | |
| 34 | /* |
| 35 | ** Set the soft heap-size limit for the current thread. Passing a |
| 36 | ** zero or 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 ){ |
| 48 | sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit); |
| 49 | }else{ |
| 50 | sqlite3_memory_alarm(0, 0, 0); |
| 51 | } |
| 52 | overage = sqlite3_memory_used() - n; |
| 53 | if( overage>0 ){ |
| 54 | sqlite3_release_memory(overage); |
| 55 | } |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* |
| 59 | ** Release memory held by SQLite instances created by the current thread. |
| 60 | */ |
| 61 | int sqlite3_release_memory(int n){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 62 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 63 | int nRet = sqlite3VdbeReleaseMemory(n); |
| 64 | nRet += sqlite3PagerReleaseMemory(n-nRet); |
| 65 | return nRet; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 66 | #else |
| 67 | return SQLITE_OK; |
| 68 | #endif |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 69 | } |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 70 | |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 71 | /* |
| 72 | ** State information local to the memory allocation subsystem. |
| 73 | */ |
| 74 | static struct { |
| 75 | sqlite3_mutex *mutex; /* Mutex to serialize access */ |
| 76 | |
| 77 | /* |
| 78 | ** The alarm callback and its arguments. The mem0.mutex lock will |
| 79 | ** be held while the callback is running. Recursive calls into |
| 80 | ** the memory subsystem are allowed, but no new callbacks will be |
| 81 | ** issued. The alarmBusy variable is set to prevent recursive |
| 82 | ** callbacks. |
| 83 | */ |
| 84 | sqlite3_int64 alarmThreshold; |
| 85 | void (*alarmCallback)(void*, sqlite3_int64,int); |
| 86 | void *alarmArg; |
| 87 | int alarmBusy; |
| 88 | |
| 89 | /* |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 90 | ** Pointers to the end of sqlite3Config.pScratch and |
| 91 | ** sqlite3Config.pPage to a block of memory that records |
| 92 | ** which pages are available. |
| 93 | */ |
| 94 | u32 *aScratchFree; |
| 95 | u32 *aPageFree; |
| 96 | |
| 97 | /* Number of free pages for scratch and page-cache memory */ |
| 98 | u32 nScratchFree; |
| 99 | u32 nPageFree; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 100 | } mem0; |
| 101 | |
| 102 | /* |
| 103 | ** Initialize the memory allocation subsystem. |
| 104 | */ |
| 105 | int sqlite3MallocInit(void){ |
| 106 | if( sqlite3Config.m.xMalloc==0 ){ |
| 107 | sqlite3MemSetDefault(); |
| 108 | } |
| 109 | memset(&mem0, 0, sizeof(mem0)); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 110 | if( sqlite3Config.bCoreMutex ){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 111 | mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 112 | } |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 113 | if( sqlite3Config.pScratch && sqlite3Config.szScratch>=3000 |
| 114 | && sqlite3Config.nScratch>0 ){ |
| 115 | int i; |
| 116 | mem0.aScratchFree = (u32*)&((char*)sqlite3Config.pScratch) |
| 117 | [sqlite3Config.szScratch*sqlite3Config.nScratch]; |
| 118 | for(i=0; i<sqlite3Config.nScratch; i++){ mem0.aScratchFree[i] = i; } |
| 119 | mem0.nScratchFree = sqlite3Config.nScratch; |
| 120 | }else{ |
| 121 | sqlite3Config.pScratch = 0; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 122 | sqlite3Config.szScratch = 0; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 123 | } |
| 124 | if( sqlite3Config.pPage && sqlite3Config.szPage>=512 |
| 125 | && sqlite3Config.nPage>0 ){ |
| 126 | int i; |
| 127 | mem0.aPageFree = (u32*)&((char*)sqlite3Config.pPage) |
| 128 | [sqlite3Config.szPage*sqlite3Config.nPage]; |
| 129 | for(i=0; i<sqlite3Config.nPage; i++){ mem0.aPageFree[i] = i; } |
| 130 | mem0.nPageFree = sqlite3Config.nPage; |
| 131 | }else{ |
| 132 | sqlite3Config.pPage = 0; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 133 | sqlite3Config.szPage = 0; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 134 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 135 | return sqlite3Config.m.xInit(sqlite3Config.m.pAppData); |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | ** Deinitialize the memory allocation subsystem. |
| 140 | */ |
| 141 | void sqlite3MallocEnd(void){ |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 142 | sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData); |
| 143 | memset(&mem0, 0, sizeof(mem0)); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* |
| 147 | ** Return the amount of memory currently checked out. |
| 148 | */ |
| 149 | sqlite3_int64 sqlite3_memory_used(void){ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 150 | int n, mx; |
| 151 | sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0); |
| 152 | return (sqlite3_int64)n; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /* |
| 156 | ** Return the maximum amount of memory that has ever been |
| 157 | ** checked out since either the beginning of this process |
| 158 | ** or since the most recent reset. |
| 159 | */ |
| 160 | sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 161 | int n, mx; |
| 162 | sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag); |
| 163 | return (sqlite3_int64)mx; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* |
| 167 | ** Change the alarm callback |
| 168 | */ |
| 169 | int sqlite3_memory_alarm( |
| 170 | void(*xCallback)(void *pArg, sqlite3_int64 used,int N), |
| 171 | void *pArg, |
| 172 | sqlite3_int64 iThreshold |
| 173 | ){ |
| 174 | sqlite3_mutex_enter(mem0.mutex); |
| 175 | mem0.alarmCallback = xCallback; |
| 176 | mem0.alarmArg = pArg; |
| 177 | mem0.alarmThreshold = iThreshold; |
| 178 | sqlite3_mutex_leave(mem0.mutex); |
| 179 | return SQLITE_OK; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | ** Trigger the alarm |
| 184 | */ |
| 185 | static void sqlite3MallocAlarm(int nByte){ |
| 186 | void (*xCallback)(void*,sqlite3_int64,int); |
| 187 | sqlite3_int64 nowUsed; |
| 188 | void *pArg; |
| 189 | if( mem0.alarmCallback==0 || mem0.alarmBusy ) return; |
| 190 | mem0.alarmBusy = 1; |
| 191 | xCallback = mem0.alarmCallback; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 192 | nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 193 | pArg = mem0.alarmArg; |
| 194 | sqlite3_mutex_leave(mem0.mutex); |
| 195 | xCallback(pArg, nowUsed, nByte); |
| 196 | sqlite3_mutex_enter(mem0.mutex); |
| 197 | mem0.alarmBusy = 0; |
| 198 | } |
| 199 | |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 200 | /* |
| 201 | ** Do a memory allocation with statistics and alarms. Assume the |
| 202 | ** lock is already held. |
| 203 | */ |
| 204 | static int mallocWithAlarm(int n, void **pp){ |
| 205 | int nFull; |
| 206 | void *p; |
| 207 | assert( sqlite3_mutex_held(mem0.mutex) ); |
| 208 | nFull = sqlite3Config.m.xRoundup(n); |
| 209 | sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); |
| 210 | if( mem0.alarmCallback!=0 ){ |
| 211 | int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 212 | if( nUsed+nFull >= mem0.alarmThreshold ){ |
| 213 | sqlite3MallocAlarm(nFull); |
| 214 | } |
| 215 | } |
| 216 | if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ |
| 217 | p = 0; |
| 218 | }else{ |
| 219 | p = sqlite3Config.m.xMalloc(nFull); |
| 220 | if( p==0 ){ |
| 221 | sqlite3MallocAlarm(nFull); |
| 222 | p = malloc(nFull); |
| 223 | } |
| 224 | } |
| 225 | if( p ) sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); |
| 226 | *pp = p; |
| 227 | return nFull; |
| 228 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 229 | |
| 230 | /* |
| 231 | ** Allocate memory. This routine is like sqlite3_malloc() except that it |
| 232 | ** assumes the memory subsystem has already been initialized. |
| 233 | */ |
| 234 | void *sqlite3Malloc(int n){ |
| 235 | void *p; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 236 | if( n<=0 ){ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 237 | p = 0; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 238 | }else if( sqlite3Config.bMemstat ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 239 | sqlite3_mutex_enter(mem0.mutex); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 240 | mallocWithAlarm(n, &p); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 241 | sqlite3_mutex_leave(mem0.mutex); |
| 242 | }else{ |
| 243 | p = sqlite3Config.m.xMalloc(n); |
| 244 | } |
| 245 | return p; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | ** This version of the memory allocation is for use by the application. |
| 250 | ** First make sure the memory subsystem is initialized, then do the |
| 251 | ** allocation. |
| 252 | */ |
| 253 | void *sqlite3_malloc(int n){ |
| 254 | #ifndef SQLITE_OMIT_AUTOINIT |
| 255 | if( sqlite3_initialize() ) return 0; |
| 256 | #endif |
| 257 | return sqlite3Malloc(n); |
| 258 | } |
| 259 | |
| 260 | /* |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 261 | ** Each thread may only have a single outstanding allocation from |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 262 | ** xScratchMalloc(). We verify this constraint in the single-threaded |
| 263 | ** case by setting scratchAllocOut to 1 when an allocation |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 264 | ** is outstanding clearing it when the allocation is freed. |
| 265 | */ |
| 266 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 267 | static int scratchAllocOut = 0; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 268 | #endif |
| 269 | |
| 270 | |
| 271 | /* |
| 272 | ** Allocate memory that is to be used and released right away. |
| 273 | ** This routine is similar to alloca() in that it is not intended |
| 274 | ** for situations where the memory might be held long-term. This |
| 275 | ** routine is intended to get memory to old large transient data |
| 276 | ** structures that would not normally fit on the stack of an |
| 277 | ** embedded processor. |
| 278 | */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 279 | void *sqlite3ScratchMalloc(int n){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 280 | void *p; |
| 281 | assert( n>0 ); |
| 282 | if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ |
| 283 | return 0; |
| 284 | } |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 285 | |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 286 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 287 | /* Verify that no more than one scratch allocation per thread |
| 288 | ** is outstanding at one time. (This is only checked in the |
| 289 | ** single-threaded case since checking in the multi-threaded case |
| 290 | ** would be much more complicated.) */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 291 | assert( scratchAllocOut==0 ); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 292 | #endif |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 293 | |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 294 | if( sqlite3Config.szScratch<n ){ |
| 295 | goto scratch_overflow; |
| 296 | }else{ |
| 297 | sqlite3_mutex_enter(mem0.mutex); |
| 298 | if( mem0.nScratchFree==0 ){ |
| 299 | sqlite3_mutex_leave(mem0.mutex); |
| 300 | goto scratch_overflow; |
| 301 | }else{ |
| 302 | int i; |
| 303 | i = mem0.aScratchFree[--mem0.nScratchFree]; |
| 304 | sqlite3_mutex_leave(mem0.mutex); |
| 305 | i *= sqlite3Config.szScratch; |
| 306 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); |
| 307 | p = (void*)&((char*)sqlite3Config.pScratch)[i]; |
| 308 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 309 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 310 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 311 | scratchAllocOut = p!=0; |
| 312 | #endif |
| 313 | |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 314 | return p; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 315 | |
| 316 | scratch_overflow: |
| 317 | if( sqlite3Config.bMemstat ){ |
| 318 | sqlite3_mutex_enter(mem0.mutex); |
| 319 | n = mallocWithAlarm(n, &p); |
| 320 | if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); |
| 321 | sqlite3_mutex_leave(mem0.mutex); |
| 322 | }else{ |
| 323 | p = sqlite3Config.m.xMalloc(n); |
| 324 | } |
| 325 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 326 | scratchAllocOut = p!=0; |
| 327 | #endif |
| 328 | return p; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 329 | } |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 330 | void sqlite3ScratchFree(void *p){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 331 | if( p ){ |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 332 | |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 333 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 334 | /* Verify that no more than one scratch allocation per thread |
| 335 | ** is outstanding at one time. (This is only checked in the |
| 336 | ** single-threaded case since checking in the multi-threaded case |
| 337 | ** would be much more complicated.) */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 338 | assert( scratchAllocOut==1 ); |
| 339 | scratchAllocOut = 0; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 340 | #endif |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 341 | |
| 342 | if( sqlite3Config.pScratch==0 |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 343 | || p<sqlite3Config.pScratch |
| 344 | || p>=(void*)mem0.aScratchFree ){ |
| 345 | if( sqlite3Config.bMemstat ){ |
| 346 | int iSize = sqlite3MallocSize(p); |
| 347 | sqlite3_mutex_enter(mem0.mutex); |
| 348 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize); |
| 349 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); |
| 350 | sqlite3Config.m.xFree(p); |
| 351 | sqlite3_mutex_leave(mem0.mutex); |
| 352 | }else{ |
| 353 | sqlite3Config.m.xFree(p); |
| 354 | } |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 355 | }else{ |
| 356 | int i; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 357 | i = p - sqlite3Config.pScratch; |
| 358 | i /= sqlite3Config.szScratch; |
| 359 | assert( i>=0 && i<sqlite3Config.nScratch ); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 360 | sqlite3_mutex_enter(mem0.mutex); |
| 361 | assert( mem0.nScratchFree<sqlite3Config.nScratch ); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 362 | mem0.aScratchFree[mem0.nScratchFree++] = i; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 363 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 364 | sqlite3_mutex_leave(mem0.mutex); |
| 365 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
| 369 | /* |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 370 | ** Allocate memory to be used by the page cache. Make use of the |
| 371 | ** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one |
| 372 | ** and that memory is of the right size and is not completely |
| 373 | ** consumed. Otherwise, failover to sqlite3Malloc(). |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 374 | */ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 375 | void *sqlite3PageMalloc(int n){ |
| 376 | void *p; |
| 377 | assert( n>0 ); |
| 378 | assert( (n & (n-1))==0 ); |
| 379 | assert( n>=512 && n<=32768 ); |
| 380 | if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | if( sqlite3Config.szPage<n ){ |
| 385 | goto page_overflow; |
| 386 | }else{ |
| 387 | sqlite3_mutex_enter(mem0.mutex); |
| 388 | if( mem0.nPageFree==0 ){ |
| 389 | sqlite3_mutex_leave(mem0.mutex); |
| 390 | goto page_overflow; |
| 391 | }else{ |
| 392 | int i; |
| 393 | i = mem0.aPageFree[--mem0.nPageFree]; |
| 394 | sqlite3_mutex_leave(mem0.mutex); |
| 395 | i *= sqlite3Config.szPage; |
| 396 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1); |
| 397 | p = (void*)&((char*)sqlite3Config.pPage)[i]; |
| 398 | } |
| 399 | } |
| 400 | return p; |
| 401 | |
| 402 | page_overflow: |
| 403 | if( sqlite3Config.bMemstat ){ |
| 404 | sqlite3_mutex_enter(mem0.mutex); |
| 405 | n = mallocWithAlarm(n, &p); |
| 406 | if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n); |
| 407 | sqlite3_mutex_leave(mem0.mutex); |
| 408 | }else{ |
| 409 | p = sqlite3Config.m.xMalloc(n); |
| 410 | } |
| 411 | return p; |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 412 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 413 | void sqlite3PageFree(void *p){ |
| 414 | if( p ){ |
| 415 | if( sqlite3Config.pPage==0 |
| 416 | || p<sqlite3Config.pPage |
| 417 | || p>=(void*)mem0.aPageFree ){ |
| 418 | if( sqlite3Config.bMemstat ){ |
| 419 | int iSize = sqlite3MallocSize(p); |
| 420 | sqlite3_mutex_enter(mem0.mutex); |
| 421 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize); |
| 422 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); |
| 423 | sqlite3Config.m.xFree(p); |
| 424 | sqlite3_mutex_leave(mem0.mutex); |
| 425 | }else{ |
| 426 | sqlite3Config.m.xFree(p); |
| 427 | } |
| 428 | }else{ |
| 429 | int i; |
| 430 | i = p - sqlite3Config.pPage; |
| 431 | i /= sqlite3Config.szPage; |
| 432 | assert( i>=0 && i<sqlite3Config.nPage ); |
| 433 | sqlite3_mutex_enter(mem0.mutex); |
| 434 | assert( mem0.nPageFree<sqlite3Config.nPage ); |
| 435 | mem0.aPageFree[mem0.nPageFree++] = i; |
| 436 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1); |
| 437 | sqlite3_mutex_leave(mem0.mutex); |
| 438 | } |
| 439 | } |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 443 | ** Return the size of a memory allocation previously obtained from |
| 444 | ** sqlite3Malloc() or sqlite3_malloc(). |
| 445 | */ |
| 446 | int sqlite3MallocSize(void *p){ |
| 447 | return sqlite3Config.m.xSize(p); |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | ** Free memory previously obtained from sqlite3Malloc(). |
| 452 | */ |
| 453 | void sqlite3_free(void *p){ |
| 454 | if( p==0 ) return; |
| 455 | if( sqlite3Config.bMemstat ){ |
| 456 | sqlite3_mutex_enter(mem0.mutex); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 457 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 458 | sqlite3Config.m.xFree(p); |
| 459 | sqlite3_mutex_leave(mem0.mutex); |
| 460 | }else{ |
| 461 | sqlite3Config.m.xFree(p); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | ** Change the size of an existing memory allocation |
| 467 | */ |
| 468 | void *sqlite3Realloc(void *pOld, int nBytes){ |
| 469 | int nOld, nNew; |
| 470 | void *pNew; |
| 471 | if( pOld==0 ){ |
| 472 | return sqlite3Malloc(nBytes); |
| 473 | } |
| 474 | if( nBytes<=0 ){ |
| 475 | sqlite3_free(pOld); |
| 476 | return 0; |
| 477 | } |
| 478 | nOld = sqlite3MallocSize(pOld); |
| 479 | if( sqlite3Config.bMemstat ){ |
| 480 | sqlite3_mutex_enter(mem0.mutex); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 481 | sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 482 | nNew = sqlite3Config.m.xRoundup(nBytes); |
| 483 | if( nOld==nNew ){ |
| 484 | pNew = pOld; |
| 485 | }else{ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 486 | if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= |
| 487 | mem0.alarmThreshold ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 488 | sqlite3MallocAlarm(nNew-nOld); |
| 489 | } |
| 490 | if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ |
| 491 | pNew = 0; |
| 492 | }else{ |
| 493 | pNew = sqlite3Config.m.xRealloc(pOld, nNew); |
| 494 | if( pNew==0 ){ |
| 495 | sqlite3MallocAlarm(nBytes); |
| 496 | pNew = sqlite3Config.m.xRealloc(pOld, nNew); |
| 497 | } |
| 498 | } |
| 499 | if( pNew ){ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame^] | 500 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | sqlite3_mutex_leave(mem0.mutex); |
| 504 | }else{ |
| 505 | pNew = sqlite3Config.m.xRealloc(pOld, nBytes); |
| 506 | } |
| 507 | return pNew; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | ** The public interface to sqlite3Realloc. Make sure that the memory |
| 512 | ** subsystem is initialized prior to invoking sqliteRealloc. |
| 513 | */ |
| 514 | void *sqlite3_realloc(void *pOld, int n){ |
| 515 | #ifndef SQLITE_OMIT_AUTOINIT |
| 516 | if( sqlite3_initialize() ) return 0; |
| 517 | #endif |
| 518 | return sqlite3Realloc(pOld, n); |
| 519 | } |
| 520 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 521 | |
| 522 | /* |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 523 | ** Allocate and zero memory. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 524 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 525 | void *sqlite3MallocZero(int n){ |
| 526 | void *p = sqlite3Malloc(n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 527 | if( p ){ |
| 528 | memset(p, 0, n); |
| 529 | } |
| 530 | return p; |
| 531 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 532 | |
| 533 | /* |
| 534 | ** Allocate and zero memory. If the allocation fails, make |
| 535 | ** the mallocFailed flag in the connection pointer. |
| 536 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 537 | void *sqlite3DbMallocZero(sqlite3 *db, int n){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 538 | void *p = sqlite3DbMallocRaw(db, n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 539 | if( p ){ |
| 540 | memset(p, 0, n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 541 | } |
| 542 | return p; |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | ** Allocate and zero memory. If the allocation fails, make |
| 547 | ** the mallocFailed flag in the connection pointer. |
| 548 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 549 | void *sqlite3DbMallocRaw(sqlite3 *db, int n){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 550 | void *p = 0; |
| 551 | if( !db || db->mallocFailed==0 ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 552 | p = sqlite3Malloc(n); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 553 | if( !p && db ){ |
| 554 | db->mallocFailed = 1; |
| 555 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 556 | } |
| 557 | return p; |
| 558 | } |
| 559 | |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 560 | /* |
| 561 | ** Resize the block of memory pointed to by p to n bytes. If the |
| 562 | ** resize fails, set the mallocFailed flag inthe connection object. |
| 563 | */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 564 | void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ |
| 565 | void *pNew = 0; |
| 566 | if( db->mallocFailed==0 ){ |
| 567 | pNew = sqlite3_realloc(p, n); |
| 568 | if( !pNew ){ |
| 569 | db->mallocFailed = 1; |
| 570 | } |
| 571 | } |
| 572 | return pNew; |
| 573 | } |
| 574 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 575 | /* |
| 576 | ** Attempt to reallocate p. If the reallocation fails, then free p |
| 577 | ** and set the mallocFailed flag in the database connection. |
| 578 | */ |
| 579 | void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 580 | void *pNew; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 581 | pNew = sqlite3DbRealloc(db, p, n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 582 | if( !pNew ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 583 | sqlite3_free(p); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 584 | } |
| 585 | return pNew; |
| 586 | } |
| 587 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 588 | /* |
| 589 | ** Make a copy of a string in memory obtained from sqliteMalloc(). These |
| 590 | ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This |
| 591 | ** is because when memory debugging is turned on, these two functions are |
| 592 | ** called via macros that record the current file and line number in the |
| 593 | ** ThreadData structure. |
| 594 | */ |
| 595 | char *sqlite3StrDup(const char *z){ |
| 596 | char *zNew; |
| 597 | int n; |
| 598 | if( z==0 ) return 0; |
| 599 | n = strlen(z)+1; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 600 | zNew = sqlite3Malloc(n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 601 | if( zNew ) memcpy(zNew, z, n); |
| 602 | return zNew; |
| 603 | } |
| 604 | char *sqlite3StrNDup(const char *z, int n){ |
| 605 | char *zNew; |
| 606 | if( z==0 ) return 0; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 607 | zNew = sqlite3Malloc(n+1); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 608 | if( zNew ){ |
| 609 | memcpy(zNew, z, n); |
| 610 | zNew[n] = 0; |
| 611 | } |
| 612 | return zNew; |
| 613 | } |
| 614 | |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 615 | char *sqlite3DbStrDup(sqlite3 *db, const char *z){ |
| 616 | char *zNew = sqlite3StrDup(z); |
| 617 | if( z && !zNew ){ |
| 618 | db->mallocFailed = 1; |
| 619 | } |
| 620 | return zNew; |
| 621 | } |
| 622 | char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ |
| 623 | char *zNew = sqlite3StrNDup(z, n); |
| 624 | if( z && !zNew ){ |
| 625 | db->mallocFailed = 1; |
| 626 | } |
| 627 | return zNew; |
| 628 | } |
| 629 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 630 | /* |
| 631 | ** Create a string from the 2nd and subsequent arguments (up to the |
| 632 | ** first NULL argument), store the string in memory obtained from |
| 633 | ** sqliteMalloc() and make the pointer indicated by the 1st argument |
| 634 | ** point to that string. The 1st argument must either be NULL or |
| 635 | ** point to memory obtained from sqliteMalloc(). |
| 636 | */ |
| 637 | void sqlite3SetString(char **pz, ...){ |
| 638 | va_list ap; |
| 639 | int nByte; |
| 640 | const char *z; |
| 641 | char *zResult; |
| 642 | |
| 643 | assert( pz!=0 ); |
| 644 | nByte = 1; |
| 645 | va_start(ap, pz); |
| 646 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 647 | nByte += strlen(z); |
| 648 | } |
| 649 | va_end(ap); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 650 | sqlite3_free(*pz); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 651 | *pz = zResult = sqlite3Malloc(nByte); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 652 | if( zResult==0 ){ |
| 653 | return; |
| 654 | } |
| 655 | *zResult = 0; |
| 656 | va_start(ap, pz); |
| 657 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 658 | int n = strlen(z); |
| 659 | memcpy(zResult, z, n); |
| 660 | zResult += n; |
| 661 | } |
| 662 | zResult[0] = 0; |
| 663 | va_end(ap); |
| 664 | } |
| 665 | |
| 666 | |
| 667 | /* |
| 668 | ** This function must be called before exiting any API function (i.e. |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 669 | ** returning control to the user) that has called sqlite3_malloc or |
| 670 | ** sqlite3_realloc. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 671 | ** |
| 672 | ** The returned value is normally a copy of the second argument to this |
| 673 | ** function. However, if a malloc() failure has occured since the previous |
| 674 | ** invocation SQLITE_NOMEM is returned instead. |
| 675 | ** |
| 676 | ** If the first argument, db, is not NULL and a malloc() error has occured, |
| 677 | ** then the connection error-code (the value returned by sqlite3_errcode()) |
| 678 | ** is set to SQLITE_NOMEM. |
| 679 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 680 | int sqlite3ApiExit(sqlite3* db, int rc){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 681 | /* If the db handle is not NULL, then we must hold the connection handle |
| 682 | ** mutex here. Otherwise the read (and possible write) of db->mallocFailed |
| 683 | ** is unsafe, as is the call to sqlite3Error(). |
| 684 | */ |
| 685 | assert( !db || sqlite3_mutex_held(db->mutex) ); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 686 | if( db && db->mallocFailed ){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 687 | sqlite3Error(db, SQLITE_NOMEM, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 688 | db->mallocFailed = 0; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 689 | rc = SQLITE_NOMEM; |
| 690 | } |
| 691 | return rc & (db ? db->errMask : 0xff); |
| 692 | } |