extensions/sqlite/sqlite-source/mem2.c
author Scott Ehlert <ds@alliedmods.net>
Sun Mar 30 07:00:22 2008 +0000 (2008-03-30)
changeset 1952 b7aa2b626807
permissions -rw-r--r--
Spring Cleaning, Part Ichi (1)
Various minor things done to project files
Updated sample extension project file and updated makefile to the new unified version (more changes likely on the way)
Updated regex project file and makefile
ds@1952
     1
/*
ds@1952
     2
** 2007 August 15
ds@1952
     3
**
ds@1952
     4
** The author disclaims copyright to this source code.  In place of
ds@1952
     5
** a legal notice, here is a blessing:
ds@1952
     6
**
ds@1952
     7
**    May you do good and not evil.
ds@1952
     8
**    May you find forgiveness for yourself and forgive others.
ds@1952
     9
**    May you share freely, never taking more than you give.
ds@1952
    10
**
ds@1952
    11
*************************************************************************
ds@1952
    12
** This file contains the C functions that implement a memory
ds@1952
    13
** allocation subsystem for use by SQLite.  
ds@1952
    14
**
ds@1952
    15
** $Id$
ds@1952
    16
*/
ds@1952
    17
ds@1952
    18
/*
ds@1952
    19
** This version of the memory allocator is used only if the
ds@1952
    20
** SQLITE_MEMDEBUG macro is defined and SQLITE_OMIT_MEMORY_ALLOCATION
ds@1952
    21
** is not defined.
ds@1952
    22
*/
ds@1952
    23
#if defined(SQLITE_MEMDEBUG) && !defined(SQLITE_OMIT_MEMORY_ALLOCATION)
ds@1952
    24
ds@1952
    25
/*
ds@1952
    26
** We will eventually construct multiple memory allocation subsystems
ds@1952
    27
** suitable for use in various contexts:
ds@1952
    28
**
ds@1952
    29
**    *  Normal multi-threaded builds
ds@1952
    30
**    *  Normal single-threaded builds
ds@1952
    31
**    *  Debugging builds
ds@1952
    32
**
ds@1952
    33
** This version is suitable for use in debugging builds.
ds@1952
    34
**
ds@1952
    35
** Features:
ds@1952
    36
**
ds@1952
    37
**    * Every allocate has guards at both ends.
ds@1952
    38
**    * New allocations are initialized with randomness
ds@1952
    39
**    * Allocations are overwritten with randomness when freed
ds@1952
    40
**    * Optional logs of malloc activity generated
ds@1952
    41
**    * Summary of outstanding allocations with backtraces to the
ds@1952
    42
**      point of allocation.
ds@1952
    43
**    * The ability to simulate memory allocation failure
ds@1952
    44
*/
ds@1952
    45
#include "sqliteInt.h"
ds@1952
    46
#include <stdio.h>
ds@1952
    47
ds@1952
    48
/*
ds@1952
    49
** The backtrace functionality is only available with GLIBC
ds@1952
    50
*/
ds@1952
    51
#ifdef __GLIBC__
ds@1952
    52
  extern int backtrace(void**,int);
ds@1952
    53
  extern void backtrace_symbols_fd(void*const*,int,int);
ds@1952
    54
#else
ds@1952
    55
# define backtrace(A,B) 0
ds@1952
    56
# define backtrace_symbols_fd(A,B,C)
ds@1952
    57
#endif
ds@1952
    58
ds@1952
    59
/*
ds@1952
    60
** Each memory allocation looks like this:
ds@1952
    61
**
ds@1952
    62
**  ------------------------------------------------------------------------
ds@1952
    63
**  | Title |  backtrace pointers |  MemBlockHdr |  allocation |  EndGuard |
ds@1952
    64
**  ------------------------------------------------------------------------
ds@1952
    65
**
ds@1952
    66
** The application code sees only a pointer to the allocation.  We have
ds@1952
    67
** to back up from the allocation pointer to find the MemBlockHdr.  The
ds@1952
    68
** MemBlockHdr tells us the size of the allocation and the number of
ds@1952
    69
** backtrace pointers.  There is also a guard word at the end of the
ds@1952
    70
** MemBlockHdr.
ds@1952
    71
*/
ds@1952
    72
struct MemBlockHdr {
ds@1952
    73
  struct MemBlockHdr *pNext, *pPrev;  /* Linked list of all unfreed memory */
ds@1952
    74
  int iSize;                          /* Size of this allocation */
ds@1952
    75
  char nBacktrace;                    /* Number of backtraces on this alloc */
ds@1952
    76
  char nBacktraceSlots;               /* Available backtrace slots */
ds@1952
    77
  short nTitle;                       /* Bytes of title; includes '\0' */
ds@1952
    78
  int iForeGuard;                     /* Guard word for sanity */
ds@1952
    79
};
ds@1952
    80
ds@1952
    81
/*
ds@1952
    82
** Guard words
ds@1952
    83
*/
ds@1952
    84
#define FOREGUARD 0x80F5E153
ds@1952
    85
#define REARGUARD 0xE4676B53
ds@1952
    86
ds@1952
    87
/*
ds@1952
    88
** All of the static variables used by this module are collected
ds@1952
    89
** into a single structure named "mem".  This is to keep the
ds@1952
    90
** static variables organized and to reduce namespace pollution
ds@1952
    91
** when this module is combined with other in the amalgamation.
ds@1952
    92
*/
ds@1952
    93
static struct {
ds@1952
    94
  /*
ds@1952
    95
  ** The alarm callback and its arguments.  The mem.mutex lock will
ds@1952
    96
  ** be held while the callback is running.  Recursive calls into
ds@1952
    97
  ** the memory subsystem are allowed, but no new callbacks will be
ds@1952
    98
  ** issued.  The alarmBusy variable is set to prevent recursive
ds@1952
    99
  ** callbacks.
ds@1952
   100
  */
ds@1952
   101
  sqlite3_int64 alarmThreshold;
ds@1952
   102
  void (*alarmCallback)(void*, sqlite3_int64, int);
ds@1952
   103
  void *alarmArg;
ds@1952
   104
  int alarmBusy;
ds@1952
   105
  
ds@1952
   106
  /*
ds@1952
   107
  ** Mutex to control access to the memory allocation subsystem.
ds@1952
   108
  */
ds@1952
   109
  sqlite3_mutex *mutex;
ds@1952
   110
  
ds@1952
   111
  /*
ds@1952
   112
  ** Current allocation and high-water mark.
ds@1952
   113
  */
ds@1952
   114
  sqlite3_int64 nowUsed;
ds@1952
   115
  sqlite3_int64 mxUsed;
ds@1952
   116
  
ds@1952
   117
  /*
ds@1952
   118
  ** Head and tail of a linked list of all outstanding allocations
ds@1952
   119
  */
ds@1952
   120
  struct MemBlockHdr *pFirst;
ds@1952
   121
  struct MemBlockHdr *pLast;
ds@1952
   122
  
ds@1952
   123
  /*
ds@1952
   124
  ** The number of levels of backtrace to save in new allocations.
ds@1952
   125
  */
ds@1952
   126
  int nBacktrace;
ds@1952
   127
ds@1952
   128
  /*
ds@1952
   129
  ** Title text to insert in front of each block
ds@1952
   130
  */
ds@1952
   131
  int nTitle;        /* Bytes of zTitle to save.  Includes '\0' and padding */
ds@1952
   132
  char zTitle[100];  /* The title text */
ds@1952
   133
ds@1952
   134
  /*
ds@1952
   135
  ** These values are used to simulate malloc failures.  When
ds@1952
   136
  ** iFail is 1, simulate a malloc failures and reset the value
ds@1952
   137
  ** to iReset.
ds@1952
   138
  */
ds@1952
   139
  int iFail;    /* Decrement and fail malloc when this is 1 */
ds@1952
   140
  int iReset;   /* When malloc fails set iiFail to this value */
ds@1952
   141
  int iFailCnt;         /* Number of failures */
ds@1952
   142
  int iBenignFailCnt;   /* Number of benign failures */
ds@1952
   143
  int iNextIsBenign;    /* True if the next call to malloc may fail benignly */
ds@1952
   144
  int iIsBenign;        /* All malloc calls may fail benignly */
ds@1952
   145
ds@1952
   146
  /* 
ds@1952
   147
  ** sqlite3MallocDisallow() increments the following counter.
ds@1952
   148
  ** sqlite3MallocAllow() decrements it.
ds@1952
   149
  */
ds@1952
   150
  int disallow; /* Do not allow memory allocation */
ds@1952
   151
  
ds@1952
   152
  
ds@1952
   153
} mem;
ds@1952
   154
ds@1952
   155
ds@1952
   156
/*
ds@1952
   157
** Enter the mutex mem.mutex. Allocate it if it is not already allocated.
ds@1952
   158
*/
ds@1952
   159
static void enterMem(void){
ds@1952
   160
  if( mem.mutex==0 ){
ds@1952
   161
    mem.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM);
ds@1952
   162
  }
ds@1952
   163
  sqlite3_mutex_enter(mem.mutex);
ds@1952
   164
}
ds@1952
   165
ds@1952
   166
/*
ds@1952
   167
** Return the amount of memory currently checked out.
ds@1952
   168
*/
ds@1952
   169
sqlite3_int64 sqlite3_memory_used(void){
ds@1952
   170
  sqlite3_int64 n;
ds@1952
   171
  enterMem();
ds@1952
   172
  n = mem.nowUsed;
ds@1952
   173
  sqlite3_mutex_leave(mem.mutex);  
ds@1952
   174
  return n;
ds@1952
   175
}
ds@1952
   176
ds@1952
   177
/*
ds@1952
   178
** Return the maximum amount of memory that has ever been
ds@1952
   179
** checked out since either the beginning of this process
ds@1952
   180
** or since the most recent reset.
ds@1952
   181
*/
ds@1952
   182
sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
ds@1952
   183
  sqlite3_int64 n;
ds@1952
   184
  enterMem();
ds@1952
   185
  n = mem.mxUsed;
ds@1952
   186
  if( resetFlag ){
ds@1952
   187
    mem.mxUsed = mem.nowUsed;
ds@1952
   188
  }
ds@1952
   189
  sqlite3_mutex_leave(mem.mutex);  
ds@1952
   190
  return n;
ds@1952
   191
}
ds@1952
   192
ds@1952
   193
/*
ds@1952
   194
** Change the alarm callback
ds@1952
   195
*/
ds@1952
   196
int sqlite3_memory_alarm(
ds@1952
   197
  void(*xCallback)(void *pArg, sqlite3_int64 used, int N),
ds@1952
   198
  void *pArg,
ds@1952
   199
  sqlite3_int64 iThreshold
ds@1952
   200
){
ds@1952
   201
  enterMem();
ds@1952
   202
  mem.alarmCallback = xCallback;
ds@1952
   203
  mem.alarmArg = pArg;
ds@1952
   204
  mem.alarmThreshold = iThreshold;
ds@1952
   205
  sqlite3_mutex_leave(mem.mutex);
ds@1952
   206
  return SQLITE_OK;
ds@1952
   207
}
ds@1952
   208
ds@1952
   209
/*
ds@1952
   210
** Trigger the alarm 
ds@1952
   211
*/
ds@1952
   212
static void sqlite3MemsysAlarm(int nByte){
ds@1952
   213
  void (*xCallback)(void*,sqlite3_int64,int);
ds@1952
   214
  sqlite3_int64 nowUsed;
ds@1952
   215
  void *pArg;
ds@1952
   216
  if( mem.alarmCallback==0 || mem.alarmBusy  ) return;
ds@1952
   217
  mem.alarmBusy = 1;
ds@1952
   218
  xCallback = mem.alarmCallback;
ds@1952
   219
  nowUsed = mem.nowUsed;
ds@1952
   220
  pArg = mem.alarmArg;
ds@1952
   221
  sqlite3_mutex_leave(mem.mutex);
ds@1952
   222
  xCallback(pArg, nowUsed, nByte);
ds@1952
   223
  sqlite3_mutex_enter(mem.mutex);
ds@1952
   224
  mem.alarmBusy = 0;
ds@1952
   225
}
ds@1952
   226
ds@1952
   227
/*
ds@1952
   228
** Given an allocation, find the MemBlockHdr for that allocation.
ds@1952
   229
**
ds@1952
   230
** This routine checks the guards at either end of the allocation and
ds@1952
   231
** if they are incorrect it asserts.
ds@1952
   232
*/
ds@1952
   233
static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
ds@1952
   234
  struct MemBlockHdr *p;
ds@1952
   235
  int *pInt;
ds@1952
   236
ds@1952
   237
  p = (struct MemBlockHdr*)pAllocation;
ds@1952
   238
  p--;
ds@1952
   239
  assert( p->iForeGuard==FOREGUARD );
ds@1952
   240
  assert( (p->iSize & 3)==0 );
ds@1952
   241
  pInt = (int*)pAllocation;
ds@1952
   242
  assert( pInt[p->iSize/sizeof(int)]==REARGUARD );
ds@1952
   243
  return p;
ds@1952
   244
}
ds@1952
   245
ds@1952
   246
/*
ds@1952
   247
** This routine is called once the first time a simulated memory
ds@1952
   248
** failure occurs.  The sole purpose of this routine is to provide
ds@1952
   249
** a convenient place to set a debugger breakpoint when debugging
ds@1952
   250
** errors related to malloc() failures.
ds@1952
   251
*/
ds@1952
   252
static void sqlite3MemsysFailed(void){
ds@1952
   253
  mem.iFailCnt = 0;
ds@1952
   254
  mem.iBenignFailCnt = 0;
ds@1952
   255
}
ds@1952
   256
ds@1952
   257
/*
ds@1952
   258
** Allocate nByte bytes of memory.
ds@1952
   259
*/
ds@1952
   260
void *sqlite3_malloc(int nByte){
ds@1952
   261
  struct MemBlockHdr *pHdr;
ds@1952
   262
  void **pBt;
ds@1952
   263
  char *z;
ds@1952
   264
  int *pInt;
ds@1952
   265
  void *p = 0;
ds@1952
   266
  int totalSize;
ds@1952
   267
ds@1952
   268
  if( nByte>0 ){
ds@1952
   269
    enterMem();
ds@1952
   270
    assert( mem.disallow==0 );
ds@1952
   271
    if( mem.alarmCallback!=0 && mem.nowUsed+nByte>=mem.alarmThreshold ){
ds@1952
   272
      sqlite3MemsysAlarm(nByte);
ds@1952
   273
    }
ds@1952
   274
    nByte = (nByte+3)&~3;
ds@1952
   275
    totalSize = nByte + sizeof(*pHdr) + sizeof(int) +
ds@1952
   276
                 mem.nBacktrace*sizeof(void*) + mem.nTitle;
ds@1952
   277
    if( mem.iFail>0 ){
ds@1952
   278
      if( mem.iFail==1 ){
ds@1952
   279
        p = 0;
ds@1952
   280
        mem.iFail = mem.iReset;
ds@1952
   281
        if( mem.iFailCnt==0 ){
ds@1952
   282
          sqlite3MemsysFailed();  /* A place to set a breakpoint */
ds@1952
   283
        }
ds@1952
   284
        mem.iFailCnt++;
ds@1952
   285
        if( mem.iNextIsBenign || mem.iIsBenign ){
ds@1952
   286
          mem.iBenignFailCnt++;
ds@1952
   287
        }
ds@1952
   288
      }else{
ds@1952
   289
        p = malloc(totalSize);
ds@1952
   290
        mem.iFail--;
ds@1952
   291
      }
ds@1952
   292
    }else{
ds@1952
   293
      p = malloc(totalSize);
ds@1952
   294
      if( p==0 ){
ds@1952
   295
        sqlite3MemsysAlarm(nByte);
ds@1952
   296
        p = malloc(totalSize);
ds@1952
   297
      }
ds@1952
   298
    }
ds@1952
   299
    if( p ){
ds@1952
   300
      z = p;
ds@1952
   301
      pBt = (void**)&z[mem.nTitle];
ds@1952
   302
      pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
ds@1952
   303
      pHdr->pNext = 0;
ds@1952
   304
      pHdr->pPrev = mem.pLast;
ds@1952
   305
      if( mem.pLast ){
ds@1952
   306
        mem.pLast->pNext = pHdr;
ds@1952
   307
      }else{
ds@1952
   308
        mem.pFirst = pHdr;
ds@1952
   309
      }
ds@1952
   310
      mem.pLast = pHdr;
ds@1952
   311
      pHdr->iForeGuard = FOREGUARD;
ds@1952
   312
      pHdr->nBacktraceSlots = mem.nBacktrace;
ds@1952
   313
      pHdr->nTitle = mem.nTitle;
ds@1952
   314
      if( mem.nBacktrace ){
ds@1952
   315
        void *aAddr[40];
ds@1952
   316
        pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
ds@1952
   317
        memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
ds@1952
   318
      }else{
ds@1952
   319
        pHdr->nBacktrace = 0;
ds@1952
   320
      }
ds@1952
   321
      if( mem.nTitle ){
ds@1952
   322
        memcpy(z, mem.zTitle, mem.nTitle);
ds@1952
   323
      }
ds@1952
   324
      pHdr->iSize = nByte;
ds@1952
   325
      pInt = (int*)&pHdr[1];
ds@1952
   326
      pInt[nByte/sizeof(int)] = REARGUARD;
ds@1952
   327
      memset(pInt, 0x65, nByte);
ds@1952
   328
      mem.nowUsed += nByte;
ds@1952
   329
      if( mem.nowUsed>mem.mxUsed ){
ds@1952
   330
        mem.mxUsed = mem.nowUsed;
ds@1952
   331
      }
ds@1952
   332
      p = (void*)pInt;
ds@1952
   333
    }
ds@1952
   334
    sqlite3_mutex_leave(mem.mutex);
ds@1952
   335
  }
ds@1952
   336
  mem.iNextIsBenign = 0;
ds@1952
   337
  return p; 
ds@1952
   338
}
ds@1952
   339
ds@1952
   340
/*
ds@1952
   341
** Free memory.
ds@1952
   342
*/
ds@1952
   343
void sqlite3_free(void *pPrior){
ds@1952
   344
  struct MemBlockHdr *pHdr;
ds@1952
   345
  void **pBt;
ds@1952
   346
  char *z;
ds@1952
   347
  if( pPrior==0 ){
ds@1952
   348
    return;
ds@1952
   349
  }
ds@1952
   350
  assert( mem.mutex!=0 );
ds@1952
   351
  pHdr = sqlite3MemsysGetHeader(pPrior);
ds@1952
   352
  pBt = (void**)pHdr;
ds@1952
   353
  pBt -= pHdr->nBacktraceSlots;
ds@1952
   354
  sqlite3_mutex_enter(mem.mutex);
ds@1952
   355
  mem.nowUsed -= pHdr->iSize;
ds@1952
   356
  if( pHdr->pPrev ){
ds@1952
   357
    assert( pHdr->pPrev->pNext==pHdr );
ds@1952
   358
    pHdr->pPrev->pNext = pHdr->pNext;
ds@1952
   359
  }else{
ds@1952
   360
    assert( mem.pFirst==pHdr );
ds@1952
   361
    mem.pFirst = pHdr->pNext;
ds@1952
   362
  }
ds@1952
   363
  if( pHdr->pNext ){
ds@1952
   364
    assert( pHdr->pNext->pPrev==pHdr );
ds@1952
   365
    pHdr->pNext->pPrev = pHdr->pPrev;
ds@1952
   366
  }else{
ds@1952
   367
    assert( mem.pLast==pHdr );
ds@1952
   368
    mem.pLast = pHdr->pPrev;
ds@1952
   369
  }
ds@1952
   370
  z = (char*)pBt;
ds@1952
   371
  z -= pHdr->nTitle;
ds@1952
   372
  memset(z, 0x2b, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
ds@1952
   373
                  pHdr->iSize + sizeof(int) + pHdr->nTitle);
ds@1952
   374
  free(z);
ds@1952
   375
  sqlite3_mutex_leave(mem.mutex);  
ds@1952
   376
}
ds@1952
   377
ds@1952
   378
/*
ds@1952
   379
** Change the size of an existing memory allocation.
ds@1952
   380
**
ds@1952
   381
** For this debugging implementation, we *always* make a copy of the
ds@1952
   382
** allocation into a new place in memory.  In this way, if the 
ds@1952
   383
** higher level code is using pointer to the old allocation, it is 
ds@1952
   384
** much more likely to break and we are much more liking to find
ds@1952
   385
** the error.
ds@1952
   386
*/
ds@1952
   387
void *sqlite3_realloc(void *pPrior, int nByte){
ds@1952
   388
  struct MemBlockHdr *pOldHdr;
ds@1952
   389
  void *pNew;
ds@1952
   390
  if( pPrior==0 ){
ds@1952
   391
    return sqlite3_malloc(nByte);
ds@1952
   392
  }
ds@1952
   393
  if( nByte<=0 ){
ds@1952
   394
    sqlite3_free(pPrior);
ds@1952
   395
    return 0;
ds@1952
   396
  }
ds@1952
   397
  assert( mem.disallow==0 );
ds@1952
   398
  pOldHdr = sqlite3MemsysGetHeader(pPrior);
ds@1952
   399
  pNew = sqlite3_malloc(nByte);
ds@1952
   400
  if( pNew ){
ds@1952
   401
    memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
ds@1952
   402
    if( nByte>pOldHdr->iSize ){
ds@1952
   403
      memset(&((char*)pNew)[pOldHdr->iSize], 0x2b, nByte - pOldHdr->iSize);
ds@1952
   404
    }
ds@1952
   405
    sqlite3_free(pPrior);
ds@1952
   406
  }
ds@1952
   407
  return pNew;
ds@1952
   408
}
ds@1952
   409
ds@1952
   410
/*
ds@1952
   411
** Set the number of backtrace levels kept for each allocation.
ds@1952
   412
** A value of zero turns of backtracing.  The number is always rounded
ds@1952
   413
** up to a multiple of 2.
ds@1952
   414
*/
ds@1952
   415
void sqlite3_memdebug_backtrace(int depth){
ds@1952
   416
  if( depth<0 ){ depth = 0; }
ds@1952
   417
  if( depth>20 ){ depth = 20; }
ds@1952
   418
  depth = (depth+1)&0xfe;
ds@1952
   419
  mem.nBacktrace = depth;
ds@1952
   420
}
ds@1952
   421
ds@1952
   422
/*
ds@1952
   423
** Set the title string for subsequent allocations.
ds@1952
   424
*/
ds@1952
   425
void sqlite3_memdebug_settitle(const char *zTitle){
ds@1952
   426
  int n = strlen(zTitle) + 1;
ds@1952
   427
  enterMem();
ds@1952
   428
  if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
ds@1952
   429
  memcpy(mem.zTitle, zTitle, n);
ds@1952
   430
  mem.zTitle[n] = 0;
ds@1952
   431
  mem.nTitle = (n+3)&~3;
ds@1952
   432
  sqlite3_mutex_leave(mem.mutex);
ds@1952
   433
}
ds@1952
   434
ds@1952
   435
/*
ds@1952
   436
** Open the file indicated and write a log of all unfreed memory 
ds@1952
   437
** allocations into that log.
ds@1952
   438
*/
ds@1952
   439
void sqlite3_memdebug_dump(const char *zFilename){
ds@1952
   440
  FILE *out;
ds@1952
   441
  struct MemBlockHdr *pHdr;
ds@1952
   442
  void **pBt;
ds@1952
   443
  out = fopen(zFilename, "w");
ds@1952
   444
  if( out==0 ){
ds@1952
   445
    fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
ds@1952
   446
                    zFilename);
ds@1952
   447
    return;
ds@1952
   448
  }
ds@1952
   449
  for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
ds@1952
   450
    char *z = (char*)pHdr;
ds@1952
   451
    z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
ds@1952
   452
    fprintf(out, "**** %d bytes at %p from %s ****\n", 
ds@1952
   453
            pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
ds@1952
   454
    if( pHdr->nBacktrace ){
ds@1952
   455
      fflush(out);
ds@1952
   456
      pBt = (void**)pHdr;
ds@1952
   457
      pBt -= pHdr->nBacktraceSlots;
ds@1952
   458
      backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
ds@1952
   459
      fprintf(out, "\n");
ds@1952
   460
    }
ds@1952
   461
  }
ds@1952
   462
  fclose(out);
ds@1952
   463
}
ds@1952
   464
ds@1952
   465
/*
ds@1952
   466
** This routine is used to simulate malloc failures.
ds@1952
   467
**
ds@1952
   468
** After calling this routine, there will be iFail successful
ds@1952
   469
** memory allocations and then a failure.  If iRepeat is 1
ds@1952
   470
** all subsequent memory allocations will fail.  If iRepeat is
ds@1952
   471
** 0, only a single allocation will fail.  If iRepeat is negative
ds@1952
   472
** then the previous setting for iRepeat is unchanged.
ds@1952
   473
**
ds@1952
   474
** Each call to this routine overrides the previous.  To disable
ds@1952
   475
** the simulated allocation failure mechanism, set iFail to -1.
ds@1952
   476
**
ds@1952
   477
** This routine returns the number of simulated failures that have
ds@1952
   478
** occurred since the previous call.
ds@1952
   479
*/
ds@1952
   480
int sqlite3_memdebug_fail(int iFail, int iRepeat, int *piBenign){
ds@1952
   481
  int n = mem.iFailCnt;
ds@1952
   482
  if( piBenign ){
ds@1952
   483
    *piBenign = mem.iBenignFailCnt;
ds@1952
   484
  }
ds@1952
   485
  mem.iFail = iFail+1;
ds@1952
   486
  if( iRepeat>=0 ){
ds@1952
   487
    mem.iReset = iRepeat;
ds@1952
   488
  }
ds@1952
   489
  mem.iFailCnt = 0;
ds@1952
   490
  mem.iBenignFailCnt = 0;
ds@1952
   491
  return n;
ds@1952
   492
}
ds@1952
   493
ds@1952
   494
int sqlite3_memdebug_pending(){
ds@1952
   495
  return (mem.iFail-1);
ds@1952
   496
}
ds@1952
   497
ds@1952
   498
/*
ds@1952
   499
** The following three functions are used to indicate to the test 
ds@1952
   500
** infrastructure which malloc() calls may fail benignly without
ds@1952
   501
** affecting functionality. This can happen when resizing hash tables 
ds@1952
   502
** (failing to resize a hash-table is a performance hit, but not an 
ds@1952
   503
** error) or sometimes during a rollback operation.
ds@1952
   504
**
ds@1952
   505
** If the argument is true, sqlite3MallocBenignFailure() indicates that the
ds@1952
   506
** next call to allocate memory may fail benignly.
ds@1952
   507
**
ds@1952
   508
** If sqlite3MallocEnterBenignBlock() is called with a non-zero argument,
ds@1952
   509
** then all memory allocations requested before the next call to
ds@1952
   510
** sqlite3MallocLeaveBenignBlock() may fail benignly.
ds@1952
   511
*/
ds@1952
   512
void sqlite3MallocBenignFailure(int isBenign){
ds@1952
   513
  if( isBenign ){
ds@1952
   514
    mem.iNextIsBenign = 1;
ds@1952
   515
  }
ds@1952
   516
}
ds@1952
   517
void sqlite3MallocEnterBenignBlock(int isBenign){
ds@1952
   518
  if( isBenign ){
ds@1952
   519
    mem.iIsBenign = 1;
ds@1952
   520
  }
ds@1952
   521
}
ds@1952
   522
void sqlite3MallocLeaveBenignBlock(){
ds@1952
   523
  mem.iIsBenign = 0;
ds@1952
   524
}
ds@1952
   525
ds@1952
   526
/*
ds@1952
   527
** The following two routines are used to assert that no memory
ds@1952
   528
** allocations occur between one call and the next.  The use of
ds@1952
   529
** these routines does not change the computed results in any way.
ds@1952
   530
** These routines are like asserts.
ds@1952
   531
*/
ds@1952
   532
void sqlite3MallocDisallow(void){
ds@1952
   533
  assert( mem.mutex!=0 );
ds@1952
   534
  sqlite3_mutex_enter(mem.mutex);
ds@1952
   535
  mem.disallow++;
ds@1952
   536
  sqlite3_mutex_leave(mem.mutex);
ds@1952
   537
}
ds@1952
   538
void sqlite3MallocAllow(void){
ds@1952
   539
  assert( mem.mutex );
ds@1952
   540
  sqlite3_mutex_enter(mem.mutex);
ds@1952
   541
  assert( mem.disallow>0 );
ds@1952
   542
  mem.disallow--;
ds@1952
   543
  sqlite3_mutex_leave(mem.mutex);
ds@1952
   544
}
ds@1952
   545
ds@1952
   546
#endif /* SQLITE_MEMDEBUG && !SQLITE_OMIT_MEMORY_ALLOCATION */