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