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 | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame^] | 15 | ** $Id: malloc.c,v 1.20 2008/06/18 18:12:04 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; |
| 100 | |
| 101 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 102 | ** Performance statistics |
| 103 | */ |
| 104 | sqlite3_int64 nowUsed; /* Main memory currently in use */ |
| 105 | sqlite3_int64 mxUsed; /* Highwater mark for nowUsed */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 106 | int mxReq; /* Max request size for ordinary mallocs */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 107 | int mxScratchReq; /* Max request size for xTemp mallocs */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 108 | } mem0; |
| 109 | |
| 110 | /* |
| 111 | ** Initialize the memory allocation subsystem. |
| 112 | */ |
| 113 | int sqlite3MallocInit(void){ |
| 114 | if( sqlite3Config.m.xMalloc==0 ){ |
| 115 | sqlite3MemSetDefault(); |
| 116 | } |
| 117 | memset(&mem0, 0, sizeof(mem0)); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame^] | 118 | if( sqlite3Config.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 | } |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame^] | 121 | if( sqlite3Config.pScratch && sqlite3Config.szScratch>=3000 |
| 122 | && sqlite3Config.nScratch>0 ){ |
| 123 | int i; |
| 124 | mem0.aScratchFree = (u32*)&((char*)sqlite3Config.pScratch) |
| 125 | [sqlite3Config.szScratch*sqlite3Config.nScratch]; |
| 126 | for(i=0; i<sqlite3Config.nScratch; i++){ mem0.aScratchFree[i] = i; } |
| 127 | mem0.nScratchFree = sqlite3Config.nScratch; |
| 128 | }else{ |
| 129 | sqlite3Config.pScratch = 0; |
| 130 | } |
| 131 | if( sqlite3Config.pPage && sqlite3Config.szPage>=512 |
| 132 | && sqlite3Config.nPage>0 ){ |
| 133 | int i; |
| 134 | mem0.aPageFree = (u32*)&((char*)sqlite3Config.pPage) |
| 135 | [sqlite3Config.szPage*sqlite3Config.nPage]; |
| 136 | for(i=0; i<sqlite3Config.nPage; i++){ mem0.aPageFree[i] = i; } |
| 137 | mem0.nPageFree = sqlite3Config.nPage; |
| 138 | }else{ |
| 139 | sqlite3Config.pPage = 0; |
| 140 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 141 | return sqlite3Config.m.xInit(sqlite3Config.m.pAppData); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | ** Deinitialize the memory allocation subsystem. |
| 146 | */ |
| 147 | void sqlite3MallocEnd(void){ |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame^] | 148 | sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData); |
| 149 | memset(&mem0, 0, sizeof(mem0)); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* |
| 153 | ** Return the amount of memory currently checked out. |
| 154 | */ |
| 155 | sqlite3_int64 sqlite3_memory_used(void){ |
| 156 | sqlite3_int64 n; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame^] | 157 | sqlite3_initialize(); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 158 | sqlite3_mutex_enter(mem0.mutex); |
| 159 | n = mem0.nowUsed; |
| 160 | sqlite3_mutex_leave(mem0.mutex); |
| 161 | return n; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | ** Return the maximum amount of memory that has ever been |
| 166 | ** checked out since either the beginning of this process |
| 167 | ** or since the most recent reset. |
| 168 | */ |
| 169 | sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ |
| 170 | sqlite3_int64 n; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame^] | 171 | sqlite3_initialize(); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 172 | sqlite3_mutex_enter(mem0.mutex); |
| 173 | n = mem0.mxUsed; |
| 174 | if( resetFlag ){ |
| 175 | mem0.mxUsed = mem0.nowUsed; |
| 176 | } |
| 177 | sqlite3_mutex_leave(mem0.mutex); |
| 178 | return n; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | ** Change the alarm callback |
| 183 | */ |
| 184 | int sqlite3_memory_alarm( |
| 185 | void(*xCallback)(void *pArg, sqlite3_int64 used,int N), |
| 186 | void *pArg, |
| 187 | sqlite3_int64 iThreshold |
| 188 | ){ |
| 189 | sqlite3_mutex_enter(mem0.mutex); |
| 190 | mem0.alarmCallback = xCallback; |
| 191 | mem0.alarmArg = pArg; |
| 192 | mem0.alarmThreshold = iThreshold; |
| 193 | sqlite3_mutex_leave(mem0.mutex); |
| 194 | return SQLITE_OK; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | ** Trigger the alarm |
| 199 | */ |
| 200 | static void sqlite3MallocAlarm(int nByte){ |
| 201 | void (*xCallback)(void*,sqlite3_int64,int); |
| 202 | sqlite3_int64 nowUsed; |
| 203 | void *pArg; |
| 204 | if( mem0.alarmCallback==0 || mem0.alarmBusy ) return; |
| 205 | mem0.alarmBusy = 1; |
| 206 | xCallback = mem0.alarmCallback; |
| 207 | nowUsed = mem0.nowUsed; |
| 208 | pArg = mem0.alarmArg; |
| 209 | sqlite3_mutex_leave(mem0.mutex); |
| 210 | xCallback(pArg, nowUsed, nByte); |
| 211 | sqlite3_mutex_enter(mem0.mutex); |
| 212 | mem0.alarmBusy = 0; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | /* |
| 217 | ** Allocate memory. This routine is like sqlite3_malloc() except that it |
| 218 | ** assumes the memory subsystem has already been initialized. |
| 219 | */ |
| 220 | void *sqlite3Malloc(int n){ |
| 221 | void *p; |
| 222 | int nFull; |
| 223 | if( n<=0 ){ |
| 224 | return 0; |
| 225 | }else if( sqlite3Config.bMemstat ){ |
| 226 | nFull = sqlite3Config.m.xRoundup(n); |
| 227 | sqlite3_mutex_enter(mem0.mutex); |
| 228 | if( n>mem0.mxReq ) mem0.mxReq = n; |
| 229 | if( mem0.alarmCallback!=0 && mem0.nowUsed+nFull>=mem0.alarmThreshold ){ |
| 230 | sqlite3MallocAlarm(nFull); |
| 231 | } |
| 232 | if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ |
| 233 | p = 0; |
| 234 | }else{ |
| 235 | p = sqlite3Config.m.xMalloc(nFull); |
| 236 | if( p==0 ){ |
| 237 | sqlite3MallocAlarm(nFull); |
| 238 | p = malloc(nFull); |
| 239 | } |
| 240 | } |
| 241 | if( p ){ |
| 242 | mem0.nowUsed += nFull; |
| 243 | if( mem0.nowUsed>mem0.mxUsed ){ |
| 244 | mem0.mxUsed = mem0.nowUsed; |
| 245 | } |
| 246 | } |
| 247 | sqlite3_mutex_leave(mem0.mutex); |
| 248 | }else{ |
| 249 | p = sqlite3Config.m.xMalloc(n); |
| 250 | } |
| 251 | return p; |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | ** This version of the memory allocation is for use by the application. |
| 256 | ** First make sure the memory subsystem is initialized, then do the |
| 257 | ** allocation. |
| 258 | */ |
| 259 | void *sqlite3_malloc(int n){ |
| 260 | #ifndef SQLITE_OMIT_AUTOINIT |
| 261 | if( sqlite3_initialize() ) return 0; |
| 262 | #endif |
| 263 | return sqlite3Malloc(n); |
| 264 | } |
| 265 | |
| 266 | /* |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 267 | ** Each thread may only have a single outstanding allocation from |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 268 | ** xScratchMalloc(). We verify this constraint in the single-threaded |
| 269 | ** case by setting scratchAllocOut to 1 when an allocation |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 270 | ** is outstanding clearing it when the allocation is freed. |
| 271 | */ |
| 272 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 273 | static int scratchAllocOut = 0; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 274 | #endif |
| 275 | |
| 276 | |
| 277 | /* |
| 278 | ** Allocate memory that is to be used and released right away. |
| 279 | ** This routine is similar to alloca() in that it is not intended |
| 280 | ** for situations where the memory might be held long-term. This |
| 281 | ** routine is intended to get memory to old large transient data |
| 282 | ** structures that would not normally fit on the stack of an |
| 283 | ** embedded processor. |
| 284 | */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 285 | void *sqlite3ScratchMalloc(int n){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 286 | void *p; |
| 287 | assert( n>0 ); |
| 288 | if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ |
| 289 | return 0; |
| 290 | } |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame^] | 291 | |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 292 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame^] | 293 | /* Verify that no more than one scratch allocation per thread |
| 294 | ** is outstanding at one time. (This is only checked in the |
| 295 | ** single-threaded case since checking in the multi-threaded case |
| 296 | ** would be much more complicated.) */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 297 | assert( scratchAllocOut==0 ); |
| 298 | scratchAllocOut = 1; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 299 | #endif |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame^] | 300 | |
| 301 | sqlite3_mutex_enter(mem0.mutex); |
| 302 | if( n>mem0.mxScratchReq ) mem0.mxScratchReq = n; |
| 303 | if( mem0.nScratchFree==0 || sqlite3Config.szScratch>=n ){ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 304 | p = sqlite3Config.m.xMalloc(n); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 305 | }else{ |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame^] | 306 | int i; |
| 307 | i = mem0.aScratchFree[--mem0.nScratchFree]; |
| 308 | i *= sqlite3Config.szScratch; |
| 309 | p = (void*)&((char*)sqlite3Config.pScratch)[i]; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 310 | } |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame^] | 311 | sqlite3_mutex_leave(mem0.mutex); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 312 | return p; |
| 313 | } |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 314 | void sqlite3ScratchFree(void *p){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 315 | if( p ){ |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame^] | 316 | |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 317 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame^] | 318 | /* Verify that no more than one scratch allocation per thread |
| 319 | ** is outstanding at one time. (This is only checked in the |
| 320 | ** single-threaded case since checking in the multi-threaded case |
| 321 | ** would be much more complicated.) */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 322 | assert( scratchAllocOut==1 ); |
| 323 | scratchAllocOut = 0; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 324 | #endif |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame^] | 325 | |
| 326 | if( sqlite3Config.pScratch==0 |
| 327 | || p<sqlite3Config.pScratch |
| 328 | || p>=(void*)mem0.aScratchFree ){ |
| 329 | sqlite3Config.m.xFree(p); |
| 330 | }else{ |
| 331 | int i; |
| 332 | sqlite3_mutex_enter(mem0.mutex); |
| 333 | assert( mem0.nScratchFree<sqlite3Config.nScratch ); |
| 334 | i = p - sqlite3Config.pScratch; |
| 335 | i /= sqlite3Config.szScratch; |
| 336 | assert( i>=0 && i<sqlite3Config.nScratch ); |
| 337 | mem0.aScratchFree[mem0.nScratchFree++] = i; |
| 338 | sqlite3_mutex_leave(mem0.mutex); |
| 339 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
| 343 | /* |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 344 | ** Place holders for the page-cache memory allocator. |
| 345 | */ |
| 346 | void *sqlite3PageMalloc(int iSize){ |
| 347 | return sqlite3Malloc(iSize); |
| 348 | } |
| 349 | void sqlite3PageFree(void *pOld){ |
| 350 | sqlite3_free(pOld); |
| 351 | } |
| 352 | |
| 353 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 354 | ** Return the size of a memory allocation previously obtained from |
| 355 | ** sqlite3Malloc() or sqlite3_malloc(). |
| 356 | */ |
| 357 | int sqlite3MallocSize(void *p){ |
| 358 | return sqlite3Config.m.xSize(p); |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | ** Free memory previously obtained from sqlite3Malloc(). |
| 363 | */ |
| 364 | void sqlite3_free(void *p){ |
| 365 | if( p==0 ) return; |
| 366 | if( sqlite3Config.bMemstat ){ |
| 367 | sqlite3_mutex_enter(mem0.mutex); |
| 368 | mem0.nowUsed -= sqlite3MallocSize(p); |
| 369 | sqlite3Config.m.xFree(p); |
| 370 | sqlite3_mutex_leave(mem0.mutex); |
| 371 | }else{ |
| 372 | sqlite3Config.m.xFree(p); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | ** Change the size of an existing memory allocation |
| 378 | */ |
| 379 | void *sqlite3Realloc(void *pOld, int nBytes){ |
| 380 | int nOld, nNew; |
| 381 | void *pNew; |
| 382 | if( pOld==0 ){ |
| 383 | return sqlite3Malloc(nBytes); |
| 384 | } |
| 385 | if( nBytes<=0 ){ |
| 386 | sqlite3_free(pOld); |
| 387 | return 0; |
| 388 | } |
| 389 | nOld = sqlite3MallocSize(pOld); |
| 390 | if( sqlite3Config.bMemstat ){ |
| 391 | sqlite3_mutex_enter(mem0.mutex); |
| 392 | if( nBytes>mem0.mxReq ) mem0.mxReq = nBytes; |
| 393 | nNew = sqlite3Config.m.xRoundup(nBytes); |
| 394 | if( nOld==nNew ){ |
| 395 | pNew = pOld; |
| 396 | }else{ |
| 397 | if( mem0.nowUsed+nNew-nOld>=mem0.alarmThreshold ){ |
| 398 | sqlite3MallocAlarm(nNew-nOld); |
| 399 | } |
| 400 | if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ |
| 401 | pNew = 0; |
| 402 | }else{ |
| 403 | pNew = sqlite3Config.m.xRealloc(pOld, nNew); |
| 404 | if( pNew==0 ){ |
| 405 | sqlite3MallocAlarm(nBytes); |
| 406 | pNew = sqlite3Config.m.xRealloc(pOld, nNew); |
| 407 | } |
| 408 | } |
| 409 | if( pNew ){ |
| 410 | mem0.nowUsed += nNew-nOld; |
| 411 | if( mem0.nowUsed>mem0.mxUsed ){ |
| 412 | mem0.mxUsed = mem0.nowUsed; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | sqlite3_mutex_leave(mem0.mutex); |
| 417 | }else{ |
| 418 | pNew = sqlite3Config.m.xRealloc(pOld, nBytes); |
| 419 | } |
| 420 | return pNew; |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | ** The public interface to sqlite3Realloc. Make sure that the memory |
| 425 | ** subsystem is initialized prior to invoking sqliteRealloc. |
| 426 | */ |
| 427 | void *sqlite3_realloc(void *pOld, int n){ |
| 428 | #ifndef SQLITE_OMIT_AUTOINIT |
| 429 | if( sqlite3_initialize() ) return 0; |
| 430 | #endif |
| 431 | return sqlite3Realloc(pOld, n); |
| 432 | } |
| 433 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 434 | |
| 435 | /* |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 436 | ** Allocate and zero memory. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 437 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 438 | void *sqlite3MallocZero(int n){ |
| 439 | void *p = sqlite3Malloc(n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 440 | if( p ){ |
| 441 | memset(p, 0, n); |
| 442 | } |
| 443 | return p; |
| 444 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 445 | |
| 446 | /* |
| 447 | ** Allocate and zero memory. If the allocation fails, make |
| 448 | ** the mallocFailed flag in the connection pointer. |
| 449 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 450 | void *sqlite3DbMallocZero(sqlite3 *db, int n){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 451 | void *p = sqlite3DbMallocRaw(db, n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 452 | if( p ){ |
| 453 | memset(p, 0, n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 454 | } |
| 455 | return p; |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | ** Allocate and zero memory. If the allocation fails, make |
| 460 | ** the mallocFailed flag in the connection pointer. |
| 461 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 462 | void *sqlite3DbMallocRaw(sqlite3 *db, int n){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 463 | void *p = 0; |
| 464 | if( !db || db->mallocFailed==0 ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 465 | p = sqlite3Malloc(n); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 466 | if( !p && db ){ |
| 467 | db->mallocFailed = 1; |
| 468 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 469 | } |
| 470 | return p; |
| 471 | } |
| 472 | |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 473 | /* |
| 474 | ** Resize the block of memory pointed to by p to n bytes. If the |
| 475 | ** resize fails, set the mallocFailed flag inthe connection object. |
| 476 | */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 477 | void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ |
| 478 | void *pNew = 0; |
| 479 | if( db->mallocFailed==0 ){ |
| 480 | pNew = sqlite3_realloc(p, n); |
| 481 | if( !pNew ){ |
| 482 | db->mallocFailed = 1; |
| 483 | } |
| 484 | } |
| 485 | return pNew; |
| 486 | } |
| 487 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 488 | /* |
| 489 | ** Attempt to reallocate p. If the reallocation fails, then free p |
| 490 | ** and set the mallocFailed flag in the database connection. |
| 491 | */ |
| 492 | void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 493 | void *pNew; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 494 | pNew = sqlite3DbRealloc(db, p, n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 495 | if( !pNew ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 496 | sqlite3_free(p); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 497 | } |
| 498 | return pNew; |
| 499 | } |
| 500 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 501 | /* |
| 502 | ** Make a copy of a string in memory obtained from sqliteMalloc(). These |
| 503 | ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This |
| 504 | ** is because when memory debugging is turned on, these two functions are |
| 505 | ** called via macros that record the current file and line number in the |
| 506 | ** ThreadData structure. |
| 507 | */ |
| 508 | char *sqlite3StrDup(const char *z){ |
| 509 | char *zNew; |
| 510 | int n; |
| 511 | if( z==0 ) return 0; |
| 512 | n = strlen(z)+1; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 513 | zNew = sqlite3Malloc(n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 514 | if( zNew ) memcpy(zNew, z, n); |
| 515 | return zNew; |
| 516 | } |
| 517 | char *sqlite3StrNDup(const char *z, int n){ |
| 518 | char *zNew; |
| 519 | if( z==0 ) return 0; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 520 | zNew = sqlite3Malloc(n+1); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 521 | if( zNew ){ |
| 522 | memcpy(zNew, z, n); |
| 523 | zNew[n] = 0; |
| 524 | } |
| 525 | return zNew; |
| 526 | } |
| 527 | |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 528 | char *sqlite3DbStrDup(sqlite3 *db, const char *z){ |
| 529 | char *zNew = sqlite3StrDup(z); |
| 530 | if( z && !zNew ){ |
| 531 | db->mallocFailed = 1; |
| 532 | } |
| 533 | return zNew; |
| 534 | } |
| 535 | char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ |
| 536 | char *zNew = sqlite3StrNDup(z, n); |
| 537 | if( z && !zNew ){ |
| 538 | db->mallocFailed = 1; |
| 539 | } |
| 540 | return zNew; |
| 541 | } |
| 542 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 543 | /* |
| 544 | ** Create a string from the 2nd and subsequent arguments (up to the |
| 545 | ** first NULL argument), store the string in memory obtained from |
| 546 | ** sqliteMalloc() and make the pointer indicated by the 1st argument |
| 547 | ** point to that string. The 1st argument must either be NULL or |
| 548 | ** point to memory obtained from sqliteMalloc(). |
| 549 | */ |
| 550 | void sqlite3SetString(char **pz, ...){ |
| 551 | va_list ap; |
| 552 | int nByte; |
| 553 | const char *z; |
| 554 | char *zResult; |
| 555 | |
| 556 | assert( pz!=0 ); |
| 557 | nByte = 1; |
| 558 | va_start(ap, pz); |
| 559 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 560 | nByte += strlen(z); |
| 561 | } |
| 562 | va_end(ap); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 563 | sqlite3_free(*pz); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 564 | *pz = zResult = sqlite3Malloc(nByte); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 565 | if( zResult==0 ){ |
| 566 | return; |
| 567 | } |
| 568 | *zResult = 0; |
| 569 | va_start(ap, pz); |
| 570 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 571 | int n = strlen(z); |
| 572 | memcpy(zResult, z, n); |
| 573 | zResult += n; |
| 574 | } |
| 575 | zResult[0] = 0; |
| 576 | va_end(ap); |
| 577 | } |
| 578 | |
| 579 | |
| 580 | /* |
| 581 | ** This function must be called before exiting any API function (i.e. |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 582 | ** returning control to the user) that has called sqlite3_malloc or |
| 583 | ** sqlite3_realloc. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 584 | ** |
| 585 | ** The returned value is normally a copy of the second argument to this |
| 586 | ** function. However, if a malloc() failure has occured since the previous |
| 587 | ** invocation SQLITE_NOMEM is returned instead. |
| 588 | ** |
| 589 | ** If the first argument, db, is not NULL and a malloc() error has occured, |
| 590 | ** then the connection error-code (the value returned by sqlite3_errcode()) |
| 591 | ** is set to SQLITE_NOMEM. |
| 592 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 593 | int sqlite3ApiExit(sqlite3* db, int rc){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 594 | /* If the db handle is not NULL, then we must hold the connection handle |
| 595 | ** mutex here. Otherwise the read (and possible write) of db->mallocFailed |
| 596 | ** is unsafe, as is the call to sqlite3Error(). |
| 597 | */ |
| 598 | assert( !db || sqlite3_mutex_held(db->mutex) ); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 599 | if( db && db->mallocFailed ){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 600 | sqlite3Error(db, SQLITE_NOMEM, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 601 | db->mallocFailed = 0; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 602 | rc = SQLITE_NOMEM; |
| 603 | } |
| 604 | return rc & (db ? db->errMask : 0xff); |
| 605 | } |