| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2007 August 15 | 2 ** 2007 August 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 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 | 172 |
| 173 /* | 173 /* |
| 174 ** Return the number of bytes currently allocated at address p. | 174 ** Return the number of bytes currently allocated at address p. |
| 175 */ | 175 */ |
| 176 static int sqlite3MemSize(void *p){ | 176 static int sqlite3MemSize(void *p){ |
| 177 struct MemBlockHdr *pHdr; | 177 struct MemBlockHdr *pHdr; |
| 178 if( !p ){ | 178 if( !p ){ |
| 179 return 0; | 179 return 0; |
| 180 } | 180 } |
| 181 pHdr = sqlite3MemsysGetHeader(p); | 181 pHdr = sqlite3MemsysGetHeader(p); |
| 182 return pHdr->iSize; | 182 return (int)pHdr->iSize; |
| 183 } | 183 } |
| 184 | 184 |
| 185 /* | 185 /* |
| 186 ** Initialize the memory allocation subsystem. | 186 ** Initialize the memory allocation subsystem. |
| 187 */ | 187 */ |
| 188 static int sqlite3MemInit(void *NotUsed){ | 188 static int sqlite3MemInit(void *NotUsed){ |
| 189 UNUSED_PARAMETER(NotUsed); | 189 UNUSED_PARAMETER(NotUsed); |
| 190 assert( (sizeof(struct MemBlockHdr)&7) == 0 ); | 190 assert( (sizeof(struct MemBlockHdr)&7) == 0 ); |
| 191 if( !sqlite3GlobalConfig.bMemstat ){ | 191 if( !sqlite3GlobalConfig.bMemstat ){ |
| 192 /* If memory status is enabled, then the malloc.c wrapper will already | 192 /* If memory status is enabled, then the malloc.c wrapper will already |
| (...skipping 21 matching lines...) Expand all Loading... |
| 214 /* | 214 /* |
| 215 ** Fill a buffer with pseudo-random bytes. This is used to preset | 215 ** Fill a buffer with pseudo-random bytes. This is used to preset |
| 216 ** the content of a new memory allocation to unpredictable values and | 216 ** the content of a new memory allocation to unpredictable values and |
| 217 ** to clear the content of a freed allocation to unpredictable values. | 217 ** to clear the content of a freed allocation to unpredictable values. |
| 218 */ | 218 */ |
| 219 static void randomFill(char *pBuf, int nByte){ | 219 static void randomFill(char *pBuf, int nByte){ |
| 220 unsigned int x, y, r; | 220 unsigned int x, y, r; |
| 221 x = SQLITE_PTR_TO_INT(pBuf); | 221 x = SQLITE_PTR_TO_INT(pBuf); |
| 222 y = nByte | 1; | 222 y = nByte | 1; |
| 223 while( nByte >= 4 ){ | 223 while( nByte >= 4 ){ |
| 224 x = (x>>1) ^ (-(x&1) & 0xd0000001); | 224 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); |
| 225 y = y*1103515245 + 12345; | 225 y = y*1103515245 + 12345; |
| 226 r = x ^ y; | 226 r = x ^ y; |
| 227 *(int*)pBuf = r; | 227 *(int*)pBuf = r; |
| 228 pBuf += 4; | 228 pBuf += 4; |
| 229 nByte -= 4; | 229 nByte -= 4; |
| 230 } | 230 } |
| 231 while( nByte-- > 0 ){ | 231 while( nByte-- > 0 ){ |
| 232 x = (x>>1) ^ (-(x&1) & 0xd0000001); | 232 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); |
| 233 y = y*1103515245 + 12345; | 233 y = y*1103515245 + 12345; |
| 234 r = x ^ y; | 234 r = x ^ y; |
| 235 *(pBuf++) = r & 0xff; | 235 *(pBuf++) = r & 0xff; |
| 236 } | 236 } |
| 237 } | 237 } |
| 238 | 238 |
| 239 /* | 239 /* |
| 240 ** Allocate nByte bytes of memory. | 240 ** Allocate nByte bytes of memory. |
| 241 */ | 241 */ |
| 242 static void *sqlite3MemMalloc(int nByte){ | 242 static void *sqlite3MemMalloc(int nByte){ |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 } | 317 } |
| 318 if( pHdr->pNext ){ | 318 if( pHdr->pNext ){ |
| 319 assert( pHdr->pNext->pPrev==pHdr ); | 319 assert( pHdr->pNext->pPrev==pHdr ); |
| 320 pHdr->pNext->pPrev = pHdr->pPrev; | 320 pHdr->pNext->pPrev = pHdr->pPrev; |
| 321 }else{ | 321 }else{ |
| 322 assert( mem.pLast==pHdr ); | 322 assert( mem.pLast==pHdr ); |
| 323 mem.pLast = pHdr->pPrev; | 323 mem.pLast = pHdr->pPrev; |
| 324 } | 324 } |
| 325 z = (char*)pBt; | 325 z = (char*)pBt; |
| 326 z -= pHdr->nTitle; | 326 z -= pHdr->nTitle; |
| 327 adjustStats(pHdr->iSize, -1); | 327 adjustStats((int)pHdr->iSize, -1); |
| 328 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) + | 328 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) + |
| 329 pHdr->iSize + sizeof(int) + pHdr->nTitle); | 329 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle); |
| 330 free(z); | 330 free(z); |
| 331 sqlite3_mutex_leave(mem.mutex); | 331 sqlite3_mutex_leave(mem.mutex); |
| 332 } | 332 } |
| 333 | 333 |
| 334 /* | 334 /* |
| 335 ** Change the size of an existing memory allocation. | 335 ** Change the size of an existing memory allocation. |
| 336 ** | 336 ** |
| 337 ** For this debugging implementation, we *always* make a copy of the | 337 ** For this debugging implementation, we *always* make a copy of the |
| 338 ** allocation into a new place in memory. In this way, if the | 338 ** allocation into a new place in memory. In this way, if the |
| 339 ** higher level code is using pointer to the old allocation, it is | 339 ** higher level code is using pointer to the old allocation, it is |
| 340 ** much more likely to break and we are much more liking to find | 340 ** much more likely to break and we are much more liking to find |
| 341 ** the error. | 341 ** the error. |
| 342 */ | 342 */ |
| 343 static void *sqlite3MemRealloc(void *pPrior, int nByte){ | 343 static void *sqlite3MemRealloc(void *pPrior, int nByte){ |
| 344 struct MemBlockHdr *pOldHdr; | 344 struct MemBlockHdr *pOldHdr; |
| 345 void *pNew; | 345 void *pNew; |
| 346 assert( mem.disallow==0 ); | 346 assert( mem.disallow==0 ); |
| 347 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */ | 347 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */ |
| 348 pOldHdr = sqlite3MemsysGetHeader(pPrior); | 348 pOldHdr = sqlite3MemsysGetHeader(pPrior); |
| 349 pNew = sqlite3MemMalloc(nByte); | 349 pNew = sqlite3MemMalloc(nByte); |
| 350 if( pNew ){ | 350 if( pNew ){ |
| 351 memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize); | 351 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize)); |
| 352 if( nByte>pOldHdr->iSize ){ | 352 if( nByte>pOldHdr->iSize ){ |
| 353 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - pOldHdr->iSize); | 353 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize); |
| 354 } | 354 } |
| 355 sqlite3MemFree(pPrior); | 355 sqlite3MemFree(pPrior); |
| 356 } | 356 } |
| 357 return pNew; | 357 return pNew; |
| 358 } | 358 } |
| 359 | 359 |
| 360 /* | 360 /* |
| 361 ** Populate the low-level memory allocation function pointers in | 361 ** Populate the low-level memory allocation function pointers in |
| 362 ** sqlite3GlobalConfig.m with pointers to the routines in this file. | 362 ** sqlite3GlobalConfig.m with pointers to the routines in this file. |
| 363 */ | 363 */ |
| (...skipping 23 matching lines...) Expand all Loading... |
| 387 } | 387 } |
| 388 } | 388 } |
| 389 | 389 |
| 390 /* | 390 /* |
| 391 ** Return TRUE if the mask of type in eType matches the type of the | 391 ** Return TRUE if the mask of type in eType matches the type of the |
| 392 ** allocation p. Also return true if p==NULL. | 392 ** allocation p. Also return true if p==NULL. |
| 393 ** | 393 ** |
| 394 ** This routine is designed for use within an assert() statement, to | 394 ** This routine is designed for use within an assert() statement, to |
| 395 ** verify the type of an allocation. For example: | 395 ** verify the type of an allocation. For example: |
| 396 ** | 396 ** |
| 397 ** assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); | 397 ** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 398 */ | 398 */ |
| 399 int sqlite3MemdebugHasType(void *p, u8 eType){ | 399 int sqlite3MemdebugHasType(void *p, u8 eType){ |
| 400 int rc = 1; | 400 int rc = 1; |
| 401 if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){ | 401 if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){ |
| 402 struct MemBlockHdr *pHdr; | 402 struct MemBlockHdr *pHdr; |
| 403 pHdr = sqlite3MemsysGetHeader(p); | 403 pHdr = sqlite3MemsysGetHeader(p); |
| 404 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ | 404 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ |
| 405 if( (pHdr->eType&eType)==0 ){ | 405 if( (pHdr->eType&eType)==0 ){ |
| 406 rc = 0; | 406 rc = 0; |
| 407 } | 407 } |
| 408 } | 408 } |
| 409 return rc; | 409 return rc; |
| 410 } | 410 } |
| 411 | 411 |
| 412 /* | 412 /* |
| 413 ** Return TRUE if the mask of type in eType matches no bits of the type of the | 413 ** Return TRUE if the mask of type in eType matches no bits of the type of the |
| 414 ** allocation p. Also return true if p==NULL. | 414 ** allocation p. Also return true if p==NULL. |
| 415 ** | 415 ** |
| 416 ** This routine is designed for use within an assert() statement, to | 416 ** This routine is designed for use within an assert() statement, to |
| 417 ** verify the type of an allocation. For example: | 417 ** verify the type of an allocation. For example: |
| 418 ** | 418 ** |
| 419 ** assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); | 419 ** assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); |
| 420 */ | 420 */ |
| 421 int sqlite3MemdebugNoType(void *p, u8 eType){ | 421 int sqlite3MemdebugNoType(void *p, u8 eType){ |
| 422 int rc = 1; | 422 int rc = 1; |
| 423 if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){ | 423 if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){ |
| 424 struct MemBlockHdr *pHdr; | 424 struct MemBlockHdr *pHdr; |
| 425 pHdr = sqlite3MemsysGetHeader(p); | 425 pHdr = sqlite3MemsysGetHeader(p); |
| 426 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ | 426 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ |
| 427 if( (pHdr->eType&eType)!=0 ){ | 427 if( (pHdr->eType&eType)!=0 ){ |
| 428 rc = 0; | 428 rc = 0; |
| 429 } | 429 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 458 mem.zTitle[n] = 0; | 458 mem.zTitle[n] = 0; |
| 459 mem.nTitle = ROUND8(n); | 459 mem.nTitle = ROUND8(n); |
| 460 sqlite3_mutex_leave(mem.mutex); | 460 sqlite3_mutex_leave(mem.mutex); |
| 461 } | 461 } |
| 462 | 462 |
| 463 void sqlite3MemdebugSync(){ | 463 void sqlite3MemdebugSync(){ |
| 464 struct MemBlockHdr *pHdr; | 464 struct MemBlockHdr *pHdr; |
| 465 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ | 465 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ |
| 466 void **pBt = (void**)pHdr; | 466 void **pBt = (void**)pHdr; |
| 467 pBt -= pHdr->nBacktraceSlots; | 467 pBt -= pHdr->nBacktraceSlots; |
| 468 mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]); | 468 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]); |
| 469 } | 469 } |
| 470 } | 470 } |
| 471 | 471 |
| 472 /* | 472 /* |
| 473 ** Open the file indicated and write a log of all unfreed memory | 473 ** Open the file indicated and write a log of all unfreed memory |
| 474 ** allocations into that log. | 474 ** allocations into that log. |
| 475 */ | 475 */ |
| 476 void sqlite3MemdebugDump(const char *zFilename){ | 476 void sqlite3MemdebugDump(const char *zFilename){ |
| 477 FILE *out; | 477 FILE *out; |
| 478 struct MemBlockHdr *pHdr; | 478 struct MemBlockHdr *pHdr; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 int i; | 519 int i; |
| 520 int nTotal = 0; | 520 int nTotal = 0; |
| 521 for(i=0; i<NCSIZE; i++){ | 521 for(i=0; i<NCSIZE; i++){ |
| 522 nTotal += mem.nAlloc[i]; | 522 nTotal += mem.nAlloc[i]; |
| 523 } | 523 } |
| 524 return nTotal; | 524 return nTotal; |
| 525 } | 525 } |
| 526 | 526 |
| 527 | 527 |
| 528 #endif /* SQLITE_MEMDEBUG */ | 528 #endif /* SQLITE_MEMDEBUG */ |
| OLD | NEW |