From 0d84e5b2a71d92d82dd559bd268dc9ecd7a2c966 Mon Sep 17 00:00:00 2001
From: danielk1977 <danielk1977@noemail.net>
Date: Fri, 27 Jun 2008 14:05:24 +0000
Subject: [PATCH] Fix mem3.c (broken by (5320)). (CVS 5321)

FossilOrigin-Name: fef90a21aea11f15371d3fcf44548d786dd8819b
---
 manifest               | 20 +++++++-------
 manifest.uuid          |  2 +-
 src/mem3.c             | 38 +++++++++++++------------
 src/mem5.c             |  6 +++-
 src/sqliteInt.h        |  4 +--
 src/test_malloc.c      | 63 ++++++++++++------------------------------
 test/permutations.test | 10 +++----
 7 files changed, 61 insertions(+), 82 deletions(-)

diff --git a/manifest b/manifest
index f58701d8fa..d600f9658e 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Change\smem5.c\sso\sthat\sthe\sminimum\sallocation\ssize\sis\sruntime\sconfigurable.\s(CVS\s5320)
-D 2008-06-27T13:27:04
+C Fix\smem3.c\s(broken\sby\s(5320)).\s(CVS\s5321)
+D 2008-06-27T14:05:25
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in 325dfac0a0dd1cb4d975f1ace6453157892e6042
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -119,9 +119,9 @@ 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/mem3.c abaf65e3fa911b8acdbb67a50a54ec149af55736
 F src/mem4.c 6703adb1717b26d9d70a1c2586b4b7b7ffee7909
-F src/mem5.c 2ad92d6aa224650f9fa9ad41fbf27b6cdae8a76f
+F src/mem5.c 6c711af220f2c1566724dc439a34a98f94ab01a0
 F src/mutex.c a485a0eac8ee2cd95f66e565b4c6696c18db968f
 F src/mutex.h e52ffa1dfc6a6077e8b1823d2c2b7dfcbcf85594
 F src/mutex_os2.c 9c5637aa4c307c552566d0f0b3bd206245b54a97
@@ -144,7 +144,7 @@ F src/select.c 5b213de002382aeaac5c9c2d3be7c98900bc3056
 F src/shell.c 479807b87f0409289eec4a776cd6ae56d30544b1
 F src/sqlite.h.in bcdf4f68ce70ca30f0c3d351858383ce398e3c66
 F src/sqlite3ext.h 7bf1651c6fb7183831e5113b4b91662bbe67e5bf
-F src/sqliteInt.h a3d505360be0385fdd6d02fef82f3f10e952259e
+F src/sqliteInt.h 35e420ced9e27af5ee387cd40f2e7a431665f858
 F src/sqliteLimit.h f435e728c6b620ef7312814d660a81f9356eb5c8
 F src/status.c 6cb10377992505bd69f1ca1d75c1240a65f25a58
 F src/table.c 1fa8f8113ac9cbc09ae4801c6d2a7f0af82c5822
@@ -166,7 +166,7 @@ F src/test_devsym.c 6012cb8e3acf812513511025a4fa5d626e0ba19b
 F src/test_func.c f4aafa10f17d52c43a64b47717265802e6e552b3
 F src/test_hexio.c 2f1122aa3f012fa0142ee3c36ce5c902a70cd12f
 F src/test_loadext.c df8ab3a6481ddebbdf0d28ebac5d9e0790f7860f
-F src/test_malloc.c 68da4add2f3f5f2aace5a15122df80e5f8454af4
+F src/test_malloc.c 62296810d3ee914fc3e05f2a8a69d8f8f42c32e1
 F src/test_md5.c 28209a4e2068711b5443c33104fe41f21d160071
 F src/test_mutex.c b1433eb96520656fb3e4a218253a94ac32bd5775
 F src/test_onefile.c 243157b10275251c5dc2d6619aee2ff9ae22379c
@@ -422,7 +422,7 @@ F test/pager2.test c025f91b75fe65e85febda64d9416428b8a5cab5
 F test/pager3.test 2323bf27fd5bd887b580247e5bce500ceee994b4
 F test/pageropt.test 6df72c441db0a037b7ec6990d16311c24fbea77b
 F test/pagesize.test e0a8b3fe80f8b8e808d94a00734c7a18c76c407e
-F test/permutations.test 17aa068f5f6612a9e91ec316463f94141436445a
+F test/permutations.test af83024a469d1cade2a6ca9ce33e61bcdd7c2672
 F test/pragma.test 44bc52afa72cabfeae82641e0033ca24919f3ce8
 F test/pragma2.test 5364893491b9231dd170e3459bfc2e2342658b47
 F test/printf.test c3405535b418d454e8a52196a0fc592ec9eec58d
@@ -594,7 +594,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81
 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
-P 0b01ec5cf7725a02d4c12167df125cef578f6219
-R 0a2d2902a0d1d00bcf1712dac8ab54a9
+P 4f95f4cdf77e134fab42148e10198c7b008d4ae6
+R 63274e6bfa4b3133971620ba2cd4bf5a
 U danielk1977
-Z 48c1b0881009fcf3cbbdeacd5d1ca6e8
+Z 6121d4e53f8a4f4f8796267fc367b534
diff --git a/manifest.uuid b/manifest.uuid
index 7fab162c17..cfa6ece185 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-4f95f4cdf77e134fab42148e10198c7b008d4ae6
\ No newline at end of file
+fef90a21aea11f15371d3fcf44548d786dd8819b
\ No newline at end of file
diff --git a/src/mem3.c b/src/mem3.c
index 2582dda553..38c89f77ff 100644
--- a/src/mem3.c
+++ b/src/mem3.c
@@ -23,7 +23,7 @@
 ** This version of the memory allocation subsystem is included
 ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
 **
-** $Id: mem3.c,v 1.17 2008/06/25 14:57:54 danielk1977 Exp $
+** $Id: mem3.c,v 1.18 2008/06/27 14:05:25 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 
@@ -546,6 +546,23 @@ static int memsys3Roundup(int n){
 ** Initialize this module.
 */
 static int memsys3Init(void *NotUsed){
+  if( !sqlite3Config.pHeap ){
+    return SQLITE_ERROR;
+  }
+
+  /* Store a pointer to the memory block in global structure mem3. */
+  assert( sizeof(Mem3Block)==8 );
+  mem3.aPool = (Mem3Block *)sqlite3Config.pHeap;
+  mem3.nPool = (sqlite3Config.nHeap / sizeof(Mem3Block)) - 2;
+
+  /* Initialize the master block. */
+  mem3.szMaster = mem3.nPool;
+  mem3.mnMaster = mem3.szMaster;
+  mem3.iMaster = 1;
+  mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
+  mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
+  mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
+
   return SQLITE_OK;
 }
 
@@ -644,7 +661,7 @@ void sqlite3Memsys3Dump(const char *zFilename){
 ** This routine is only called by sqlite3_config(), and therefore
 ** is not required to be threadsafe (it is not).
 */
-void sqlite3MemSetMemsys3(u8 *pBlock, int nBlock){
+const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
   static const sqlite3_mem_methods mempoolMethods = {
      memsys3Malloc,
      memsys3Free,
@@ -655,22 +672,7 @@ void sqlite3MemSetMemsys3(u8 *pBlock, int nBlock){
      memsys3Shutdown,
      0
   };
-
-  /* Configure the functions to call to allocate memory. */
-  sqlite3_config(SQLITE_CONFIG_MALLOC, &mempoolMethods);
-
-  /* Store a pointer to the memory block in global structure mem3. */
-  assert( sizeof(Mem3Block)==8 );
-  mem3.aPool = (Mem3Block *)pBlock;
-  mem3.nPool = (nBlock / sizeof(Mem3Block)) - 2;
-
-  /* Initialize the master block. */
-  mem3.szMaster = mem3.nPool;
-  mem3.mnMaster = mem3.szMaster;
-  mem3.iMaster = 1;
-  mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
-  mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
-  mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
+  return &mempoolMethods;
 }
 
 #endif /* SQLITE_ENABLE_MEMSYS3 */
diff --git a/src/mem5.c b/src/mem5.c
index acbe92d15d..6161945d6c 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.9 2008/06/27 13:27:04 danielk1977 Exp $
+** $Id: mem5.c,v 1.10 2008/06/27 14:05:25 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 
@@ -411,6 +411,10 @@ static int memsys5Init(void *NotUsed){
   int nMinLog;                 /* Log of minimum allocation size in bytes*/
   int iOffset;
 
+  if( !zByte ){
+    return SQLITE_ERROR;
+  }
+
   nMinLog = memsys5Log(sqlite3Config.mnReq);
   mem5.nAtom = (1<<nMinLog);
   while( sizeof(Mem5Link)>mem5.nAtom ){
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index c5d42152fb..b9ecdf6198 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -11,7 +11,7 @@
 *************************************************************************
 ** Internal interface definitions for SQLite.
 **
-** @(#) $Id: sqliteInt.h,v 1.733 2008/06/27 13:27:04 danielk1977 Exp $
+** @(#) $Id: sqliteInt.h,v 1.734 2008/06/27 14:05:25 danielk1977 Exp $
 */
 #ifndef _SQLITEINT_H_
 #define _SQLITEINT_H_
@@ -1806,8 +1806,8 @@ void sqlite3ScratchFree(void*);
 void *sqlite3PageMalloc(int);
 void sqlite3PageFree(void*);
 void sqlite3MemSetDefault(void);
-void sqlite3MemSetMemsys3(u8 *pBlock, int nBlock);
 const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
+const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
 void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
 
 #ifndef SQLITE_MUTEX_NOOP
diff --git a/src/test_malloc.c b/src/test_malloc.c
index 17f6a43fe3..906ee26a75 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.32 2008/06/27 13:27:04 danielk1977 Exp $
+** $Id: test_malloc.c,v 1.33 2008/06/27 14:05:25 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 #include "tcl.h"
@@ -930,40 +930,6 @@ static int test_config_pagecache(
   return TCL_OK;
 }
 
-/*
-** Usage:    
-** 
-**   sqlite3_config_memsys3 NBYTE
-**   sqlite3_config_memsys5 NBYTE
-**
-*/
-static int test_config_memsys3(
-  void * clientData, 
-  Tcl_Interp *interp,
-  int objc,
-  Tcl_Obj *CONST objv[]
-){
-  int sz, rc;
-  static char buf[1048576];
-  if( objc!=2 ){
-    Tcl_WrongNumArgs(interp, 1, objv, "NBYTE");
-    return TCL_ERROR;
-  }
-  if( Tcl_GetIntFromObj(interp, objv[1], &sz) ) return TCL_ERROR;
-  if( sz<=0 ){
-    sqlite3_mem_methods m;
-    memset(&m, 0, sizeof(sqlite3_mem_methods));
-    rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &m);
-  }else{
-    if( sz>sizeof(buf) ){
-      sz = sizeof(buf);
-    }
-    rc = sqlite3_config((int)clientData, buf, sz);
-  }
-  Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
-  return TCL_OK;
-}
-
 /*
 ** Usage:
 **
@@ -996,10 +962,19 @@ static int test_config_heap(
   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);
+  if( nByte==0 ){
+    sqlite3_mem_methods m;
+    memset(&m, 0, sizeof(sqlite3_mem_methods));
+    rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &m);
+  }else{
+    if( nByte>sizeof(zBuf) ){
+      nByte = sizeof(zBuf);
+    }
+    rc = sqlite3_config(SQLITE_CONFIG_HEAP, zBuf, nByte, nMinAlloc);
+    if( isMemsys3 && rc==SQLITE_OK ){
+       rc = sqlite3_config(SQLITE_CONFIG_MEMSYS3);
+    }
   }
-  rc = sqlite3_config(SQLITE_CONFIG_HEAP, zBuf, nByte, nMinAlloc);
 
   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
   return TCL_OK;
@@ -1025,14 +1000,14 @@ static int test_dump_memsys3(
   }
 
   switch( (int)clientData ){
-    case SQLITE_CONFIG_MEMSYS3: {
+    case 3: {
 #ifdef SQLITE_ENABLE_MEMSYS3
       extern void sqlite3Memsys3Dump(const char*);
       sqlite3Memsys3Dump(Tcl_GetString(objv[1]));
       break;
 #endif
     }
-    case SQLITE_CONFIG_MEMSYS5: {
+    case 5: {
 #ifdef SQLITE_ENABLE_MEMSYS5
       extern void sqlite3Memsys5Dump(const char*);
       sqlite3Memsys5Dump(Tcl_GetString(objv[1]));
@@ -1145,11 +1120,9 @@ int Sqlitetest_malloc_Init(Tcl_Interp *interp){
      { "sqlite3_config_pagecache",   test_config_pagecache         ,0. },
      { "sqlite3_status",             test_status                   ,0. },
      { "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 }
+     { "sqlite3_config_heap",        test_config_heap              ,0 },
+     { "sqlite3_dump_memsys3",       test_dump_memsys3             ,3 },
+     { "sqlite3_dump_memsys5",       test_dump_memsys3             ,5 }
   };
   int i;
   for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
diff --git a/test/permutations.test b/test/permutations.test
index 81cff4d5c4..9254bd5978 100644
--- a/test/permutations.test
+++ b/test/permutations.test
@@ -9,7 +9,7 @@
 #
 #***********************************************************************
 #
-# $Id: permutations.test,v 1.8 2008/06/27 13:27:04 danielk1977 Exp $
+# $Id: permutations.test,v 1.9 2008/06/27 14:05:25 danielk1977 Exp $
 
 set testdir [file dirname $argv0]
 source $testdir/tester.tcl
@@ -341,7 +341,7 @@ ifcapable mem3 {
     catch {db close}
     sqlite3_reset_auto_extension
     sqlite3_shutdown
-    sqlite3_config_memsys3 1000000
+    sqlite3_config_heap -memsys3 1000000 0
     install_malloc_faultsim 1 
     sqlite3_initialize
     autoinstall_test_functions
@@ -349,7 +349,7 @@ ifcapable mem3 {
     catch {db close}
     sqlite3_reset_auto_extension
     sqlite3_shutdown
-    sqlite3_config_memsys3 0
+    sqlite3_config_heap 0 0
     install_malloc_faultsim 1 
     sqlite3_initialize
   }
@@ -375,7 +375,7 @@ ifcapable mem5 {
     catch {db close}
     sqlite3_reset_auto_extension
     sqlite3_shutdown
-    sqlite3_config_memsys5 0
+    sqlite3_config_heap 0 0
     install_malloc_faultsim 1 
     sqlite3_initialize
   }
@@ -395,7 +395,7 @@ ifcapable mem5 {
     catch {db close}
     sqlite3_reset_auto_extension
     sqlite3_shutdown
-    sqlite3_config_memsys5 0
+    sqlite3_config_heap 0 0
     install_malloc_faultsim 1 
     sqlite3_initialize
   }