/ Check-in [e7ed0fe6]
Login

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

Overview
Comment:Change the definition of SQLITE_CONFIG_PAGECACHE and SQLITE_CONFIG_SCRATCH to omit the magic "+4" in the buffer size calculation. (CVS 5512)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: e7ed0fe640a39053009eac52a7f055b121750e57
User & Date: drh 2008-07-31 17:16:05
Context
2008-07-31
17:20
Update the memsubsys1 test script to take into account the buffer size changes of the previous check-in. (CVS 5513) check-in: d43ff7bb user: drh tags: trunk
17:16
Change the definition of SQLITE_CONFIG_PAGECACHE and SQLITE_CONFIG_SCRATCH to omit the magic "+4" in the buffer size calculation. (CVS 5512) check-in: e7ed0fe6 user: drh tags: trunk
14:47
Documentation updates. (CVS 5511) check-in: e7fdd813 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/malloc.c.

     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   **
    13     13   ** Memory allocation functions used throughout sqlite.
    14     14   **
    15         -** $Id: malloc.c,v 1.31 2008/07/29 14:29:07 drh Exp $
           15  +** $Id: malloc.c,v 1.32 2008/07/31 17:16:05 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <stdarg.h>
    19     19   #include <ctype.h>
    20     20   
    21     21   /*
    22     22   ** This routine runs when the memory allocator sees that the
................................................................................
   111    111     memset(&mem0, 0, sizeof(mem0));
   112    112     if( sqlite3Config.bCoreMutex ){
   113    113       mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
   114    114     }
   115    115     if( sqlite3Config.pScratch && sqlite3Config.szScratch>=3000
   116    116         && sqlite3Config.nScratch>0 ){
   117    117       int i;
          118  +    sqlite3Config.szScratch -= 4;
   118    119       mem0.aScratchFree = (u32*)&((char*)sqlite3Config.pScratch)
   119    120                     [sqlite3Config.szScratch*sqlite3Config.nScratch];
   120    121       for(i=0; i<sqlite3Config.nScratch; i++){ mem0.aScratchFree[i] = i; }
   121    122       mem0.nScratchFree = sqlite3Config.nScratch;
   122    123     }else{
   123    124       sqlite3Config.pScratch = 0;
   124    125       sqlite3Config.szScratch = 0;
   125    126     }
   126    127     if( sqlite3Config.pPage && sqlite3Config.szPage>=512
   127         -      && sqlite3Config.nPage>0 ){
          128  +      && sqlite3Config.nPage>1 ){
   128    129       int i;
          130  +    int overhead;
          131  +    int sz = sqlite3Config.szPage;
          132  +    int n = sqlite3Config.nPage;
          133  +    overhead = (4*n + sz - 1)/sz;
          134  +    sqlite3Config.nPage -= overhead;
   129    135       mem0.aPageFree = (u32*)&((char*)sqlite3Config.pPage)
   130    136                     [sqlite3Config.szPage*sqlite3Config.nPage];
   131    137       for(i=0; i<sqlite3Config.nPage; i++){ mem0.aPageFree[i] = i; }
   132    138       mem0.nPageFree = sqlite3Config.nPage;
   133    139     }else{
   134    140       sqlite3Config.pPage = 0;
   135    141       sqlite3Config.szPage = 0;

Changes to src/sqlite.h.in.

    26     26   ** on how SQLite interfaces are suppose to operate.
    27     27   **
    28     28   ** The name of this file under configuration management is "sqlite.h.in".
    29     29   ** The makefile makes some minor changes to this file (such as inserting
    30     30   ** the version number) and changes its name to "sqlite3.h" as
    31     31   ** part of the build process.
    32     32   **
    33         -** @(#) $Id: sqlite.h.in,v 1.383 2008/07/31 14:47:55 drh Exp $
           33  +** @(#) $Id: sqlite.h.in,v 1.384 2008/07/31 17:16:05 drh Exp $
    34     34   */
    35     35   #ifndef _SQLITE3_H_
    36     36   #define _SQLITE3_H_
    37     37   #include <stdarg.h>     /* Needed for the definition of va_list */
    38     38   
    39     39   /*
    40     40   ** Make sure we can call this stuff from C++.
................................................................................
  1105   1105   ** boolean, which enables or disables the collection of memory allocation 
  1106   1106   ** statistics. When disabled, the following SQLite interfaces become 
  1107   1107   ** non-operational:
  1108   1108   **   <ul>
  1109   1109   **   <li> [sqlite3_memory_used()]
  1110   1110   **   <li> [sqlite3_memory_highwater()]
  1111   1111   **   <li> [sqlite3_soft_heap_limit()]
  1112         -**   <li> sqlite3_memory_status()
         1112  +**   <li> [sqlite3_status()]
  1113   1113   **   </ul>
  1114   1114   ** </dd>
  1115   1115   **
  1116   1116   ** <dt>SQLITE_CONFIG_SCRATCH</dt>
  1117   1117   ** <dd>This option specifies a static memory buffer that SQLite can use for
  1118   1118   ** scratch memory.  There are three arguments:  A pointer to the memory, the
  1119   1119   ** size of each scratch buffer (sz), and the number of buffers (N).  The sz
  1120         -** argument must be a multiple of 16. The first
  1121         -** argument should point to an allocation of at least (sz+4)*N bytes of memory.
         1120  +** argument must be a multiple of 16. The sz parameter should be a few bytes
         1121  +** larger than the actual scratch space required due internal overhead.
         1122  +** The first
         1123  +** argument should point to an allocation of at least sz*N bytes of memory.
  1122   1124   ** SQLite will use no more than one scratch buffer at once per thread, so
  1123   1125   ** N should be set to the expected maximum number of threads.  The sz
  1124   1126   ** parameter should be 6 times the size of the largest database page size.
  1125   1127   ** Scratch buffers are used as part of the btree balance operation.  If
  1126   1128   ** The btree balancer needs additional memory beyond what is provided by
  1127   1129   ** scratch buffers or if no scratch buffer space is specified, then SQLite
  1128   1130   ** goes to [sqlite3_malloc()] to obtain the memory it needs.</dd>
  1129   1131   **
  1130   1132   ** <dt>SQLITE_CONFIG_PAGECACHE</dt>
  1131   1133   ** <dd>This option specifies a static memory buffer that SQLite can use for
  1132   1134   ** the database page cache.  There are three arguments: A pointer to the
  1133   1135   ** memory, the size of each page buffer (sz), and the number of pages (N).
  1134   1136   ** The sz argument must be a power of two between 512 and 32768.  The first
  1135         -** argument should point to an allocation of at least (sz+4)*N bytes of memory.
         1137  +** argument should point to an allocation of at least sz*N bytes of memory.
  1136   1138   ** SQLite will use the memory provided by the first argument to satisfy its
  1137   1139   ** memory needs for the first N pages that it adds to cache.  If additional
  1138   1140   ** page cache memory is needed beyond what is provided by this option, then
  1139         -** SQLite goes to [sqlite3_malloc()] for the additional storage space.</dd>
         1141  +** SQLite goes to [sqlite3_malloc()] for the additional storage space.
         1142  +** The implementation might use one or more of the N buffers to hold 
         1143  +** memory accounting information. </dd>
  1140   1144   **
  1141   1145   ** <dt>SQLITE_CONFIG_HEAP</dt>
  1142   1146   ** <dd>This option specifies a static memory buffer that SQLite will use
  1143   1147   ** for all of its dynamic memory allocation needs beyond those provided
  1144   1148   ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
  1145   1149   ** There are three arguments: A pointer to the memory, the number of
  1146   1150   ** bytes in the memory buffer, and the minimum allocation size.  If
................................................................................
  6171   6175   ** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
  6172   6176   ** internal equivalents).  The value of interest is return in the
  6173   6177   ** *pHighwater parameter to [sqlite3_status()].  The value written
  6174   6178   ** into the *pCurrent parameter is undefined.</dd>
  6175   6179   **
  6176   6180   ** <dt>SQLITE_STATUS_PARSER_STACK</dt>
  6177   6181   ** <dd>This parameter records the deepest parser stack.  It is only
  6178         -** meaningful if SQLite is compiled with YYTRACKMAXSTACKDEPTH.</dd>
         6182  +** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>
  6179   6183   ** </dl>
  6180   6184   **
  6181   6185   ** New status parameters may be added from time to time.
  6182   6186   */
  6183   6187   #define SQLITE_STATUS_MEMORY_USED          0
  6184   6188   #define SQLITE_STATUS_PAGECACHE_USED       1
  6185   6189   #define SQLITE_STATUS_PAGECACHE_OVERFLOW   2

Changes to src/test_malloc.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   **
    13     13   ** This file contains code used to implement test interfaces to the
    14     14   ** memory allocation subsystem.
    15     15   **
    16         -** $Id: test_malloc.c,v 1.43 2008/07/29 14:29:07 drh Exp $
           16  +** $Id: test_malloc.c,v 1.44 2008/07/31 17:16:05 drh Exp $
    17     17   */
    18     18   #include "sqliteInt.h"
    19     19   #include "tcl.h"
    20     20   #include <stdlib.h>
    21     21   #include <string.h>
    22     22   #include <assert.h>
    23     23   
................................................................................
   882    882     if( Tcl_GetIntFromObj(interp, objv[1], &sz) ) return TCL_ERROR;
   883    883     if( Tcl_GetIntFromObj(interp, objv[2], &N) ) return TCL_ERROR;
   884    884     free(buf);
   885    885     if( sz<0 ){
   886    886       buf = 0;
   887    887       rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, 0, 0, 0);
   888    888     }else{
   889         -    buf = malloc( (sz+4)*N );
          889  +    buf = malloc( sz*N );
   890    890       rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, buf, sz, N);
   891    891     }
   892    892     pResult = Tcl_NewObj();
   893    893     Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(rc));
   894    894     Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(N));
   895    895     Tcl_SetObjResult(interp, pResult);
   896    896     return TCL_OK;
................................................................................
   922    922     if( Tcl_GetIntFromObj(interp, objv[1], &sz) ) return TCL_ERROR;
   923    923     if( Tcl_GetIntFromObj(interp, objv[2], &N) ) return TCL_ERROR;
   924    924     free(buf);
   925    925     if( sz<0 ){
   926    926       buf = 0;
   927    927       rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, 0, 0, 0);
   928    928     }else{
   929         -    buf = malloc( (sz+4)*N );
          929  +    buf = malloc( sz*N );
   930    930       rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, buf, sz, N);
   931    931     }
   932    932     pResult = Tcl_NewObj();
   933    933     Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(rc));
   934    934     Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(N));
   935    935     Tcl_SetObjResult(interp, pResult);
   936    936     return TCL_OK;