1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-07 02:42:48 +03:00

Add simple tests and fixes for sqlite3_soft_heap_limit() (CVS 2837)

FossilOrigin-Name: c2c5285442f4558dfca61b52f31b5a9cbefaed10
This commit is contained in:
danielk1977
2005-12-20 09:19:37 +00:00
parent 6ed65f5a3f
commit 5591df558a
9 changed files with 192 additions and 52 deletions

View File

@@ -1,5 +1,5 @@
C Website\schanges\sin\spreparation\sfor\sthe\srelease\sof\s2.8.17.\s(CVS\s2834) C Add\ssimple\stests\sand\sfixes\sfor\ssqlite3_soft_heap_limit()\s(CVS\s2837)
D 2005-12-19T17:53:36 D 2005-12-20T09:19:37
F Makefile.in e3c6b3a38d734d41574c04f2fc90d18de2b87102 F Makefile.in e3c6b3a38d734d41574c04f2fc90d18de2b87102
F Makefile.linux-gcc aee18d8a05546dcf1888bd4547e442008a49a092 F Makefile.linux-gcc aee18d8a05546dcf1888bd4547e442008a49a092
F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@@ -59,7 +59,7 @@ F src/os_unix.c 6394d2fa3a8bfbceb227579b44b4b343b5b54a8f
F src/os_unix.h 5768d56d28240d3fe4537fac08cc85e4fb52279e F src/os_unix.h 5768d56d28240d3fe4537fac08cc85e4fb52279e
F src/os_win.c 9feb97f49b93d451f8ef7c5dd388e05a44647dc6 F src/os_win.c 9feb97f49b93d451f8ef7c5dd388e05a44647dc6
F src/os_win.h 41a946bea10f61c158ce8645e7646b29d44f122b F src/os_win.h 41a946bea10f61c158ce8645e7646b29d44f122b
F src/pager.c f1969539777ee52900e740b0404a323a3e419c8b F src/pager.c 5227ea29acbea4b6a9c6f1dfd3e8493de5fc2a93
F src/pager.h e0acb095b3ad0bca48f2ab00c87346665643f64f F src/pager.h e0acb095b3ad0bca48f2ab00c87346665643f64f
F src/parse.y 142a4b347c82217332e2d3dfa317ff2b7ac32f9c F src/parse.y 142a4b347c82217332e2d3dfa317ff2b7ac32f9c
F src/pragma.c 8883b4d34796efa315bdd0ec1b03f580ef1575b9 F src/pragma.c 8883b4d34796efa315bdd0ec1b03f580ef1575b9
@@ -68,11 +68,11 @@ F src/printf.c f47a2f4b5387cd2ebb12e9117a1a5d6bd9a2b812
F src/random.c ff5e9a8cad790e2a51cd4d2e7737dc8540e09d1d F src/random.c ff5e9a8cad790e2a51cd4d2e7737dc8540e09d1d
F src/select.c 2292b065bc6be61e01aad39a2e1b93e332fb7e57 F src/select.c 2292b065bc6be61e01aad39a2e1b93e332fb7e57
F src/shell.c 4872acee1d2a826c73c914961e469e563204b7f9 F src/shell.c 4872acee1d2a826c73c914961e469e563204b7f9
F src/sqlite.h.in 5b16b66db128f5aac5e82721b9012bfa88958bd7 F src/sqlite.h.in 015e02efa9e8bafa31b6c270116369ddff4e9803
F src/sqliteInt.h 4e822139dc9322795a9644a3d3b281d53f6d99ed F src/sqliteInt.h b8b2b2da145c15c8577316f0b8899ba65c098ccc
F src/table.c 486dcfce532685b53b5a2b5da8bba0ded6fb2316 F src/table.c 486dcfce532685b53b5a2b5da8bba0ded6fb2316
F src/tclsqlite.c ce481c0a21a20641cdfe87c9cbbb328cfb3a58b9 F src/tclsqlite.c ce481c0a21a20641cdfe87c9cbbb328cfb3a58b9
F src/test1.c d6924b182773b2ad3b22e435e4d3bfd5a846da9e F src/test1.c 4691cc7235683324791a0be560308ca338de36de
F src/test2.c 36390cdfc70c08e5ee0b466d0654a117f398bbff F src/test2.c 36390cdfc70c08e5ee0b466d0654a117f398bbff
F src/test3.c 7c97833e33496c2b69f4fe6b9882ac60a481da97 F src/test3.c 7c97833e33496c2b69f4fe6b9882ac60a481da97
F src/test4.c a8fd681e139e1c61f22a77d07fc3a99cb28fff3f F src/test4.c a8fd681e139e1c61f22a77d07fc3a99cb28fff3f
@@ -82,7 +82,7 @@ F src/tokenize.c 7a3a3d3cc734f684a77c4dfd09eb46fcee25394c
F src/trigger.c 2925ba96d964d9b717e74006bf7e64b8a6b70d97 F src/trigger.c 2925ba96d964d9b717e74006bf7e64b8a6b70d97
F src/update.c ec8e540617b116725b5a55c8d6b4db8bc67fdd7d F src/update.c ec8e540617b116725b5a55c8d6b4db8bc67fdd7d
F src/utf.c b7bffac4260177ae7f83c01d025fe0f5ed70ce71 F src/utf.c b7bffac4260177ae7f83c01d025fe0f5ed70ce71
F src/util.c 0ed80dea0cd12dc09d4c576f6a132fd5cf963b51 F src/util.c 7e9ebebb0c8b773490dcacd67a77fe6b6bb4e5e1
F src/vacuum.c fbfdd3967fd34e2f260fafed88dcbf3c10856b94 F src/vacuum.c fbfdd3967fd34e2f260fafed88dcbf3c10856b94
F src/vdbe.c 09aaed71f076bfd4286607ee4845100b910a492f F src/vdbe.c 09aaed71f076bfd4286607ee4845100b910a492f
F src/vdbe.h 8729a4ee16ff9aeab2af9667df3cf300ff978e13 F src/vdbe.h 8729a4ee16ff9aeab2af9667df3cf300ff978e13
@@ -182,7 +182,7 @@ F test/malloc.test a5ed721cf7d1b12602ede4f98c11b65ab1582cc0
F test/malloc2.test e6e321db96d6c94cb18bf82ad7215070c41e624e F test/malloc2.test e6e321db96d6c94cb18bf82ad7215070c41e624e
F test/malloc3.test 9797d39eca7087a1022bcc0eb0b22215475c9698 F test/malloc3.test 9797d39eca7087a1022bcc0eb0b22215475c9698
F test/malloc4.test 2e29d155eb4b4808019ef47eeedfcbe9e09e0f05 F test/malloc4.test 2e29d155eb4b4808019ef47eeedfcbe9e09e0f05
F test/malloc5.test 2418a9d667a50175744ba4c147ee1c75de6cfe5c F test/malloc5.test 8dd58affc441a726d16448a7188ab175bc9e7280
F test/manydb.test d81debbf5871242e3b5df1d3bb5e14c50431b6f8 F test/manydb.test d81debbf5871242e3b5df1d3bb5e14c50431b6f8
F test/memdb.test 1860e060be810bf0775bc57408a5b7c4954bcaea F test/memdb.test 1860e060be810bf0775bc57408a5b7c4954bcaea
F test/memleak.test df2b2b96e77f8ba159a332299535b1e5f18e49ac F test/memleak.test df2b2b96e77f8ba159a332299535b1e5f18e49ac
@@ -222,7 +222,7 @@ F test/subselect.test 2d13fb7f450db3595adcdd24079a0dd1d2d6abc2
F test/sync.test 6e8b885cec5ccba2090e92c90f747a7142f53c88 F test/sync.test 6e8b885cec5ccba2090e92c90f747a7142f53c88
F test/table.test ec0e6c2186bb8f6824f470caa118524dfd8fe057 F test/table.test ec0e6c2186bb8f6824f470caa118524dfd8fe057
F test/tableapi.test 6a66d58b37d46dc0f2b3c7d4bd2617d209399bd1 F test/tableapi.test 6a66d58b37d46dc0f2b3c7d4bd2617d209399bd1
F test/tclsqlite.test 26ee144ca70738ee0f3d8b5c1113b2f13652658b F test/tclsqlite.test 19578d32a7692311918caf0ae3521d19525bcb62
F test/temptable.test 7927261befdbc7b0a7ffebb85ecc70a74fa7b15b F test/temptable.test 7927261befdbc7b0a7ffebb85ecc70a74fa7b15b
F test/tester.tcl a06c798a653daefb5bce2c85fc3a7d06450a1875 F test/tester.tcl a06c798a653daefb5bce2c85fc3a7d06450a1875
F test/thread1.test 776c9e459b75ba905193b351926ac4019b049f35 F test/thread1.test 776c9e459b75ba905193b351926ac4019b049f35
@@ -329,7 +329,7 @@ F www/tclsqlite.tcl ddcf912ea48695603c8ed7efb29f0812ef8d1b49
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b
F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513 F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513
P 2f7ec117ded31f2c13f2e3ab9dd4e9a483434601 P a927696ba30da39cb94ba666a10b7311d46826c9
R 547dcd4429db02f81b0ea35f4c0b8368 R 47b8829eca445dbf0a3ed9bcb5e9f642
U drh U danielk1977
Z 84cc1eb94b056fe4069873bc16aa8466 Z 8e415cd6471fe4543f7c65503e6ef9ab

View File

@@ -1 +1 @@
a927696ba30da39cb94ba666a10b7311d46826c9 c2c5285442f4558dfca61b52f31b5a9cbefaed10

View File

@@ -18,7 +18,7 @@
** file simultaneously, or one process from reading the database while ** file simultaneously, or one process from reading the database while
** another is writing. ** another is writing.
** **
** @(#) $Id: pager.c,v 1.227 2005/12/19 16:15:31 drh Exp $ ** @(#) $Id: pager.c,v 1.228 2005/12/20 09:19:37 danielk1977 Exp $
*/ */
#ifndef SQLITE_OMIT_DISKIO #ifndef SQLITE_OMIT_DISKIO
#include "sqliteInt.h" #include "sqliteInt.h"
@@ -2309,6 +2309,9 @@ static int hasHotJournal(Pager *pPager){
} }
} }
/*
** Try to find a page in the cache that can be recycled.
*/
static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){ static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){
PgHdr *pPg; PgHdr *pPg;
*ppPg = 0; *ppPg = 0;
@@ -2323,7 +2326,7 @@ static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){
** very slow operation, so we work hard to avoid it. But sometimes ** very slow operation, so we work hard to avoid it. But sometimes
** it can't be helped. ** it can't be helped.
*/ */
if( pPg==0 && pPager->pFirst && syncOk ){ if( pPg==0 && pPager->pFirst && syncOk && !MEMDB){
int rc = syncJournal(pPager); int rc = syncJournal(pPager);
if( rc!=0 ){ if( rc!=0 ){
sqlite3pager_rollback(pPager); sqlite3pager_rollback(pPager);

View File

@@ -12,7 +12,7 @@
** This header file defines the interface that the SQLite library ** This header file defines the interface that the SQLite library
** presents to client programs. ** presents to client programs.
** **
** @(#) $Id: sqlite.h.in,v 1.150 2005/12/19 14:18:11 danielk1977 Exp $ ** @(#) $Id: sqlite.h.in,v 1.151 2005/12/20 09:19:37 danielk1977 Exp $
*/ */
#ifndef _SQLITE3_H_ #ifndef _SQLITE3_H_
#define _SQLITE3_H_ #define _SQLITE3_H_
@@ -1289,7 +1289,7 @@ sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
** This function is only available if the library was compiled without the ** This function is only available if the library was compiled without the
** SQLITE_OMIT_MEMORY_MANAGEMENT option set. ** SQLITE_OMIT_MEMORY_MANAGEMENT option set.
*/ */
void sqlite3_soft_heap_limit(int); void sqlite3_soft_heap_limit(sqlite_int64);
/* /*
** Register a callback function with the database connection identified by the ** Register a callback function with the database connection identified by the

View File

@@ -11,7 +11,7 @@
************************************************************************* *************************************************************************
** Internal interface definitions for SQLite. ** Internal interface definitions for SQLite.
** **
** @(#) $Id: sqliteInt.h,v 1.441 2005/12/19 14:18:11 danielk1977 Exp $ ** @(#) $Id: sqliteInt.h,v 1.442 2005/12/20 09:19:37 danielk1977 Exp $
*/ */
#ifndef _SQLITEINT_H_ #ifndef _SQLITEINT_H_
#define _SQLITEINT_H_ #define _SQLITEINT_H_
@@ -268,20 +268,19 @@ extern int sqlite3_iMallocReset; /* Set iMallocFail to this when it reaches 0 */
/* /*
** An instance of this structure is allocated for each thread that uses SQLite. ** An instance of this structure is allocated for each thread that uses SQLite.
*/ */
typedef struct SqliteTsd SqliteTsd;
struct SqliteTsd { struct SqliteTsd {
int isInit; /* True if structure has been initialised */ u8 isInit; /* True if structure has been initialised */
int mallocFailed; /* True after a malloc() has failed */ u8 mallocFailed; /* True after a malloc() has failed */
#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT #ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
unsigned int nSoftHeapLimit; /* (uint)-1 for unlimited */ i64 nSoftHeapLimit; /* -ve for unlimited */
unsigned int nAlloc; /* Number of bytes currently allocated */ i64 nAlloc; /* Number of bytes currently allocated */
Pager *pPager; /* Linked list of all pagers in this thread */ Pager *pPager; /* Linked list of all pagers in this thread */
#endif #endif
#ifndef NDEBUG
int mallocAllowed; /* assert() in sqlite3Malloc() if not set */
#endif
#ifdef SQLITE_MEMDEBUG #ifdef SQLITE_MEMDEBUG
i64 nMaxAlloc; /* High water mark of SqliteTsd.nAlloc */
int mallocAllowed; /* assert() in sqlite3Malloc() if not set */
int isFail; /* True if all malloc() calls should fail */ int isFail; /* True if all malloc() calls should fail */
const char *zFile; /* Filename to associate debugging info with */ const char *zFile; /* Filename to associate debugging info with */
int iLine; /* Line number to associate debugging info with */ int iLine; /* Line number to associate debugging info with */
@@ -333,6 +332,7 @@ typedef struct NameContext NameContext;
typedef struct Parse Parse; typedef struct Parse Parse;
typedef struct Select Select; typedef struct Select Select;
typedef struct SrcList SrcList; typedef struct SrcList SrcList;
typedef struct SqliteTsd SqliteTsd;
typedef struct Table Table; typedef struct Table Table;
typedef struct Token Token; typedef struct Token Token;
typedef struct TriggerStack TriggerStack; typedef struct TriggerStack TriggerStack;

View File

@@ -13,7 +13,7 @@
** is not included in the SQLite library. It is used for automated ** is not included in the SQLite library. It is used for automated
** testing of the SQLite library. ** testing of the SQLite library.
** **
** $Id: test1.c,v 1.176 2005/12/14 20:11:30 drh Exp $ ** $Id: test1.c,v 1.177 2005/12/20 09:19:37 danielk1977 Exp $
*/ */
#include "sqliteInt.h" #include "sqliteInt.h"
#include "tcl.h" #include "tcl.h"
@@ -827,17 +827,65 @@ static int sqlite_malloc_stat(
char **argv /* Text of each argument */ char **argv /* Text of each argument */
){ ){
char zBuf[200]; char zBuf[200];
sprintf(zBuf, "%d %d %d", sqlite3_nMalloc, sqlite3_nFree, sqlite3_iMallocFail); sprintf(zBuf, "%d %d %d", sqlite3_nMalloc,sqlite3_nFree,sqlite3_iMallocFail);
Tcl_AppendResult(interp, zBuf, 0); Tcl_AppendResult(interp, zBuf, 0);
return TCL_OK; return TCL_OK;
} }
/*
** This function implements a Tcl command that may be invoked using any of
** the four forms enumerated below.
**
** sqlite_malloc_outstanding
** Return a summary of all unfreed blocks of memory allocated by the
** current thread. See comments above function sqlite3OutstandingMallocs()
** in util.c for a description of the returned value.
**
** sqlite_malloc_outstanding -bytes
** Return the total amount of unfreed memory (in bytes) allocated by
** this thread.
**
** sqlite_malloc_outstanding -maxbytes
** Return the maximum amount of dynamic memory in use at one time
** by this thread.
**
** sqlite_malloc_outstanding -clearmaxbytes
** Set the value returned by [sqlite_malloc_outstanding -maxbytes]
** to the current value of [sqlite_malloc_outstanding -bytes].
*/
static int sqlite_malloc_outstanding( static int sqlite_malloc_outstanding(
void *NotUsed, ClientData clientData,
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
int argc, /* Number of arguments */ int objc, /* Number of arguments */
char **argv /* Text of each argument */ Tcl_Obj *CONST objv[] /* Command arguments */
){ ){
extern int sqlite3OutstandingMallocs(Tcl_Interp *interp); extern int sqlite3OutstandingMallocs(Tcl_Interp *interp);
if( objc!=1 && objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "?-bytes?");
return TCL_ERROR;
}
if( objc==2 ){
const char *zArg = Tcl_GetString(objv[1]);
if( 0==strcmp(zArg, "-bytes") ){
Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3Tsd()->nAlloc));
#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
}else if( 0==strcmp(zArg, "-maxbytes") ){
Tcl_SetObjResult(interp, Tcl_NewWideIntObj(sqlite3Tsd()->nMaxAlloc));
}else if( 0==strcmp(zArg, "-clearmaxbytes") ){
sqlite3Tsd()->nMaxAlloc = sqlite3Tsd()->nAlloc;
#endif
}else{
Tcl_AppendResult(interp, "bad option \"", zArg,
"\": must be -bytes, -maxbytes or -clearmaxbytes", 0
);
return TCL_ERROR;
}
return TCL_OK;
}
return sqlite3OutstandingMallocs(interp); return sqlite3OutstandingMallocs(interp);
} }
#endif #endif
@@ -3113,7 +3161,6 @@ int Sqlitetest1_Init(Tcl_Interp *interp){
#ifdef SQLITE_MEMDEBUG #ifdef SQLITE_MEMDEBUG
{ "sqlite_malloc_fail", (Tcl_CmdProc*)sqlite_malloc_fail }, { "sqlite_malloc_fail", (Tcl_CmdProc*)sqlite_malloc_fail },
{ "sqlite_malloc_stat", (Tcl_CmdProc*)sqlite_malloc_stat }, { "sqlite_malloc_stat", (Tcl_CmdProc*)sqlite_malloc_stat },
{ "sqlite_malloc_outstanding", (Tcl_CmdProc*)sqlite_malloc_outstanding},
#endif #endif
{ "sqlite_bind", (Tcl_CmdProc*)test_bind }, { "sqlite_bind", (Tcl_CmdProc*)test_bind },
{ "breakpoint", (Tcl_CmdProc*)test_breakpoint }, { "breakpoint", (Tcl_CmdProc*)test_breakpoint },
@@ -3197,6 +3244,9 @@ int Sqlitetest1_Init(Tcl_Interp *interp){
{ "add_test_collate", test_collate, 0 }, { "add_test_collate", test_collate, 0 },
{ "add_test_collate_needed", test_collate_needed, 0 }, { "add_test_collate_needed", test_collate_needed, 0 },
{ "add_test_function", test_function, 0 }, { "add_test_function", test_function, 0 },
#endif
#ifdef SQLITE_MEMDEBUG
{ "sqlite_malloc_outstanding", sqlite_malloc_outstanding, 0},
#endif #endif
{ "sqlite3_test_errstr", test_errstr, 0 }, { "sqlite3_test_errstr", test_errstr, 0 },
{ "tcl_variable_type", tcl_variable_type, 0 }, { "tcl_variable_type", tcl_variable_type, 0 },

View File

@@ -14,7 +14,7 @@
** This file contains functions for allocating memory, comparing ** This file contains functions for allocating memory, comparing
** strings, and stuff like that. ** strings, and stuff like that.
** **
** $Id: util.c,v 1.156 2005/12/19 14:18:11 danielk1977 Exp $ ** $Id: util.c,v 1.157 2005/12/20 09:19:37 danielk1977 Exp $
*/ */
#include "sqliteInt.h" #include "sqliteInt.h"
#include "os.h" #include "os.h"
@@ -64,6 +64,8 @@
** * Audit outstanding memory allocations (i.e check for leaks). ** * Audit outstanding memory allocations (i.e check for leaks).
*/ */
#define MAX(x,y) ((x)>(y)?(x):(y))
#ifdef SQLITE_MEMDEBUG #ifdef SQLITE_MEMDEBUG
/*-------------------------------------------------------------------------- /*--------------------------------------------------------------------------
** Begin code for memory allocation system test layer. ** Begin code for memory allocation system test layer.
@@ -155,17 +157,11 @@ const char *sqlite3_malloc_id = 0;
#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT #ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
/* /*
** Set the soft heap-size limit for the current thread. ** Set the soft heap-size limit for the current thread. Passing a negative
** value indicates no limit.
*/ */
void sqlite3_soft_heap_limit(int n){ void sqlite3_soft_heap_limit(sqlite_int64 n){
unsigned int N; sqlite3Tsd()->nSoftHeapLimit = n;
if( n<0 ){
/* No limit */
N = 0xFFFFFFFF;
}else{
N = n;
}
sqlite3Tsd()->nSoftHeapLimit = N;
} }
/* /*
@@ -434,6 +430,10 @@ int sqlite3OutstandingMallocs(Tcl_Interp *interp){
** This is the test layer's wrapper around sqlite3Os.xMalloc(). ** This is the test layer's wrapper around sqlite3Os.xMalloc().
*/ */
static void * OSMALLOC(int n){ static void * OSMALLOC(int n){
#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
SqliteTsd *pTsd = sqlite3Tsd();
pTsd->nMaxAlloc = MAX(pTsd->nMaxAlloc, pTsd->nAlloc);
#endif
if( !failMalloc() ){ if( !failMalloc() ){
u32 *p; u32 *p;
p = (u32 *)sqlite3Os.xMalloc(n + TESTALLOC_OVERHEAD); p = (u32 *)sqlite3Os.xMalloc(n + TESTALLOC_OVERHEAD);
@@ -462,6 +462,10 @@ void OSFREE(void *pFree){
** This is the test layer's wrapper around sqlite3Os.xRealloc(). ** This is the test layer's wrapper around sqlite3Os.xRealloc().
*/ */
void * OSREALLOC(void *pRealloc, int n){ void * OSREALLOC(void *pRealloc, int n){
#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
SqliteTsd *pTsd = sqlite3Tsd();
pTsd->nMaxAlloc = MAX(pTsd->nMaxAlloc, pTsd->nAlloc);
#endif
if( !failMalloc() ){ if( !failMalloc() ){
u32 *p = (u32 *)getOsPointer(pRealloc); u32 *p = (u32 *)getOsPointer(pRealloc);
checkGuards(p); checkGuards(p);
@@ -515,8 +519,10 @@ int OSSIZEOF(void *p){
#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT #ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
static void handleSoftLimit(int n){ static void handleSoftLimit(int n){
SqliteTsd *pTsd = sqlite3Tsd(); SqliteTsd *pTsd = sqlite3Tsd();
pTsd->nAlloc += n; pTsd->nAlloc += (i64)n;
while( pTsd->nAlloc>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) ); if( n>0 && pTsd->nSoftHeapLimit>0 ){
while( pTsd->nAlloc>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) );
}
} }
#else #else
#define handleSoftLimit() #define handleSoftLimit()
@@ -534,6 +540,12 @@ void *sqlite3MallocRaw(int n){
handleSoftLimit(n); handleSoftLimit(n);
while( !(p = OSMALLOC(n)) && sqlite3_release_memory(n) ); while( !(p = OSMALLOC(n)) && sqlite3_release_memory(n) );
if( !p ){ if( !p ){
/* If the allocation failed, call handleSoftLimit() again, this time
** with the additive inverse of the argument passed to
** handleSoftLimit() above. This is so the SqliteTsd.nAlloc variable is
** still correct after a malloc() failure.
*/
handleSoftLimit(n * -1);
sqlite3Tsd()->mallocFailed = 1; sqlite3Tsd()->mallocFailed = 1;
OSMALLOC_FAILED(); OSMALLOC_FAILED();
} }
@@ -559,6 +571,12 @@ void *sqlite3Realloc(void *p, int n){
handleSoftLimit(n - OSSIZEOF(p)); handleSoftLimit(n - OSSIZEOF(p));
while( !(np = OSREALLOC(p, n)) && sqlite3_release_memory(n) ); while( !(np = OSREALLOC(p, n)) && sqlite3_release_memory(n) );
if( !np ){ if( !np ){
/* If the allocation failed, call handleSoftLimit() again, this time
** with the additive inverse of the argument passed to
** handleSoftLimit() above. This is so the SqliteTsd.nAlloc variable is
** still correct after a malloc() failure.
*/
handleSoftLimit(OSSIZEOF(p) - n);
pTsd->mallocFailed = 1; pTsd->mallocFailed = 1;
OSMALLOC_FAILED(); OSMALLOC_FAILED();
} }
@@ -571,6 +589,7 @@ void *sqlite3Realloc(void *p, int n){
** value returned by a previous call to sqlite3Malloc() or sqlite3Realloc(). ** value returned by a previous call to sqlite3Malloc() or sqlite3Realloc().
*/ */
void sqlite3FreeX(void *p){ void sqlite3FreeX(void *p){
handleSoftLimit(0 - OSSIZEOF(p));
if( p ){ if( p ){
OSFREE(p); OSFREE(p);
} }
@@ -1275,7 +1294,7 @@ void *sqlite3TextToPtr(const char *z){
SqliteTsd *sqlite3Tsd(){ SqliteTsd *sqlite3Tsd(){
SqliteTsd *pTsd = sqlite3Os.xThreadSpecificData(sizeof(SqliteTsd)); SqliteTsd *pTsd = sqlite3Os.xThreadSpecificData(sizeof(SqliteTsd));
if( pTsd && !pTsd->isInit ){ if( pTsd && !pTsd->isInit ){
pTsd->nSoftHeapLimit = 0xFFFFFFFF; pTsd->nSoftHeapLimit = -1;
#ifndef NDEBUG #ifndef NDEBUG
pTsd->mallocAllowed = 1; pTsd->mallocAllowed = 1;
#endif #endif

View File

@@ -9,7 +9,7 @@
# #
#*********************************************************************** #***********************************************************************
# #
# $Id: malloc5.test,v 1.1 2005/12/19 14:18:12 danielk1977 Exp $ # $Id: malloc5.test,v 1.2 2005/12/20 09:19:38 danielk1977 Exp $
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
# NOTES ON EXPECTED BEHAVIOUR # NOTES ON EXPECTED BEHAVIOUR
@@ -95,7 +95,6 @@ do_test malloc5-1.9 {
} }
} {} } {}
do_test malloc5-2.1 { do_test malloc5-2.1 {
# Put some data in tables abc and def. Both tables are still wholly # Put some data in tables abc and def. Both tables are still wholly
# contained within their root pages. # contained within their root pages.
@@ -116,13 +115,82 @@ do_test malloc5-2.2 {
BEGIN; BEGIN;
SELECT * FROM def; SELECT * FROM def;
} }
set data [list]
db eval {SELECT * FROM abc} { db eval {SELECT * FROM abc} {
incr nRelease [db release_memory] incr nRelease [db release_memory]
lappend data $a $b $c lappend data $a $b $c
} }
execsql {
COMMIT;
}
list $nRelease $data list $nRelease $data
} [list $pgalloc [list 1 2 3 4 5 6]] } [list $pgalloc [list 1 2 3 4 5 6]]
do_test malloc5-3.1 {
# Simple test to show that if two pagers are opened from within this
# thread, memory is freed from both when sqlite3_release_memory() is
# called.
execsql {
BEGIN;
SELECT * FROM abc;
}
execsql {
SELECT * FROM sqlite_master;
BEGIN;
SELECT * FROM def;
} db2
db release_memory
} [expr $::pgalloc * 2]
do_test malloc5-3.2 {
concat \
[execsql {SELECT * FROM abc; COMMIT}] \
[execsql {SELECT * FROM def; COMMIT} db2]
} {1 2 3 4 5 6 7 8 9 10 11 12}
db2 close
sqlite_malloc_outstanding -clearmaxbytes
# The following two test cases each execute a transaction in which
# 10000 rows are inserted into table abc. The first test case is used
# to ensure that more than 1MB of dynamic memory is used to perform
# the transaction.
#
# The second test case sets the "soft-heap-limit" to 100,000 bytes (0.1 MB)
# and tests to see that this limit is not exceeded at any point during
# transaction execution.
#
do_test malloc5-4.1 {
execsql {BEGIN;}
execsql {DELETE FROM abc;}
for {set i 0} {$i < 10000} {incr i} {
execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
}
execsql {COMMIT;}
set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes]
expr $::nMaxBytes > 1000000
} {1}
do_test malloc5-4.2 {
db release_memory
sqlite_malloc_outstanding -clearmaxbytes
db soft_heap_limit 100000
execsql {BEGIN;}
for {set i 0} {$i < 10000} {incr i} {
execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
}
execsql {COMMIT;}
set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes]
expr $::nMaxBytes <= 100000
} {1}
do_test malloc5-4.3 {
# Check that the content of table abc is at least roughly as expected.
execsql {
SELECT count(*), sum(a), sum(b) FROM abc;
}
} [list 20000 [expr int(20000.0 * 4999.5)] [expr int(20000.0 * 4999.5)]]
# Unset the soft-heap-limit value.
db soft_heap_limit -1
finish_test finish_test

View File

@@ -15,7 +15,7 @@
# interface is pretty well tested. This file contains some addition # interface is pretty well tested. This file contains some addition
# tests for fringe issues that the main test suite does not cover. # tests for fringe issues that the main test suite does not cover.
# #
# $Id: tclsqlite.test,v 1.47 2005/12/16 06:54:03 danielk1977 Exp $ # $Id: tclsqlite.test,v 1.48 2005/12/20 09:19:38 danielk1977 Exp $
set testdir [file dirname $argv0] set testdir [file dirname $argv0]
source $testdir/tester.tcl source $testdir/tester.tcl
@@ -34,7 +34,7 @@ do_test tcl-1.1 {
do_test tcl-1.2 { do_test tcl-1.2 {
set v [catch {db bogus} msg] set v [catch {db bogus} msg]
lappend v $msg lappend v $msg
} {1 {bad option "bogus": must be authorizer, busy, cache, changes, close, collate, collation_needed, commit_hook, complete, copy, errorcode, eval, exists, function, last_insert_rowid, nullvalue, onecolumn, profile, progress, rekey, rollback_hook, soft_heap_limit, timeout, total_changes, trace, transaction, update_hook, or version}} } {1 {bad option "bogus": must be authorizer, busy, cache, changes, close, collate, collation_needed, commit_hook, complete, copy, errorcode, eval, exists, function, last_insert_rowid, nullvalue, onecolumn, profile, progress, rekey, rollback_hook, release_memory, soft_heap_limit, timeout, total_changes, trace, transaction, update_hook, or version}}
do_test tcl-1.3 { do_test tcl-1.3 {
execsql {CREATE TABLE t1(a int, b int)} execsql {CREATE TABLE t1(a int, b int)}
execsql {INSERT INTO t1 VALUES(10,20)} execsql {INSERT INTO t1 VALUES(10,20)}