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