/ Check-in [c20f7563]
Login

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

Overview
Comment:Avoid attempting to reclaim memory from in-memory databases in sqlite3_release_memory(). (CVS 3812)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: c20f7563c0ffa1df47df5464f1f1cc4703ffa9b4
User & Date: danielk1977 2007-04-05 13:12:14
Context
2007-04-05
14:29
Use the MEMDB macro instead of OMIT_MEMORYDB in pager_recycle(). (CVS 3813) check-in: 97c51598 user: danielk1977 tags: trunk
13:12
Avoid attempting to reclaim memory from in-memory databases in sqlite3_release_memory(). (CVS 3812) check-in: c20f7563 user: danielk1977 tags: trunk
11:54
Add some assert() statements to pager.c. (CVS 3811) check-in: 973b2a5f user: danielk1977 tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/pager.c.

    14     14   ** The pager is used to access a database disk file.  It implements
    15     15   ** atomic commit and rollback through the use of a journal file that
    16     16   ** is separate from the database file.  The pager also implements file
    17     17   ** locking to prevent two processes from writing the same database
    18     18   ** file simultaneously, or one process from reading the database while
    19     19   ** another is writing.
    20     20   **
    21         -** @(#) $Id: pager.c,v 1.320 2007/04/05 11:54:43 danielk1977 Exp $
           21  +** @(#) $Id: pager.c,v 1.321 2007/04/05 13:12:14 danielk1977 Exp $
    22     22   */
    23     23   #ifndef SQLITE_OMIT_DISKIO
    24     24   #include "sqliteInt.h"
    25     25   #include "os.h"
    26     26   #include "pager.h"
    27     27   #include <assert.h>
    28     28   #include <string.h>
................................................................................
  2520   2520   **
  2521   2521   ** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It 
  2522   2522   ** does not set the pPager->errCode variable.
  2523   2523   */
  2524   2524   static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){
  2525   2525     PgHdr *pPg;
  2526   2526     *ppPg = 0;
         2527  +
         2528  +  assert(!MEMDB);
  2527   2529   
  2528   2530     /* Find a page to recycle.  Try to locate a page that does not
  2529   2531     ** require us to do an fsync() on the journal.
  2530   2532     */
  2531   2533     pPg = pPager->pFirstSynced;
  2532   2534   
  2533   2535     /* If we could not find a page that does not require an fsync()
................................................................................
  2564   2566     assert( pPg->nRef==0 );
  2565   2567   
  2566   2568     /* Write the page to the database file if it is dirty.
  2567   2569     */
  2568   2570     if( pPg->dirty ){
  2569   2571       int rc;
  2570   2572       assert( pPg->needSync==0 );
  2571         -    assert( !MEMDB );
  2572   2573       makeClean(pPg);
  2573   2574       pPg->dirty = 1;
  2574   2575       pPg->pDirty = 0;
  2575   2576       rc = pager_write_pagelist( pPg );
  2576   2577       if( rc!=SQLITE_OK ){
  2577   2578         return rc;
  2578   2579       }
................................................................................
  2635   2636     */
  2636   2637     for(i=0; i<=1; i++){
  2637   2638   
  2638   2639       /* Loop through all the SQLite pagers opened by the current thread. */
  2639   2640       for(p=pTsdro->pPager; p && (nReq<0 || nReleased<nReq); p=p->pNext){
  2640   2641         PgHdr *pPg;
  2641   2642         int rc;
         2643  +
         2644  +#ifndef SQLITE_OMIT_MEMORYDB
         2645  +      if( p->memDb ){
         2646  +        continue;
         2647  +      }
         2648  +#endif
  2642   2649   
  2643   2650         /* For each pager, try to free as many pages as possible (without 
  2644   2651         ** calling fsync() if this is the first iteration of the outermost 
  2645   2652         ** loop).
  2646   2653         */
  2647   2654         while( SQLITE_OK==(rc = pager_recycle(p, i, &pPg)) && pPg) {
  2648   2655           /* We've found a page to free. At this point the page has been 
................................................................................
  2838   2845   /*
  2839   2846   ** Allocate or recycle space for a single page.
  2840   2847   */
  2841   2848   static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
  2842   2849     int rc = SQLITE_OK;
  2843   2850     PgHdr *pPg;
  2844   2851   
  2845         -  if( !(pPager->pFirstSynced && pPager->pFirstSynced->pgno==0) && (
  2846         -      pPager->nPage<pPager->mxPage || pPager->pFirst==0 || MEMDB ||
         2852  +  if( MEMDB || (!(pPager->pFirstSynced && pPager->pFirstSynced->pgno==0) && (
         2853  +      pPager->nPage<pPager->mxPage || pPager->pFirst==0 || 
  2847   2854         (pPager->pFirstSynced==0 && pPager->doNotSync)
  2848         -  ) ){
         2855  +  )) ){
  2849   2856       /* Create a new page */
  2850   2857       if( pPager->nPage>=pPager->nHash ){
  2851   2858         pager_resize_hash_table(pPager,
  2852   2859            pPager->nHash<256 ? 256 : pPager->nHash*2);
  2853   2860         if( pPager->nHash==0 ){
  2854   2861           rc = SQLITE_NOMEM;
  2855   2862           goto pager_allocate_out;

Changes to test/malloc5.test.

     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   #
    12     12   # This file contains test cases focused on the two memory-management APIs, 
    13     13   # sqlite3_soft_heap_limit() and sqlite3_release_memory().
    14     14   #
    15         -# $Id: malloc5.test,v 1.8 2007/04/02 05:07:48 danielk1977 Exp $
           15  +# $Id: malloc5.test,v 1.9 2007/04/05 13:12:14 danielk1977 Exp $
    16     16   
    17     17   #---------------------------------------------------------------------------
    18     18   # NOTES ON EXPECTED BEHAVIOUR
    19     19   #
    20     20   #---------------------------------------------------------------------------
    21     21   
    22     22   
................................................................................
   217    217     execsql {
   218    218       SELECT count(*), sum(a), sum(b) FROM abc;
   219    219     }
   220    220   } [list 20000 [expr int(20000.0 * 4999.5)] [expr int(20000.0 * 4999.5)]]
   221    221   
   222    222   # Restore the soft heap limit.
   223    223   sqlite3_soft_heap_limit $::soft_limit
   224         -finish_test
          224  +
          225  +# Test that there are no problems calling sqlite3_release_memory when
          226  +# there are open in-memory databases.
          227  +#
          228  +# At one point these tests would cause a seg-fault.
          229  +#
          230  +do_test malloc5-5.1 {
          231  +  db close
          232  +  sqlite3 db :memory:
          233  +  execsql {
          234  +    BEGIN;
          235  +    CREATE TABLE abc(a, b, c);
          236  +    INSERT INTO abc VALUES('abcdefghi', 1234567890, NULL);
          237  +    INSERT INTO abc SELECT * FROM abc;
          238  +    INSERT INTO abc SELECT * FROM abc;
          239  +    INSERT INTO abc SELECT * FROM abc;
          240  +    INSERT INTO abc SELECT * FROM abc;
          241  +    INSERT INTO abc SELECT * FROM abc;
          242  +    INSERT INTO abc SELECT * FROM abc;
          243  +    INSERT INTO abc SELECT * FROM abc;
          244  +  }
          245  +  sqlite3_release_memory
          246  +} 0
          247  +do_test malloc5-5.1 {
          248  +  sqlite3_soft_heap_limit 5000
          249  +  execsql {
          250  +    COMMIT;
          251  +    PRAGMA temp_store = memory;
          252  +    SELECT * FROM abc ORDER BY a;
          253  +  }
          254  +  expr 1
          255  +} {1}
   225    256   
          257  +sqlite3_soft_heap_limit $::soft_limit
          258  +finish_test
   226    259   catch {db close}
   227    260