/ Check-in [44f8e313]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add a runtime interface to enable memory-management features. (CVS 2894)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:44f8e3139aa3ad3fcce9028565dd9ad23bbe62a2
User & Date: danielk1977 2006-01-09 09:59:49
Context
2006-01-09
16:12
Fix some errors to do with attached databases and text encodings in shared-cache mode. (CVS 2895) check-in: 3e75d3d5 user: danielk1977 tags: trunk
09:59
Add a runtime interface to enable memory-management features. (CVS 2894) check-in: 44f8e313 user: danielk1977 tags: trunk
06:29
Rename DbSchema to "Schema" and SqliteTsd to "ThreadData". (CVS 2893) check-in: 82b81f69 user: danielk1977 tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/btree.c.

     5      5   ** a legal notice, here is a blessing:
     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12         -** $Id: btree.c,v 1.287 2006/01/09 06:29:48 danielk1977 Exp $
           12  +** $Id: btree.c,v 1.288 2006/01/09 09:59:49 danielk1977 Exp $
    13     13   **
    14     14   ** This file implements a external (disk-based) database using BTrees.
    15     15   ** For a detailed discussion of BTrees, refer to
    16     16   **
    17     17   **     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
    18     18   **     "Sorting And Searching", pages 473-480. Addison-Wesley
    19     19   **     Publishing Company, Reading, Massachusetts.
................................................................................
  6500   6500   }
  6501   6501   
  6502   6502   #ifndef SQLITE_OMIT_SHARED_CACHE
  6503   6503   /*
  6504   6504   ** Enable the shared pager and schema features.
  6505   6505   */
  6506   6506   int sqlite3_enable_shared_cache(int enable){
  6507         -  ThreadData *pTsd = sqlite3ThreadData();
  6508         -  if( pTsd->pPager ){
         6507  +  ThreadData *pTd = sqlite3ThreadData();
         6508  +  
         6509  +  /* It is only legal to call sqlite3_enable_shared_cache() when there
         6510  +  ** are no currently open b-trees that were opened by the calling thread.
         6511  +  ** This condition is only easy to detect if the shared-cache were 
         6512  +  ** previously enabled (and is being disabled). 
         6513  +  */
         6514  +  if( pTd->pBtree && !enable ){
         6515  +    assert( pTd->useSharedData );
  6509   6516       return SQLITE_MISUSE;
  6510   6517     }
  6511         -  pTsd->useSharedData = enable;
         6518  +
         6519  +  pTd->useSharedData = enable;
  6512   6520     return SQLITE_OK;
  6513   6521   }
  6514   6522   #endif

Changes to src/pager.c.

    14     14   ** The pager is used to access a database disk file.  It implements
    15     15   ** atomic commit and rollback through the use of a journal file that
    16     16   ** is separate from the database file.  The pager also implements file
    17     17   ** locking to prevent two processes from writing the same database
    18     18   ** file simultaneously, or one process from reading the database while
    19     19   ** another is writing.
    20     20   **
    21         -** @(#) $Id: pager.c,v 1.232 2006/01/09 06:29:49 danielk1977 Exp $
           21  +** @(#) $Id: pager.c,v 1.233 2006/01/09 09:59:49 danielk1977 Exp $
    22     22   */
    23     23   #ifndef SQLITE_OMIT_DISKIO
    24     24   #include "sqliteInt.h"
    25     25   #include "os.h"
    26     26   #include "pager.h"
    27     27   #include <assert.h>
    28     28   #include <string.h>
................................................................................
  1619   1619     int i;
  1620   1620     int tempFile = 0;
  1621   1621     int memDb = 0;
  1622   1622     int readOnly = 0;
  1623   1623     int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
  1624   1624     int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
  1625   1625     char zTemp[SQLITE_TEMPNAME_SIZE];
         1626  +#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
  1626   1627     ThreadData *pTsd = sqlite3ThreadData();
         1628  +#endif
  1627   1629   
  1628   1630     /* If malloc() has already failed return SQLITE_NOMEM. Before even
  1629   1631     ** testing for this, set *ppPager to NULL so the caller knows the pager
  1630   1632     ** structure was never allocated. 
  1631   1633     */
  1632   1634     *ppPager = 0;
  1633   1635     if( sqlite3ThreadData()->mallocFailed ){
................................................................................
  1719   1721     pPager->pFirstSynced = 0;
  1720   1722     pPager->pLast = 0;
  1721   1723     pPager->nExtra = FORCE_ALIGNMENT(nExtra);
  1722   1724     pPager->sectorSize = PAGER_SECTOR_SIZE;
  1723   1725     pPager->pBusyHandler = 0;
  1724   1726     memset(pPager->aHash, 0, sizeof(pPager->aHash));
  1725   1727     *ppPager = pPager;
  1726         -  pPager->pNext = pTsd->pPager;
  1727         -  pTsd->pPager = pPager;
         1728  +#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
         1729  +  if( pTsd->useMemoryManagement ){
         1730  +    pPager->pNext = pTsd->pPager;
         1731  +    pTsd->pPager = pPager;
         1732  +  }
         1733  +#endif
  1728   1734     return SQLITE_OK;
  1729   1735   }
  1730   1736   
  1731   1737   /*
  1732   1738   ** Set the busy handler function.
  1733   1739   */
  1734   1740   void sqlite3pager_set_busyhandler(Pager *pPager, BusyHandler *pBusyHandler){
................................................................................
  2084   2090     ** if( pPager->tempFile ){
  2085   2091     **   sqlite3OsDelete(pPager->zFilename);
  2086   2092     ** }
  2087   2093     */
  2088   2094   
  2089   2095   #ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
  2090   2096     /* Remove the pager from the linked list of pagers starting at 
  2091         -  ** ThreadData.pPager.
         2097  +  ** ThreadData.pPager if memory-management is enabled.
  2092   2098     */
  2093         -  if( pPager==pTsd->pPager ){
  2094         -    pTsd->pPager = pPager->pNext;
  2095         -  }else{
  2096         -    Pager *pTmp;
  2097         -    for(pTmp = pTsd->pPager; pTmp->pNext!=pPager; pTmp=pTmp->pNext);
  2098         -    pTmp->pNext = pPager->pNext;
         2099  +  if( pTsd->useMemoryManagement ){
         2100  +    if( pPager==pTsd->pPager ){
         2101  +      pTsd->pPager = pPager->pNext;
         2102  +    }else{
         2103  +      Pager *pTmp;
         2104  +      for(pTmp = pTsd->pPager; pTmp->pNext!=pPager; pTmp=pTmp->pNext);
         2105  +      pTmp->pNext = pPager->pNext;
         2106  +    }
  2099   2107     }
  2100   2108   #endif
  2101   2109   
  2102   2110     sqliteFree(pPager);
  2103   2111     return SQLITE_OK;
  2104   2112   }
  2105   2113   

Changes to src/sqlite.h.in.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This header file defines the interface that the SQLite library
    13     13   ** presents to client programs.
    14     14   **
    15         -** @(#) $Id: sqlite.h.in,v 1.152 2005/12/30 16:28:02 danielk1977 Exp $
           15  +** @(#) $Id: sqlite.h.in,v 1.153 2006/01/09 09:59:49 danielk1977 Exp $
    16     16   */
    17     17   #ifndef _SQLITE3_H_
    18     18   #define _SQLITE3_H_
    19     19   #include <stdarg.h>     /* Needed for the definition of va_list */
    20     20   
    21     21   /*
    22     22   ** Make sure we can call this stuff from C++.
................................................................................
  1272   1272   ** Return the sqlite3* database handle to which the prepared statement given
  1273   1273   ** in the argument belongs.  This is the same database handle that was
  1274   1274   ** the first argument to the sqlite3_prepare() that was used to create
  1275   1275   ** the statement in the first place.
  1276   1276   */
  1277   1277   sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
  1278   1278   
  1279         -/*
  1280         -** Place a "soft" limit on the amount of heap memory that may be allocated by
  1281         -** SQLite within the current thread. If an internal allocation is requested 
  1282         -** that would exceed the specified limit, sqlite3_release_memory() is invoked
  1283         -** one or more times to free up some space before the allocation is made.
  1284         -**
  1285         -** The limit is called "soft", because if sqlite3_release_memory() cannot free
  1286         -** sufficient memory to prevent the limit from being exceeded, the memory is
  1287         -** allocated anyway and the current operation proceeds.
  1288         -**
  1289         -** This function is only available if the library was compiled without the 
  1290         -** SQLITE_OMIT_MEMORY_MANAGEMENT option set.
  1291         -*/
  1292         -void sqlite3_soft_heap_limit(sqlite_int64);
  1293         -
  1294   1279   /*
  1295   1280   ** Register a callback function with the database connection identified by the 
  1296   1281   ** first argument to be invoked whenever a row is updated, inserted or deleted.
  1297   1282   ** Any callback set by a previous call to this function for the same 
  1298   1283   ** database connection is overridden.
  1299   1284   **
  1300   1285   ** The second argument is a pointer to the function to invoke when a 
................................................................................
  1332   1317   ** rolled back if an explicit "ROLLBACK" statement is executed, or
  1333   1318   ** an error or constraint causes an implicit rollback to occur. The 
  1334   1319   ** callback is not invoked if a transaction is automatically rolled
  1335   1320   ** back because the database connection is closed.
  1336   1321   */
  1337   1322   void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
  1338   1323   
  1339         -int sqlite3_release_memory(int);
  1340         -
  1341   1324   /*
  1342   1325   ** This function is only available if the library is compiled without
  1343   1326   ** the SQLITE_OMIT_SHARED_CACHE macro defined. It is used to enable or
  1344   1327   ** disable (if the argument is true or false, respectively) the 
  1345   1328   ** "shared pager" feature.
  1346   1329   */
  1347   1330   int sqlite3_enable_shared_cache(int);
  1348   1331   
         1332  +/*
         1333  +** This function is only available if the library is compiled without
         1334  +** the SQLITE_OMIT_MEMORY_MANAGEMENT macro defined. It is used to enable or
         1335  +** disable (if the argument is true or false, respectively) the 
         1336  +** "memory management" features (accessed via the sqlite3_soft_heap_limit()
         1337  +** and sqlite3_release_memory() APIs).
         1338  +*/
         1339  +int sqlite3_enable_memory_management(int);
         1340  +
         1341  +/*
         1342  +** Attempt to free N bytes of heap memory by deallocating non-essential
         1343  +** memory allocations held by the database library (example: memory 
         1344  +** used to cache database pages to improve performance).
         1345  +**
         1346  +** This function is a no-op unless memory-management has been enabled.
         1347  +*/
         1348  +int sqlite3_release_memory(int);
         1349  +
         1350  +/*
         1351  +** Place a "soft" limit on the amount of heap memory that may be allocated by
         1352  +** SQLite within the current thread. If an internal allocation is requested 
         1353  +** that would exceed the specified limit, sqlite3_release_memory() is invoked
         1354  +** one or more times to free up some space before the allocation is made.
         1355  +**
         1356  +** The limit is called "soft", because if sqlite3_release_memory() cannot free
         1357  +** sufficient memory to prevent the limit from being exceeded, the memory is
         1358  +** allocated anyway and the current operation proceeds.
         1359  +**
         1360  +** This function is only available if the library was compiled without the 
         1361  +** SQLITE_OMIT_MEMORY_MANAGEMENT option set. It is a no-op unless 
         1362  +** memory-management has been enabled.
         1363  +*/
         1364  +void sqlite3_soft_heap_limit(sqlite_int64);
         1365  +
  1349   1366   /*
  1350   1367   ** Undo the hack that converts floating point types to integer for
  1351   1368   ** builds on processors without floating point support.
  1352   1369   */
  1353   1370   #ifdef SQLITE_OMIT_FLOATING_POINT
  1354   1371   # undef double
  1355   1372   #endif
  1356   1373   
  1357   1374   #ifdef __cplusplus
  1358   1375   }  /* End of the 'extern "C"' block */
  1359   1376   #endif
  1360   1377   #endif

Changes to src/sqliteInt.h.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Internal interface definitions for SQLite.
    13     13   **
    14         -** @(#) $Id: sqliteInt.h,v 1.454 2006/01/09 06:29:49 danielk1977 Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.455 2006/01/09 09:59:49 danielk1977 Exp $
    15     15   */
    16     16   #ifndef _SQLITEINT_H_
    17     17   #define _SQLITEINT_H_
    18     18   
    19     19   /*
    20     20   ** Extra interface definitions for those who need them
    21     21   */
................................................................................
   290    290   */
   291    291   struct ThreadData {
   292    292     u8 isInit;               /* True if structure has been initialised */
   293    293     u8 mallocFailed;         /* True after a malloc() has failed */
   294    294     u8 disableReleaseMemory; /* True to make sqlite3_release_memory() a no-op */
   295    295   
   296    296   #ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
          297  +  u8 useMemoryManagement;  /* True if memory-management is enabled */
   297    298     i64 nSoftHeapLimit;      /* Suggested max mem allocation.  No limit if <0 */
   298    299     i64 nAlloc;              /* Number of bytes currently allocated */
   299    300     Pager *pPager;           /* Linked list of all pagers in this thread */
   300    301   #endif
   301    302   
   302    303   #ifndef SQLITE_OMIT_SHARED_CACHE
   303    304     u8 useSharedData;        /* True if shared pagers and schemas are enabled */

Changes to src/test1.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Code for testing the printf() interface to SQLite.  This code
    13     13   ** is not included in the SQLite library.  It is used for automated
    14     14   ** testing of the SQLite library.
    15     15   **
    16         -** $Id: test1.c,v 1.184 2006/01/09 06:29:49 danielk1977 Exp $
           16  +** $Id: test1.c,v 1.185 2006/01/09 09:59:49 danielk1977 Exp $
    17     17   */
    18     18   #include "sqliteInt.h"
    19     19   #include "tcl.h"
    20     20   #include "os.h"
    21     21   #include <stdlib.h>
    22     22   #include <string.h>
    23     23   
................................................................................
   899    899     ClientData clientData,
   900    900     Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   901    901     int objc,              /* Number of arguments */
   902    902     Tcl_Obj *CONST objv[]  /* Command arguments */
   903    903   ){
   904    904     extern int sqlite3OutstandingMallocs(Tcl_Interp *interp);
   905    905   
   906         -  if( objc!=1 && objc!=2 ){
   907         -    Tcl_WrongNumArgs(interp, 1, objv, "?-bytes?");
   908         -    return TCL_ERROR;
   909         -  }
   910         -
          906  +#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
   911    907     if( objc==2 ){
          908  +    ThreadData *pTd = sqlite3ThreadData();
   912    909       const char *zArg = Tcl_GetString(objv[1]);
   913    910       if( 0==strcmp(zArg, "-bytes") ){
   914         -      Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3ThreadData()->nAlloc));
   915         -#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
          911  +      Tcl_SetObjResult(interp, Tcl_NewIntObj(pTd->nAlloc));
   916    912       }else if( 0==strcmp(zArg, "-maxbytes") ){
   917         -      Tcl_SetObjResult(interp, Tcl_NewWideIntObj(sqlite3ThreadData()->nMaxAlloc));
          913  +      Tcl_SetObjResult(interp, Tcl_NewWideIntObj(pTd->nMaxAlloc));
   918    914       }else if( 0==strcmp(zArg, "-clearmaxbytes") ){
   919         -      sqlite3ThreadData()->nMaxAlloc = sqlite3ThreadData()->nAlloc;
   920         -#endif
          915  +      pTd->nMaxAlloc = pTd->nAlloc;
   921    916       }else{
   922    917         Tcl_AppendResult(interp, "bad option \"", zArg, 
   923    918           "\": must be -bytes, -maxbytes or -clearmaxbytes", 0
   924    919         );
   925    920         return TCL_ERROR;
   926    921       }
   927    922   
   928    923       return TCL_OK;
   929    924     }
          925  +#endif
          926  +
          927  +  if( objc!=1 ){
          928  +    Tcl_WrongNumArgs(interp, 1, objv, "?-bytes?");
          929  +    return TCL_ERROR;
          930  +  }
   930    931   
   931    932     return sqlite3OutstandingMallocs(interp);
   932    933   }
   933    934   #endif
   934    935   
   935    936   /*
   936         -** Usage: sqlite3_enable_shared_cache BOOLEAN
          937  +** Usage: sqlite3_enable_shared_cache      BOOLEAN
          938  +** Usage: sqlite3_enable_memory_management BOOLEAN
   937    939   **
   938    940   */
   939         -#ifndef SQLITE_OMIT_SHARED_CACHE
   940         -static int test_enable_shared_cache(
   941         -  ClientData clientData,
          941  +#if !defined(SQLITE_OMIT_SHARED_CACHE)||!defined(SQLITE_OMIT_MEMORY_MANAGEMENT)
          942  +static int test_enable(
          943  +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
   942    944     Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   943    945     int objc,              /* Number of arguments */
   944    946     Tcl_Obj *CONST objv[]  /* Command arguments */
   945    947   ){
   946    948     int rc;
   947    949     int enable;
          950  +  int ret = 0;
          951  +  int (*xFunc)(int) = (int(*)(int))clientData;
   948    952     ThreadData *pTsd = sqlite3ThreadData();
   949         -  Tcl_SetObjResult(interp, Tcl_NewBooleanObj(pTsd->useSharedData));
   950    953   
          954  +#ifndef SQLITE_OMIT_SHARED_CACHE
          955  +  if( xFunc==sqlite3_enable_shared_cache ){
          956  +    ret = pTsd->useSharedData;
          957  +  }else
          958  +#endif
          959  +#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
          960  +  {
          961  +    assert( xFunc==sqlite3_enable_memory_management );
          962  +    ret = pTsd->useMemoryManagement;
          963  +  }
          964  +#endif
   951    965     if( objc!=2 ){
   952    966       Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN");
   953    967       return TCL_ERROR;
   954    968     }
   955    969     if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){
   956    970       return TCL_ERROR;
   957    971     }
   958         -  rc = sqlite3_enable_shared_cache(enable);
          972  +  rc = xFunc(enable);
   959    973     if( rc!=SQLITE_OK ){
   960    974       Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
   961    975       return TCL_ERROR;
   962    976     }
          977  +  Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret));
   963    978     return TCL_OK;
   964    979   }
   965    980   #endif
   966    981   
   967    982   /*
   968    983   ** Usage:  sqlite_abort
   969    984   **
................................................................................
  3410   3425   #endif
  3411   3426   #ifdef SQLITE_MEMDEBUG
  3412   3427        { "sqlite_malloc_outstanding", sqlite_malloc_outstanding, 0},
  3413   3428   #endif
  3414   3429        { "sqlite3_test_errstr",     test_errstr, 0             },
  3415   3430        { "tcl_variable_type",       tcl_variable_type, 0       },
  3416   3431   #ifndef SQLITE_OMIT_SHARED_CACHE
  3417         -     { "sqlite3_enable_shared_cache", test_enable_shared_cache, 0},
         3432  +     { "sqlite3_enable_shared_cache", test_enable, sqlite3_enable_shared_cache},
         3433  +#endif
         3434  +#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
         3435  +     { "sqlite3_enable_memory_management", test_enable, 
         3436  +       sqlite3_enable_memory_management},
  3418   3437   #endif
  3419   3438     };
  3420   3439     static int bitmask_size = sizeof(Bitmask)*8;
  3421   3440     int i;
  3422   3441     extern int sqlite3_os_trace;
  3423   3442     extern int sqlite3_where_trace;
  3424   3443     extern int sqlite3_sync_count, sqlite3_fullsync_count;

Changes to src/util.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Utility functions used throughout sqlite.
    13     13   **
    14     14   ** This file contains functions for allocating memory, comparing
    15     15   ** strings, and stuff like that.
    16     16   **
    17         -** $Id: util.c,v 1.163 2006/01/09 06:29:49 danielk1977 Exp $
           17  +** $Id: util.c,v 1.164 2006/01/09 09:59:49 danielk1977 Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "os.h"
    21     21   #include <stdarg.h>
    22     22   #include <ctype.h>
    23     23   
    24     24   /*
................................................................................
   525    525     ThreadData *pTsd = sqlite3ThreadData();
   526    526     pTsd->nAlloc += (i64)n;
   527    527     if( n>0 && pTsd->nSoftHeapLimit>0 ){
   528    528       while( pTsd->nAlloc>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) );
   529    529     }
   530    530   }
   531    531   #else
   532         -#define handleSoftLimit()
          532  +#define handleSoftLimit(x)
   533    533   #endif
   534    534   
   535    535   /*
   536    536   ** Allocate and return N bytes of uninitialised memory by calling
   537    537   ** sqlite3OsMalloc(). If the Malloc() call fails, attempt to free memory 
   538    538   ** by calling sqlite3_release_memory().
   539    539   */
................................................................................
  1303   1303   
  1304   1304   /*
  1305   1305   ** Return a pointer to the ThreadData associated with the calling thread.
  1306   1306   */
  1307   1307   ThreadData *sqlite3ThreadData(){
  1308   1308     ThreadData *pTsd = sqlite3OsThreadSpecificData(sizeof(ThreadData));
  1309   1309     if( pTsd && !pTsd->isInit ){
         1310  +#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
  1310   1311       pTsd->nSoftHeapLimit = -1;
         1312  +#endif
  1311   1313   #ifndef NDEBUG
  1312   1314       pTsd->mallocAllowed = 1;
  1313   1315   #endif
  1314   1316       pTsd->isInit = 1;
  1315   1317     }
  1316   1318     return pTsd;
  1317   1319   }
................................................................................
  1320   1322   ** Clear the "mallocFailed" flag. This should be invoked before exiting any
  1321   1323   ** entry points that may have called sqliteMalloc().
  1322   1324   */
  1323   1325   void sqlite3MallocClearFailed(){
  1324   1326     sqlite3ThreadData()->mallocFailed = 0;
  1325   1327   }
  1326   1328   
         1329  +#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
         1330  +/*
         1331  +** Enable the shared pager and schema features.
         1332  +*/
         1333  +int sqlite3_enable_memory_management(int enable){
         1334  +  ThreadData *pTsd = sqlite3ThreadData();
         1335  +
         1336  +  /* It is only legal to call sqlite3_enable_memory_management() when there
         1337  +  ** are no currently open connections that were opened by the calling 
         1338  +  ** thread. This condition is only easy to detect if the feature were
         1339  +  ** previously enabled (and is being disabled). 
         1340  +  */
         1341  +  if( pTsd->pPager && !enable ){
         1342  +    return SQLITE_MISUSE;
         1343  +  }
         1344  +  pTsd->useMemoryManagement = enable;
         1345  +  return SQLITE_OK;
         1346  +}
         1347  +#endif
  1327   1348   
  1328   1349   #ifndef NDEBUG
  1329   1350   /*
  1330   1351   ** This function sets a flag in the thread-specific-data structure that will
  1331   1352   ** cause an assert to fail if sqliteMalloc() or sqliteRealloc() is called.
  1332   1353   */
  1333   1354   void sqlite3MallocDisallow(){

Changes to test/malloc5.test.

     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   #
    12     12   # This file contains test cases focused on the two memory-management APIs, 
    13     13   # sqlite3_soft_heap_limit() and sqlite3_release_memory().
    14     14   #
    15         -# $Id: malloc5.test,v 1.4 2006/01/05 15:50:07 drh Exp $
           15  +# $Id: malloc5.test,v 1.5 2006/01/09 09:59:49 danielk1977 Exp $
    16     16   
    17     17   #---------------------------------------------------------------------------
    18     18   # NOTES ON EXPECTED BEHAVIOUR
    19     19   #
    20     20   #---------------------------------------------------------------------------
           21  +
    21     22   
    22     23   set testdir [file dirname $argv0]
    23     24   source $testdir/tester.tcl
           25  +db close
    24     26   
    25     27   # Only run these tests if memory debugging is turned on.
    26     28   if {[info command sqlite_malloc_stat]==""} {
    27     29      puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..."
    28     30      finish_test
    29     31      return
    30     32   }
           33  +
           34  +# Skip these tests if OMIT_MEMORY_MANAGEMENT was defined at compile time.
           35  +ifcapable !memorymanage {
           36  +   finish_test
           37  +   return
           38  +}
           39  +
           40  +sqlite3_enable_memory_management 1
           41  +sqlite3 db test.db
    31     42   
    32     43   do_test malloc5-1.1 {
    33     44     # Simplest possible test. Call sqlite3_release_memory when there is exactly
    34     45     # one unused page in a single pager cache. This test case set's the 
    35     46     # value of the ::pgalloc variable, which is used in subsequent tests.
    36     47     #
    37     48     # Note: Even though executing this statement on an empty database 
................................................................................
   201    212     execsql {
   202    213       SELECT count(*), sum(a), sum(b) FROM abc;
   203    214     }
   204    215   } [list 20000 [expr int(20000.0 * 4999.5)] [expr int(20000.0 * 4999.5)]]
   205    216   
   206    217   # Restore the soft heap limit.
   207    218   sqlite3_soft_heap_limit $::soft_limit
   208         -
   209    219   finish_test
          220  +
          221  +catch {db close}
          222  +sqlite3_enable_memory_management 0
          223  +