engine/sqlite/src/mem4.cpp
author Sebastian Brannstrom <sebastianb@symbian.org>
Sat, 16 Oct 2010 15:36:19 +0100
branch3rded
changeset 275 e2c3225833c3
parent 2 29cda98b007e
permissions -rw-r--r--
Fix for bug 2818
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 14
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: mem4.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 attempts to obtain memory
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    20
** from mmap() if the size of the allocation is close to the size
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    21
** of a virtual memory page.  If the size of the allocation is different
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    22
** from the virtual memory page size, then ordinary malloc() is used.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    23
** Ordinary malloc is also used if space allocated to mmap() is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    24
** exhausted.
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
** Enable this memory allocation by compiling with -DSQLITE_MMAP_HEAP_SIZE=nnn
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    27
** where nnn is the maximum number of bytes of mmap-ed memory you want 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    28
** to support.   This module may choose to use less memory than requested.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    29
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    30
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    31
#if defined(SQLITE_MMAP_HEAP_SIZE)
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
#if defined(SQLITE_MEMDEBUG) || defined(SQLITE_MEMORY_SIZE)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    34
# error cannot use SQLITE_MMAP_HEAP_SIZE with either SQLITE_MEMDEBUG \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    35
        or SQLITE_MEMORY_SIZE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    36
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    37
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    38
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    39
** This is a test version of the memory allocator that attempts to
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    40
** use mmap() and madvise() for allocations and frees of approximately
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    41
** the virtual memory page size.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    42
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    43
#include <sys/types.h>
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    44
#include <sys/mman.h>
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    45
#include <errno.h>
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    46
#include "sqliteInt.h"
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    47
#include <unistd.h>
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
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
** All of the static variables used by this module are collected
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    52
** into a single structure named "mem".  This is to keep the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    53
** static variables organized and to reduce namespace pollution
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    54
** when this module is combined with other in the amalgamation.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    55
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    56
static struct {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    57
  /*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    58
  ** The alarm callback and its arguments.  The mem.mutex lock will
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    59
  ** be held while the callback is running.  Recursive calls into
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    60
  ** the memory subsystem are allowed, but no new callbacks will be
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    61
  ** issued.  The alarmBusy variable is set to prevent recursive
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    62
  ** callbacks.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    63
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    64
  sqlite3_int64 alarmThreshold;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    65
  void (*alarmCallback)(void*, sqlite3_int64,int);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    66
  void *alarmArg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    67
  int alarmBusy;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    68
  
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    69
  /*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    70
  ** Mutex to control access to the memory allocation subsystem.
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
  sqlite3_mutex *mutex;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    73
  
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    74
  /*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    75
  ** Current allocation and high-water mark.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    76
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    77
  sqlite3_int64 nowUsed;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    78
  sqlite3_int64 mxUsed;
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
  ** Current allocation and high-water marks for mmap allocated memory.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    82
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    83
  sqlite3_int64 nowUsedMMap;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    84
  sqlite3_int64 mxUsedMMap;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    85
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
  ** Size of a single mmap page.  Obtained from sysconf().
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    88
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    89
  int szPage;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    90
  int mnPage;
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
  ** The number of available mmap pages.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    94
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    95
  int nPage;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    96
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
  ** Index of the first free page.  0 means no pages have been freed.
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
  int firstFree;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   101
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   102
  /* First unused page on the top of the heap.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   103
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   104
  int firstUnused;
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
  /*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   107
  ** Bulk memory obtained from from mmap().
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   108
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   109
  char *mmapHeap;   /* first byte of the heap */ 
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
} mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   112
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
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   115
** 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
   116
** The mmap() region is initialized the first time this routine is called.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   117
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   118
static void memsys4Enter(void){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   119
  if( mem.mutex==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   120
    mem.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM);
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
  sqlite3_mutex_enter(mem.mutex);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   123
}
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
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   126
** Attempt to free memory to the mmap heap.  This only works if
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   127
** the pointer p is within the range of memory addresses that
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   128
** comprise the mmap heap.  Return 1 if the memory was freed
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   129
** successfully.  Return 0 if the pointer is out of range.
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
static int mmapFree(void *p){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   132
  char *z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   133
  int idx, *a;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   134
  if( mem.mmapHeap==MAP_FAILED || mem.nPage==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   135
    return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   136
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   137
  z = (char*)p;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   138
  idx = (z - mem.mmapHeap)/mem.szPage;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   139
  if( idx<1 || idx>=mem.nPage ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   140
    return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   141
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   142
  a = (int*)mem.mmapHeap;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   143
  a[idx] = a[mem.firstFree];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   144
  mem.firstFree = idx;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   145
  mem.nowUsedMMap -= mem.szPage;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   146
  madvise(p, mem.szPage, MADV_DONTNEED);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   147
  return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   148
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   149
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
** Attempt to allocate nBytes from the mmap heap.  Return a pointer
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   152
** to the allocated page.  Or, return NULL if the allocation fails.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   153
** 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   154
** The allocation will fail if nBytes is not the right size.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   155
** Or, the allocation will fail if the mmap heap has been exhausted.
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
static void *mmapAlloc(int nBytes){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   158
  int idx = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   159
  if( nBytes>mem.szPage || nBytes<mem.mnPage ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   160
    return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   161
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   162
  if( mem.nPage==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   163
    mem.szPage = sysconf(_SC_PAGE_SIZE);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   164
    mem.mnPage = mem.szPage - mem.szPage/10;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   165
    mem.nPage = SQLITE_MMAP_HEAP_SIZE/mem.szPage;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   166
    if( mem.nPage * sizeof(int) > mem.szPage ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   167
      mem.nPage = mem.szPage/sizeof(int);
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
    mem.mmapHeap =  mmap(0, mem.szPage*mem.nPage, PROT_WRITE|PROT_READ,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   170
                         MAP_ANONYMOUS|MAP_SHARED, -1, 0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   171
    if( mem.mmapHeap==MAP_FAILED ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   172
      mem.firstUnused = errno;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   173
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   174
      mem.firstUnused = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   175
      mem.nowUsedMMap = mem.szPage;
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
  if( mem.mmapHeap==MAP_FAILED ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   179
    return 0;
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
  if( mem.firstFree ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   182
    int idx = mem.firstFree;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   183
    int *a = (int*)mem.mmapHeap;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   184
    mem.firstFree = a[idx];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   185
  }else if( mem.firstUnused<mem.nPage ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   186
    idx = mem.firstUnused++;
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
  if( idx ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   189
    mem.nowUsedMMap += mem.szPage;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   190
    if( mem.nowUsedMMap>mem.mxUsedMMap ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   191
      mem.mxUsedMMap = mem.nowUsedMMap;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   192
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   193
    return (void*)&mem.mmapHeap[idx*mem.szPage];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   194
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   195
    return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   196
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   197
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   198
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   199
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   200
** Release the mmap-ed memory region if it is currently allocated and
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   201
** is not in use.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   202
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   203
static void mmapUnmap(void){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   204
  if( mem.mmapHeap==MAP_FAILED ) return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   205
  if( mem.nPage==0 ) return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   206
  if( mem.nowUsedMMap>mem.szPage ) return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   207
  munmap(mem.mmapHeap, mem.nPage*mem.szPage);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   208
  mem.nowUsedMMap = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   209
  mem.nPage = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   210
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   211
    
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
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   214
** Return the amount of memory currently checked out.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   215
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   216
sqlite3_int64 sqlite3_memory_used(void){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   217
  sqlite3_int64 n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   218
  memsys4Enter();
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   219
  n = mem.nowUsed + mem.nowUsedMMap;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   220
  sqlite3_mutex_leave(mem.mutex);  
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   221
  return n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   222
}
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
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   225
** Return the maximum amount of memory that has ever been
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   226
** checked out since either the beginning of this process
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   227
** or since the most recent reset.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   228
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   229
sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   230
  sqlite3_int64 n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   231
  memsys4Enter();
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   232
  n = mem.mxUsed + mem.mxUsedMMap;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   233
  if( resetFlag ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   234
    mem.mxUsed = mem.nowUsed;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   235
    mem.mxUsedMMap = mem.nowUsedMMap;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   236
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   237
  sqlite3_mutex_leave(mem.mutex);  
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   238
  return n;
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
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
** Change the alarm callback
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   243
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   244
int sqlite3_memory_alarm(
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   245
  void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   246
  void *pArg,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   247
  sqlite3_int64 iThreshold
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
  memsys4Enter();
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   250
  mem.alarmCallback = xCallback;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   251
  mem.alarmArg = pArg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   252
  mem.alarmThreshold = iThreshold;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   253
  sqlite3_mutex_leave(mem.mutex);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   254
  return SQLITE_OK;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   255
}
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
** Trigger the alarm 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   259
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   260
static void sqlite3MemsysAlarm(int nByte){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   261
  void (*xCallback)(void*,sqlite3_int64,int);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   262
  sqlite3_int64 nowUsed;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   263
  void *pArg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   264
  if( mem.alarmCallback==0 || mem.alarmBusy  ) return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   265
  mem.alarmBusy = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   266
  xCallback = mem.alarmCallback;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   267
  nowUsed = mem.nowUsed;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   268
  pArg = mem.alarmArg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   269
  sqlite3_mutex_leave(mem.mutex);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   270
  xCallback(pArg, nowUsed, nByte);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   271
  sqlite3_mutex_enter(mem.mutex);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   272
  mem.alarmBusy = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   273
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   274
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   275
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   276
** Allocate nBytes of memory
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   277
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   278
static void *memsys4Malloc(int nBytes){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   279
  sqlite3_int64 *p = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   280
  if( mem.alarmCallback!=0
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   281
         && mem.nowUsed+mem.nowUsedMMap+nBytes>=mem.alarmThreshold ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   282
    sqlite3MemsysAlarm(nBytes);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   283
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   284
  if( (p = mmapAlloc(nBytes))==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   285
    p = malloc(nBytes+8);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   286
    if( p==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   287
      sqlite3MemsysAlarm(nBytes);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   288
      p = malloc(nBytes+8);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   289
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   290
    if( p ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   291
      p[0] = nBytes;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   292
      p++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   293
      mem.nowUsed += nBytes;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   294
      if( mem.nowUsed>mem.mxUsed ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   295
        mem.mxUsed = mem.nowUsed;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   296
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   297
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   298
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   299
  return (void*)p; 
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
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   302
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   303
** Return the size of a memory allocation
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
static int memsys4Size(void *pPrior){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   306
  char *z = (char*)pPrior;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   307
  int idx = mem.nPage ? (z - mem.mmapHeap)/mem.szPage : 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   308
  int nByte;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   309
  if( idx>=1 && idx<mem.nPage ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   310
    nByte = mem.szPage;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   311
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   312
    sqlite3_int64 *p = pPrior;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   313
    p--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   314
    nByte = (int)*p;
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
  return nByte;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   317
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   318
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   319
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   320
** Free memory.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   321
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   322
static void memsys4Free(void *pPrior){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   323
  sqlite3_int64 *p;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   324
  int nByte;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   325
  if( mmapFree(pPrior)==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   326
    p = pPrior;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   327
    p--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   328
    nByte = (int)*p;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   329
    mem.nowUsed -= nByte;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   330
    free(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   331
    if( mem.nowUsed==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   332
      mmapUnmap();
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   333
    }      
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   334
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   335
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   336
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
** Allocate nBytes of memory
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   339
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   340
void *sqlite3_malloc(int nBytes){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   341
  sqlite3_int64 *p = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   342
  if( nBytes>0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   343
    memsys4Enter();
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   344
    p = memsys4Malloc(nBytes);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   345
    sqlite3_mutex_leave(mem.mutex);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   346
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   347
  return (void*)p; 
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
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
** Free memory.
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
void sqlite3_free(void *pPrior){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   354
  if( pPrior==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   355
    return;
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
  assert( mem.mutex!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   358
  sqlite3_mutex_enter(mem.mutex);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   359
  memsys4Free(pPrior);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   360
  sqlite3_mutex_leave(mem.mutex);  
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   361
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   362
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   363
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   364
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   365
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   366
** Change the size of an existing memory allocation
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   367
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   368
void *sqlite3_realloc(void *pPrior, int nBytes){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   369
  int nOld;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   370
  sqlite3_int64 *p;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   371
  if( pPrior==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   372
    return sqlite3_malloc(nBytes);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   373
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   374
  if( nBytes<=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   375
    sqlite3_free(pPrior);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   376
    return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   377
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   378
  nOld = memsys4Size(pPrior);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   379
  if( nBytes<=nOld && nBytes>=nOld-128 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   380
    return pPrior;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   381
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   382
  assert( mem.mutex!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   383
  sqlite3_mutex_enter(mem.mutex);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   384
  p = memsys4Malloc(nBytes);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   385
  if( p ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   386
    if( nOld<nBytes ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   387
      memcpy(p, pPrior, nOld);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   388
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   389
      memcpy(p, pPrior, nBytes);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   390
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   391
    memsys4Free(pPrior);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   392
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   393
  assert( mem.mutex!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   394
  sqlite3_mutex_leave(mem.mutex);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   395
  return (void*)p;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   396
}
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
#endif /* !SQLITE_MEMDEBUG && !SQLITE_OMIT_MEMORY_ALLOCATION */