Fix up some details to do with the mem3.c (memsys3) allocator. If the library is compiled with SQLITE_ENABLE_MEMSYS3, the memsys3 allocator can be selected at runtime. (CVS 5303)

FossilOrigin-Name: 9c6c8e01b31abfe2bc8e650bbfdb504021dc7e59
This commit is contained in:
danielk1977 2008-06-25 10:34:34 +00:00
parent 29b13cc9d9
commit 32155ef0ff
8 changed files with 121 additions and 68 deletions

View File

@ -1,5 +1,5 @@
C Fixed\stypo\sto\sclose\s#1731.\s(CVS\s5302)
D 2008-06-25T08:02:44
C Fix\sup\ssome\sdetails\sto\sdo\swith\sthe\smem3.c\s(memsys3)\sallocator.\sIf\sthe\slibrary\sis\scompiled\swith\sSQLITE_ENABLE_MEMSYS3,\sthe\smemsys3\sallocator\scan\sbe\sselected\sat\sruntime.\s(CVS\s5303)
D 2008-06-25T10:34:35
F Makefile.arm-wince-mingw32ce-gcc ac5f7b2cef0cd850d6f755ba6ee4ab961b1fadf7
F Makefile.in ff6f90048555a0088f6a4b7406bed5e55a7c4eff
F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@ -114,12 +114,12 @@ F src/insert.c f8c7da31409ec19a769b960a4a2b9cca7bab80bd
F src/journal.c cffd2cd214e58c0e99c3ff632b3bee6c7cbb260e
F src/legacy.c 3626c71fb70912abec9a4312beba753a9ce800df
F src/loadext.c 40024a0f476c1279494876b9a002001b29e5d3e3
F src/main.c 3f7fd6edaa90bb8905e455156055a212ba77178b
F src/main.c 5ddfc15bdc034253bdb204522f77f2548d01f2c6
F src/malloc.c f52166df8abd7ff6990dbee1a0ce3534addc8617
F src/md5.c 008216bbb5d34c6fbab5357aa68575ad8a31516a
F src/mem1.c 8340fa5f969e9f9b9bdeb54106457a2003456d2b
F src/mem2.c 23f9538f35fbcd5665afe7056a56be0c7ed65aa7
F src/mem3.c 62bb1344e648ca70e651f1e994733382e4fe9b4e
F src/mem3.c 8ecfb0a61a363f34922fe3f73e698adf229eb55a
F src/mem4.c 6703adb1717b26d9d70a1c2586b4b7b7ffee7909
F src/mem5.c ad31a0a481b86b86f4ac0b6d952e69727d4e113a
F src/mutex.c a485a0eac8ee2cd95f66e565b4c6696c18db968f
@ -142,9 +142,9 @@ F src/printf.c 8b063da9dcde26b7c500a01444b718d86f21bc6e
F src/random.c 5c754319d38abdd6acd74601ee0105504adc508a
F src/select.c 79f60dc4a7e90bb907c7a2cca42f45276d1ead99
F src/shell.c 61fa61932ed52825720ebfd3f8381b8d550ef766
F src/sqlite.h.in 6a187a16ddcd30590acc54f00dd3edd7dccfe22c
F src/sqlite.h.in 3ad63197bdc9fa1623c1e139ee6f39e7d2ccfa5b
F src/sqlite3ext.h f162a72daef5ebf8b211fe8c0ec96e85d22fbf9b
F src/sqliteInt.h 5ed4ae2a9338533370517a361623e38a125724a4
F src/sqliteInt.h 5f0b40ff6924972923b7921cd36731ed292cf4da
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 e16aea435daf9e3645951656063bacb5f2ccfd5a
F src/test_malloc.c 9cb0a261b2e744c1daf0ed407501eb6506ce9f75
F src/test_md5.c 28209a4e2068711b5443c33104fe41f21d160071
F src/test_mutex.c b1433eb96520656fb3e4a218253a94ac32bd5775
F src/test_onefile.c 1f87d4a21cbfb24a7c35e4333fa0bd34d641f68d
@ -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 794de3d1d3f97b6081f70f51ea26072d9f0b2e42
F test/permutations.test 501db15728957fa7893117c167f37cc7f1bb9387
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 e53403b6d742be83c5b130ed6cef4035d081d613
R df5e49160c5ef25074362372086fedf0
U mihailim
Z 90518ac21d6f6444d31a20f1d9ccee33
P 9902be8d66453d3f4cfdc7f81a4ebe9bc6729204
R 1a2b1623ff42cc1c532b2d95d3c3b31f
U danielk1977
Z 75609bc694f005e3e95998b63bdcf69a

View File

@ -1 +1 @@
9902be8d66453d3f4cfdc7f81a4ebe9bc6729204
9c6c8e01b31abfe2bc8e650bbfdb504021dc7e59

View File

@ -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.459 2008/06/24 19:02:55 danielk1977 Exp $
** $Id: main.c,v 1.460 2008/06/25 10:34:35 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
@ -149,11 +149,11 @@ int sqlite3_config(int op, ...){
sqlite3Config.bFullMutex = 1;
break;
}
#ifdef SQLITE_ENABLE_MEMPOOL
case SQLITE_CONFIG_MEMPOOL: {
#ifdef SQLITE_ENABLE_MEMSYS3
case SQLITE_CONFIG_MEMSYS3: {
u8 *pMem = va_arg(ap, u8*);
int nMem = va_arg(ap, int);
rc = sqlite3MemSetMempool(pMem, nMem);
sqlite3MemSetMemsys3(pMem, nMem);
break;
}
#endif

View File

@ -13,25 +13,28 @@
** allocation subsystem for use by SQLite.
**
** This version of the memory allocation subsystem omits all
** use of malloc(). All dynamically allocatable memory is
** contained in a static array, mem3.aPool[]. The size of this
** fixed memory pool is SQLITE_MEMORY_SIZE bytes.
** use of malloc(). The SQLite user supplies a block of memory
** before calling sqlite3_initialize() from which allocations
** are made and returned by the xMalloc() and xRealloc()
** implementations. Once sqlite3_initialize() has been called,
** the amount of memory available to SQLite is fixed and cannot
** be changed.
**
** This version of the memory allocation subsystem is used if
** and only if SQLITE_MEMORY_SIZE is defined.
** This version of the memory allocation subsystem is included
** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
**
** $Id: mem3.c,v 1.15 2008/06/24 19:02:55 danielk1977 Exp $
** $Id: mem3.c,v 1.16 2008/06/25 10:34:35 danielk1977 Exp $
*/
#include "sqliteInt.h"
/*
** This version of the memory allocator is only built into the library
** SQLITE_ENABLE_MEMPOOL is defined. Defining this symbol does not
** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
** mean that the library will use a memory-pool by default, just that
** it is available. The mempool allocator is activated by calling
** sqlite3_config().
*/
#ifdef SQLITE_ENABLE_MEMPOOL
#ifdef SQLITE_ENABLE_MEMSYS3
/*
** Maximum size (in Mem3Blocks) of a "small" chunk.
@ -347,8 +350,11 @@ static void memsys3Merge(u32 *pRoot){
/*
** Return a block of memory of at least nBytes in size.
** Return NULL if unable.
**
** This function assumes that the necessary mutexes, if any, are
** already held by the caller. Hence "Unsafe".
*/
static void *memsys3Malloc(int nByte){
static void *memsys3MallocUnsafe(int nByte){
u32 i;
int nBlock;
int toFree;
@ -426,8 +432,11 @@ static void *memsys3Malloc(int nByte){
/*
** Free an outstanding memory allocation.
**
** This function assumes that the necessary mutexes, if any, are
** already held by the caller. Hence "Unsafe".
*/
void memsys3Free(void *pOld){
void memsys3FreeUnsafe(void *pOld){
Mem3Block *p = (Mem3Block*)pOld;
int i;
u32 size, x;
@ -466,11 +475,11 @@ void memsys3Free(void *pOld){
/*
** Allocate nBytes of memory.
*/
static void *mempoolMalloc(int nBytes){
static void *memsys3Malloc(int nBytes){
sqlite3_int64 *p;
assert( nBytes>0 ); /* malloc.c filters out 0 byte requests */
memsys3Enter();
p = memsys3Malloc(nBytes);
p = memsys3MallocUnsafe(nBytes);
memsys3Leave();
return (void*)p;
}
@ -478,10 +487,10 @@ static void *mempoolMalloc(int nBytes){
/*
** Free memory.
*/
void mempoolFree(void *pPrior){
void memsys3Free(void *pPrior){
assert( pPrior );
memsys3Enter();
memsys3Free(pPrior);
memsys3FreeUnsafe(pPrior);
memsys3Leave();
}
@ -490,7 +499,7 @@ void mempoolFree(void *pPrior){
** size returned omits the 8-byte header overhead. This only
** works for chunks that are currently checked out.
*/
static int mempoolSize(void *p){
static int memsys3Size(void *p){
Mem3Block *pBlock = (Mem3Block*)p;
assert( pBlock );
assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
@ -500,7 +509,7 @@ static int mempoolSize(void *p){
/*
** Change the size of an existing memory allocation
*/
void *mempoolRealloc(void *pPrior, int nBytes){
void *memsys3Realloc(void *pPrior, int nBytes){
int nOld;
void *p;
if( pPrior==0 ){
@ -510,19 +519,19 @@ void *mempoolRealloc(void *pPrior, int nBytes){
sqlite3_free(pPrior);
return 0;
}
nOld = mempoolSize(pPrior);
nOld = memsys3Size(pPrior);
if( nBytes<=nOld && nBytes>=nOld-128 ){
return pPrior;
}
memsys3Enter();
p = mempoolMalloc(nBytes);
p = memsys3MallocUnsafe(nBytes);
if( p ){
if( nOld<nBytes ){
memcpy(p, pPrior, nOld);
}else{
memcpy(p, pPrior, nBytes);
}
mempoolFree(pPrior);
memsys3FreeUnsafe(pPrior);
}
memsys3Leave();
return p;
@ -531,22 +540,21 @@ void *mempoolRealloc(void *pPrior, int nBytes){
/*
** Round up a request size to the next valid allocation size.
*/
static int mempoolRoundup(int n){
/* TODO: Fix me */
return n;
static int memsys3Roundup(int n){
return (n+7) & ~7;
}
/*
** Initialize this module.
*/
static int mempoolInit(void *NotUsed){
static int memsys3Init(void *NotUsed){
return SQLITE_OK;
}
/*
** Deinitialize this module.
*/
static void mempoolShutdown(void *NotUsed){
static void memsys3Shutdown(void *NotUsed){
return;
}
@ -556,9 +564,8 @@ static void mempoolShutdown(void *NotUsed){
** Open the file indicated and write a log of all unfreed memory
** allocations into that log.
*/
#if 0
void sqlite3MemdebugDump(const char *zFilename){
#ifdef SQLITE_DEBUG
void sqlite3Memsys3Dump(const char *zFilename){
FILE *out;
int i, j;
u32 size;
@ -574,7 +581,7 @@ void sqlite3MemdebugDump(const char *zFilename){
}
memsys3Enter();
fprintf(out, "CHUNKS:\n");
for(i=1; i<=SQLITE_MEMORY_SIZE/8; i+=size/4){
for(i=1; i<=mem3.nPool; i+=size/4){
size = mem3.aPool[i-1].u.hdr.size4x;
if( size/4<=1 ){
fprintf(out, "%p size error\n", &mem3.aPool[i]);
@ -617,15 +624,14 @@ void sqlite3MemdebugDump(const char *zFilename){
fprintf(out, "\n");
}
fprintf(out, "master=%d\n", mem3.iMaster);
fprintf(out, "nowUsed=%d\n", SQLITE_MEMORY_SIZE - mem3.szMaster*8);
fprintf(out, "mxUsed=%d\n", SQLITE_MEMORY_SIZE - mem3.mnMaster*8);
fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
sqlite3_mutex_leave(mem3.mutex);
if( out==stdout ){
fflush(stdout);
}else{
fclose(out);
}
#endif
}
#endif
@ -640,15 +646,15 @@ void sqlite3MemdebugDump(const char *zFilename){
** This routine is only called by sqlite3_config(), and therefore
** is not required to be threadsafe (it is not).
*/
void sqlite3MemSetMempool(u8 *pBlock, int nBlock){
void sqlite3MemSetMemsys3(u8 *pBlock, int nBlock){
static const sqlite3_mem_methods mempoolMethods = {
mempoolMalloc,
mempoolFree,
mempoolRealloc,
mempoolSize,
mempoolRoundup,
mempoolInit,
mempoolShutdown,
memsys3Malloc,
memsys3Free,
memsys3Realloc,
memsys3Size,
memsys3Roundup,
memsys3Init,
memsys3Shutdown,
0
};
@ -669,4 +675,4 @@ void sqlite3MemSetMempool(u8 *pBlock, int nBlock){
mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
}
#endif /* SQLITE_MEMPOOL_MALLOC */
#endif /* SQLITE_ENABLE_MEMSYS3 */

View File

@ -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.355 2008/06/24 19:02:55 danielk1977 Exp $
** @(#) $Id: sqlite.h.in,v 1.356 2008/06/25 10:34:35 danielk1977 Exp $
*/
#ifndef _SQLITE3_H_
#define _SQLITE3_H_
@ -1102,6 +1102,19 @@ struct sqlite3_mem_methods {
** routines with a wrapper used to track mutex usage for performance
** profiling or testing, for example.</dd>
**
** <dt>SQLITE_CONFIG_MEMSYS3</dt>
** <dd>This option is only available if SQLite is compiled with the
** SQLITE_ENABLE_MEMSYS3 symbol defined. If available, then it is used
** to install an alternative set of built-in memory allocation routines
** known as the "memsys3" allocator (in the same way as SQLITE_CONFIG_MALLOC
** may be used to install an external set of memory allocation routines).
** This options must be passed two arguments, a pointer to a large blob of
** allocated memory (type char*) and the size of the block of memory in bytes
** (type int). The memsys3 allocator manages this block of memory and uses
** it to satisfy all requests for dynamic memory made by the library. The
** caller must ensure that the block of memory remains valid for as long
** as the memsys3 allocator is in use.</dd>
**
** </dl>
*/
#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
@ -1115,7 +1128,7 @@ struct sqlite3_mem_methods {
#define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
#define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
#define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
#define SQLITE_CONFIG_MEMPOOL 12 /* u8*, int */
#define SQLITE_CONFIG_MEMSYS3 12 /* u8*, int */
/*
** CAPI3REF: Enable Or Disable Extended Result Codes {F12200}

View File

@ -11,7 +11,7 @@
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.726 2008/06/24 12:46:31 drh Exp $
** @(#) $Id: sqliteInt.h,v 1.727 2008/06/25 10:34:35 danielk1977 Exp $
*/
#ifndef _SQLITEINT_H_
#define _SQLITEINT_H_
@ -1803,6 +1803,7 @@ void sqlite3ScratchFree(void*);
void *sqlite3PageMalloc(int);
void sqlite3PageFree(void*);
void sqlite3MemSetDefault(void);
void sqlite3MemSetMemsys3(u8 *pBlock, int nBlock);
void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
#ifndef SQLITE_MUTEX_NOOP

View File

@ -13,7 +13,7 @@
** This file contains code used to implement test interfaces to the
** memory allocation subsystem.
**
** $Id: test_malloc.c,v 1.29 2008/06/24 19:02:55 danielk1977 Exp $
** $Id: test_malloc.c,v 1.30 2008/06/25 10:34:35 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "tcl.h"
@ -931,10 +931,10 @@ static int test_config_pagecache(
}
/*
** Usage: sqlite3_config_mempool NBYTE
** Usage: sqlite3_config_memsys3 NBYTE
**
*/
static int test_config_mempool(
static int test_config_memsys3(
void * clientData,
Tcl_Interp *interp,
int objc,
@ -956,12 +956,37 @@ static int test_config_mempool(
if( sz>sizeof(buf) ){
sz = sizeof(buf);
}
rc = sqlite3_config(SQLITE_CONFIG_MEMPOOL, buf, sz);
rc = sqlite3_config(SQLITE_CONFIG_MEMSYS3, buf, sz);
}
Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
return TCL_OK;
}
/*
** Usage: sqlite3_dump_memsys3 FILENAME
**
** Write a summary of unfreed memsys3 allocations to FILENAME.
*/
static int test_dump_memsys3(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
return TCL_ERROR;
}
#if defined(SQLITE_MEMDEBUG) || defined(SQLITE_MEMORY_SIZE) \
|| defined(SQLITE_POW2_MEMORY_SIZE)
{
extern void sqlite3Memsys3Dump(const char*);
sqlite3Memsys3Dump(Tcl_GetString(objv[1]));
}
#endif
return TCL_OK;
}
/*
** Usage: sqlite3_status OPCODE RESETFLAG
**
@ -1061,7 +1086,8 @@ int Sqlitetest_malloc_Init(Tcl_Interp *interp){
{ "sqlite3_memdebug_log", test_memdebug_log },
{ "sqlite3_config_scratch", test_config_scratch },
{ "sqlite3_config_pagecache", test_config_pagecache },
{ "sqlite3_config_mempool", test_config_mempool },
{ "sqlite3_config_memsys3", test_config_memsys3 },
{ "sqlite3_dump_memsys3", test_dump_memsys3 },
{ "sqlite3_status", test_status },
{ "install_malloc_faultsim", test_install_malloc_faultsim },
};

View File

@ -9,7 +9,7 @@
#
#***********************************************************************
#
# $Id: permutations.test,v 1.4 2008/06/24 19:02:55 danielk1977 Exp $
# $Id: permutations.test,v 1.5 2008/06/25 10:34:35 danielk1977 Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
@ -329,20 +329,27 @@ run_tests "autovacuum_ioerr" -description {
pragma auto_vacuum = 1
} -include ioerr.test
run_tests "mempool" -description {
run_tests "memsys3" -description {
Run tests using the allocator in mem3.c.
} -exclude {
incrblob2.test manydb.test autovacuum.test bigrow.test
collate5.test delete3.test index2.test ioerr.test join3.test
pagesize.test bitvec.test capi3.test memsubsys1.test limit.test
memdb.test capi3c.test
} -initialize {
catch {db close}
sqlite3_reset_auto_extension
sqlite3_shutdown
sqlite3_config_mempool 1000000
sqlite3_config_memsys3 1000000
install_malloc_faultsim 1
sqlite3_initialize
autoinstall_test_functions
} -shutdown {
catch {db close}
sqlite3_reset_auto_extension
sqlite3_shutdown
sqlite3_config_mempool 0
sqlite3_config_memsys3 0
install_malloc_faultsim 1
sqlite3_initialize
}