]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Add mem6.c, a new allocator. More to come. (CVS 5467)
authordanielk1977 <danielk1977@noemail.net>
Thu, 24 Jul 2008 08:20:40 +0000 (08:20 +0000)
committerdanielk1977 <danielk1977@noemail.net>
Thu, 24 Jul 2008 08:20:40 +0000 (08:20 +0000)
FossilOrigin-Name: 192bc192185a7b475ef9331e2a4a0dc68083ec03

main.mk
manifest
manifest.uuid
src/main.c
src/mem1.c
src/mem2.c
src/mem6.c [new file with mode: 0644]
src/sqlite.h.in
src/sqliteInt.h
src/test_malloc.c
test/permutations.test

diff --git a/main.mk b/main.mk
index 226c4c694ef52bd466443fce90ca8c699e2efa42..7a18f2f542f78a454aaa69d926989eaa5cc5bb2c 100644 (file)
--- a/main.mk
+++ b/main.mk
@@ -51,7 +51,7 @@ TCCX = $(TCC) $(OPTS) -I. -I$(TOP)/src -I$(TOP) -I$(TOP)/ext/rtree
 LIBOBJ+= alter.o analyze.o attach.o auth.o bitvec.o btmutex.o btree.o build.o \
          callback.o complete.o date.o delete.o \
          expr.o fault.o func.o global.o hash.o insert.o journal.o loadext.o \
-         main.o malloc.o mem1.o mem2.o mem3.o mem4.o mem5.o \
+         main.o malloc.o mem1.o mem2.o mem3.o mem4.o mem5.o mem6.o \
          mutex.o mutex_os2.o mutex_unix.o mutex_w32.o \
          opcodes.o os.o os_os2.o os_unix.o os_win.o \
          pager.o parse.o pragma.o prepare.o printf.o random.o \
@@ -114,6 +114,7 @@ SRC = \
   $(TOP)/src/mem3.c \
   $(TOP)/src/mem4.c \
   $(TOP)/src/mem5.c \
+  $(TOP)/src/mem6.c \
   $(TOP)/src/mutex.c \
   $(TOP)/src/mutex.h \
   $(TOP)/src/mutex_os2.c \
index c62f7c2f41166eb2144003a01731ffb2491333c5..09e8f8ba5682196edd7b920086bb1c1277b878bb 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Error\smessages\sfrom\svirtual\stables\sstore\son\sthe\ssqlite3_vtab->zErrMsg\sfield\nare\sreported\sback\sup\sto\sthe\sapplication\sinterface\sand\smemory\sis\sreclaimed.\s(CVS\s5466)
-D 2008-07-23T21:07:25
+C Add\smem6.c,\sa\snew\sallocator.\sMore\sto\scome.\s(CVS\s5467)
+D 2008-07-24T08:20:40
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in 77ff156061bb870aa0a8b3d545c670d08070f7e6
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -76,7 +76,7 @@ F ext/rtree/rtree_util.tcl ee0a0311eb12175319d78bfb37302320496cee6e
 F ext/rtree/viewrtree.tcl 09526398dae87a5a87c5aac2b3854dbaf8376869
 F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895
 F ltmain.sh 09fe5815427dc7d0abb188bbcdf0e34896577210
-F main.mk 4b4961d11a76f6f6c54f1137326ea6a556f359f8
+F main.mk ece0e4f0b8ba76c690c3332bded6291f2d391a6a
 F mkdll.sh ce8c8cc3528d63eda1702eb65b17c2d710eb3b89
 F mkextu.sh 416f9b7089d80e5590a29692c9d9280a10dbad9f
 F mkextw.sh 4123480947681d9b434a5e7b1ee08135abe409ac
@@ -115,14 +115,15 @@ F src/insert.c e8efc17d037346e4a4a6949e72aab850befe3d5d
 F src/journal.c cffd2cd214e58c0e99c3ff632b3bee6c7cbb260e
 F src/legacy.c 3626c71fb70912abec9a4312beba753a9ce800df
 F src/loadext.c ae0eed9fa96d74172d2a90ee63b5bc36d284295c
-F src/main.c c89f35b92cb9e3bf405a58a5ab8ae5c618d5e1fb
+F src/main.c 6aeae6ec84ad3e023eaa619651079d45900959bc
 F src/malloc.c c4b525896b8c188dab98609180d13dbeeeb33a84
 F src/md5.c 008216bbb5d34c6fbab5357aa68575ad8a31516a
-F src/mem1.c 8340fa5f969e9f9b9bdeb54106457a2003456d2b
-F src/mem2.c 0fc5bd6581c80f3ebd9d0cdf0f3f9c08826397ba
+F src/mem1.c 08f95af5095ba5d1fae17deb0ea186e8cdefd8fa
+F src/mem2.c 1e14a86d12dff279427cc52bb41ce5d80f138137
 F src/mem3.c c73e935d0b900abc51d5fa45f880937b062f4a9f
 F src/mem4.c 6703adb1717b26d9d70a1c2586b4b7b7ffee7909
 F src/mem5.c 0b0ba1c2a02d86eb812dea6debacee841e3856f7
+F src/mem6.c c80dcbf6dbfe225282abcd9de2387dfca2c8515e
 F src/mutex.c a485a0eac8ee2cd95f66e565b4c6696c18db968f
 F src/mutex.h e52ffa1dfc6a6077e8b1823d2c2b7dfcbcf85594
 F src/mutex_os2.c 9c5637aa4c307c552566d0f0b3bd206245b54a97
@@ -143,9 +144,9 @@ F src/printf.c 2174222bc346a11b1eac2a654ccc4f635355ae7e
 F src/random.c 5c754319d38abdd6acd74601ee0105504adc508a
 F src/select.c 859ea5194b05fb2f1f816368062478cda5baa9b8
 F src/shell.c 4b835fe734304ac22a3385868cd3790c1e4f7aa1
-F src/sqlite.h.in 30cad9d3e7d47a63f89687605ac8730586f970bc
+F src/sqlite.h.in 30a57188b126a001dbc28955885fb9698a3b02e9
 F src/sqlite3ext.h 1e3887c9bd3ae66cb599e922824b04cd0d0f2c3e
-F src/sqliteInt.h ee9c3be4c7466cd36c1ecd36f7a51f05c3f3fd54
+F src/sqliteInt.h 424d3e4d843c4c95258dd38a920d8448831ec741
 F src/sqliteLimit.h f435e728c6b620ef7312814d660a81f9356eb5c8
 F src/status.c 2076bc996618b1a112a797f05c1d62026ae01402
 F src/table.c 22744786199c9195720c15a7a42cb97b2e2728d8
@@ -167,7 +168,7 @@ F src/test_devsym.c 6012cb8e3acf812513511025a4fa5d626e0ba19b
 F src/test_func.c 24a556989685495013e08f311ae31c4ef86ddb8c
 F src/test_hexio.c 2f1122aa3f012fa0142ee3c36ce5c902a70cd12f
 F src/test_loadext.c df8ab3a6481ddebbdf0d28ebac5d9e0790f7860f
-F src/test_malloc.c a77b0425720cd003b3ea02b8695343e73a326a54
+F src/test_malloc.c e898fc34dc96286e87a23a60579c15db7967d23a
 F src/test_md5.c 28209a4e2068711b5443c33104fe41f21d160071
 F src/test_mutex.c d3422d9f60cc1330249d102e74b333f0d24a0cb6
 F src/test_onefile.c 243157b10275251c5dc2d6619aee2ff9ae22379c
@@ -435,7 +436,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 3a68576f19a46f737a3c3fc5e88e8d450688c53e
+F test/permutations.test 3ed44783c79005f82585e68955b393521cd12d9e
 F test/pragma.test 2c675ed9a288094ed62bf55b35fbc749e25670fb
 F test/pragma2.test 5364893491b9231dd170e3459bfc2e2342658b47
 F test/printf.test 262a5acd3158f788e9bdf7f18d718f3af32ff6ef
@@ -611,7 +612,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81
 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
-P cb5e75dfa5c7048bd0994afac19846f70bfa7a4d
-R da2e59546e4fa7b050269870dc1e776f
-U drh
-Z 3024197f824f60bae53687eb95f7ee5e
+P 4a9dd5e782a363e93d4705fa3671bc6cf0bb5a33
+R 8b283cbfe876ed053eb7874d7f342505
+U danielk1977
+Z 8afc40bb2e219ef5496de2d6671efc02
index 23a75eabfb3ac4b1eaf27430d562356b9dae3af0..339856820c29483f42c97f260bbd8410e6397a87 100644 (file)
@@ -1 +1 @@
-4a9dd5e782a363e93d4705fa3671bc6cf0bb5a33
\ No newline at end of file
+192bc192185a7b475ef9331e2a4a0dc68083ec03
\ No newline at end of file
index 70a9ad9e28f9fd7cefdea5be2f48400aa93326f3..8cd1d64142472f9168958a7ecd4cb31d57e78167 100644 (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.480 2008/07/22 05:13:30 shane Exp $
+** $Id: main.c,v 1.481 2008/07/24 08:20:40 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -267,6 +267,13 @@ int sqlite3_config(int op, ...){
     }
 #endif
 
+#if defined(SQLITE_ENABLE_MEMSYS6)
+    case SQLITE_CONFIG_CHUNKALLOC: {
+      sqlite3Config.m = *sqlite3MemGetMemsys6();
+      break;
+    }
+#endif
+
     default: {
       rc = SQLITE_ERROR;
       break;
index 7f164f8ced7d7e07ab9c435a2af21e3a914e9f5a..359ce5d8ccf65b241c674378b70a66070a48fc18 100644 (file)
@@ -17,7 +17,7 @@
 ** This file contains implementations of the low-level memory allocation
 ** routines specified in the sqlite3_mem_methods object.
 **
-** $Id: mem1.c,v 1.23 2008/06/23 15:10:25 danielk1977 Exp $
+** $Id: mem1.c,v 1.24 2008/07/24 08:20:40 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 
@@ -120,13 +120,7 @@ static void sqlite3MemShutdown(void *NotUsed){
   return;
 }
 
-/*
-** This routine is the only routine in this file with external linkage.
-**
-** Populate the low-level memory allocation function pointers in
-** sqlite3Config.m with pointers to the routines in this file.
-*/
-void sqlite3MemSetDefault(void){
+sqlite3_mem_methods *sqlite3MemGetDefault(void){
   static const sqlite3_mem_methods defaultMethods = {
      sqlite3MemMalloc,
      sqlite3MemFree,
@@ -137,7 +131,17 @@ void sqlite3MemSetDefault(void){
      sqlite3MemShutdown,
      0
   };
-  sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
+  return &defaultMethods;
+}
+
+/*
+** This routine is the only routine in this file with external linkage.
+**
+** Populate the low-level memory allocation function pointers in
+** sqlite3Config.m with pointers to the routines in this file.
+*/
+void sqlite3MemSetDefault(void){
+  sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetDefault());
 }
 
 #endif /* SQLITE_SYSTEM_MALLOC */
index 5e9a999b7e158975d1d07c7dd48c0e3a927504cb..46ae707e6be3579dba373853da1ec152586703d5 100644 (file)
@@ -19,7 +19,7 @@
 ** This file contains implementations of the low-level memory allocation
 ** routines specified in the sqlite3_mem_methods object.
 **
-** $Id: mem2.c,v 1.34 2008/07/10 18:13:42 drh Exp $
+** $Id: mem2.c,v 1.35 2008/07/24 08:20:40 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 
@@ -304,11 +304,7 @@ static void *sqlite3MemRealloc(void *pPrior, int nByte){
 }
 
 
-/*
-** Populate the low-level memory allocation function pointers in
-** sqlite3Config.m with pointers to the routines in this file.
-*/
-void sqlite3MemSetDefault(void){
+sqlite3_mem_methods *sqlite3MemGetDefault(void){
   static const sqlite3_mem_methods defaultMethods = {
      sqlite3MemMalloc,
      sqlite3MemFree,
@@ -319,7 +315,15 @@ void sqlite3MemSetDefault(void){
      sqlite3MemShutdown,
      0
   };
-  sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
+  return &defaultMethods;
+}
+
+/*
+** Populate the low-level memory allocation function pointers in
+** sqlite3Config.m with pointers to the routines in this file.
+*/
+void sqlite3MemSetDefault(void){
+  sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetDefault());
 }
 
 /*
diff --git a/src/mem6.c b/src/mem6.c
new file mode 100644 (file)
index 0000000..a1b9898
--- /dev/null
@@ -0,0 +1,410 @@
+/*
+** 2008 July 24
+**
+** The author disclaims copyright to this source code.  In place of
+** a legal notice, here is a blessing:
+**
+**    May you do good and not evil.
+**    May you find forgiveness for yourself and forgive others.
+**    May you share freely, never taking more than you give.
+**
+*************************************************************************
+**
+** $Id: mem6.c,v 1.1 2008/07/24 08:20:40 danielk1977 Exp $
+*/
+
+#ifdef SQLITE_ENABLE_MEMSYS6
+
+/*
+** Maximum size of any allocation is ((1<<LOGMAX)*Mem6Chunk.nAtom). Since
+** Mem6Chunk.nAtom is always at least 8, this is not really a practical
+** limitation.
+*/
+#define LOGMAX 30
+
+#include "sqliteInt.h"
+
+typedef struct Mem6Chunk Mem6Chunk;
+typedef struct Mem6Link Mem6Link;
+
+/*
+** A minimum allocation is an instance of the following structure.
+** Larger allocations are an array of these structures where the
+** size of the array is a power of 2.
+*/
+struct Mem6Link {
+  int next;       /* Index of next free chunk */
+  int prev;       /* Index of previous free chunk */
+};
+
+/*
+** Masks used for mem5.aCtrl[] elements.
+*/
+#define CTRL_LOGSIZE  0x1f    /* Log2 Size of this block relative to POW2_MIN */
+#define CTRL_FREE     0x20    /* True if not checked out */
+
+struct Mem6Chunk {
+  Mem6Chunk *pNext;
+
+  /*
+  ** Lists of free blocks of various sizes.
+  */
+  int aiFreelist[LOGMAX+1];
+
+  int nCheckedOut; /* Number of currently outstanding allocations */
+
+  /*
+  ** Space for tracking which blocks are checked out and the size
+  ** of each block. One byte per block.
+  */
+  u8 *aCtrl;
+
+  /*
+  ** Memory available for allocation
+  */
+  int nAtom;       /* Smallest possible allocation in bytes */
+  int nBlock;      /* Number of nAtom sized blocks in zPool */
+  u8 *zPool;       /* Pointer to memory chunk from which allocations are made */
+};
+
+#define MEM6LINK(idx) ((Mem6Link *)(&pChunk->zPool[(idx)*pChunk->nAtom]))
+
+/*
+** Unlink the chunk at pChunk->aPool[i] from list it is currently
+** on.  It should be found on pChunk->aiFreelist[iLogsize].
+*/
+static void memsys6Unlink(Mem6Chunk *pChunk, int i, int iLogsize){
+  int next, prev;
+  assert( i>=0 && i<pChunk->nBlock );
+  assert( iLogsize>=0 && iLogsize<=LOGMAX );
+  assert( (pChunk->aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
+
+  next = MEM6LINK(i)->next;
+  prev = MEM6LINK(i)->prev;
+  if( prev<0 ){
+    pChunk->aiFreelist[iLogsize] = next;
+  }else{
+    MEM6LINK(prev)->next = next;
+  }
+  if( next>=0 ){
+    MEM6LINK(next)->prev = prev;
+  }
+}
+
+/*
+** Link the chunk at mem5.aPool[i] so that is on the iLogsize
+** free list.
+*/
+static void memsys6Link(Mem6Chunk *pChunk, int i, int iLogsize){
+  int x;
+  assert( i>=0 && i<pChunk->nBlock );
+  assert( iLogsize>=0 && iLogsize<=LOGMAX );
+  assert( (pChunk->aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
+
+  x = MEM6LINK(i)->next = pChunk->aiFreelist[iLogsize];
+  MEM6LINK(i)->prev = -1;
+  if( x>=0 ){
+    assert( x<pChunk->nBlock );
+    MEM6LINK(x)->prev = i;
+  }
+  pChunk->aiFreelist[iLogsize] = i;
+}
+
+
+/*
+** Find the first entry on the freelist iLogsize.  Unlink that
+** entry and return its index. 
+*/
+static int memsys6UnlinkFirst(Mem6Chunk *pChunk, int iLogsize){
+  int i;
+  int iFirst;
+
+  assert( iLogsize>=0 && iLogsize<=LOGMAX );
+  i = iFirst = pChunk->aiFreelist[iLogsize];
+  assert( iFirst>=0 );
+  while( i>0 ){
+    if( i<iFirst ) iFirst = i;
+    i = MEM6LINK(i)->next;
+  }
+  memsys6Unlink(pChunk, iFirst, iLogsize);
+  return iFirst;
+}
+
+/*
+** Allocate and return a block of nByte bytes from chunk pChunk. If the
+** allocation request cannot be satisfied, return 0.
+*/
+static void *chunkMalloc(Mem6Chunk *pChunk, int nByte){
+  int i;           /* Index of a mem5.aPool[] slot */
+  int iBin;        /* Index into mem5.aiFreelist[] */
+  int iFullSz;     /* Size of allocation rounded up to power of 2 */
+  int iLogsize;    /* Log2 of iFullSz/POW2_MIN */
+
+  /* Round nByte up to the next valid power of two */
+  if( nByte>(pChunk->nBlock*pChunk->nAtom) ) return 0;
+  for(iFullSz=pChunk->nAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
+
+  /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
+  ** block.  If not, then split a block of the next larger power of
+  ** two in order to create a new free block of size iLogsize.
+  */
+  for(iBin=iLogsize; pChunk->aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
+  if( iBin>LOGMAX ) return 0;
+  i = memsys6UnlinkFirst(pChunk, iBin);
+  while( iBin>iLogsize ){
+    int newSize;
+
+    iBin--;
+    newSize = 1 << iBin;
+    pChunk->aCtrl[i+newSize] = CTRL_FREE | iBin;
+    memsys6Link(pChunk, i+newSize, iBin);
+  }
+  pChunk->aCtrl[i] = iLogsize;
+
+  /* Return a pointer to the allocated memory. */
+  pChunk->nCheckedOut++;
+  return (void*)&pChunk->zPool[i*pChunk->nAtom];
+}
+
+/*
+** Free the allocation pointed to by p, which is guaranteed to be non-zero
+** and a part of chunk object pChunk.
+*/
+static void chunkFree(Mem6Chunk *pChunk, void *pOld){
+  u32 size, iLogsize;
+  int iBlock;             
+
+  /* Set iBlock to the index of the block pointed to by pOld in 
+  ** the array of pChunk->nAtom byte blocks pointed to by pChunk->zPool.
+  */
+  iBlock = ((u8 *)pOld-pChunk->zPool)/pChunk->nAtom;
+
+  /* Check that the pointer pOld points to a valid, non-free block. */
+  assert( iBlock>=0 && iBlock<pChunk->nBlock );
+  assert( ((u8 *)pOld-pChunk->zPool)%pChunk->nAtom==0 );
+  assert( (pChunk->aCtrl[iBlock] & CTRL_FREE)==0 );
+
+  iLogsize = pChunk->aCtrl[iBlock] & CTRL_LOGSIZE;
+  size = 1<<iLogsize;
+  assert( iBlock+size-1<pChunk->nBlock );
+
+  pChunk->aCtrl[iBlock] |= CTRL_FREE;
+  pChunk->aCtrl[iBlock+size-1] |= CTRL_FREE;
+
+  pChunk->aCtrl[iBlock] = CTRL_FREE | iLogsize;
+  while( iLogsize<LOGMAX ){
+    int iBuddy;
+    if( (iBlock>>iLogsize) & 1 ){
+      iBuddy = iBlock - size;
+    }else{
+      iBuddy = iBlock + size;
+    }
+    assert( iBuddy>=0 );
+    if( (iBuddy+(1<<iLogsize))>pChunk->nBlock ) break;
+    if( pChunk->aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
+    memsys6Unlink(pChunk, iBuddy, iLogsize);
+    iLogsize++;
+    if( iBuddy<iBlock ){
+      pChunk->aCtrl[iBuddy] = CTRL_FREE | iLogsize;
+      pChunk->aCtrl[iBlock] = 0;
+      iBlock = iBuddy;
+    }else{
+      pChunk->aCtrl[iBlock] = CTRL_FREE | iLogsize;
+      pChunk->aCtrl[iBuddy] = 0;
+    }
+    size *= 2;
+  }
+  pChunk->nCheckedOut--;
+  memsys6Link(pChunk, iBlock, iLogsize);
+}
+
+/*
+** Return the actual size of the block pointed to by p, which is guaranteed
+** to have been allocated from chunk pChunk.
+*/
+static int chunkSize(Mem6Chunk *pChunk, void *p){
+  int iSize = 0;
+  if( p ){
+    int i = ((u8 *)p-pChunk->zPool)/pChunk->nAtom;
+    assert( i>=0 && i<pChunk->nBlock );
+    iSize = pChunk->nAtom * (1 << (pChunk->aCtrl[i]&CTRL_LOGSIZE));
+  }
+  return iSize;
+}
+
+/*
+** Return true if there are currently no outstanding allocations.
+*/
+static int chunkIsEmpty(Mem6Chunk *pChunk){
+  return (pChunk->nCheckedOut==0);
+}
+
+/*
+** Initialize the buffer zChunk, which is nChunk bytes in size, as
+** an Mem6Chunk object. Return a copy of the zChunk pointer.
+*/
+static Mem6Chunk *chunkInit(u8 *zChunk, int nChunk, int nMinAlloc){
+  int ii;
+  int iOffset;
+  Mem6Chunk *pChunk = (Mem6Chunk *)zChunk;
+
+  assert( nChunk>sizeof(Mem6Chunk) );
+  assert( nMinAlloc>sizeof(Mem6Link) );
+
+  memset(pChunk, 0, sizeof(Mem6Chunk));
+  pChunk->nAtom = nMinAlloc;
+  pChunk->nBlock = ((nChunk-sizeof(Mem6Chunk)) / (pChunk->nAtom+sizeof(u8)));
+
+  pChunk->zPool = (u8 *)&pChunk[1];
+  pChunk->aCtrl = &pChunk->zPool[pChunk->nBlock*pChunk->nAtom];
+
+  for(ii=0; ii<=LOGMAX; ii++){
+    pChunk->aiFreelist[ii] = -1;
+  }
+
+  iOffset = 0;
+  for(ii=LOGMAX; ii>=0; ii--){
+    int nAlloc = (1<<ii);
+    if( (iOffset+nAlloc)<=pChunk->nBlock ){
+      pChunk->aCtrl[iOffset] = ii | CTRL_FREE;
+      memsys6Link(pChunk, iOffset, ii);
+      iOffset += nAlloc;
+    }
+    assert((iOffset+nAlloc)>pChunk->nBlock);
+  }
+
+  return pChunk;
+}
+
+struct Mem6Global {
+  sqlite3_mem_methods parent;     /* Used to allocate chunks */
+  int nChunkSize;                 /* Size of each chunk, in bytes. */
+  int nMinAlloc;                  /* Minimum allowed allocation size */
+
+  /* This data structure will be fixed... */
+  Mem6Chunk *pChunk;              /* Singly linked list of all memory chunks */
+} mem6;
+
+/*
+** The argument is a pointer that may or may not have been allocated from
+** one of the Mem6Chunk objects managed within mem6. If it is, return
+** a pointer to the owner chunk. If not, return 0.
+*/
+static Mem6Chunk *findChunk(u8 *p){
+  Mem6Chunk *pChunk;
+  for(pChunk=mem6.pChunk; pChunk; pChunk=pChunk->pNext){
+    if( p>=pChunk->zPool && p<=&pChunk->zPool[pChunk->nBlock*pChunk->nAtom] ){
+      return pChunk;
+    }
+  }
+  return 0;
+}
+
+static void freeChunk(Mem6Chunk *pChunk){
+  Mem6Chunk **pp = &mem6.pChunk;
+  for( pp=&mem6.pChunk; *pp!=pChunk; pp = &(*pp)->pNext );
+  *pp = (*pp)->pNext;
+  mem6.parent.xFree(pChunk);
+}
+
+static void *memsys6Malloc(int nByte){
+  Mem6Chunk *pChunk;
+  void *p;
+  if( nByte>=mem6.nChunkSize/3 ){
+    return mem6.parent.xMalloc(nByte);
+  }
+  for(pChunk=mem6.pChunk; pChunk; pChunk=pChunk->pNext){
+    p = chunkMalloc(pChunk, nByte);
+    if( p ){
+      return p;
+    }
+  }
+
+  p = mem6.parent.xMalloc(mem6.nChunkSize);
+  if( p ){
+    pChunk = chunkInit((u8 *)p, mem6.nChunkSize, mem6.nMinAlloc);
+    pChunk->pNext = mem6.pChunk;
+    mem6.pChunk = pChunk;
+    p = chunkMalloc(pChunk, nByte);
+    assert(p);
+  }
+
+  return p;
+}
+
+static int memsys6Size(void *p){
+  Mem6Chunk *pChunk = findChunk(p);
+  return (pChunk ? chunkSize(pChunk, p) : mem6.parent.xSize(p));
+}
+
+static void memsys6Free(void *p){
+  Mem6Chunk *pChunk = findChunk(p);
+  if( pChunk ){
+    chunkFree(pChunk, p);
+    if( chunkIsEmpty(pChunk) ){
+      freeChunk(pChunk);
+    }
+  }else{
+    mem6.parent.xFree(p);
+  }
+}
+
+static void *memsys6Realloc(void *p, int nByte){
+  Mem6Chunk *pChunk = findChunk(p);
+  void *p2;
+
+  if( !pChunk ){
+    return mem6.parent.xRealloc(p, nByte);
+  }
+
+  p2 = memsys6Malloc(nByte);
+  if( p2 ){
+    assert( memsys6Size(p)<nByte );
+    memcpy(p2, p, memsys6Size(p));
+    memsys6Free(p);
+  }
+  return p2;
+}
+
+
+static int memsys6Roundup(int n){
+  int iFullSz;
+  for(iFullSz=mem6.nMinAlloc; iFullSz<n; iFullSz *= 2);
+  return iFullSz;
+}
+
+static int memsys6Init(void *pCtx){
+  mem6.parent = *sqlite3MemGetDefault();
+  mem6.nChunkSize = (1<<16);
+  mem6.nMinAlloc = 16;
+  mem6.pChunk = 0;
+
+  /* Initialize the parent allocator. */
+  mem6.parent.xInit(mem6.parent.pAppData);
+
+  return SQLITE_OK;
+}
+
+static void memsys6Shutdown(void *pCtx){
+}
+
+/*
+** This routine is the only routine in this file with external 
+** linkage. It returns a pointer to a static sqlite3_mem_methods
+** struct populated with the memsys6 methods.
+*/
+const sqlite3_mem_methods *sqlite3MemGetMemsys6(void){
+  static const sqlite3_mem_methods memsys6Methods = {
+     memsys6Malloc,
+     memsys6Free,
+     memsys6Realloc,
+     memsys6Size,
+     memsys6Roundup,
+     memsys6Init,
+     memsys6Shutdown,
+     0
+  };
+  return &memsys6Methods;
+}
+
+#endif
index 20545ab8c78f2fb0b8e9e89eb835d0d1877eaee4..3a7f1a220304fe1de64c782d24cc309e76fcbe40 100644 (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.377 2008/07/23 18:25:56 drh Exp $
+** @(#) $Id: sqlite.h.in,v 1.378 2008/07/24 08:20:40 danielk1977 Exp $
 */
 #ifndef _SQLITE3_H_
 #define _SQLITE3_H_
@@ -1158,6 +1158,8 @@ struct sqlite3_mem_methods {
 #define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
 #define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
 
+#define SQLITE_CONFIG_CHUNKALLOC   12  /* nil */
+
 
 /*
 ** CAPI3REF: Enable Or Disable Extended Result Codes {H12200} <S10700>
index 07d7a3f81495adc83e1d14fbacbc4be9baf3720a..28d6176d59c57676b20b12227ff6d22a09ff3deb 100644 (file)
@@ -11,7 +11,7 @@
 *************************************************************************
 ** Internal interface definitions for SQLite.
 **
-** @(#) $Id: sqliteInt.h,v 1.743 2008/07/23 18:17:32 drh Exp $
+** @(#) $Id: sqliteInt.h,v 1.744 2008/07/24 08:20:40 danielk1977 Exp $
 */
 #ifndef _SQLITEINT_H_
 #define _SQLITEINT_H_
@@ -1841,8 +1841,10 @@ void sqlite3ScratchFree(void*);
 void *sqlite3PageMalloc(int);
 void sqlite3PageFree(void*);
 void sqlite3MemSetDefault(void);
+sqlite3_mem_methods *sqlite3MemGetDefault(void);
 const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
 const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
+const sqlite3_mem_methods *sqlite3MemGetMemsys6(void);
 void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
 
 #ifndef SQLITE_MUTEX_NOOP
index 052f198eae5aa05bbb04361bf61b6fc71131a595..30df0348fbfcebb15cdb125c39b9a582008d323e 100644 (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.38 2008/07/16 12:25:32 drh Exp $
+** $Id: test_malloc.c,v 1.39 2008/07/24 08:20:40 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 #include "tcl.h"
@@ -956,6 +956,26 @@ static int test_config_memstatus(
   return TCL_OK;
 }
 
+/*
+** Usage:    sqlite3_config_chunkalloc 
+**
+*/
+static int test_config_chunkalloc(
+  void * clientData,
+  Tcl_Interp *interp,
+  int objc,
+  Tcl_Obj *CONST objv[]
+){
+  int rc;
+  if( objc!=1 ){
+    Tcl_WrongNumArgs(interp, 1, objv, "");
+    return TCL_ERROR;
+  }
+  rc = sqlite3_config(SQLITE_CONFIG_CHUNKALLOC);
+  Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
+  return TCL_OK;
+}
+
 /*
 ** Usage:
 **
@@ -1142,6 +1162,7 @@ int Sqlitetest_malloc_Init(Tcl_Interp *interp){
      { "install_malloc_faultsim",    test_install_malloc_faultsim  ,0 },
      { "sqlite3_config_heap",        test_config_heap              ,0 },
      { "sqlite3_config_memstatus",   test_config_memstatus         ,0 },
+     { "sqlite3_config_chunkalloc",  test_config_chunkalloc        ,0 },
      { "sqlite3_dump_memsys3",       test_dump_memsys3             ,3 },
      { "sqlite3_dump_memsys5",       test_dump_memsys3             ,5 }
   };
index 215745192f22e161f528dee3126d757e7e5637a0..d918986b577f13751fd130641421209e28a30703 100644 (file)
@@ -9,7 +9,7 @@
 #
 #***********************************************************************
 #
-# $Id: permutations.test,v 1.13 2008/07/16 12:25:32 drh Exp $
+# $Id: permutations.test,v 1.14 2008/07/24 08:20:40 danielk1977 Exp $
 
 set testdir [file dirname $argv0]
 source $testdir/tester.tcl
@@ -439,6 +439,27 @@ ifcapable threadsafe {
   }
 }
 
+  run_tests "memsys6" -description {
+    Run tests using the allocator in mem6.c.
+  } -include {
+    select1.test 
+  } -initialize {
+    catch {db close}
+    sqlite3_reset_auto_extension
+    sqlite3_shutdown
+    sqlite3_config_chunkalloc
+    install_malloc_faultsim 1 
+    sqlite3_initialize
+    autoinstall_test_functions
+  } -shutdown {
+    catch {db close}
+    sqlite3_reset_auto_extension
+    sqlite3_shutdown
+    sqlite3_config_heap 0 0
+    install_malloc_faultsim 1 
+    sqlite3_initialize
+  }
+
 # run_tests "crash_safe_append" -description {
 #   Run crash.test with persistent journals on a SAFE_APPEND file-system.
 # } -initialize {