/ Check-in [e36201a3]
Login

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

Overview
Comment:Added new configuration options to query the low-level malloc interface and to provide memory buffers for scratch, page-cache, and heap memory allocators. (CVS 5231)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: e36201a352f832c97c5c0fcb872c1f094cac03d2
User & Date: drh 2008-06-18 13:27:47
Context
2008-06-18
13:47
Add documentation on the sqlite3_mutex_methods object. (CVS 5232) check-in: c3c7bfc9 user: drh tags: trunk
13:27
Added new configuration options to query the low-level malloc interface and to provide memory buffers for scratch, page-cache, and heap memory allocators. (CVS 5231) check-in: e36201a3 user: drh tags: trunk
09:45
Add some test infrastructure and cases for the new mutex api. (CVS 5230) check-in: 262baee9 user: danielk1977 tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/main.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Main file for the SQLite library.  The routines in this file
    13     13   ** implement the programmer interface to the library.  Routines in
    14     14   ** other files are for internal use by SQLite and should not be
    15     15   ** accessed by users of the library.
    16     16   **
    17         -** $Id: main.c,v 1.447 2008/06/18 09:45:56 danielk1977 Exp $
           17  +** $Id: main.c,v 1.448 2008/06/18 13:27:47 drh Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include <ctype.h>
    21     21   
    22     22   #ifdef SQLITE_ENABLE_FTS3
    23     23   # include "fts3.h"
    24     24   #endif
................................................................................
   146    146         sqlite3Config.bFullMutex = 1;
   147    147         break;
   148    148       }
   149    149       case SQLITE_CONFIG_MALLOC: {
   150    150         /* Specify an alternative malloc implementation */
   151    151         sqlite3Config.m = *va_arg(ap, sqlite3_mem_methods*);
   152    152         break;
          153  +    }
          154  +    case SQLITE_CONFIG_GETMALLOC: {
          155  +      /* Specify an alternative malloc implementation */
          156  +      if( sqlite3Config.m.xMalloc==0 ) sqlite3MemSetDefault();
          157  +      *va_arg(ap, sqlite3_mem_methods*) = sqlite3Config.m;
          158  +      break;
   153    159       }
   154    160       case SQLITE_CONFIG_MUTEX: {
   155    161         /* Specify an alternative mutex implementation */
   156    162         sqlite3Config.mutex = *va_arg(ap, sqlite3_mutex_methods*);
   157    163         break;
   158    164       }
   159    165       case SQLITE_CONFIG_GETMUTEX: {
................................................................................
   161    167         *va_arg(ap, sqlite3_mutex_methods*) = sqlite3Config.mutex;
   162    168         break;
   163    169       }
   164    170       case SQLITE_CONFIG_MEMSTATUS: {
   165    171         /* Enable or disable the malloc status collection */
   166    172         sqlite3Config.bMemstat = va_arg(ap, int);
   167    173         break;
          174  +    }
          175  +    case SQLITE_CONFIG_SCRATCH: {
          176  +      /* Designate a buffer for scratch memory space */
          177  +      sqlite3Config.pScratch = va_arg(ap, void*);
          178  +      sqlite3Config.szScratch = va_arg(ap, int);
          179  +      sqlite3Config.nScratch = va_arg(ap, int);
          180  +      break;
          181  +    }
          182  +    case SQLITE_CONFIG_PAGECACHE: {
          183  +      /* Designate a buffer for scratch memory space */
          184  +      sqlite3Config.pPage = va_arg(ap, void*);
          185  +      sqlite3Config.szPage = va_arg(ap, int);
          186  +      sqlite3Config.nPage = va_arg(ap, int);
          187  +      break;
          188  +    }
          189  +    case SQLITE_CONFIG_HEAP: {
          190  +      /* Designate a buffer for scratch memory space */
          191  +      sqlite3Config.pHeap = va_arg(ap, void*);
          192  +      sqlite3Config.nHeap = va_arg(ap, int);
          193  +      sqlite3Config.mnReq = va_arg(ap, int);
          194  +      break;
   168    195       }
   169    196       default: {
   170    197         rc = SQLITE_ERROR;
   171    198         break;
   172    199       }
   173    200     }
   174    201     va_end(ap);

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.332 2008/06/18 09:45:56 danielk1977 Exp $
           33  +** @(#) $Id: sqlite.h.in,v 1.333 2008/06/18 13:27:47 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++.
................................................................................
  1028   1028   **
  1029   1029   ** <dt>SQLITE_CONFIG_MALLOC</dt>
  1030   1030   ** <dd>This option takes a single argument which is a pointer to an
  1031   1031   ** instance of the [sqlite3_mem_methods] structure.  The argument specifics
  1032   1032   ** alternative low-level memory allocation routines to be used in place
  1033   1033   ** the memory allocation routines built into SQLite.</dd>
  1034   1034   **
         1035  +** <dt>SQLITE_CONFIG_GETMALLOC</dt>
         1036  +** <dd>This option takes a single argument which is a pointer to an
         1037  +** instance of the [sqlite3_mem_methods] structure.  The [sqlite3_mem_methods]
         1038  +** structure is filled with the currently defined memory allocation routines.
         1039  +** This option can be used to overload the default memory allocation
         1040  +** routines with a wrapper that simulations memory allocation failure or
         1041  +** tracks memory usage, for example.</dd>
         1042  +**
  1035   1043   ** <dt>SQLITE_CONFIG_MEMSTATUS</dt>
  1036   1044   ** <dd>This option takes single boolean argument which enables or disables
  1037   1045   ** the collection of memory allocation statistics.  When disabled, the
  1038   1046   ** following SQLite interfaces become non-operational:
  1039   1047   **   <ul>
  1040   1048   **   <li> [sqlite3_memory_used()]
  1041   1049   **   <li> [sqlite3_memory_highwater()]
  1042   1050   **   <li> [sqlite3_soft_heap_limit()]
  1043   1051   **   <li> sqlite3_memory_status()
  1044   1052   **   </ul>
  1045   1053   ** </dd>
         1054  +**
         1055  +** <dt>SQLITE_CONFIG_SCRATCH</dt>
         1056  +** <dd>This option specifies a static memory buffer that SQLite can use for
         1057  +** scratch memory.  There are three arguments:  A pointer to the memory, the
         1058  +** size of each scratch buffer (sz), and the number of buffers (N).  The first
         1059  +** argument should point to an allocation of at least sz*N bytes of memory.
         1060  +** SQLite will use no more than one scratch buffer at once per thread, so
         1061  +** N should be set to the expected maximum number of threads.  The sz 
         1062  +** parameter should be 6 times the size of the largest database page size.
         1063  +** Scratch buffers are used as part of the btree balance operation.  If
         1064  +** The btree balancer needs additional memory beyond what is provided by
         1065  +** scratch buffers or if no scratch buffer space is specified, then SQLite
         1066  +** goes to [sqlite3_malloc()] to obtain the memory it needs.
         1067  +** </dd>
         1068  +**
         1069  +** <dt>SQLITE_CONFIG_PAGECACHE</dt>
         1070  +** <dd>This option specifies a static memory buffer that SQLite can use for
         1071  +** the database page cache.  There are three arguments:
         1072  +** A pointer to the memory, the
         1073  +** size of each page buffer (sz), and the number of pages (N).  The first
         1074  +** argument should point to an allocation of at least sz*N bytes of memory.
         1075  +** SQLite will use the memory provided by the first argument to satisfy
         1076  +** its memory needs for the first N pages that it adds to cache.  If 
         1077  +** additional page cache memory is needed beyond what is provided by
         1078  +** this option, then SQLite goes to [sqlite3_malloc()] for the additional
         1079  +** storage space.
         1080  +** </dd>
         1081  +**
         1082  +** <dt>SQLITE_CONFIG_HEAP</dt>
         1083  +** <dd>This option specifies a static memory buffer that SQLite will use
         1084  +** for all of its dynamic memory allocation needs beyond those provided
         1085  +** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
         1086  +** There are three arguments: A pointer to the memory, the number of
         1087  +** bytes in the memory buffer, and the minimum allocation size.  When
         1088  +** this configuration option is used, SQLite never calls the system
         1089  +** malloc() implementation but instead uses the supplied memory buffer
         1090  +** to satisfy all [sqlite3_malloc()] requests.
         1091  +** </dd>
         1092  +**
         1093  +** <dt>SQLITE_CONFIG_MUTEX</dt>
         1094  +** <dd>This option takes a single argument which is a pointer to an
         1095  +** instance of the [sqlite3_mutex_methods] structure.  The argument specifics
         1096  +** alternative low-level mutex routines to be used in place
         1097  +** the mutex routines built into SQLite.</dd>
         1098  +**
         1099  +** <dt>SQLITE_CONFIG_GETMALLOC</dt>
         1100  +** <dd>This option takes a single argument which is a pointer to an
         1101  +** instance of the [sqlite3_mutex_methods] structure.  The
         1102  +** [sqlite3_mutex_methods]
         1103  +** structure is filled with the currently defined mutex routines.
         1104  +** This option can be used to overload the default mutex allocation
         1105  +** routines with a wrapper used to track mutex usage for performance
         1106  +** profiling or testing, for example.</dd>
         1107  +**
  1046   1108   ** </dl>
  1047   1109   */ 
  1048   1110   #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
  1049   1111   #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
  1050   1112   #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
  1051   1113   #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
  1052         -#define SQLITE_CONFIG_MEMSTATUS     5  /* boolean */
  1053         -#define SQLITE_CONFIG_MUTEX         6  /* sqlite3_mutex_methods* */
  1054         -#define SQLITE_CONFIG_GETMUTEX      7  /* sqlite3_mutex_methods* */
  1055         -
  1056         -/* These options are to be added later.  Currently unused and undocumented. */
  1057         -#define SQLITE_CONFIG_HEAP          8  /* void*, int64, min, max, tmp */
         1114  +#define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
         1115  +#define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
         1116  +#define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
         1117  +#define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
         1118  +#define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
         1119  +#define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
         1120  +#define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
  1058   1121   
  1059   1122   
  1060   1123   /*
  1061   1124   ** CAPI3REF: Enable Or Disable Extended Result Codes {F12200}
  1062   1125   **
  1063   1126   ** The sqlite3_extended_result_codes() routine enables or disables the
  1064   1127   ** [SQLITE_IOERR_READ | extended result codes] feature of SQLite.

Changes to src/sqliteInt.h.

     7      7   **    May you do good and not evil.
     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   ** Internal interface definitions for SQLite.
    13     13   **
    14         -** @(#) $Id: sqliteInt.h,v 1.712 2008/06/17 17:21:18 danielk1977 Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.713 2008/06/18 13:27:47 drh Exp $
    15     15   */
    16     16   #ifndef _SQLITEINT_H_
    17     17   #define _SQLITEINT_H_
    18     18   
    19     19   /*
    20     20   ** Include the configuration header output by 'configure' if we're using the
    21     21   ** autoconf-based build
................................................................................
  1729   1729     int iDb;            /* 0 for main database.  1 for TEMP, 2.. for ATTACHed */
  1730   1730     char **pzErrMsg;    /* Error message stored here */
  1731   1731     int rc;             /* Result code stored here */
  1732   1732   } InitData;
  1733   1733   
  1734   1734   /*
  1735   1735   ** Structure containing global configuration data for the SQLite library.
         1736  +**
         1737  +** This structure also contains some state information.
  1736   1738   */
  1737   1739   struct Sqlite3Config {
  1738   1740     int bMemstat;                     /* True to enable memory status */
  1739   1741     int bCoreMutex;                   /* True to enable core mutexing */
  1740   1742     int bFullMutex;                   /* True to enable full mutexing */
  1741   1743     sqlite3_mem_methods m;            /* Low-level memory allocation interface */
  1742   1744     sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
  1743   1745     void *pHeap;                      /* Heap storage space */
  1744         -  sqlite3_int64 nHeap;              /* Size of pHeap[] */
  1745         -  int mnReq, mxReq;                 /* Min and max memory request sizes */
  1746         -  int nTemp;                        /* Part of pHeap for temporary allos */
         1746  +  int nHeap;                        /* Size of pHeap[] */
         1747  +  int mnReq, mxReq;                 /* Min and max heap requests sizes */
         1748  +  void *pScratch;                   /* Scratch memory */
         1749  +  int szScratch;                    /* Size of each scratch buffer */
         1750  +  int nScratch;                     /* Number of scratch buffers */
         1751  +  void *pPage;                      /* Page cache memory */
         1752  +  int szPage;                       /* Size of each page in pPage[] */
         1753  +  int nPage;                        /* Number of pages in pPage[] */
  1747   1754   };
  1748   1755   
  1749   1756   /*
  1750   1757   ** Assuming zIn points to the first byte of a UTF-8 character,
  1751   1758   ** advance zIn to point to the first byte of the next UTF-8 character.
  1752   1759   */
  1753   1760   #define SQLITE_SKIP_UTF8(zIn) {                        \