Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: third_party/sqlite/sqlite-src-3100200/src/malloc.c

Issue 1610543003: [sql] Import reference version of SQLite 3.10.2. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 ** 2001 September 15 2 ** 2001 September 15
3 ** 3 **
4 ** The author disclaims copyright to this source code. In place of 4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing: 5 ** a legal notice, here is a blessing:
6 ** 6 **
7 ** May you do good and not evil. 7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others. 8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give. 9 ** May you share freely, never taking more than you give.
10 ** 10 **
(...skipping 27 matching lines...) Expand all
38 */ 38 */
39 typedef struct ScratchFreeslot { 39 typedef struct ScratchFreeslot {
40 struct ScratchFreeslot *pNext; /* Next unused scratch buffer */ 40 struct ScratchFreeslot *pNext; /* Next unused scratch buffer */
41 } ScratchFreeslot; 41 } ScratchFreeslot;
42 42
43 /* 43 /*
44 ** State information local to the memory allocation subsystem. 44 ** State information local to the memory allocation subsystem.
45 */ 45 */
46 static SQLITE_WSD struct Mem0Global { 46 static SQLITE_WSD struct Mem0Global {
47 sqlite3_mutex *mutex; /* Mutex to serialize access */ 47 sqlite3_mutex *mutex; /* Mutex to serialize access */
48 48 sqlite3_int64 alarmThreshold; /* The soft heap limit */
49 /*
50 ** The alarm callback and its arguments. The mem0.mutex lock will
51 ** be held while the callback is running. Recursive calls into
52 ** the memory subsystem are allowed, but no new callbacks will be
53 ** issued.
54 */
55 sqlite3_int64 alarmThreshold;
56 void (*alarmCallback)(void*, sqlite3_int64,int);
57 void *alarmArg;
58 49
59 /* 50 /*
60 ** Pointers to the end of sqlite3GlobalConfig.pScratch memory 51 ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
61 ** (so that a range test can be used to determine if an allocation 52 ** (so that a range test can be used to determine if an allocation
62 ** being freed came from pScratch) and a pointer to the list of 53 ** being freed came from pScratch) and a pointer to the list of
63 ** unused scratch allocations. 54 ** unused scratch allocations.
64 */ 55 */
65 void *pScratchEnd; 56 void *pScratchEnd;
66 ScratchFreeslot *pScratchFree; 57 ScratchFreeslot *pScratchFree;
67 u32 nScratchFree; 58 u32 nScratchFree;
68 59
69 /* 60 /*
70 ** True if heap is nearly "full" where "full" is defined by the 61 ** True if heap is nearly "full" where "full" is defined by the
71 ** sqlite3_soft_heap_limit() setting. 62 ** sqlite3_soft_heap_limit() setting.
72 */ 63 */
73 int nearlyFull; 64 int nearlyFull;
74 } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 }; 65 } mem0 = { 0, 0, 0, 0, 0, 0 };
75 66
76 #define mem0 GLOBAL(struct Mem0Global, mem0) 67 #define mem0 GLOBAL(struct Mem0Global, mem0)
77 68
78 /* 69 /*
79 ** This routine runs when the memory allocator sees that the 70 ** Return the memory allocator mutex. sqlite3_status() needs it.
80 ** total memory allocation is about to exceed the soft heap
81 ** limit.
82 */ 71 */
83 static void softHeapLimitEnforcer( 72 sqlite3_mutex *sqlite3MallocMutex(void){
84 void *NotUsed, 73 return mem0.mutex;
85 sqlite3_int64 NotUsed2,
86 int allocSize
87 ){
88 UNUSED_PARAMETER2(NotUsed, NotUsed2);
89 sqlite3_release_memory(allocSize);
90 }
91
92 /*
93 ** Change the alarm callback
94 */
95 static int sqlite3MemoryAlarm(
96 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
97 void *pArg,
98 sqlite3_int64 iThreshold
99 ){
100 int nUsed;
101 sqlite3_mutex_enter(mem0.mutex);
102 mem0.alarmCallback = xCallback;
103 mem0.alarmArg = pArg;
104 mem0.alarmThreshold = iThreshold;
105 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
106 mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
107 sqlite3_mutex_leave(mem0.mutex);
108 return SQLITE_OK;
109 } 74 }
110 75
111 #ifndef SQLITE_OMIT_DEPRECATED 76 #ifndef SQLITE_OMIT_DEPRECATED
112 /* 77 /*
113 ** Deprecated external interface. Internal/core SQLite code 78 ** Deprecated external interface. It used to set an alarm callback
114 ** should call sqlite3MemoryAlarm. 79 ** that was invoked when memory usage grew too large. Now it is a
80 ** no-op.
115 */ 81 */
116 int sqlite3_memory_alarm( 82 int sqlite3_memory_alarm(
117 void(*xCallback)(void *pArg, sqlite3_int64 used,int N), 83 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
118 void *pArg, 84 void *pArg,
119 sqlite3_int64 iThreshold 85 sqlite3_int64 iThreshold
120 ){ 86 ){
121 return sqlite3MemoryAlarm(xCallback, pArg, iThreshold); 87 (void)xCallback;
88 (void)pArg;
89 (void)iThreshold;
90 return SQLITE_OK;
122 } 91 }
123 #endif 92 #endif
124 93
125 /* 94 /*
126 ** Set the soft heap-size limit for the library. Passing a zero or 95 ** Set the soft heap-size limit for the library. Passing a zero or
127 ** negative value indicates no limit. 96 ** negative value indicates no limit.
128 */ 97 */
129 sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){ 98 sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
130 sqlite3_int64 priorLimit; 99 sqlite3_int64 priorLimit;
131 sqlite3_int64 excess; 100 sqlite3_int64 excess;
101 sqlite3_int64 nUsed;
132 #ifndef SQLITE_OMIT_AUTOINIT 102 #ifndef SQLITE_OMIT_AUTOINIT
133 int rc = sqlite3_initialize(); 103 int rc = sqlite3_initialize();
134 if( rc ) return -1; 104 if( rc ) return -1;
135 #endif 105 #endif
136 sqlite3_mutex_enter(mem0.mutex); 106 sqlite3_mutex_enter(mem0.mutex);
137 priorLimit = mem0.alarmThreshold; 107 priorLimit = mem0.alarmThreshold;
108 if( n<0 ){
109 sqlite3_mutex_leave(mem0.mutex);
110 return priorLimit;
111 }
112 mem0.alarmThreshold = n;
113 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
114 mem0.nearlyFull = (n>0 && n<=nUsed);
138 sqlite3_mutex_leave(mem0.mutex); 115 sqlite3_mutex_leave(mem0.mutex);
139 if( n<0 ) return priorLimit;
140 if( n>0 ){
141 sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
142 }else{
143 sqlite3MemoryAlarm(0, 0, 0);
144 }
145 excess = sqlite3_memory_used() - n; 116 excess = sqlite3_memory_used() - n;
146 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff)); 117 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
147 return priorLimit; 118 return priorLimit;
148 } 119 }
149 void sqlite3_soft_heap_limit(int n){ 120 void sqlite3_soft_heap_limit(int n){
150 if( n<0 ) n = 0; 121 if( n<0 ) n = 0;
151 sqlite3_soft_heap_limit64(n); 122 sqlite3_soft_heap_limit64(n);
152 } 123 }
153 124
154 /* 125 /*
155 ** Initialize the memory allocation subsystem. 126 ** Initialize the memory allocation subsystem.
156 */ 127 */
157 int sqlite3MallocInit(void){ 128 int sqlite3MallocInit(void){
129 int rc;
158 if( sqlite3GlobalConfig.m.xMalloc==0 ){ 130 if( sqlite3GlobalConfig.m.xMalloc==0 ){
159 sqlite3MemSetDefault(); 131 sqlite3MemSetDefault();
160 } 132 }
161 memset(&mem0, 0, sizeof(mem0)); 133 memset(&mem0, 0, sizeof(mem0));
162 if( sqlite3GlobalConfig.bCoreMutex ){ 134 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
163 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
164 }
165 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100 135 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
166 && sqlite3GlobalConfig.nScratch>0 ){ 136 && sqlite3GlobalConfig.nScratch>0 ){
167 int i, n, sz; 137 int i, n, sz;
168 ScratchFreeslot *pSlot; 138 ScratchFreeslot *pSlot;
169 sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch); 139 sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
170 sqlite3GlobalConfig.szScratch = sz; 140 sqlite3GlobalConfig.szScratch = sz;
171 pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch; 141 pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
172 n = sqlite3GlobalConfig.nScratch; 142 n = sqlite3GlobalConfig.nScratch;
173 mem0.pScratchFree = pSlot; 143 mem0.pScratchFree = pSlot;
174 mem0.nScratchFree = n; 144 mem0.nScratchFree = n;
175 for(i=0; i<n-1; i++){ 145 for(i=0; i<n-1; i++){
176 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot); 146 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
177 pSlot = pSlot->pNext; 147 pSlot = pSlot->pNext;
178 } 148 }
179 pSlot->pNext = 0; 149 pSlot->pNext = 0;
180 mem0.pScratchEnd = (void*)&pSlot[1]; 150 mem0.pScratchEnd = (void*)&pSlot[1];
181 }else{ 151 }else{
182 mem0.pScratchEnd = 0; 152 mem0.pScratchEnd = 0;
183 sqlite3GlobalConfig.pScratch = 0; 153 sqlite3GlobalConfig.pScratch = 0;
184 sqlite3GlobalConfig.szScratch = 0; 154 sqlite3GlobalConfig.szScratch = 0;
185 sqlite3GlobalConfig.nScratch = 0; 155 sqlite3GlobalConfig.nScratch = 0;
186 } 156 }
187 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512 157 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
188 || sqlite3GlobalConfig.nPage<1 ){ 158 || sqlite3GlobalConfig.nPage<=0 ){
189 sqlite3GlobalConfig.pPage = 0; 159 sqlite3GlobalConfig.pPage = 0;
190 sqlite3GlobalConfig.szPage = 0; 160 sqlite3GlobalConfig.szPage = 0;
191 sqlite3GlobalConfig.nPage = 0;
192 } 161 }
193 return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData); 162 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
163 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
164 return rc;
194 } 165 }
195 166
196 /* 167 /*
197 ** Return true if the heap is currently under memory pressure - in other 168 ** Return true if the heap is currently under memory pressure - in other
198 ** words if the amount of heap used is close to the limit set by 169 ** words if the amount of heap used is close to the limit set by
199 ** sqlite3_soft_heap_limit(). 170 ** sqlite3_soft_heap_limit().
200 */ 171 */
201 int sqlite3HeapNearlyFull(void){ 172 int sqlite3HeapNearlyFull(void){
202 return mem0.nearlyFull; 173 return mem0.nearlyFull;
203 } 174 }
204 175
205 /* 176 /*
206 ** Deinitialize the memory allocation subsystem. 177 ** Deinitialize the memory allocation subsystem.
207 */ 178 */
208 void sqlite3MallocEnd(void){ 179 void sqlite3MallocEnd(void){
209 if( sqlite3GlobalConfig.m.xShutdown ){ 180 if( sqlite3GlobalConfig.m.xShutdown ){
210 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData); 181 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
211 } 182 }
212 memset(&mem0, 0, sizeof(mem0)); 183 memset(&mem0, 0, sizeof(mem0));
213 } 184 }
214 185
215 /* 186 /*
216 ** Return the amount of memory currently checked out. 187 ** Return the amount of memory currently checked out.
217 */ 188 */
218 sqlite3_int64 sqlite3_memory_used(void){ 189 sqlite3_int64 sqlite3_memory_used(void){
219 int n, mx; 190 sqlite3_int64 res, mx;
220 sqlite3_int64 res; 191 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
221 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
222 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
223 return res; 192 return res;
224 } 193 }
225 194
226 /* 195 /*
227 ** Return the maximum amount of memory that has ever been 196 ** Return the maximum amount of memory that has ever been
228 ** checked out since either the beginning of this process 197 ** checked out since either the beginning of this process
229 ** or since the most recent reset. 198 ** or since the most recent reset.
230 */ 199 */
231 sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ 200 sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
232 int n, mx; 201 sqlite3_int64 res, mx;
233 sqlite3_int64 res; 202 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
234 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag); 203 return mx;
235 res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
236 return res;
237 } 204 }
238 205
239 /* 206 /*
240 ** Trigger the alarm 207 ** Trigger the alarm
241 */ 208 */
242 static void sqlite3MallocAlarm(int nByte){ 209 static void sqlite3MallocAlarm(int nByte){
243 void (*xCallback)(void*,sqlite3_int64,int); 210 if( mem0.alarmThreshold<=0 ) return;
244 sqlite3_int64 nowUsed;
245 void *pArg;
246 if( mem0.alarmCallback==0 ) return;
247 xCallback = mem0.alarmCallback;
248 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
249 pArg = mem0.alarmArg;
250 mem0.alarmCallback = 0;
251 sqlite3_mutex_leave(mem0.mutex); 211 sqlite3_mutex_leave(mem0.mutex);
252 xCallback(pArg, nowUsed, nByte); 212 sqlite3_release_memory(nByte);
253 sqlite3_mutex_enter(mem0.mutex); 213 sqlite3_mutex_enter(mem0.mutex);
254 mem0.alarmCallback = xCallback;
255 mem0.alarmArg = pArg;
256 } 214 }
257 215
258 /* 216 /*
259 ** Do a memory allocation with statistics and alarms. Assume the 217 ** Do a memory allocation with statistics and alarms. Assume the
260 ** lock is already held. 218 ** lock is already held.
261 */ 219 */
262 static int mallocWithAlarm(int n, void **pp){ 220 static int mallocWithAlarm(int n, void **pp){
263 int nFull; 221 int nFull;
264 void *p; 222 void *p;
265 assert( sqlite3_mutex_held(mem0.mutex) ); 223 assert( sqlite3_mutex_held(mem0.mutex) );
266 nFull = sqlite3GlobalConfig.m.xRoundup(n); 224 nFull = sqlite3GlobalConfig.m.xRoundup(n);
267 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); 225 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
268 if( mem0.alarmCallback!=0 ){ 226 if( mem0.alarmThreshold>0 ){
269 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 227 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
270 if( nUsed >= mem0.alarmThreshold - nFull ){ 228 if( nUsed >= mem0.alarmThreshold - nFull ){
271 mem0.nearlyFull = 1; 229 mem0.nearlyFull = 1;
272 sqlite3MallocAlarm(nFull); 230 sqlite3MallocAlarm(nFull);
273 }else{ 231 }else{
274 mem0.nearlyFull = 0; 232 mem0.nearlyFull = 0;
275 } 233 }
276 } 234 }
277 p = sqlite3GlobalConfig.m.xMalloc(nFull); 235 p = sqlite3GlobalConfig.m.xMalloc(nFull);
278 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 236 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
279 if( p==0 && mem0.alarmCallback ){ 237 if( p==0 && mem0.alarmThreshold>0 ){
280 sqlite3MallocAlarm(nFull); 238 sqlite3MallocAlarm(nFull);
281 p = sqlite3GlobalConfig.m.xMalloc(nFull); 239 p = sqlite3GlobalConfig.m.xMalloc(nFull);
282 } 240 }
283 #endif 241 #endif
284 if( p ){ 242 if( p ){
285 nFull = sqlite3MallocSize(p); 243 nFull = sqlite3MallocSize(p);
286 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); 244 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
287 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1); 245 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
288 } 246 }
289 *pp = p; 247 *pp = p;
290 return nFull; 248 return nFull;
291 } 249 }
292 250
293 /* 251 /*
294 ** Allocate memory. This routine is like sqlite3_malloc() except that it 252 ** Allocate memory. This routine is like sqlite3_malloc() except that it
295 ** assumes the memory subsystem has already been initialized. 253 ** assumes the memory subsystem has already been initialized.
296 */ 254 */
297 void *sqlite3Malloc(u64 n){ 255 void *sqlite3Malloc(u64 n){
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 ** for situations where the memory might be held long-term. This 307 ** for situations where the memory might be held long-term. This
350 ** routine is intended to get memory to old large transient data 308 ** routine is intended to get memory to old large transient data
351 ** structures that would not normally fit on the stack of an 309 ** structures that would not normally fit on the stack of an
352 ** embedded processor. 310 ** embedded processor.
353 */ 311 */
354 void *sqlite3ScratchMalloc(int n){ 312 void *sqlite3ScratchMalloc(int n){
355 void *p; 313 void *p;
356 assert( n>0 ); 314 assert( n>0 );
357 315
358 sqlite3_mutex_enter(mem0.mutex); 316 sqlite3_mutex_enter(mem0.mutex);
359 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); 317 sqlite3StatusHighwater(SQLITE_STATUS_SCRATCH_SIZE, n);
360 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){ 318 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
361 p = mem0.pScratchFree; 319 p = mem0.pScratchFree;
362 mem0.pScratchFree = mem0.pScratchFree->pNext; 320 mem0.pScratchFree = mem0.pScratchFree->pNext;
363 mem0.nScratchFree--; 321 mem0.nScratchFree--;
364 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); 322 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
365 sqlite3_mutex_leave(mem0.mutex); 323 sqlite3_mutex_leave(mem0.mutex);
366 }else{ 324 }else{
367 sqlite3_mutex_leave(mem0.mutex); 325 sqlite3_mutex_leave(mem0.mutex);
368 p = sqlite3Malloc(n); 326 p = sqlite3Malloc(n);
369 if( sqlite3GlobalConfig.bMemstat && p ){ 327 if( sqlite3GlobalConfig.bMemstat && p ){
370 sqlite3_mutex_enter(mem0.mutex); 328 sqlite3_mutex_enter(mem0.mutex);
371 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p)); 329 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
372 sqlite3_mutex_leave(mem0.mutex); 330 sqlite3_mutex_leave(mem0.mutex);
373 } 331 }
374 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); 332 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
375 } 333 }
376 assert( sqlite3_mutex_notheld(mem0.mutex) ); 334 assert( sqlite3_mutex_notheld(mem0.mutex) );
377 335
378 336
379 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 337 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
380 /* Verify that no more than two scratch allocations per thread 338 /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
381 ** are outstanding at one time. (This is only checked in the 339 ** buffers per thread.
382 ** single-threaded case since checking in the multi-threaded case 340 **
383 ** would be much more complicated.) */ 341 ** This can only be checked in single-threaded mode.
384 assert( scratchAllocOut<=1 ); 342 */
343 assert( scratchAllocOut==0 );
385 if( p ) scratchAllocOut++; 344 if( p ) scratchAllocOut++;
386 #endif 345 #endif
387 346
388 return p; 347 return p;
389 } 348 }
390 void sqlite3ScratchFree(void *p){ 349 void sqlite3ScratchFree(void *p){
391 if( p ){ 350 if( p ){
392 351
393 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 352 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
394 /* Verify that no more than two scratch allocation per thread 353 /* Verify that no more than two scratch allocation per thread
395 ** is outstanding at one time. (This is only checked in the 354 ** is outstanding at one time. (This is only checked in the
396 ** single-threaded case since checking in the multi-threaded case 355 ** single-threaded case since checking in the multi-threaded case
397 ** would be much more complicated.) */ 356 ** would be much more complicated.) */
398 assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); 357 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
399 scratchAllocOut--; 358 scratchAllocOut--;
400 #endif 359 #endif
401 360
402 if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){ 361 if( SQLITE_WITHIN(p, sqlite3GlobalConfig.pScratch, mem0.pScratchEnd) ){
403 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */ 362 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
404 ScratchFreeslot *pSlot; 363 ScratchFreeslot *pSlot;
405 pSlot = (ScratchFreeslot*)p; 364 pSlot = (ScratchFreeslot*)p;
406 sqlite3_mutex_enter(mem0.mutex); 365 sqlite3_mutex_enter(mem0.mutex);
407 pSlot->pNext = mem0.pScratchFree; 366 pSlot->pNext = mem0.pScratchFree;
408 mem0.pScratchFree = pSlot; 367 mem0.pScratchFree = pSlot;
409 mem0.nScratchFree++; 368 mem0.nScratchFree++;
410 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch ); 369 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
411 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); 370 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
412 sqlite3_mutex_leave(mem0.mutex); 371 sqlite3_mutex_leave(mem0.mutex);
413 }else{ 372 }else{
414 /* Release memory back to the heap */ 373 /* Release memory back to the heap */
415 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); 374 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
416 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) ); 375 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
417 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 376 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
418 if( sqlite3GlobalConfig.bMemstat ){ 377 if( sqlite3GlobalConfig.bMemstat ){
419 int iSize = sqlite3MallocSize(p); 378 int iSize = sqlite3MallocSize(p);
420 sqlite3_mutex_enter(mem0.mutex); 379 sqlite3_mutex_enter(mem0.mutex);
421 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize); 380 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
422 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); 381 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
423 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1); 382 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
424 sqlite3GlobalConfig.m.xFree(p); 383 sqlite3GlobalConfig.m.xFree(p);
425 sqlite3_mutex_leave(mem0.mutex); 384 sqlite3_mutex_leave(mem0.mutex);
426 }else{ 385 }else{
427 sqlite3GlobalConfig.m.xFree(p); 386 sqlite3GlobalConfig.m.xFree(p);
428 } 387 }
429 } 388 }
430 } 389 }
431 } 390 }
432 391
433 /* 392 /*
434 ** TRUE if p is a lookaside memory allocation from db 393 ** TRUE if p is a lookaside memory allocation from db
435 */ 394 */
436 #ifndef SQLITE_OMIT_LOOKASIDE 395 #ifndef SQLITE_OMIT_LOOKASIDE
437 static int isLookaside(sqlite3 *db, void *p){ 396 static int isLookaside(sqlite3 *db, void *p){
438 return p>=db->lookaside.pStart && p<db->lookaside.pEnd; 397 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
439 } 398 }
440 #else 399 #else
441 #define isLookaside(A,B) 0 400 #define isLookaside(A,B) 0
442 #endif 401 #endif
443 402
444 /* 403 /*
445 ** Return the size of a memory allocation previously obtained from 404 ** Return the size of a memory allocation previously obtained from
446 ** sqlite3Malloc() or sqlite3_malloc(). 405 ** sqlite3Malloc() or sqlite3_malloc().
447 */ 406 */
448 int sqlite3MallocSize(void *p){ 407 int sqlite3MallocSize(void *p){
449 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 408 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
450 return sqlite3GlobalConfig.m.xSize(p); 409 return sqlite3GlobalConfig.m.xSize(p);
451 } 410 }
452 int sqlite3DbMallocSize(sqlite3 *db, void *p){ 411 int sqlite3DbMallocSize(sqlite3 *db, void *p){
453 if( db==0 ){ 412 assert( p!=0 );
454 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); 413 if( db==0 || !isLookaside(db,p) ){
455 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 414 #if SQLITE_DEBUG
456 return sqlite3MallocSize(p); 415 if( db==0 ){
416 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
417 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
418 }else{
419 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
420 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
421 }
422 #endif
423 return sqlite3GlobalConfig.m.xSize(p);
457 }else{ 424 }else{
458 assert( sqlite3_mutex_held(db->mutex) ); 425 assert( sqlite3_mutex_held(db->mutex) );
459 if( isLookaside(db, p) ){ 426 return db->lookaside.sz;
460 return db->lookaside.sz;
461 }else{
462 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
463 assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
464 return sqlite3GlobalConfig.m.xSize(p);
465 }
466 } 427 }
467 } 428 }
468 sqlite3_uint64 sqlite3_msize(void *p){ 429 sqlite3_uint64 sqlite3_msize(void *p){
469 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); 430 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
470 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 431 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
471 return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p); 432 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
472 } 433 }
473 434
474 /* 435 /*
475 ** Free memory previously obtained from sqlite3Malloc(). 436 ** Free memory previously obtained from sqlite3Malloc().
476 */ 437 */
477 void sqlite3_free(void *p){ 438 void sqlite3_free(void *p){
478 if( p==0 ) return; /* IMP: R-49053-54554 */ 439 if( p==0 ) return; /* IMP: R-49053-54554 */
479 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 440 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
480 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); 441 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
481 if( sqlite3GlobalConfig.bMemstat ){ 442 if( sqlite3GlobalConfig.bMemstat ){
482 sqlite3_mutex_enter(mem0.mutex); 443 sqlite3_mutex_enter(mem0.mutex);
483 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); 444 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
484 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1); 445 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
485 sqlite3GlobalConfig.m.xFree(p); 446 sqlite3GlobalConfig.m.xFree(p);
486 sqlite3_mutex_leave(mem0.mutex); 447 sqlite3_mutex_leave(mem0.mutex);
487 }else{ 448 }else{
488 sqlite3GlobalConfig.m.xFree(p); 449 sqlite3GlobalConfig.m.xFree(p);
489 } 450 }
490 } 451 }
491 452
492 /* 453 /*
493 ** Add the size of memory allocation "p" to the count in 454 ** Add the size of memory allocation "p" to the count in
494 ** *db->pnBytesFreed. 455 ** *db->pnBytesFreed.
(...skipping 20 matching lines...) Expand all
515 /* Trash all content in the buffer being freed */ 476 /* Trash all content in the buffer being freed */
516 memset(p, 0xaa, db->lookaside.sz); 477 memset(p, 0xaa, db->lookaside.sz);
517 #endif 478 #endif
518 pBuf->pNext = db->lookaside.pFree; 479 pBuf->pNext = db->lookaside.pFree;
519 db->lookaside.pFree = pBuf; 480 db->lookaside.pFree = pBuf;
520 db->lookaside.nOut--; 481 db->lookaside.nOut--;
521 return; 482 return;
522 } 483 }
523 } 484 }
524 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 485 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
525 assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 486 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
526 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); 487 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
527 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 488 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
528 sqlite3_free(p); 489 sqlite3_free(p);
529 } 490 }
530 491
531 /* 492 /*
532 ** Change the size of an existing memory allocation 493 ** Change the size of an existing memory allocation
533 */ 494 */
534 void *sqlite3Realloc(void *pOld, u64 nBytes){ 495 void *sqlite3Realloc(void *pOld, u64 nBytes){
535 int nOld, nNew, nDiff; 496 int nOld, nNew, nDiff;
536 void *pNew; 497 void *pNew;
537 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); 498 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
538 assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) ); 499 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
539 if( pOld==0 ){ 500 if( pOld==0 ){
540 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ 501 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
541 } 502 }
542 if( nBytes==0 ){ 503 if( nBytes==0 ){
543 sqlite3_free(pOld); /* IMP: R-26507-47431 */ 504 sqlite3_free(pOld); /* IMP: R-26507-47431 */
544 return 0; 505 return 0;
545 } 506 }
546 if( nBytes>=0x7fffff00 ){ 507 if( nBytes>=0x7fffff00 ){
547 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */ 508 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
548 return 0; 509 return 0;
549 } 510 }
550 nOld = sqlite3MallocSize(pOld); 511 nOld = sqlite3MallocSize(pOld);
551 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second 512 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
552 ** argument to xRealloc is always a value returned by a prior call to 513 ** argument to xRealloc is always a value returned by a prior call to
553 ** xRoundup. */ 514 ** xRoundup. */
554 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes); 515 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
555 if( nOld==nNew ){ 516 if( nOld==nNew ){
556 pNew = pOld; 517 pNew = pOld;
557 }else if( sqlite3GlobalConfig.bMemstat ){ 518 }else if( sqlite3GlobalConfig.bMemstat ){
558 sqlite3_mutex_enter(mem0.mutex); 519 sqlite3_mutex_enter(mem0.mutex);
559 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes); 520 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
560 nDiff = nNew - nOld; 521 nDiff = nNew - nOld;
561 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= 522 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
562 mem0.alarmThreshold-nDiff ){ 523 mem0.alarmThreshold-nDiff ){
563 sqlite3MallocAlarm(nDiff); 524 sqlite3MallocAlarm(nDiff);
564 } 525 }
565 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 526 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
566 if( pNew==0 && mem0.alarmCallback ){ 527 if( pNew==0 && mem0.alarmThreshold>0 ){
567 sqlite3MallocAlarm((int)nBytes); 528 sqlite3MallocAlarm((int)nBytes);
568 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 529 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
569 } 530 }
570 if( pNew ){ 531 if( pNew ){
571 nNew = sqlite3MallocSize(pNew); 532 nNew = sqlite3MallocSize(pNew);
572 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld); 533 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
573 } 534 }
574 sqlite3_mutex_leave(mem0.mutex); 535 sqlite3_mutex_leave(mem0.mutex);
575 }else{ 536 }else{
576 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 537 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
577 } 538 }
578 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */ 539 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
579 return pNew; 540 return pNew;
580 } 541 }
581 542
582 /* 543 /*
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
695 if( n<=db->lookaside.sz ){ 656 if( n<=db->lookaside.sz ){
696 return p; 657 return p;
697 } 658 }
698 pNew = sqlite3DbMallocRaw(db, n); 659 pNew = sqlite3DbMallocRaw(db, n);
699 if( pNew ){ 660 if( pNew ){
700 memcpy(pNew, p, db->lookaside.sz); 661 memcpy(pNew, p, db->lookaside.sz);
701 sqlite3DbFree(db, p); 662 sqlite3DbFree(db, p);
702 } 663 }
703 }else{ 664 }else{
704 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 665 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
705 assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 666 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
706 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 667 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
707 pNew = sqlite3_realloc64(p, n); 668 pNew = sqlite3_realloc64(p, n);
708 if( !pNew ){ 669 if( !pNew ){
709 db->mallocFailed = 1; 670 db->mallocFailed = 1;
710 } 671 }
711 sqlite3MemdebugSetType(pNew, 672 sqlite3MemdebugSetType(pNew,
712 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); 673 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
713 } 674 }
714 } 675 }
715 return pNew; 676 return pNew;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
757 assert( (n&0x7fffffff)==n ); 718 assert( (n&0x7fffffff)==n );
758 zNew = sqlite3DbMallocRaw(db, n+1); 719 zNew = sqlite3DbMallocRaw(db, n+1);
759 if( zNew ){ 720 if( zNew ){
760 memcpy(zNew, z, (size_t)n); 721 memcpy(zNew, z, (size_t)n);
761 zNew[n] = 0; 722 zNew[n] = 0;
762 } 723 }
763 return zNew; 724 return zNew;
764 } 725 }
765 726
766 /* 727 /*
767 ** Create a string from the zFromat argument and the va_list that follows. 728 ** Free any prior content in *pz and replace it with a copy of zNew.
768 ** Store the string in memory obtained from sqliteMalloc() and make *pz
769 ** point to that string.
770 */ 729 */
771 void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){ 730 void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
772 va_list ap;
773 char *z;
774
775 va_start(ap, zFormat);
776 z = sqlite3VMPrintf(db, zFormat, ap);
777 va_end(ap);
778 sqlite3DbFree(db, *pz); 731 sqlite3DbFree(db, *pz);
779 *pz = z; 732 *pz = sqlite3DbStrDup(db, zNew);
780 } 733 }
781 734
782 /* 735 /*
783 ** Take actions at the end of an API call to indicate an OOM error 736 ** Take actions at the end of an API call to indicate an OOM error
784 */ 737 */
785 static SQLITE_NOINLINE int apiOomError(sqlite3 *db){ 738 static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
786 db->mallocFailed = 0; 739 db->mallocFailed = 0;
787 sqlite3Error(db, SQLITE_NOMEM); 740 sqlite3Error(db, SQLITE_NOMEM);
788 return SQLITE_NOMEM; 741 return SQLITE_NOMEM;
789 } 742 }
790 743
791 /* 744 /*
792 ** This function must be called before exiting any API function (i.e. 745 ** This function must be called before exiting any API function (i.e.
793 ** returning control to the user) that has called sqlite3_malloc or 746 ** returning control to the user) that has called sqlite3_malloc or
794 ** sqlite3_realloc. 747 ** sqlite3_realloc.
795 ** 748 **
796 ** The returned value is normally a copy of the second argument to this 749 ** The returned value is normally a copy of the second argument to this
797 ** function. However, if a malloc() failure has occurred since the previous 750 ** function. However, if a malloc() failure has occurred since the previous
798 ** invocation SQLITE_NOMEM is returned instead. 751 ** invocation SQLITE_NOMEM is returned instead.
799 ** 752 **
800 ** If the first argument, db, is not NULL and a malloc() error has occurred, 753 ** If an OOM as occurred, then the connection error-code (the value
801 ** then the connection error-code (the value returned by sqlite3_errcode()) 754 ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
802 ** is set to SQLITE_NOMEM.
803 */ 755 */
804 int sqlite3ApiExit(sqlite3* db, int rc){ 756 int sqlite3ApiExit(sqlite3* db, int rc){
805 /* If the db handle is not NULL, then we must hold the connection handle 757 /* If the db handle must hold the connection handle mutex here.
806 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 758 ** Otherwise the read (and possible write) of db->mallocFailed
807 ** is unsafe, as is the call to sqlite3Error(). 759 ** is unsafe, as is the call to sqlite3Error().
808 */ 760 */
809 assert( !db || sqlite3_mutex_held(db->mutex) ); 761 assert( db!=0 );
810 if( db==0 ) return rc & 0xff; 762 assert( sqlite3_mutex_held(db->mutex) );
811 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){ 763 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
812 return apiOomError(db); 764 return apiOomError(db);
813 } 765 }
814 return rc & db->errMask; 766 return rc & db->errMask;
815 } 767 }
OLDNEW
« no previous file with comments | « third_party/sqlite/sqlite-src-3100200/src/main.c ('k') | third_party/sqlite/sqlite-src-3100200/src/mem0.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698