Blame src/mem5.c

Packit 87b942
/*
Packit 87b942
** 2007 October 14
Packit 87b942
**
Packit 87b942
** The author disclaims copyright to this source code.  In place of
Packit 87b942
** a legal notice, here is a blessing:
Packit 87b942
**
Packit 87b942
**    May you do good and not evil.
Packit 87b942
**    May you find forgiveness for yourself and forgive others.
Packit 87b942
**    May you share freely, never taking more than you give.
Packit 87b942
**
Packit 87b942
*************************************************************************
Packit 87b942
** This file contains the C functions that implement a memory
Packit 87b942
** allocation subsystem for use by SQLite. 
Packit 87b942
**
Packit 87b942
** This version of the memory allocation subsystem omits all
Packit 87b942
** use of malloc(). The application gives SQLite a block of memory
Packit 87b942
** before calling sqlite3_initialize() from which allocations
Packit 87b942
** are made and returned by the xMalloc() and xRealloc() 
Packit 87b942
** implementations. Once sqlite3_initialize() has been called,
Packit 87b942
** the amount of memory available to SQLite is fixed and cannot
Packit 87b942
** be changed.
Packit 87b942
**
Packit 87b942
** This version of the memory allocation subsystem is included
Packit 87b942
** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
Packit 87b942
**
Packit 87b942
** This memory allocator uses the following algorithm:
Packit 87b942
**
Packit 87b942
**   1.  All memory allocation sizes are rounded up to a power of 2.
Packit 87b942
**
Packit 87b942
**   2.  If two adjacent free blocks are the halves of a larger block,
Packit 87b942
**       then the two blocks are coalesced into the single larger block.
Packit 87b942
**
Packit 87b942
**   3.  New memory is allocated from the first available free block.
Packit 87b942
**
Packit 87b942
** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
Packit 87b942
** Concerning Dynamic Storage Allocation". Journal of the Association for
Packit 87b942
** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
Packit 87b942
** 
Packit 87b942
** Let n be the size of the largest allocation divided by the minimum
Packit 87b942
** allocation size (after rounding all sizes up to a power of 2.)  Let M
Packit 87b942
** be the maximum amount of memory ever outstanding at one time.  Let
Packit 87b942
** N be the total amount of memory available for allocation.  Robson
Packit 87b942
** proved that this memory allocator will never breakdown due to 
Packit 87b942
** fragmentation as long as the following constraint holds:
Packit 87b942
**
Packit 87b942
**      N >=  M*(1 + log2(n)/2) - n + 1
Packit 87b942
**
Packit 87b942
** The sqlite3_status() logic tracks the maximum values of n and M so
Packit 87b942
** that an application can, at any time, verify this constraint.
Packit 87b942
*/
Packit 87b942
#include "sqliteInt.h"
Packit 87b942
Packit 87b942
/*
Packit 87b942
** This version of the memory allocator is used only when 
Packit 87b942
** SQLITE_ENABLE_MEMSYS5 is defined.
Packit 87b942
*/
Packit 87b942
#ifdef SQLITE_ENABLE_MEMSYS5
Packit 87b942
Packit 87b942
/*
Packit 87b942
** A minimum allocation is an instance of the following structure.
Packit 87b942
** Larger allocations are an array of these structures where the
Packit 87b942
** size of the array is a power of 2.
Packit 87b942
**
Packit 87b942
** The size of this object must be a power of two.  That fact is
Packit 87b942
** verified in memsys5Init().
Packit 87b942
*/
Packit 87b942
typedef struct Mem5Link Mem5Link;
Packit 87b942
struct Mem5Link {
Packit 87b942
  int next;       /* Index of next free chunk */
Packit 87b942
  int prev;       /* Index of previous free chunk */
Packit 87b942
};
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Maximum size of any allocation is ((1<
Packit 87b942
** mem5.szAtom is always at least 8 and 32-bit integers are used,
Packit 87b942
** it is not actually possible to reach this limit.
Packit 87b942
*/
Packit 87b942
#define LOGMAX 30
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Masks used for mem5.aCtrl[] elements.
Packit 87b942
*/
Packit 87b942
#define CTRL_LOGSIZE  0x1f    /* Log2 Size of this block */
Packit 87b942
#define CTRL_FREE     0x20    /* True if not checked out */
Packit 87b942
Packit 87b942
/*
Packit 87b942
** All of the static variables used by this module are collected
Packit 87b942
** into a single structure named "mem5".  This is to keep the
Packit 87b942
** static variables organized and to reduce namespace pollution
Packit 87b942
** when this module is combined with other in the amalgamation.
Packit 87b942
*/
Packit 87b942
static SQLITE_WSD struct Mem5Global {
Packit 87b942
  /*
Packit 87b942
  ** Memory available for allocation
Packit 87b942
  */
Packit 87b942
  int szAtom;      /* Smallest possible allocation in bytes */
Packit 87b942
  int nBlock;      /* Number of szAtom sized blocks in zPool */
Packit 87b942
  u8 *zPool;       /* Memory available to be allocated */
Packit 87b942
  
Packit 87b942
  /*
Packit 87b942
  ** Mutex to control access to the memory allocation subsystem.
Packit 87b942
  */
Packit 87b942
  sqlite3_mutex *mutex;
Packit 87b942
Packit 87b942
#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
Packit 87b942
  /*
Packit 87b942
  ** Performance statistics
Packit 87b942
  */
Packit 87b942
  u64 nAlloc;         /* Total number of calls to malloc */
Packit 87b942
  u64 totalAlloc;     /* Total of all malloc calls - includes internal frag */
Packit 87b942
  u64 totalExcess;    /* Total internal fragmentation */
Packit 87b942
  u32 currentOut;     /* Current checkout, including internal fragmentation */
Packit 87b942
  u32 currentCount;   /* Current number of distinct checkouts */
Packit 87b942
  u32 maxOut;         /* Maximum instantaneous currentOut */
Packit 87b942
  u32 maxCount;       /* Maximum instantaneous currentCount */
Packit 87b942
  u32 maxRequest;     /* Largest allocation (exclusive of internal frag) */
Packit 87b942
#endif
Packit 87b942
  
Packit 87b942
  /*
Packit 87b942
  ** Lists of free blocks.  aiFreelist[0] is a list of free blocks of
Packit 87b942
  ** size mem5.szAtom.  aiFreelist[1] holds blocks of size szAtom*2.
Packit 87b942
  ** aiFreelist[2] holds free blocks of size szAtom*4.  And so forth.
Packit 87b942
  */
Packit 87b942
  int aiFreelist[LOGMAX+1];
Packit 87b942
Packit 87b942
  /*
Packit 87b942
  ** Space for tracking which blocks are checked out and the size
Packit 87b942
  ** of each block.  One byte per block.
Packit 87b942
  */
Packit 87b942
  u8 *aCtrl;
Packit 87b942
Packit 87b942
} mem5;
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Access the static variable through a macro for SQLITE_OMIT_WSD.
Packit 87b942
*/
Packit 87b942
#define mem5 GLOBAL(struct Mem5Global, mem5)
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Assuming mem5.zPool is divided up into an array of Mem5Link
Packit 87b942
** structures, return a pointer to the idx-th such link.
Packit 87b942
*/
Packit 87b942
#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Unlink the chunk at mem5.aPool[i] from list it is currently
Packit 87b942
** on.  It should be found on mem5.aiFreelist[iLogsize].
Packit 87b942
*/
Packit 87b942
static void memsys5Unlink(int i, int iLogsize){
Packit 87b942
  int next, prev;
Packit 87b942
  assert( i>=0 && i
Packit 87b942
  assert( iLogsize>=0 && iLogsize<=LOGMAX );
Packit 87b942
  assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
Packit 87b942
Packit 87b942
  next = MEM5LINK(i)->next;
Packit 87b942
  prev = MEM5LINK(i)->prev;
Packit 87b942
  if( prev<0 ){
Packit 87b942
    mem5.aiFreelist[iLogsize] = next;
Packit 87b942
  }else{
Packit 87b942
    MEM5LINK(prev)->next = next;
Packit 87b942
  }
Packit 87b942
  if( next>=0 ){
Packit 87b942
    MEM5LINK(next)->prev = prev;
Packit 87b942
  }
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Link the chunk at mem5.aPool[i] so that is on the iLogsize
Packit 87b942
** free list.
Packit 87b942
*/
Packit 87b942
static void memsys5Link(int i, int iLogsize){
Packit 87b942
  int x;
Packit 87b942
  assert( sqlite3_mutex_held(mem5.mutex) );
Packit 87b942
  assert( i>=0 && i
Packit 87b942
  assert( iLogsize>=0 && iLogsize<=LOGMAX );
Packit 87b942
  assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
Packit 87b942
Packit 87b942
  x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
Packit 87b942
  MEM5LINK(i)->prev = -1;
Packit 87b942
  if( x>=0 ){
Packit 87b942
    assert( x
Packit 87b942
    MEM5LINK(x)->prev = i;
Packit 87b942
  }
Packit 87b942
  mem5.aiFreelist[iLogsize] = i;
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Obtain or release the mutex needed to access global data structures.
Packit 87b942
*/
Packit 87b942
static void memsys5Enter(void){
Packit 87b942
  sqlite3_mutex_enter(mem5.mutex);
Packit 87b942
}
Packit 87b942
static void memsys5Leave(void){
Packit 87b942
  sqlite3_mutex_leave(mem5.mutex);
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Return the size of an outstanding allocation, in bytes.
Packit 87b942
** This only works for chunks that are currently checked out.
Packit 87b942
*/
Packit 87b942
static int memsys5Size(void *p){
Packit 87b942
  int iSize, i;
Packit 87b942
  assert( p!=0 );
Packit 87b942
  i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
Packit 87b942
  assert( i>=0 && i
Packit 87b942
  iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
Packit 87b942
  return iSize;
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Return a block of memory of at least nBytes in size.
Packit 87b942
** Return NULL if unable.  Return NULL if nBytes==0.
Packit 87b942
**
Packit 87b942
** The caller guarantees that nByte is positive.
Packit 87b942
**
Packit 87b942
** The caller has obtained a mutex prior to invoking this
Packit 87b942
** routine so there is never any chance that two or more
Packit 87b942
** threads can be in this routine at the same time.
Packit 87b942
*/
Packit 87b942
static void *memsys5MallocUnsafe(int nByte){
Packit 87b942
  int i;           /* Index of a mem5.aPool[] slot */
Packit 87b942
  int iBin;        /* Index into mem5.aiFreelist[] */
Packit 87b942
  int iFullSz;     /* Size of allocation rounded up to power of 2 */
Packit 87b942
  int iLogsize;    /* Log2 of iFullSz/POW2_MIN */
Packit 87b942
Packit 87b942
  /* nByte must be a positive */
Packit 87b942
  assert( nByte>0 );
Packit 87b942
Packit 87b942
  /* No more than 1GiB per allocation */
Packit 87b942
  if( nByte > 0x40000000 ) return 0;
Packit 87b942
Packit 87b942
#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
Packit 87b942
  /* Keep track of the maximum allocation request.  Even unfulfilled
Packit 87b942
  ** requests are counted */
Packit 87b942
  if( (u32)nByte>mem5.maxRequest ){
Packit 87b942
    mem5.maxRequest = nByte;
Packit 87b942
  }
Packit 87b942
#endif
Packit 87b942
Packit 87b942
Packit 87b942
  /* Round nByte up to the next valid power of two */
Packit 87b942
  for(iFullSz=mem5.szAtom,iLogsize=0; iFullSz
Packit 87b942
Packit 87b942
  /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
Packit 87b942
  ** block.  If not, then split a block of the next larger power of
Packit 87b942
  ** two in order to create a new free block of size iLogsize.
Packit 87b942
  */
Packit 87b942
  for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
Packit 87b942
  if( iBin>LOGMAX ){
Packit 87b942
    testcase( sqlite3GlobalConfig.xLog!=0 );
Packit 87b942
    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
Packit 87b942
    return 0;
Packit 87b942
  }
Packit 87b942
  i = mem5.aiFreelist[iBin];
Packit 87b942
  memsys5Unlink(i, iBin);
Packit 87b942
  while( iBin>iLogsize ){
Packit 87b942
    int newSize;
Packit 87b942
Packit 87b942
    iBin--;
Packit 87b942
    newSize = 1 << iBin;
Packit 87b942
    mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
Packit 87b942
    memsys5Link(i+newSize, iBin);
Packit 87b942
  }
Packit 87b942
  mem5.aCtrl[i] = iLogsize;
Packit 87b942
Packit 87b942
#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
Packit 87b942
  /* Update allocator performance statistics. */
Packit 87b942
  mem5.nAlloc++;
Packit 87b942
  mem5.totalAlloc += iFullSz;
Packit 87b942
  mem5.totalExcess += iFullSz - nByte;
Packit 87b942
  mem5.currentCount++;
Packit 87b942
  mem5.currentOut += iFullSz;
Packit 87b942
  if( mem5.maxCount
Packit 87b942
  if( mem5.maxOut
Packit 87b942
#endif
Packit 87b942
Packit 87b942
#ifdef SQLITE_DEBUG
Packit 87b942
  /* Make sure the allocated memory does not assume that it is set to zero
Packit 87b942
  ** or retains a value from a previous allocation */
Packit 87b942
  memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
Packit 87b942
#endif
Packit 87b942
Packit 87b942
  /* Return a pointer to the allocated memory. */
Packit 87b942
  return (void*)&mem5.zPool[i*mem5.szAtom];
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Free an outstanding memory allocation.
Packit 87b942
*/
Packit 87b942
static void memsys5FreeUnsafe(void *pOld){
Packit 87b942
  u32 size, iLogsize;
Packit 87b942
  int iBlock;
Packit 87b942
Packit 87b942
  /* Set iBlock to the index of the block pointed to by pOld in 
Packit 87b942
  ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
Packit 87b942
  */
Packit 87b942
  iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
Packit 87b942
Packit 87b942
  /* Check that the pointer pOld points to a valid, non-free block. */
Packit 87b942
  assert( iBlock>=0 && iBlock
Packit 87b942
  assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
Packit 87b942
  assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
Packit 87b942
Packit 87b942
  iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
Packit 87b942
  size = 1<
Packit 87b942
  assert( iBlock+size-1<(u32)mem5.nBlock );
Packit 87b942
Packit 87b942
  mem5.aCtrl[iBlock] |= CTRL_FREE;
Packit 87b942
  mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
Packit 87b942
Packit 87b942
#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
Packit 87b942
  assert( mem5.currentCount>0 );
Packit 87b942
  assert( mem5.currentOut>=(size*mem5.szAtom) );
Packit 87b942
  mem5.currentCount--;
Packit 87b942
  mem5.currentOut -= size*mem5.szAtom;
Packit 87b942
  assert( mem5.currentOut>0 || mem5.currentCount==0 );
Packit 87b942
  assert( mem5.currentCount>0 || mem5.currentOut==0 );
Packit 87b942
#endif
Packit 87b942
Packit 87b942
  mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
Packit 87b942
  while( ALWAYS(iLogsize
Packit 87b942
    int iBuddy;
Packit 87b942
    if( (iBlock>>iLogsize) & 1 ){
Packit 87b942
      iBuddy = iBlock - size;
Packit 87b942
      assert( iBuddy>=0 );
Packit 87b942
    }else{
Packit 87b942
      iBuddy = iBlock + size;
Packit 87b942
      if( iBuddy>=mem5.nBlock ) break;
Packit 87b942
    }
Packit 87b942
    if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
Packit 87b942
    memsys5Unlink(iBuddy, iLogsize);
Packit 87b942
    iLogsize++;
Packit 87b942
    if( iBuddy
Packit 87b942
      mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
Packit 87b942
      mem5.aCtrl[iBlock] = 0;
Packit 87b942
      iBlock = iBuddy;
Packit 87b942
    }else{
Packit 87b942
      mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
Packit 87b942
      mem5.aCtrl[iBuddy] = 0;
Packit 87b942
    }
Packit 87b942
    size *= 2;
Packit 87b942
  }
Packit 87b942
Packit 87b942
#ifdef SQLITE_DEBUG
Packit 87b942
  /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
Packit 87b942
  ** not used after being freed */
Packit 87b942
  memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
Packit 87b942
#endif
Packit 87b942
Packit 87b942
  memsys5Link(iBlock, iLogsize);
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Allocate nBytes of memory.
Packit 87b942
*/
Packit 87b942
static void *memsys5Malloc(int nBytes){
Packit 87b942
  sqlite3_int64 *p = 0;
Packit 87b942
  if( nBytes>0 ){
Packit 87b942
    memsys5Enter();
Packit 87b942
    p = memsys5MallocUnsafe(nBytes);
Packit 87b942
    memsys5Leave();
Packit 87b942
  }
Packit 87b942
  return (void*)p; 
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Free memory.
Packit 87b942
**
Packit 87b942
** The outer layer memory allocator prevents this routine from
Packit 87b942
** being called with pPrior==0.
Packit 87b942
*/
Packit 87b942
static void memsys5Free(void *pPrior){
Packit 87b942
  assert( pPrior!=0 );
Packit 87b942
  memsys5Enter();
Packit 87b942
  memsys5FreeUnsafe(pPrior);
Packit 87b942
  memsys5Leave();  
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Change the size of an existing memory allocation.
Packit 87b942
**
Packit 87b942
** The outer layer memory allocator prevents this routine from
Packit 87b942
** being called with pPrior==0.  
Packit 87b942
**
Packit 87b942
** nBytes is always a value obtained from a prior call to
Packit 87b942
** memsys5Round().  Hence nBytes is always a non-negative power
Packit 87b942
** of two.  If nBytes==0 that means that an oversize allocation
Packit 87b942
** (an allocation larger than 0x40000000) was requested and this
Packit 87b942
** routine should return 0 without freeing pPrior.
Packit 87b942
*/
Packit 87b942
static void *memsys5Realloc(void *pPrior, int nBytes){
Packit 87b942
  int nOld;
Packit 87b942
  void *p;
Packit 87b942
  assert( pPrior!=0 );
Packit 87b942
  assert( (nBytes&(nBytes-1))==0 );  /* EV: R-46199-30249 */
Packit 87b942
  assert( nBytes>=0 );
Packit 87b942
  if( nBytes==0 ){
Packit 87b942
    return 0;
Packit 87b942
  }
Packit 87b942
  nOld = memsys5Size(pPrior);
Packit 87b942
  if( nBytes<=nOld ){
Packit 87b942
    return pPrior;
Packit 87b942
  }
Packit 87b942
  p = memsys5Malloc(nBytes);
Packit 87b942
  if( p ){
Packit 87b942
    memcpy(p, pPrior, nOld);
Packit 87b942
    memsys5Free(pPrior);
Packit 87b942
  }
Packit 87b942
  return p;
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Round up a request size to the next valid allocation size.  If
Packit 87b942
** the allocation is too large to be handled by this allocation system,
Packit 87b942
** return 0.
Packit 87b942
**
Packit 87b942
** All allocations must be a power of two and must be expressed by a
Packit 87b942
** 32-bit signed integer.  Hence the largest allocation is 0x40000000
Packit 87b942
** or 1073741824 bytes.
Packit 87b942
*/
Packit 87b942
static int memsys5Roundup(int n){
Packit 87b942
  int iFullSz;
Packit 87b942
  if( n > 0x40000000 ) return 0;
Packit 87b942
  for(iFullSz=mem5.szAtom; iFullSz
Packit 87b942
  return iFullSz;
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Return the ceiling of the logarithm base 2 of iValue.
Packit 87b942
**
Packit 87b942
** Examples:   memsys5Log(1) -> 0
Packit 87b942
**             memsys5Log(2) -> 1
Packit 87b942
**             memsys5Log(4) -> 2
Packit 87b942
**             memsys5Log(5) -> 3
Packit 87b942
**             memsys5Log(8) -> 3
Packit 87b942
**             memsys5Log(9) -> 4
Packit 87b942
*/
Packit 87b942
static int memsys5Log(int iValue){
Packit 87b942
  int iLog;
Packit 87b942
  for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<
Packit 87b942
  return iLog;
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Initialize the memory allocator.
Packit 87b942
**
Packit 87b942
** This routine is not threadsafe.  The caller must be holding a mutex
Packit 87b942
** to prevent multiple threads from entering at the same time.
Packit 87b942
*/
Packit 87b942
static int memsys5Init(void *NotUsed){
Packit 87b942
  int ii;            /* Loop counter */
Packit 87b942
  int nByte;         /* Number of bytes of memory available to this allocator */
Packit 87b942
  u8 *zByte;         /* Memory usable by this allocator */
Packit 87b942
  int nMinLog;       /* Log base 2 of minimum allocation size in bytes */
Packit 87b942
  int iOffset;       /* An offset into mem5.aCtrl[] */
Packit 87b942
Packit 87b942
  UNUSED_PARAMETER(NotUsed);
Packit 87b942
Packit 87b942
  /* For the purposes of this routine, disable the mutex */
Packit 87b942
  mem5.mutex = 0;
Packit 87b942
Packit 87b942
  /* The size of a Mem5Link object must be a power of two.  Verify that
Packit 87b942
  ** this is case.
Packit 87b942
  */
Packit 87b942
  assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
Packit 87b942
Packit 87b942
  nByte = sqlite3GlobalConfig.nHeap;
Packit 87b942
  zByte = (u8*)sqlite3GlobalConfig.pHeap;
Packit 87b942
  assert( zByte!=0 );  /* sqlite3_config() does not allow otherwise */
Packit 87b942
Packit 87b942
  /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
Packit 87b942
  nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
Packit 87b942
  mem5.szAtom = (1<
Packit 87b942
  while( (int)sizeof(Mem5Link)>mem5.szAtom ){
Packit 87b942
    mem5.szAtom = mem5.szAtom << 1;
Packit 87b942
  }
Packit 87b942
Packit 87b942
  mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
Packit 87b942
  mem5.zPool = zByte;
Packit 87b942
  mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
Packit 87b942
Packit 87b942
  for(ii=0; ii<=LOGMAX; ii++){
Packit 87b942
    mem5.aiFreelist[ii] = -1;
Packit 87b942
  }
Packit 87b942
Packit 87b942
  iOffset = 0;
Packit 87b942
  for(ii=LOGMAX; ii>=0; ii--){
Packit 87b942
    int nAlloc = (1<
Packit 87b942
    if( (iOffset+nAlloc)<=mem5.nBlock ){
Packit 87b942
      mem5.aCtrl[iOffset] = ii | CTRL_FREE;
Packit 87b942
      memsys5Link(iOffset, ii);
Packit 87b942
      iOffset += nAlloc;
Packit 87b942
    }
Packit 87b942
    assert((iOffset+nAlloc)>mem5.nBlock);
Packit 87b942
  }
Packit 87b942
Packit 87b942
  /* If a mutex is required for normal operation, allocate one */
Packit 87b942
  if( sqlite3GlobalConfig.bMemstat==0 ){
Packit 87b942
    mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
Packit 87b942
  }
Packit 87b942
Packit 87b942
  return SQLITE_OK;
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Deinitialize this module.
Packit 87b942
*/
Packit 87b942
static void memsys5Shutdown(void *NotUsed){
Packit 87b942
  UNUSED_PARAMETER(NotUsed);
Packit 87b942
  mem5.mutex = 0;
Packit 87b942
  return;
Packit 87b942
}
Packit 87b942
Packit 87b942
#ifdef SQLITE_TEST
Packit 87b942
/*
Packit 87b942
** Open the file indicated and write a log of all unfreed memory 
Packit 87b942
** allocations into that log.
Packit 87b942
*/
Packit 87b942
void sqlite3Memsys5Dump(const char *zFilename){
Packit 87b942
  FILE *out;
Packit 87b942
  int i, j, n;
Packit 87b942
  int nMinLog;
Packit 87b942
Packit 87b942
  if( zFilename==0 || zFilename[0]==0 ){
Packit 87b942
    out = stdout;
Packit 87b942
  }else{
Packit 87b942
    out = fopen(zFilename, "w");
Packit 87b942
    if( out==0 ){
Packit 87b942
      fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
Packit 87b942
                      zFilename);
Packit 87b942
      return;
Packit 87b942
    }
Packit 87b942
  }
Packit 87b942
  memsys5Enter();
Packit 87b942
  nMinLog = memsys5Log(mem5.szAtom);
Packit 87b942
  for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
Packit 87b942
    for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
Packit 87b942
    fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
Packit 87b942
  }
Packit 87b942
  fprintf(out, "mem5.nAlloc       = %llu\n", mem5.nAlloc);
Packit 87b942
  fprintf(out, "mem5.totalAlloc   = %llu\n", mem5.totalAlloc);
Packit 87b942
  fprintf(out, "mem5.totalExcess  = %llu\n", mem5.totalExcess);
Packit 87b942
  fprintf(out, "mem5.currentOut   = %u\n", mem5.currentOut);
Packit 87b942
  fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
Packit 87b942
  fprintf(out, "mem5.maxOut       = %u\n", mem5.maxOut);
Packit 87b942
  fprintf(out, "mem5.maxCount     = %u\n", mem5.maxCount);
Packit 87b942
  fprintf(out, "mem5.maxRequest   = %u\n", mem5.maxRequest);
Packit 87b942
  memsys5Leave();
Packit 87b942
  if( out==stdout ){
Packit 87b942
    fflush(stdout);
Packit 87b942
  }else{
Packit 87b942
    fclose(out);
Packit 87b942
  }
Packit 87b942
}
Packit 87b942
#endif
Packit 87b942
Packit 87b942
/*
Packit 87b942
** This routine is the only routine in this file with external 
Packit 87b942
** linkage. It returns a pointer to a static sqlite3_mem_methods
Packit 87b942
** struct populated with the memsys5 methods.
Packit 87b942
*/
Packit 87b942
const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
Packit 87b942
  static const sqlite3_mem_methods memsys5Methods = {
Packit 87b942
     memsys5Malloc,
Packit 87b942
     memsys5Free,
Packit 87b942
     memsys5Realloc,
Packit 87b942
     memsys5Size,
Packit 87b942
     memsys5Roundup,
Packit 87b942
     memsys5Init,
Packit 87b942
     memsys5Shutdown,
Packit 87b942
     0
Packit 87b942
  };
Packit 87b942
  return &memsys5Methods;
Packit 87b942
}
Packit 87b942
Packit 87b942
#endif /* SQLITE_ENABLE_MEMSYS5 */