diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/mem3.c | 38 | ||||
-rw-r--r-- | src/mem5.c | 6 | ||||
-rw-r--r-- | src/sqliteInt.h | 4 | ||||
-rw-r--r-- | src/test_malloc.c | 63 |
4 files changed, 45 insertions, 66 deletions
diff --git a/src/mem3.c b/src/mem3.c index 2582dda55..38c89f77f 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 acbe92d15..6161945d6 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 c5d42152f..b9ecdf619 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 17f6a43fe..906ee26a7 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" @@ -931,40 +931,6 @@ static int test_config_pagecache( } /* -** 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: ** ** sqlite3_config_heap ?-memsys3? NBYTE NMINALLOC @@ -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++){ |