1.1 --- a/extensions/sqlite/sqlite-source/mem2.c Fri Jul 11 06:51:21 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,546 +0,0 @@
1.4 -/*
1.5 -** 2007 August 15
1.6 -**
1.7 -** The author disclaims copyright to this source code. In place of
1.8 -** a legal notice, here is a blessing:
1.9 -**
1.10 -** May you do good and not evil.
1.11 -** May you find forgiveness for yourself and forgive others.
1.12 -** May you share freely, never taking more than you give.
1.13 -**
1.14 -*************************************************************************
1.15 -** This file contains the C functions that implement a memory
1.16 -** allocation subsystem for use by SQLite.
1.17 -**
1.18 -** $Id$
1.19 -*/
1.20 -
1.21 -/*
1.22 -** This version of the memory allocator is used only if the
1.23 -** SQLITE_MEMDEBUG macro is defined and SQLITE_OMIT_MEMORY_ALLOCATION
1.24 -** is not defined.
1.25 -*/
1.26 -#if defined(SQLITE_MEMDEBUG) && !defined(SQLITE_OMIT_MEMORY_ALLOCATION)
1.27 -
1.28 -/*
1.29 -** We will eventually construct multiple memory allocation subsystems
1.30 -** suitable for use in various contexts:
1.31 -**
1.32 -** * Normal multi-threaded builds
1.33 -** * Normal single-threaded builds
1.34 -** * Debugging builds
1.35 -**
1.36 -** This version is suitable for use in debugging builds.
1.37 -**
1.38 -** Features:
1.39 -**
1.40 -** * Every allocate has guards at both ends.
1.41 -** * New allocations are initialized with randomness
1.42 -** * Allocations are overwritten with randomness when freed
1.43 -** * Optional logs of malloc activity generated
1.44 -** * Summary of outstanding allocations with backtraces to the
1.45 -** point of allocation.
1.46 -** * The ability to simulate memory allocation failure
1.47 -*/
1.48 -#include "sqliteInt.h"
1.49 -#include <stdio.h>
1.50 -
1.51 -/*
1.52 -** The backtrace functionality is only available with GLIBC
1.53 -*/
1.54 -#ifdef __GLIBC__
1.55 - extern int backtrace(void**,int);
1.56 - extern void backtrace_symbols_fd(void*const*,int,int);
1.57 -#else
1.58 -# define backtrace(A,B) 0
1.59 -# define backtrace_symbols_fd(A,B,C)
1.60 -#endif
1.61 -
1.62 -/*
1.63 -** Each memory allocation looks like this:
1.64 -**
1.65 -** ------------------------------------------------------------------------
1.66 -** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard |
1.67 -** ------------------------------------------------------------------------
1.68 -**
1.69 -** The application code sees only a pointer to the allocation. We have
1.70 -** to back up from the allocation pointer to find the MemBlockHdr. The
1.71 -** MemBlockHdr tells us the size of the allocation and the number of
1.72 -** backtrace pointers. There is also a guard word at the end of the
1.73 -** MemBlockHdr.
1.74 -*/
1.75 -struct MemBlockHdr {
1.76 - struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */
1.77 - int iSize; /* Size of this allocation */
1.78 - char nBacktrace; /* Number of backtraces on this alloc */
1.79 - char nBacktraceSlots; /* Available backtrace slots */
1.80 - short nTitle; /* Bytes of title; includes '\0' */
1.81 - int iForeGuard; /* Guard word for sanity */
1.82 -};
1.83 -
1.84 -/*
1.85 -** Guard words
1.86 -*/
1.87 -#define FOREGUARD 0x80F5E153
1.88 -#define REARGUARD 0xE4676B53
1.89 -
1.90 -/*
1.91 -** All of the static variables used by this module are collected
1.92 -** into a single structure named "mem". This is to keep the
1.93 -** static variables organized and to reduce namespace pollution
1.94 -** when this module is combined with other in the amalgamation.
1.95 -*/
1.96 -static struct {
1.97 - /*
1.98 - ** The alarm callback and its arguments. The mem.mutex lock will
1.99 - ** be held while the callback is running. Recursive calls into
1.100 - ** the memory subsystem are allowed, but no new callbacks will be
1.101 - ** issued. The alarmBusy variable is set to prevent recursive
1.102 - ** callbacks.
1.103 - */
1.104 - sqlite3_int64 alarmThreshold;
1.105 - void (*alarmCallback)(void*, sqlite3_int64, int);
1.106 - void *alarmArg;
1.107 - int alarmBusy;
1.108 -
1.109 - /*
1.110 - ** Mutex to control access to the memory allocation subsystem.
1.111 - */
1.112 - sqlite3_mutex *mutex;
1.113 -
1.114 - /*
1.115 - ** Current allocation and high-water mark.
1.116 - */
1.117 - sqlite3_int64 nowUsed;
1.118 - sqlite3_int64 mxUsed;
1.119 -
1.120 - /*
1.121 - ** Head and tail of a linked list of all outstanding allocations
1.122 - */
1.123 - struct MemBlockHdr *pFirst;
1.124 - struct MemBlockHdr *pLast;
1.125 -
1.126 - /*
1.127 - ** The number of levels of backtrace to save in new allocations.
1.128 - */
1.129 - int nBacktrace;
1.130 -
1.131 - /*
1.132 - ** Title text to insert in front of each block
1.133 - */
1.134 - int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */
1.135 - char zTitle[100]; /* The title text */
1.136 -
1.137 - /*
1.138 - ** These values are used to simulate malloc failures. When
1.139 - ** iFail is 1, simulate a malloc failures and reset the value
1.140 - ** to iReset.
1.141 - */
1.142 - int iFail; /* Decrement and fail malloc when this is 1 */
1.143 - int iReset; /* When malloc fails set iiFail to this value */
1.144 - int iFailCnt; /* Number of failures */
1.145 - int iBenignFailCnt; /* Number of benign failures */
1.146 - int iNextIsBenign; /* True if the next call to malloc may fail benignly */
1.147 - int iIsBenign; /* All malloc calls may fail benignly */
1.148 -
1.149 - /*
1.150 - ** sqlite3MallocDisallow() increments the following counter.
1.151 - ** sqlite3MallocAllow() decrements it.
1.152 - */
1.153 - int disallow; /* Do not allow memory allocation */
1.154 -
1.155 -
1.156 -} mem;
1.157 -
1.158 -
1.159 -/*
1.160 -** Enter the mutex mem.mutex. Allocate it if it is not already allocated.
1.161 -*/
1.162 -static void enterMem(void){
1.163 - if( mem.mutex==0 ){
1.164 - mem.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM);
1.165 - }
1.166 - sqlite3_mutex_enter(mem.mutex);
1.167 -}
1.168 -
1.169 -/*
1.170 -** Return the amount of memory currently checked out.
1.171 -*/
1.172 -sqlite3_int64 sqlite3_memory_used(void){
1.173 - sqlite3_int64 n;
1.174 - enterMem();
1.175 - n = mem.nowUsed;
1.176 - sqlite3_mutex_leave(mem.mutex);
1.177 - return n;
1.178 -}
1.179 -
1.180 -/*
1.181 -** Return the maximum amount of memory that has ever been
1.182 -** checked out since either the beginning of this process
1.183 -** or since the most recent reset.
1.184 -*/
1.185 -sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
1.186 - sqlite3_int64 n;
1.187 - enterMem();
1.188 - n = mem.mxUsed;
1.189 - if( resetFlag ){
1.190 - mem.mxUsed = mem.nowUsed;
1.191 - }
1.192 - sqlite3_mutex_leave(mem.mutex);
1.193 - return n;
1.194 -}
1.195 -
1.196 -/*
1.197 -** Change the alarm callback
1.198 -*/
1.199 -int sqlite3_memory_alarm(
1.200 - void(*xCallback)(void *pArg, sqlite3_int64 used, int N),
1.201 - void *pArg,
1.202 - sqlite3_int64 iThreshold
1.203 -){
1.204 - enterMem();
1.205 - mem.alarmCallback = xCallback;
1.206 - mem.alarmArg = pArg;
1.207 - mem.alarmThreshold = iThreshold;
1.208 - sqlite3_mutex_leave(mem.mutex);
1.209 - return SQLITE_OK;
1.210 -}
1.211 -
1.212 -/*
1.213 -** Trigger the alarm
1.214 -*/
1.215 -static void sqlite3MemsysAlarm(int nByte){
1.216 - void (*xCallback)(void*,sqlite3_int64,int);
1.217 - sqlite3_int64 nowUsed;
1.218 - void *pArg;
1.219 - if( mem.alarmCallback==0 || mem.alarmBusy ) return;
1.220 - mem.alarmBusy = 1;
1.221 - xCallback = mem.alarmCallback;
1.222 - nowUsed = mem.nowUsed;
1.223 - pArg = mem.alarmArg;
1.224 - sqlite3_mutex_leave(mem.mutex);
1.225 - xCallback(pArg, nowUsed, nByte);
1.226 - sqlite3_mutex_enter(mem.mutex);
1.227 - mem.alarmBusy = 0;
1.228 -}
1.229 -
1.230 -/*
1.231 -** Given an allocation, find the MemBlockHdr for that allocation.
1.232 -**
1.233 -** This routine checks the guards at either end of the allocation and
1.234 -** if they are incorrect it asserts.
1.235 -*/
1.236 -static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
1.237 - struct MemBlockHdr *p;
1.238 - int *pInt;
1.239 -
1.240 - p = (struct MemBlockHdr*)pAllocation;
1.241 - p--;
1.242 - assert( p->iForeGuard==FOREGUARD );
1.243 - assert( (p->iSize & 3)==0 );
1.244 - pInt = (int*)pAllocation;
1.245 - assert( pInt[p->iSize/sizeof(int)]==REARGUARD );
1.246 - return p;
1.247 -}
1.248 -
1.249 -/*
1.250 -** This routine is called once the first time a simulated memory
1.251 -** failure occurs. The sole purpose of this routine is to provide
1.252 -** a convenient place to set a debugger breakpoint when debugging
1.253 -** errors related to malloc() failures.
1.254 -*/
1.255 -static void sqlite3MemsysFailed(void){
1.256 - mem.iFailCnt = 0;
1.257 - mem.iBenignFailCnt = 0;
1.258 -}
1.259 -
1.260 -/*
1.261 -** Allocate nByte bytes of memory.
1.262 -*/
1.263 -void *sqlite3_malloc(int nByte){
1.264 - struct MemBlockHdr *pHdr;
1.265 - void **pBt;
1.266 - char *z;
1.267 - int *pInt;
1.268 - void *p = 0;
1.269 - int totalSize;
1.270 -
1.271 - if( nByte>0 ){
1.272 - enterMem();
1.273 - assert( mem.disallow==0 );
1.274 - if( mem.alarmCallback!=0 && mem.nowUsed+nByte>=mem.alarmThreshold ){
1.275 - sqlite3MemsysAlarm(nByte);
1.276 - }
1.277 - nByte = (nByte+3)&~3;
1.278 - totalSize = nByte + sizeof(*pHdr) + sizeof(int) +
1.279 - mem.nBacktrace*sizeof(void*) + mem.nTitle;
1.280 - if( mem.iFail>0 ){
1.281 - if( mem.iFail==1 ){
1.282 - p = 0;
1.283 - mem.iFail = mem.iReset;
1.284 - if( mem.iFailCnt==0 ){
1.285 - sqlite3MemsysFailed(); /* A place to set a breakpoint */
1.286 - }
1.287 - mem.iFailCnt++;
1.288 - if( mem.iNextIsBenign || mem.iIsBenign ){
1.289 - mem.iBenignFailCnt++;
1.290 - }
1.291 - }else{
1.292 - p = malloc(totalSize);
1.293 - mem.iFail--;
1.294 - }
1.295 - }else{
1.296 - p = malloc(totalSize);
1.297 - if( p==0 ){
1.298 - sqlite3MemsysAlarm(nByte);
1.299 - p = malloc(totalSize);
1.300 - }
1.301 - }
1.302 - if( p ){
1.303 - z = p;
1.304 - pBt = (void**)&z[mem.nTitle];
1.305 - pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
1.306 - pHdr->pNext = 0;
1.307 - pHdr->pPrev = mem.pLast;
1.308 - if( mem.pLast ){
1.309 - mem.pLast->pNext = pHdr;
1.310 - }else{
1.311 - mem.pFirst = pHdr;
1.312 - }
1.313 - mem.pLast = pHdr;
1.314 - pHdr->iForeGuard = FOREGUARD;
1.315 - pHdr->nBacktraceSlots = mem.nBacktrace;
1.316 - pHdr->nTitle = mem.nTitle;
1.317 - if( mem.nBacktrace ){
1.318 - void *aAddr[40];
1.319 - pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
1.320 - memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
1.321 - }else{
1.322 - pHdr->nBacktrace = 0;
1.323 - }
1.324 - if( mem.nTitle ){
1.325 - memcpy(z, mem.zTitle, mem.nTitle);
1.326 - }
1.327 - pHdr->iSize = nByte;
1.328 - pInt = (int*)&pHdr[1];
1.329 - pInt[nByte/sizeof(int)] = REARGUARD;
1.330 - memset(pInt, 0x65, nByte);
1.331 - mem.nowUsed += nByte;
1.332 - if( mem.nowUsed>mem.mxUsed ){
1.333 - mem.mxUsed = mem.nowUsed;
1.334 - }
1.335 - p = (void*)pInt;
1.336 - }
1.337 - sqlite3_mutex_leave(mem.mutex);
1.338 - }
1.339 - mem.iNextIsBenign = 0;
1.340 - return p;
1.341 -}
1.342 -
1.343 -/*
1.344 -** Free memory.
1.345 -*/
1.346 -void sqlite3_free(void *pPrior){
1.347 - struct MemBlockHdr *pHdr;
1.348 - void **pBt;
1.349 - char *z;
1.350 - if( pPrior==0 ){
1.351 - return;
1.352 - }
1.353 - assert( mem.mutex!=0 );
1.354 - pHdr = sqlite3MemsysGetHeader(pPrior);
1.355 - pBt = (void**)pHdr;
1.356 - pBt -= pHdr->nBacktraceSlots;
1.357 - sqlite3_mutex_enter(mem.mutex);
1.358 - mem.nowUsed -= pHdr->iSize;
1.359 - if( pHdr->pPrev ){
1.360 - assert( pHdr->pPrev->pNext==pHdr );
1.361 - pHdr->pPrev->pNext = pHdr->pNext;
1.362 - }else{
1.363 - assert( mem.pFirst==pHdr );
1.364 - mem.pFirst = pHdr->pNext;
1.365 - }
1.366 - if( pHdr->pNext ){
1.367 - assert( pHdr->pNext->pPrev==pHdr );
1.368 - pHdr->pNext->pPrev = pHdr->pPrev;
1.369 - }else{
1.370 - assert( mem.pLast==pHdr );
1.371 - mem.pLast = pHdr->pPrev;
1.372 - }
1.373 - z = (char*)pBt;
1.374 - z -= pHdr->nTitle;
1.375 - memset(z, 0x2b, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
1.376 - pHdr->iSize + sizeof(int) + pHdr->nTitle);
1.377 - free(z);
1.378 - sqlite3_mutex_leave(mem.mutex);
1.379 -}
1.380 -
1.381 -/*
1.382 -** Change the size of an existing memory allocation.
1.383 -**
1.384 -** For this debugging implementation, we *always* make a copy of the
1.385 -** allocation into a new place in memory. In this way, if the
1.386 -** higher level code is using pointer to the old allocation, it is
1.387 -** much more likely to break and we are much more liking to find
1.388 -** the error.
1.389 -*/
1.390 -void *sqlite3_realloc(void *pPrior, int nByte){
1.391 - struct MemBlockHdr *pOldHdr;
1.392 - void *pNew;
1.393 - if( pPrior==0 ){
1.394 - return sqlite3_malloc(nByte);
1.395 - }
1.396 - if( nByte<=0 ){
1.397 - sqlite3_free(pPrior);
1.398 - return 0;
1.399 - }
1.400 - assert( mem.disallow==0 );
1.401 - pOldHdr = sqlite3MemsysGetHeader(pPrior);
1.402 - pNew = sqlite3_malloc(nByte);
1.403 - if( pNew ){
1.404 - memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
1.405 - if( nByte>pOldHdr->iSize ){
1.406 - memset(&((char*)pNew)[pOldHdr->iSize], 0x2b, nByte - pOldHdr->iSize);
1.407 - }
1.408 - sqlite3_free(pPrior);
1.409 - }
1.410 - return pNew;
1.411 -}
1.412 -
1.413 -/*
1.414 -** Set the number of backtrace levels kept for each allocation.
1.415 -** A value of zero turns of backtracing. The number is always rounded
1.416 -** up to a multiple of 2.
1.417 -*/
1.418 -void sqlite3_memdebug_backtrace(int depth){
1.419 - if( depth<0 ){ depth = 0; }
1.420 - if( depth>20 ){ depth = 20; }
1.421 - depth = (depth+1)&0xfe;
1.422 - mem.nBacktrace = depth;
1.423 -}
1.424 -
1.425 -/*
1.426 -** Set the title string for subsequent allocations.
1.427 -*/
1.428 -void sqlite3_memdebug_settitle(const char *zTitle){
1.429 - int n = strlen(zTitle) + 1;
1.430 - enterMem();
1.431 - if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
1.432 - memcpy(mem.zTitle, zTitle, n);
1.433 - mem.zTitle[n] = 0;
1.434 - mem.nTitle = (n+3)&~3;
1.435 - sqlite3_mutex_leave(mem.mutex);
1.436 -}
1.437 -
1.438 -/*
1.439 -** Open the file indicated and write a log of all unfreed memory
1.440 -** allocations into that log.
1.441 -*/
1.442 -void sqlite3_memdebug_dump(const char *zFilename){
1.443 - FILE *out;
1.444 - struct MemBlockHdr *pHdr;
1.445 - void **pBt;
1.446 - out = fopen(zFilename, "w");
1.447 - if( out==0 ){
1.448 - fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
1.449 - zFilename);
1.450 - return;
1.451 - }
1.452 - for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
1.453 - char *z = (char*)pHdr;
1.454 - z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
1.455 - fprintf(out, "**** %d bytes at %p from %s ****\n",
1.456 - pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
1.457 - if( pHdr->nBacktrace ){
1.458 - fflush(out);
1.459 - pBt = (void**)pHdr;
1.460 - pBt -= pHdr->nBacktraceSlots;
1.461 - backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
1.462 - fprintf(out, "\n");
1.463 - }
1.464 - }
1.465 - fclose(out);
1.466 -}
1.467 -
1.468 -/*
1.469 -** This routine is used to simulate malloc failures.
1.470 -**
1.471 -** After calling this routine, there will be iFail successful
1.472 -** memory allocations and then a failure. If iRepeat is 1
1.473 -** all subsequent memory allocations will fail. If iRepeat is
1.474 -** 0, only a single allocation will fail. If iRepeat is negative
1.475 -** then the previous setting for iRepeat is unchanged.
1.476 -**
1.477 -** Each call to this routine overrides the previous. To disable
1.478 -** the simulated allocation failure mechanism, set iFail to -1.
1.479 -**
1.480 -** This routine returns the number of simulated failures that have
1.481 -** occurred since the previous call.
1.482 -*/
1.483 -int sqlite3_memdebug_fail(int iFail, int iRepeat, int *piBenign){
1.484 - int n = mem.iFailCnt;
1.485 - if( piBenign ){
1.486 - *piBenign = mem.iBenignFailCnt;
1.487 - }
1.488 - mem.iFail = iFail+1;
1.489 - if( iRepeat>=0 ){
1.490 - mem.iReset = iRepeat;
1.491 - }
1.492 - mem.iFailCnt = 0;
1.493 - mem.iBenignFailCnt = 0;
1.494 - return n;
1.495 -}
1.496 -
1.497 -int sqlite3_memdebug_pending(){
1.498 - return (mem.iFail-1);
1.499 -}
1.500 -
1.501 -/*
1.502 -** The following three functions are used to indicate to the test
1.503 -** infrastructure which malloc() calls may fail benignly without
1.504 -** affecting functionality. This can happen when resizing hash tables
1.505 -** (failing to resize a hash-table is a performance hit, but not an
1.506 -** error) or sometimes during a rollback operation.
1.507 -**
1.508 -** If the argument is true, sqlite3MallocBenignFailure() indicates that the
1.509 -** next call to allocate memory may fail benignly.
1.510 -**
1.511 -** If sqlite3MallocEnterBenignBlock() is called with a non-zero argument,
1.512 -** then all memory allocations requested before the next call to
1.513 -** sqlite3MallocLeaveBenignBlock() may fail benignly.
1.514 -*/
1.515 -void sqlite3MallocBenignFailure(int isBenign){
1.516 - if( isBenign ){
1.517 - mem.iNextIsBenign = 1;
1.518 - }
1.519 -}
1.520 -void sqlite3MallocEnterBenignBlock(int isBenign){
1.521 - if( isBenign ){
1.522 - mem.iIsBenign = 1;
1.523 - }
1.524 -}
1.525 -void sqlite3MallocLeaveBenignBlock(){
1.526 - mem.iIsBenign = 0;
1.527 -}
1.528 -
1.529 -/*
1.530 -** The following two routines are used to assert that no memory
1.531 -** allocations occur between one call and the next. The use of
1.532 -** these routines does not change the computed results in any way.
1.533 -** These routines are like asserts.
1.534 -*/
1.535 -void sqlite3MallocDisallow(void){
1.536 - assert( mem.mutex!=0 );
1.537 - sqlite3_mutex_enter(mem.mutex);
1.538 - mem.disallow++;
1.539 - sqlite3_mutex_leave(mem.mutex);
1.540 -}
1.541 -void sqlite3MallocAllow(void){
1.542 - assert( mem.mutex );
1.543 - sqlite3_mutex_enter(mem.mutex);
1.544 - assert( mem.disallow>0 );
1.545 - mem.disallow--;
1.546 - sqlite3_mutex_leave(mem.mutex);
1.547 -}
1.548 -
1.549 -#endif /* SQLITE_MEMDEBUG && !SQLITE_OMIT_MEMORY_ALLOCATION */