Change mem5.c so that the minimum allocation size is runtime configurable. (CVS 5320)

FossilOrigin-Name: 4f95f4cdf77e134fab42148e10198c7b008d4ae6
diff --git a/manifest b/manifest
index ab040e0..f58701d 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Remove\sunused\svariable.\s\sFix\sa\scompiler\swarning.\s(CVS\s5319)
-D 2008-06-27T00:52:45
+C Change\smem5.c\sso\sthat\sthe\sminimum\sallocation\ssize\sis\sruntime\sconfigurable.\s(CVS\s5320)
+D 2008-06-27T13:27:04
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in 325dfac0a0dd1cb4d975f1ace6453157892e6042
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -114,14 +114,14 @@
 F src/journal.c cffd2cd214e58c0e99c3ff632b3bee6c7cbb260e
 F src/legacy.c 3626c71fb70912abec9a4312beba753a9ce800df
 F src/loadext.c 40024a0f476c1279494876b9a002001b29e5d3e3
-F src/main.c 942283a5fc983a36504cc6476b6d054911921fec
+F src/main.c 7c326318fe039ea6bab91209f3eabef26f1ba33a
 F src/malloc.c f52166df8abd7ff6990dbee1a0ce3534addc8617
 F src/md5.c 008216bbb5d34c6fbab5357aa68575ad8a31516a
 F src/mem1.c 8340fa5f969e9f9b9bdeb54106457a2003456d2b
 F src/mem2.c 23f9538f35fbcd5665afe7056a56be0c7ed65aa7
 F src/mem3.c 8d34a22617a0eb96c05a83fc2c4fbbaed6bcfde1
 F src/mem4.c 6703adb1717b26d9d70a1c2586b4b7b7ffee7909
-F src/mem5.c d0c7c8506eb3f64c0ea0f1e8defab4043f531dad
+F src/mem5.c 2ad92d6aa224650f9fa9ad41fbf27b6cdae8a76f
 F src/mutex.c a485a0eac8ee2cd95f66e565b4c6696c18db968f
 F src/mutex.h e52ffa1dfc6a6077e8b1823d2c2b7dfcbcf85594
 F src/mutex_os2.c 9c5637aa4c307c552566d0f0b3bd206245b54a97
@@ -144,7 +144,7 @@
 F src/shell.c 479807b87f0409289eec4a776cd6ae56d30544b1
 F src/sqlite.h.in bcdf4f68ce70ca30f0c3d351858383ce398e3c66
 F src/sqlite3ext.h 7bf1651c6fb7183831e5113b4b91662bbe67e5bf
-F src/sqliteInt.h 969acf22dbe79075e486074a8ffdc1e2fc2b8b1f
+F src/sqliteInt.h a3d505360be0385fdd6d02fef82f3f10e952259e
 F src/sqliteLimit.h f435e728c6b620ef7312814d660a81f9356eb5c8
 F src/status.c 6cb10377992505bd69f1ca1d75c1240a65f25a58
 F src/table.c 1fa8f8113ac9cbc09ae4801c6d2a7f0af82c5822
@@ -166,7 +166,7 @@
 F src/test_func.c f4aafa10f17d52c43a64b47717265802e6e552b3
 F src/test_hexio.c 2f1122aa3f012fa0142ee3c36ce5c902a70cd12f
 F src/test_loadext.c df8ab3a6481ddebbdf0d28ebac5d9e0790f7860f
-F src/test_malloc.c db4b54a81e83c0e71e1780365e022ed96fac52b0
+F src/test_malloc.c 68da4add2f3f5f2aace5a15122df80e5f8454af4
 F src/test_md5.c 28209a4e2068711b5443c33104fe41f21d160071
 F src/test_mutex.c b1433eb96520656fb3e4a218253a94ac32bd5775
 F src/test_onefile.c 243157b10275251c5dc2d6619aee2ff9ae22379c
@@ -422,7 +422,7 @@
 F test/pager3.test 2323bf27fd5bd887b580247e5bce500ceee994b4
 F test/pageropt.test 6df72c441db0a037b7ec6990d16311c24fbea77b
 F test/pagesize.test e0a8b3fe80f8b8e808d94a00734c7a18c76c407e
-F test/permutations.test e915c7125487a734cf9fc94c9653fe0a05c08d29
+F test/permutations.test 17aa068f5f6612a9e91ec316463f94141436445a
 F test/pragma.test 44bc52afa72cabfeae82641e0033ca24919f3ce8
 F test/pragma2.test 5364893491b9231dd170e3459bfc2e2342658b47
 F test/printf.test c3405535b418d454e8a52196a0fc592ec9eec58d
@@ -594,7 +594,7 @@
 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
-P a4005782690f022e982df4873779a029b28b9306
-R f1af58e903b479838053b138b998370c
-U drh
-Z b29d48455395ec1bf7cf810b7d415083
+P 0b01ec5cf7725a02d4c12167df125cef578f6219
+R 0a2d2902a0d1d00bcf1712dac8ab54a9
+U danielk1977
+Z 48c1b0881009fcf3cbbdeacd5d1ca6e8
diff --git a/manifest.uuid b/manifest.uuid
index 81ddcca..7fab162 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-0b01ec5cf7725a02d4c12167df125cef578f6219
\ No newline at end of file
+4f95f4cdf77e134fab42148e10198c7b008d4ae6
\ No newline at end of file
diff --git a/src/main.c b/src/main.c
index 3b49d9d..52bcba1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -14,7 +14,7 @@
 ** other files are for internal use by SQLite and should not be
 ** accessed by users of the library.
 **
-** $Id: main.c,v 1.465 2008/06/26 10:54:12 danielk1977 Exp $
+** $Id: main.c,v 1.466 2008/06/27 13:27:04 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -175,22 +175,6 @@
       sqlite3Config.bFullMutex = 1;
       break;
     }
-#ifdef SQLITE_ENABLE_MEMSYS3
-    case SQLITE_CONFIG_MEMSYS3: {
-      u8 *pMem = va_arg(ap, u8*);
-      int nMem = va_arg(ap, int);
-      sqlite3MemSetMemsys3(pMem, nMem);
-      break;
-    }
-#endif
-#ifdef SQLITE_ENABLE_MEMSYS5
-    case SQLITE_CONFIG_MEMSYS5: {
-      u8 *pMem = va_arg(ap, u8*);
-      int nMem = va_arg(ap, int);
-      sqlite3MemSetMemsys5(pMem, nMem);
-      break;
-    }
-#endif
     case SQLITE_CONFIG_MALLOC: {
       /* Specify an alternative malloc implementation */
       sqlite3Config.m = *va_arg(ap, sqlite3_mem_methods*);
@@ -231,13 +215,34 @@
       sqlite3Config.nPage = va_arg(ap, int);
       break;
     }
+
     case SQLITE_CONFIG_HEAP: {
-      /* Designate a buffer for scratch memory space */
+      /* Designate a buffer for heap memory space */
       sqlite3Config.pHeap = va_arg(ap, void*);
       sqlite3Config.nHeap = va_arg(ap, int);
       sqlite3Config.mnReq = va_arg(ap, int);
+
+      /* Fall through to install the mem5.c/mem3.c methods. If neither
+      ** ENABLE_MEMSYS3 nor ENABLE_MEMSYS5 is defined, fall through to
+      ** the default case and return an error.
+      */
+    }
+
+#ifdef SQLITE_ENABLE_MEMSYS5
+    case SQLITE_CONFIG_MEMSYS5: {
+      sqlite3_mem_methods *p = sqlite3MemGetMemsys5();
+      sqlite3Config.m = *p;
       break;
     }
+#endif
+#ifdef SQLITE_ENABLE_MEMSYS3
+    case SQLITE_CONFIG_MEMSYS3: {
+      sqlite3_mem_methods *p = sqlite3MemGetMemsys3();
+      sqlite3Config.m = *p;
+      break;
+    }
+#endif
+
     default: {
       rc = SQLITE_ERROR;
       break;
diff --git a/src/mem5.c b/src/mem5.c
index 179528d..acbe92d 100644
--- a/src/mem5.c
+++ b/src/mem5.c
@@ -23,7 +23,7 @@
 ** This version of the memory allocation subsystem is included
 ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
 **
-** $Id: mem5.c,v 1.8 2008/06/25 14:57:54 danielk1977 Exp $
+** $Id: mem5.c,v 1.9 2008/06/27 13:27:04 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 
@@ -41,7 +41,6 @@
 #ifndef SQLITE_POW2_LOGMIN
 # define SQLITE_POW2_LOGMIN 6
 #endif
-#define POW2_MIN (1<<SQLITE_POW2_LOGMIN)
 
 /*
 ** Log2 of the maximum size of an allocation.
@@ -61,21 +60,18 @@
 ** Larger allocations are an array of these structures where the
 ** size of the array is a power of 2.
 */
-typedef struct Mem5Block Mem5Block;
-struct Mem5Block {
-  union {
-    char aData[POW2_MIN];
-    struct {
-      int next;       /* Index in mem5.aPool[] of next free chunk */
-      int prev;       /* Index in mem5.aPool[] of previous free chunk */
-    } list;
-  } u;
+typedef struct Mem5Link Mem5Link;
+struct Mem5Link {
+  int next;       /* Index of next free chunk */
+  int prev;       /* Index of previous free chunk */
 };
 
 /*
-** The size in blocks of an POW2_MAX allocation
+** Maximum size of any allocation is ((1<<LOGMAX)*mem5.nAtom). Since
+** mem5.nAtom is always at least 8, this is not really a practical
+** limitation.
 */
-#define SZ_MAX (1<<(NSIZE-1))
+#define LOGMAX 30
 
 /*
 ** Masks used for mem5.aCtrl[] elements.
@@ -122,7 +118,7 @@
   /*
   ** Lists of free blocks of various sizes.
   */
-  int aiFreelist[NSIZE];
+  int aiFreelist[LOGMAX+1];
 
   /*
   ** Space for tracking which blocks are checked out and the size
@@ -133,10 +129,13 @@
   /*
   ** Memory available for allocation
   */
-  int nBlock;
-  Mem5Block *aPool;
+  int nAtom;       /* Smallest possible allocation in bytes */
+  int nBlock;      /* Number of nAtom sized blocks in zPool */
+  u8 *zPool;
 } mem5;
 
+#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.nAtom]))
+
 /*
 ** Unlink the chunk at mem5.aPool[i] from list it is currently
 ** on.  It should be found on mem5.aiFreelist[iLogsize].
@@ -144,18 +143,18 @@
 static void memsys5Unlink(int i, int iLogsize){
   int next, prev;
   assert( i>=0 && i<mem5.nBlock );
-  assert( iLogsize>=0 && iLogsize<NSIZE );
+  assert( iLogsize>=0 && iLogsize<=LOGMAX );
   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
 
-  next = mem5.aPool[i].u.list.next;
-  prev = mem5.aPool[i].u.list.prev;
+  next = MEM5LINK(i)->next;
+  prev = MEM5LINK(i)->prev;
   if( prev<0 ){
     mem5.aiFreelist[iLogsize] = next;
   }else{
-    mem5.aPool[prev].u.list.next = next;
+    MEM5LINK(prev)->next = next;
   }
   if( next>=0 ){
-    mem5.aPool[next].u.list.prev = prev;
+    MEM5LINK(next)->prev = prev;
   }
 }
 
@@ -167,14 +166,14 @@
   int x;
   assert( sqlite3_mutex_held(mem5.mutex) );
   assert( i>=0 && i<mem5.nBlock );
-  assert( iLogsize>=0 && iLogsize<NSIZE );
+  assert( iLogsize>=0 && iLogsize<=LOGMAX );
   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
 
-  mem5.aPool[i].u.list.next = x = mem5.aiFreelist[iLogsize];
-  mem5.aPool[i].u.list.prev = -1;
+  x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
+  MEM5LINK(i)->prev = -1;
   if( x>=0 ){
     assert( x<mem5.nBlock );
-    mem5.aPool[x].u.list.prev = i;
+    MEM5LINK(x)->prev = i;
   }
   mem5.aiFreelist[iLogsize] = i;
 }
@@ -202,9 +201,9 @@
 static int memsys5Size(void *p){
   int iSize = 0;
   if( p ){
-    int i = ((Mem5Block*)p) - mem5.aPool;
+    int i = ((u8 *)p-mem5.zPool)/mem5.nAtom;
     assert( i>=0 && i<mem5.nBlock );
-    iSize = 1 << ((mem5.aCtrl[i]&CTRL_LOGSIZE) + SQLITE_POW2_LOGMIN);
+    iSize = mem5.nAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
   }
   return iSize;
 }
@@ -217,12 +216,12 @@
   int i;
   int iFirst;
 
-  assert( iLogsize>=0 && iLogsize<NSIZE );
+  assert( iLogsize>=0 && iLogsize<=LOGMAX );
   i = iFirst = mem5.aiFreelist[iLogsize];
   assert( iFirst>=0 );
   while( i>0 ){
     if( i<iFirst ) iFirst = i;
-    i = mem5.aPool[i].u.list.next;
+    i = MEM5LINK(i)->next;
   }
   memsys5Unlink(iFirst, iLogsize);
   return iFirst;
@@ -246,14 +245,14 @@
 
   /* Round nByte up to the next valid power of two */
   if( nByte>POW2_MAX ) return 0;
-  for(iFullSz=POW2_MIN, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
+  for(iFullSz=mem5.nAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
 
   /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
   ** block.  If not, then split a block of the next larger power of
   ** two in order to create a new free block of size iLogsize.
   */
-  for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<NSIZE; iBin++){}
-  if( iBin>=NSIZE ) return 0;
+  for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
+  if( iBin>LOGMAX ) return 0;
   i = memsys5UnlinkFirst(iBin);
   while( iBin>iLogsize ){
     int newSize;
@@ -275,7 +274,7 @@
   if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
 
   /* Return a pointer to the allocated memory. */
-  return (void*)&mem5.aPool[i];
+  return (void*)&mem5.zPool[i*mem5.nAtom];
 }
 
 /*
@@ -283,47 +282,55 @@
 */
 static void memsys5FreeUnsafe(void *pOld){
   u32 size, iLogsize;
-  int i;
+  int iBlock;             
 
-  i = ((Mem5Block*)pOld) - mem5.aPool;
-  assert( i>=0 && i<mem5.nBlock );
-  assert( (mem5.aCtrl[i] & CTRL_FREE)==0 );
-  iLogsize = mem5.aCtrl[i] & CTRL_LOGSIZE;
+  /* Set iBlock to the index of the block pointed to by pOld in 
+  ** the array of mem5.nAtom byte blocks pointed to by mem5.zPool.
+  */
+  iBlock = ((u8 *)pOld-mem5.zPool)/mem5.nAtom;
+
+  /* Check that the pointer pOld points to a valid, non-free block. */
+  assert( iBlock>=0 && iBlock<mem5.nBlock );
+  assert( ((u8 *)pOld-mem5.zPool)%mem5.nAtom==0 );
+  assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
+
+  iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
   size = 1<<iLogsize;
-  assert( i+size-1<mem5.nBlock );
-  mem5.aCtrl[i] |= CTRL_FREE;
-  mem5.aCtrl[i+size-1] |= CTRL_FREE;
+  assert( iBlock+size-1<mem5.nBlock );
+
+  mem5.aCtrl[iBlock] |= CTRL_FREE;
+  mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
   assert( mem5.currentCount>0 );
   assert( mem5.currentOut>=0 );
   mem5.currentCount--;
-  mem5.currentOut -= size*POW2_MIN;
+  mem5.currentOut -= size*mem5.nAtom;
   assert( mem5.currentOut>0 || mem5.currentCount==0 );
   assert( mem5.currentCount>0 || mem5.currentOut==0 );
 
-  mem5.aCtrl[i] = CTRL_FREE | iLogsize;
-  while( iLogsize<NSIZE-1 ){
+  mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
+  while( iLogsize<LOGMAX ){
     int iBuddy;
-
-    if( (i>>iLogsize) & 1 ){
-      iBuddy = i - size;
+    if( (iBlock>>iLogsize) & 1 ){
+      iBuddy = iBlock - size;
     }else{
-      iBuddy = i + size;
+      iBuddy = iBlock + size;
     }
-    assert( iBuddy>=0 && iBuddy<mem5.nBlock );
+    assert( iBuddy>=0 );
+    if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
     if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
     memsys5Unlink(iBuddy, iLogsize);
     iLogsize++;
-    if( iBuddy<i ){
+    if( iBuddy<iBlock ){
       mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
-      mem5.aCtrl[i] = 0;
-      i = iBuddy;
+      mem5.aCtrl[iBlock] = 0;
+      iBlock = iBuddy;
     }else{
-      mem5.aCtrl[i] = CTRL_FREE | iLogsize;
+      mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
       mem5.aCtrl[iBuddy] = 0;
     }
     size *= 2;
   }
-  memsys5Link(i, iLogsize);
+  memsys5Link(iBlock, iLogsize);
 }
 
 /*
@@ -344,6 +351,7 @@
 */
 static void memsys5Free(void *pPrior){
   if( pPrior==0 ){
+assert(0);
     return;
   }
   memsys5Enter();
@@ -383,14 +391,51 @@
 */
 static int memsys5Roundup(int n){
   int iFullSz;
-  for(iFullSz=POW2_MIN; iFullSz<n; iFullSz *= 2);
+  for(iFullSz=mem5.nAtom; iFullSz<n; iFullSz *= 2);
   return iFullSz;
 }
 
+static int memsys5Log(int iValue){
+  int iLog;
+  for(iLog=0; (1<<iLog)<iValue; iLog++);
+  return iLog;
+}
+
 /*
 ** Initialize this module.
 */
 static int memsys5Init(void *NotUsed){
+  int ii;
+  int nByte = sqlite3Config.nHeap;
+  u8 *zByte = (u8 *)sqlite3Config.pHeap;
+  int nMinLog;                 /* Log of minimum allocation size in bytes*/
+  int iOffset;
+
+  nMinLog = memsys5Log(sqlite3Config.mnReq);
+  mem5.nAtom = (1<<nMinLog);
+  while( sizeof(Mem5Link)>mem5.nAtom ){
+    mem5.nAtom = mem5.nAtom << 1;
+  }
+
+  mem5.nBlock = (nByte / (mem5.nAtom+sizeof(u8)));
+  mem5.zPool = zByte;
+  mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.nAtom];
+
+  for(ii=0; ii<=LOGMAX; ii++){
+    mem5.aiFreelist[ii] = -1;
+  }
+
+  iOffset = 0;
+  for(ii=LOGMAX; ii>=0; ii--){
+    int nAlloc = (1<<ii);
+    if( (iOffset+nAlloc)<=mem5.nBlock ){
+      mem5.aCtrl[iOffset] = ii | CTRL_FREE;
+      memsys5Link(iOffset, ii);
+      iOffset += nAlloc;
+    }
+    assert((iOffset+nAlloc)>mem5.nBlock);
+  }
+
   return SQLITE_OK;
 }
 
@@ -409,6 +454,7 @@
 #ifdef SQLITE_DEBUG
   FILE *out;
   int i, j, n;
+  int nMinLog;
 
   if( zFilename==0 || zFilename[0]==0 ){
     out = stdout;
@@ -421,9 +467,10 @@
     }
   }
   memsys5Enter();
-  for(i=0; i<NSIZE; i++){
-    for(n=0, j=mem5.aiFreelist[i]; j>=0; j = mem5.aPool[j].u.list.next, n++){}
-    fprintf(out, "freelist items of size %d: %d\n", POW2_MIN << i, n);
+  nMinLog = memsys5Log(mem5.nAtom);
+  for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
+    for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
+    fprintf(out, "freelist items of size %d: %d\n", mem5.nAtom << i, n);
   }
   fprintf(out, "mem5.nAlloc       = %llu\n", mem5.nAlloc);
   fprintf(out, "mem5.totalAlloc   = %llu\n", mem5.totalAlloc);
@@ -444,16 +491,10 @@
 
 /*
 ** This routine is the only routine in this file with external 
-** linkage.
-**
-** Populate the low-level memory allocation function pointers in
-** sqlite3Config.m with pointers to the routines in this file. The
-** arguments specify the block of memory to manage.
-**
-** This routine is only called by sqlite3_config(), and therefore
-** is not required to be threadsafe (it is not).
+** linkage. It returns a pointer to a static sqlite3_mem_methods
+** struct populated with the memsys5 methods.
 */
-void sqlite3MemSetMemsys5(u8 *zByte, int nByte){
+const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
   static const sqlite3_mem_methods memsys5Methods = {
      memsys5Malloc,
      memsys5Free,
@@ -464,25 +505,7 @@
      memsys5Shutdown,
      0
   };
-  int i;
-
-  mem5.nBlock = (nByte / (sizeof(Mem5Block)+sizeof(u8)));
-  mem5.nBlock -= (mem5.nBlock%SZ_MAX);
-  mem5.aPool = (Mem5Block *)zByte;
-  mem5.aCtrl = (u8 *)&mem5.aPool[mem5.nBlock];
-
-  assert( sizeof(Mem5Block)==POW2_MIN );
-  assert( mem5.nBlock>=SZ_MAX );
-  assert( (mem5.nBlock%SZ_MAX)==0 );
-
-  for(i=0; i<NSIZE; i++) mem5.aiFreelist[i] = -1;
-  for(i=0; i<=mem5.nBlock-SZ_MAX; i += SZ_MAX){
-    mem5.aCtrl[i] = (NSIZE-1) | CTRL_FREE;
-    memsys5Link(i, NSIZE-1);
-  }
-
-  /* Configure the functions to call to allocate memory. */
-  sqlite3_config(SQLITE_CONFIG_MALLOC, &memsys5Methods);
+  return &memsys5Methods;
 }
 
 #endif /* SQLITE_ENABLE_MEMSYS5 */
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index 2d65817..c5d4215 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -11,7 +11,7 @@
 *************************************************************************
 ** Internal interface definitions for SQLite.
 **
-** @(#) $Id: sqliteInt.h,v 1.732 2008/06/26 18:04:03 danielk1977 Exp $
+** @(#) $Id: sqliteInt.h,v 1.733 2008/06/27 13:27:04 danielk1977 Exp $
 */
 #ifndef _SQLITEINT_H_
 #define _SQLITEINT_H_
@@ -1807,7 +1807,7 @@
 void sqlite3PageFree(void*);
 void sqlite3MemSetDefault(void);
 void sqlite3MemSetMemsys3(u8 *pBlock, int nBlock);
-void sqlite3MemSetMemsys5(u8 *pBlock, int nBlock);
+const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
 void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
 
 #ifndef SQLITE_MUTEX_NOOP
diff --git a/src/test_malloc.c b/src/test_malloc.c
index dbb6fec..17f6a43 100644
--- a/src/test_malloc.c
+++ b/src/test_malloc.c
@@ -13,7 +13,7 @@
 ** This file contains code used to implement test interfaces to the
 ** memory allocation subsystem.
 **
-** $Id: test_malloc.c,v 1.31 2008/06/25 14:26:09 danielk1977 Exp $
+** $Id: test_malloc.c,v 1.32 2008/06/27 13:27:04 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 #include "tcl.h"
@@ -965,6 +965,47 @@
 }
 
 /*
+** Usage:
+**
+**   sqlite3_config_heap ?-memsys3? NBYTE NMINALLOC
+*/
+static int test_config_heap(
+  void * clientData, 
+  Tcl_Interp *interp,
+  int objc,
+  Tcl_Obj *CONST objv[]
+){
+  static char zBuf[1048576];
+  int nByte;         /* Size of buffer to pass to sqlite3_config() */
+  int nMinAlloc;     /* Size of minimum allocation */
+  int rc;            /* Return code of sqlite3_config() */
+  int isMemsys3 = 0; /* True if the -memsys3 switch is present */
+
+  Tcl_Obj * CONST *aArg = &objv[1];
+  int nArg = objc-1;
+
+  if( nArg>0 && 0==strcmp("-memsys3", Tcl_GetString(aArg[0])) ){
+    nArg--;
+    aArg++;
+    isMemsys3 = 1;
+  }
+  if( nArg!=2 ){
+    Tcl_WrongNumArgs(interp, 1, objv, "?-memsys3? NBYTE NMINALLOC");
+    return TCL_ERROR;
+  }
+  if( Tcl_GetIntFromObj(interp, aArg[0], &nByte) ) return TCL_ERROR;
+  if( Tcl_GetIntFromObj(interp, aArg[1], &nMinAlloc) ) return TCL_ERROR;
+
+  if( nByte>sizeof(zBuf) ){
+    nByte = sizeof(zBuf);
+  }
+  rc = sqlite3_config(SQLITE_CONFIG_HEAP, zBuf, nByte, nMinAlloc);
+
+  Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
+  return TCL_OK;
+}
+
+/*
 ** Usage:    
 **
 **   sqlite3_dump_memsys3  FILENAME
@@ -1106,6 +1147,7 @@
      { "install_malloc_faultsim",    test_install_malloc_faultsim  ,0. },
      { "sqlite3_config_memsys3", test_config_memsys3, SQLITE_CONFIG_MEMSYS3 },
      { "sqlite3_config_memsys5", test_config_memsys3, SQLITE_CONFIG_MEMSYS5 },
+     { "sqlite3_config_heap",        test_config_heap,              0 },
      { "sqlite3_dump_memsys3",   test_dump_memsys3  , SQLITE_CONFIG_MEMSYS3 },
      { "sqlite3_dump_memsys5",   test_dump_memsys3  , SQLITE_CONFIG_MEMSYS5 }
   };
diff --git a/test/permutations.test b/test/permutations.test
index 8c2f6f2..81cff4d 100644
--- a/test/permutations.test
+++ b/test/permutations.test
@@ -9,7 +9,7 @@
 #
 #***********************************************************************
 #
-# $Id: permutations.test,v 1.7 2008/06/25 14:57:54 danielk1977 Exp $
+# $Id: permutations.test,v 1.8 2008/06/27 13:27:04 danielk1977 Exp $
 
 set testdir [file dirname $argv0]
 source $testdir/tester.tcl
@@ -367,7 +367,27 @@
     catch {db close}
     sqlite3_reset_auto_extension
     sqlite3_shutdown
-    sqlite3_config_memsys5 1000000
+    sqlite3_config_heap 1000000 64
+    install_malloc_faultsim 1 
+    sqlite3_initialize
+    autoinstall_test_functions
+  } -shutdown {
+    catch {db close}
+    sqlite3_reset_auto_extension
+    sqlite3_shutdown
+    sqlite3_config_memsys5 0
+    install_malloc_faultsim 1 
+    sqlite3_initialize
+  }
+
+  run_tests "memsys5-2" -description {
+    Run tests using the allocator in mem5.c in a different configuration.
+  } -include { select1.test 
+  } -initialize {
+    catch {db close}
+    sqlite3_reset_auto_extension
+    sqlite3_shutdown
+    sqlite3_config_heap 250000 16
     install_malloc_faultsim 1 
     sqlite3_initialize
     autoinstall_test_functions