/ Check-in [d7dae06f]
Login

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

Overview
Comment:Fix issue with mem5 allocator when min request size is larger thatn 2^30.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: d7dae06fb2d57ed6b9555b774712f42077ae4155
User & Date: shaneh 2011-03-09 21:36:17
Context
2011-03-10
03:54
Minor clean-up of previous mem5 allocator fix. check-in: 36438423 user: shaneh tags: trunk
2011-03-09
21:36
Fix issue with mem5 allocator when min request size is larger thatn 2^30. check-in: d7dae06f user: shaneh tags: trunk
21:02
Omit unnecessary OP_Next and OP_Prev operators when uniqueness constraints guarantee that the code will only make one pass through the loop. check-in: f000c9b2 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/main.c.

   370    370   
   371    371   #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
   372    372       case SQLITE_CONFIG_HEAP: {
   373    373         /* Designate a buffer for heap memory space */
   374    374         sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
   375    375         sqlite3GlobalConfig.nHeap = va_arg(ap, int);
   376    376         sqlite3GlobalConfig.mnReq = va_arg(ap, int);
          377  +
          378  +      if( sqlite3GlobalConfig.mnReq<1 ){
          379  +        sqlite3GlobalConfig.mnReq = 1;
          380  +      }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
          381  +        /* cap min request size at 2^12 */
          382  +        sqlite3GlobalConfig.mnReq = (1<<12);
          383  +      }
   377    384   
   378    385         if( sqlite3GlobalConfig.pHeap==0 ){
   379    386           /* If the heap pointer is NULL, then restore the malloc implementation
   380    387           ** back to NULL pointers too.  This will cause the malloc to go
   381    388           ** back to its default implementation when sqlite3_initialize() is
   382    389           ** run.
   383    390           */

Changes to src/mem5.c.

   438    438   **             memsys5Log(4) -> 2
   439    439   **             memsys5Log(5) -> 3
   440    440   **             memsys5Log(8) -> 3
   441    441   **             memsys5Log(9) -> 4
   442    442   */
   443    443   static int memsys5Log(int iValue){
   444    444     int iLog;
   445         -  for(iLog=0; (1<<iLog)<iValue; iLog++);
          445  +  for(iLog=0; (iLog<((sizeof(int)*8)-2)) && (1<<iLog)<iValue; iLog++);
   446    446     return iLog;
   447    447   }
   448    448   
   449    449   /*
   450    450   ** Initialize the memory allocator.
   451    451   **
   452    452   ** This routine is not threadsafe.  The caller must be holding a mutex
................................................................................
   469    469     */
   470    470     assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
   471    471   
   472    472     nByte = sqlite3GlobalConfig.nHeap;
   473    473     zByte = (u8*)sqlite3GlobalConfig.pHeap;
   474    474     assert( zByte!=0 );  /* sqlite3_config() does not allow otherwise */
   475    475   
   476         -  nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
          476  +  /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
          477  +  nMinLog = sqlite3GlobalConfig.mnReq;
          478  +  assert( nMinLog>0 && nMinLog<=(1<<12) );
          479  +  nMinLog = memsys5Log(nMinLog);
   477    480     mem5.szAtom = (1<<nMinLog);
   478    481     while( (int)sizeof(Mem5Link)>mem5.szAtom ){
   479    482       mem5.szAtom = mem5.szAtom << 1;
   480    483     }
   481    484   
   482    485     mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
   483    486     mem5.zPool = zByte;

Changes to src/sqlite.h.in.

  1356   1356   ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
  1357   1357   ** to using its default memory allocator (the system malloc() implementation),
  1358   1358   ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  ^If the
  1359   1359   ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
  1360   1360   ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
  1361   1361   ** allocator is engaged to handle all of SQLites memory allocation needs.
  1362   1362   ** The first pointer (the memory pointer) must be aligned to an 8-byte
  1363         -** boundary or subsequent behavior of SQLite will be undefined.</dd>
         1363  +** boundary or subsequent behavior of SQLite will be undefined.
         1364  +** The minimum allocation size is capped at 2^12. Reasonable values
         1365  +** for the minimum allocation size are 2^5 through 2^8.</dd>
  1364   1366   **
  1365   1367   ** <dt>SQLITE_CONFIG_MUTEX</dt>
  1366   1368   ** <dd> ^(This option takes a single argument which is a pointer to an
  1367   1369   ** instance of the [sqlite3_mutex_methods] structure.  The argument specifies
  1368   1370   ** alternative low-level mutex routines to be used in place
  1369   1371   ** the mutex routines built into SQLite.)^  ^SQLite makes a copy of the
  1370   1372   ** content of the [sqlite3_mutex_methods] structure before the call to

Added test/mem5.test.

            1  +# 2011 March 9
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# This file contains tests of the mem5 allocation subsystem.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +
           18  +ifcapable !mem5 {
           19  +  finish_test
           20  +  return
           21  +}
           22  +
           23  +# The tests in this file configure the lookaside allocator after a 
           24  +# connection is opened. This will not work if there is any "presql"
           25  +# configured (SQL run within the [sqlite3] wrapper in tester.tcl).
           26  +if {[info exists ::G(perm:presql)]} {
           27  +  finish_test
           28  +  return
           29  +}
           30  +
           31  +do_test mem5-1.1 {
           32  +  catch {db close}
           33  +  sqlite3_shutdown
           34  +  sqlite3_config_heap 25000000 0
           35  +  sqlite3_config_lookaside 0 0
           36  +  sqlite3_initialize
           37  +} {SQLITE_OK}
           38  +
           39  +# try with min request size = 2^30
           40  +do_test mem5-1.2 {
           41  +  catch {db close}
           42  +  sqlite3_shutdown
           43  +  sqlite3_config_heap 1 1073741824
           44  +  sqlite3_config_lookaside 0 0
           45  +  sqlite3_initialize
           46  +} {SQLITE_NOMEM}
           47  +
           48  +# try with min request size = 2^30+1
           49  +# previously this was causing the memsys5Log() func to infinitely loop.
           50  +do_test mem5-1.3 {
           51  +  catch {db close}
           52  +  sqlite3_shutdown
           53  +  sqlite3_config_heap 1 1073741825
           54  +  sqlite3_config_lookaside 0 0
           55  +  sqlite3_initialize
           56  +} {SQLITE_NOMEM}
           57  +
           58  +do_test mem5-1.4 {
           59  +  catch {db close}
           60  +  sqlite3_shutdown
           61  +  sqlite3_config_heap 0 0
           62  +  sqlite3_config_lookaside 0 0
           63  +  sqlite3_initialize
           64  +} {SQLITE_OK}
           65  +
           66  +finish_test