Separate verbs of sqlite3_config() and sqlite3_db_config() into their
own namespaces.  Allow SQLITE3_DBCONFIG_LOOKASIDE to specific an external
memory buffer. (CVS 5536)

FossilOrigin-Name: 5dd865da5e787c10ef4c9e96647724bfab9dea01
diff --git a/src/main.c b/src/main.c
index 8400dae..68500eb 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.485 2008/08/01 18:47:02 drh Exp $
+** $Id: main.c,v 1.486 2008/08/04 20:13:27 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -294,8 +294,14 @@
 ** Set up the lookaside buffers for a database connection.
 ** Return SQLITE_OK on success.  
 ** If lookaside is already active, return SQLITE_BUSY.
+**
+** The sz parameter is the number of bytes in each lookaside slot.
+** The cnt parameter is the number of slots.  If pStart is NULL the
+** space for the lookaside memory is obtained from sqlite3_malloc().
+** If pStart is not NULL then it is sz*cnt bytes of memory to use for
+** the lookaside memory.
 */
-static int setupLookaside(sqlite3 *db, int sz, int cnt){
+static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
   void *pStart;
   if( db->lookaside.nOut ){
     return SQLITE_BUSY;
@@ -303,13 +309,20 @@
   if( sz<0 ) sz = 0;
   if( cnt<0 ) cnt = 0;
   sz = (sz+7)&~7;
-  sqlite3BeginBenignMalloc();
-  pStart = sqlite3Malloc( sz*cnt );
-  sqlite3EndBenignMalloc();
-  sqlite3_free(db->lookaside.pStart);
+  if( pBuf==0 ){
+    sqlite3BeginBenignMalloc();
+    pStart = sqlite3Malloc( sz*cnt );
+    sqlite3EndBenignMalloc();
+  }else{
+    pStart = pBuf;
+  }
+  if( db->lookaside.bMalloced ){
+    sqlite3_free(db->lookaside.pStart);
+  }
   db->lookaside.pStart = pStart;
   db->lookaside.pFree = 0;
   db->lookaside.sz = sz;
+  db->lookaside.bMalloced = pBuf==0;
   if( pStart ){
     int i;
     LookasideSlot *p;
@@ -336,10 +349,11 @@
   int rc;
   va_start(ap, op);
   switch( op ){
-    case SQLITE_CONFIG_LOOKASIDE: {
+    case SQLITE_DBCONFIG_LOOKASIDE: {
+      void *pBuf = va_arg(ap, void*);
       int sz = va_arg(ap, int);
       int cnt = va_arg(ap, int);
-      rc = setupLookaside(db, sz, cnt);
+      rc = setupLookaside(db, pBuf, sz, cnt);
       break;
     }
     default: {
@@ -545,7 +559,9 @@
   sqlite3_mutex_leave(db->mutex);
   db->magic = SQLITE_MAGIC_CLOSED;
   sqlite3_mutex_free(db->mutex);
-  sqlite3_free(db->lookaside.pStart);
+  if( db->lookaside.bMalloced ){
+    sqlite3_free(db->lookaside.pStart);
+  }
   sqlite3_free(db);
   return SQLITE_OK;
 }
@@ -1523,7 +1539,7 @@
 #endif
 
   /* Enable the lookaside-malloc subsystem */
-  setupLookaside(db, sqlite3Config.szLookaside, sqlite3Config.nLookaside);
+  setupLookaside(db, 0, sqlite3Config.szLookaside, sqlite3Config.nLookaside);
 
 opendb_out:
   if( db ){
diff --git a/src/sqlite.h.in b/src/sqlite.h.in
index 0ee086b..5df36cc 100644
--- a/src/sqlite.h.in
+++ b/src/sqlite.h.in
@@ -30,7 +30,7 @@
 ** the version number) and changes its name to "sqlite3.h" as
 ** part of the build process.
 **
-** @(#) $Id: sqlite.h.in,v 1.385 2008/08/04 13:44:57 drh Exp $
+** @(#) $Id: sqlite.h.in,v 1.386 2008/08/04 20:13:27 drh Exp $
 */
 #ifndef _SQLITE3_H_
 #define _SQLITE3_H_
@@ -983,8 +983,9 @@
 ** The second argument to sqlite3_db_config(D,V,...)  is the
 ** configuration verb - an integer code that indicates what
 ** aspect of the [database connection] is being configured.
-** Choices for this value are [SQLITE_CONFIG_LOOKASIDE].
+** The only choice for this value is [SQLITE_DBCONFIG_LOOKASIDE].
 ** New verbs are likely to be added in future releases of SQLite.
+** Additional arguments depend on the verb.
 */
 int sqlite3_db_config(sqlite3*, int op, ...);
 
@@ -1192,6 +1193,36 @@
 #define SQLITE_CONFIG_CHUNKALLOC   12  /* int threshold */
 #define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
 
+/*
+** CAPI3REF: Configuration Options {H10170} <S20000>
+** EXPERIMENTAL
+**
+** These constants are the available integer configuration options that
+** can be passed as the second argument to the [sqlite3_db_config()] interface.
+**
+** New configuration options may be added in future releases of SQLite.
+** Existing configuration options might be discontinued.  Applications
+** should check the return code from [sqlite3_db_config()] to make sure that
+** the call worked.  The [sqlite3_db_config()] interface will return a
+** non-zero [error code] if a discontinued or unsupported configuration option
+** is invoked.
+**
+** <dl>
+** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
+** <dd>This option takes three additional arguments that determine the 
+** [lookaside memory allocator] configuration for the [database connection].
+** The first argument (the third parameter to [sqlite3_db_config()] is a
+** pointer to a memory buffer to use for lookaside memory.  The first
+** argument may be NULL in which case SQLite will allocate the lookaside
+** buffer itself using [sqlite3_malloc()].  The second argument is the
+** size of each lookaside buffer slot and the third argument is the number of
+** slots.  The size of the buffer in the first argument must be greater than
+** or equal to the product of the second and third arguments.</dd>
+**
+** </dl>
+*/
+#define SQLITE_DBCONFIG_LOOKASIDE    1001  /* void* int int */
+
 
 /*
 ** CAPI3REF: Enable Or Disable Extended Result Codes {H12200} <S10700>
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index 6897671..68de4a1 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -11,7 +11,7 @@
 *************************************************************************
 ** Internal interface definitions for SQLite.
 **
-** @(#) $Id: sqliteInt.h,v 1.751 2008/08/02 15:32:40 danielk1977 Exp $
+** @(#) $Id: sqliteInt.h,v 1.752 2008/08/04 20:13:27 drh Exp $
 */
 #ifndef _SQLITEINT_H_
 #define _SQLITEINT_H_
@@ -575,6 +575,7 @@
 struct Lookaside {
   u16 sz;                 /* Size of each buffer in bytes */
   u8 bEnabled;            /* True if use lookaside.  False to ignore it */
+  u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
   int nOut;               /* Number of buffers currently checked out */
   int mxOut;              /* Highwater mark for nOut */
   LookasideSlot *pFree;   /* List if available buffers */
diff --git a/src/test_malloc.c b/src/test_malloc.c
index 8eb73d8..449cdb2 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.45 2008/08/01 16:31:14 drh Exp $
+** $Id: test_malloc.c,v 1.46 2008/08/04 20:13:27 drh Exp $
 */
 #include "sqliteInt.h"
 #include "tcl.h"
@@ -1005,8 +1005,11 @@
 
 
 /*
-** Usage:    sqlite3_db_config_lookaside  CONNECTION  SIZE  COUNT
+** Usage:    sqlite3_db_config_lookaside  CONNECTION  BUFID  SIZE  COUNT
 **
+** There are two static buffers with BUFID 1 and 2.   Each static buffer
+** is 10KB in size.  A BUFID of 0 indicates that the buffer should be NULL
+** which will cause sqlite3_db_config() to allocate space on its own.
 */
 static int test_db_config_lookaside(
   void * clientData,
@@ -1017,15 +1020,25 @@
   int rc;
   int sz, cnt;
   sqlite3 *db;
+  int bufid;
+  static char azBuf[2][10000];
   int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
-  if( objc!=4 ){
-    Tcl_WrongNumArgs(interp, 1, objv, "SIZE COUNT");
+  if( objc!=5 ){
+    Tcl_WrongNumArgs(interp, 1, objv, "BUFID SIZE COUNT");
     return TCL_ERROR;
   }
   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
-  if( Tcl_GetIntFromObj(interp, objv[2], &sz) ) return TCL_ERROR;
-  if( Tcl_GetIntFromObj(interp, objv[3], &cnt) ) return TCL_ERROR;
-  rc = sqlite3_db_config(db, SQLITE_CONFIG_LOOKASIDE, sz, cnt);
+  if( Tcl_GetIntFromObj(interp, objv[2], &bufid) ) return TCL_ERROR;
+  if( Tcl_GetIntFromObj(interp, objv[3], &sz) ) return TCL_ERROR;
+  if( Tcl_GetIntFromObj(interp, objv[4], &cnt) ) return TCL_ERROR;
+  if( bufid==0 ){
+    rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, 0, sz, cnt);
+  }else if( bufid>=1 && bufid<=2 && sz*cnt<=sizeof(azBuf[0]) ){
+    rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, azBuf[bufid], sz,cnt);
+  }else{
+    Tcl_AppendResult(interp, "illegal arguments - see documentation", (char*)0);
+    return TCL_ERROR;
+  }
   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
   return TCL_OK;
 }