mem3.c

Go to the documentation of this file.
00001 /*
00002 ** 2007 October 14
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 ** This file contains the C functions that implement a memory
00013 ** allocation subsystem for use by SQLite. 
00014 **
00015 ** This version of the memory allocation subsystem omits all
00016 ** use of malloc(). The SQLite user supplies a block of memory
00017 ** before calling sqlite3_initialize() from which allocations
00018 ** are made and returned by the xMalloc() and xRealloc() 
00019 ** implementations. Once sqlite3_initialize() has been called,
00020 ** the amount of memory available to SQLite is fixed and cannot
00021 ** be changed.
00022 **
00023 ** This version of the memory allocation subsystem is included
00024 ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
00025 **
00026 ** $Id: mem3.c,v 1.23 2008/09/02 17:52:52 danielk1977 Exp $
00027 */
00028 #include "sqliteInt.h"
00029 
00030 /*
00031 ** This version of the memory allocator is only built into the library
00032 ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
00033 ** mean that the library will use a memory-pool by default, just that
00034 ** it is available. The mempool allocator is activated by calling
00035 ** sqlite3_config().
00036 */
00037 #ifdef SQLITE_ENABLE_MEMSYS3
00038 
00039 /*
00040 ** Maximum size (in Mem3Blocks) of a "small" chunk.
00041 */
00042 #define MX_SMALL 10
00043 
00044 
00045 /*
00046 ** Number of freelist hash slots
00047 */
00048 #define N_HASH  61
00049 
00050 /*
00051 ** A memory allocation (also called a "chunk") consists of two or 
00052 ** more blocks where each block is 8 bytes.  The first 8 bytes are 
00053 ** a header that is not returned to the user.
00054 **
00055 ** A chunk is two or more blocks that is either checked out or
00056 ** free.  The first block has format u.hdr.  u.hdr.size4x is 4 times the
00057 ** size of the allocation in blocks if the allocation is free.
00058 ** The u.hdr.size4x&1 bit is true if the chunk is checked out and
00059 ** false if the chunk is on the freelist.  The u.hdr.size4x&2 bit
00060 ** is true if the previous chunk is checked out and false if the
00061 ** previous chunk is free.  The u.hdr.prevSize field is the size of
00062 ** the previous chunk in blocks if the previous chunk is on the
00063 ** freelist. If the previous chunk is checked out, then
00064 ** u.hdr.prevSize can be part of the data for that chunk and should
00065 ** not be read or written.
00066 **
00067 ** We often identify a chunk by its index in mem3.aPool[].  When
00068 ** this is done, the chunk index refers to the second block of
00069 ** the chunk.  In this way, the first chunk has an index of 1.
00070 ** A chunk index of 0 means "no such chunk" and is the equivalent
00071 ** of a NULL pointer.
00072 **
00073 ** The second block of free chunks is of the form u.list.  The
00074 ** two fields form a double-linked list of chunks of related sizes.
00075 ** Pointers to the head of the list are stored in mem3.aiSmall[] 
00076 ** for smaller chunks and mem3.aiHash[] for larger chunks.
00077 **
00078 ** The second block of a chunk is user data if the chunk is checked 
00079 ** out.  If a chunk is checked out, the user data may extend into
00080 ** the u.hdr.prevSize value of the following chunk.
00081 */
00082 typedef struct Mem3Block Mem3Block;
00083 struct Mem3Block {
00084   union {
00085     struct {
00086       u32 prevSize;   /* Size of previous chunk in Mem3Block elements */
00087       u32 size4x;     /* 4x the size of current chunk in Mem3Block elements */
00088     } hdr;
00089     struct {
00090       u32 next;       /* Index in mem3.aPool[] of next free chunk */
00091       u32 prev;       /* Index in mem3.aPool[] of previous free chunk */
00092     } list;
00093   } u;
00094 };
00095 
00096 /*
00097 ** All of the static variables used by this module are collected
00098 ** into a single structure named "mem3".  This is to keep the
00099 ** static variables organized and to reduce namespace pollution
00100 ** when this module is combined with other in the amalgamation.
00101 */
00102 static SQLITE_WSD struct Mem3Global {
00103   /*
00104   ** Memory available for allocation. nPool is the size of the array
00105   ** (in Mem3Blocks) pointed to by aPool less 2.
00106   */
00107   u32 nPool;
00108   Mem3Block *aPool;
00109 
00110   /*
00111   ** True if we are evaluating an out-of-memory callback.
00112   */
00113   int alarmBusy;
00114   
00115   /*
00116   ** Mutex to control access to the memory allocation subsystem.
00117   */
00118   sqlite3_mutex *mutex;
00119   
00120   /*
00121   ** The minimum amount of free space that we have seen.
00122   */
00123   u32 mnMaster;
00124 
00125   /*
00126   ** iMaster is the index of the master chunk.  Most new allocations
00127   ** occur off of this chunk.  szMaster is the size (in Mem3Blocks)
00128   ** of the current master.  iMaster is 0 if there is not master chunk.
00129   ** The master chunk is not in either the aiHash[] or aiSmall[].
00130   */
00131   u32 iMaster;
00132   u32 szMaster;
00133 
00134   /*
00135   ** Array of lists of free blocks according to the block size 
00136   ** for smaller chunks, or a hash on the block size for larger
00137   ** chunks.
00138   */
00139   u32 aiSmall[MX_SMALL-1];   /* For sizes 2 through MX_SMALL, inclusive */
00140   u32 aiHash[N_HASH];        /* For sizes MX_SMALL+1 and larger */
00141 } mem3 = { 97535575 };
00142 
00143 #define mem3 GLOBAL(struct Mem3Global, mem3)
00144 
00145 /*
00146 ** Unlink the chunk at mem3.aPool[i] from list it is currently
00147 ** on.  *pRoot is the list that i is a member of.
00148 */
00149 static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
00150   u32 next = mem3.aPool[i].u.list.next;
00151   u32 prev = mem3.aPool[i].u.list.prev;
00152   assert( sqlite3_mutex_held(mem3.mutex) );
00153   if( prev==0 ){
00154     *pRoot = next;
00155   }else{
00156     mem3.aPool[prev].u.list.next = next;
00157   }
00158   if( next ){
00159     mem3.aPool[next].u.list.prev = prev;
00160   }
00161   mem3.aPool[i].u.list.next = 0;
00162   mem3.aPool[i].u.list.prev = 0;
00163 }
00164 
00165 /*
00166 ** Unlink the chunk at index i from 
00167 ** whatever list is currently a member of.
00168 */
00169 static void memsys3Unlink(u32 i){
00170   u32 size, hash;
00171   assert( sqlite3_mutex_held(mem3.mutex) );
00172   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
00173   assert( i>=1 );
00174   size = mem3.aPool[i-1].u.hdr.size4x/4;
00175   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
00176   assert( size>=2 );
00177   if( size <= MX_SMALL ){
00178     memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
00179   }else{
00180     hash = size % N_HASH;
00181     memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
00182   }
00183 }
00184 
00185 /*
00186 ** Link the chunk at mem3.aPool[i] so that is on the list rooted
00187 ** at *pRoot.
00188 */
00189 static void memsys3LinkIntoList(u32 i, u32 *pRoot){
00190   assert( sqlite3_mutex_held(mem3.mutex) );
00191   mem3.aPool[i].u.list.next = *pRoot;
00192   mem3.aPool[i].u.list.prev = 0;
00193   if( *pRoot ){
00194     mem3.aPool[*pRoot].u.list.prev = i;
00195   }
00196   *pRoot = i;
00197 }
00198 
00199 /*
00200 ** Link the chunk at index i into either the appropriate
00201 ** small chunk list, or into the large chunk hash table.
00202 */
00203 static void memsys3Link(u32 i){
00204   u32 size, hash;
00205   assert( sqlite3_mutex_held(mem3.mutex) );
00206   assert( i>=1 );
00207   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
00208   size = mem3.aPool[i-1].u.hdr.size4x/4;
00209   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
00210   assert( size>=2 );
00211   if( size <= MX_SMALL ){
00212     memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
00213   }else{
00214     hash = size % N_HASH;
00215     memsys3LinkIntoList(i, &mem3.aiHash[hash]);
00216   }
00217 }
00218 
00219 /*
00220 ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
00221 ** will already be held (obtained by code in malloc.c) if
00222 ** sqlite3GlobalConfig.bMemStat is true.
00223 */
00224 static void memsys3Enter(void){
00225   if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
00226     mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
00227   }
00228   sqlite3_mutex_enter(mem3.mutex);
00229 }
00230 static void memsys3Leave(void){
00231   sqlite3_mutex_leave(mem3.mutex);
00232 }
00233 
00234 /*
00235 ** Called when we are unable to satisfy an allocation of nBytes.
00236 */
00237 static void memsys3OutOfMemory(int nByte){
00238   if( !mem3.alarmBusy ){
00239     mem3.alarmBusy = 1;
00240     assert( sqlite3_mutex_held(mem3.mutex) );
00241     sqlite3_mutex_leave(mem3.mutex);
00242     sqlite3_release_memory(nByte);
00243     sqlite3_mutex_enter(mem3.mutex);
00244     mem3.alarmBusy = 0;
00245   }
00246 }
00247 
00248 
00249 /*
00250 ** Chunk i is a free chunk that has been unlinked.  Adjust its 
00251 ** size parameters for check-out and return a pointer to the 
00252 ** user portion of the chunk.
00253 */
00254 static void *memsys3Checkout(u32 i, int nBlock){
00255   u32 x;
00256   assert( sqlite3_mutex_held(mem3.mutex) );
00257   assert( i>=1 );
00258   assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
00259   assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
00260   x = mem3.aPool[i-1].u.hdr.size4x;
00261   mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
00262   mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
00263   mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
00264   return &mem3.aPool[i];
00265 }
00266 
00267 /*
00268 ** Carve a piece off of the end of the mem3.iMaster free chunk.
00269 ** Return a pointer to the new allocation.  Or, if the master chunk
00270 ** is not large enough, return 0.
00271 */
00272 static void *memsys3FromMaster(int nBlock){
00273   assert( sqlite3_mutex_held(mem3.mutex) );
00274   assert( mem3.szMaster>=nBlock );
00275   if( nBlock>=mem3.szMaster-1 ){
00276     /* Use the entire master */
00277     void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
00278     mem3.iMaster = 0;
00279     mem3.szMaster = 0;
00280     mem3.mnMaster = 0;
00281     return p;
00282   }else{
00283     /* Split the master block.  Return the tail. */
00284     u32 newi, x;
00285     newi = mem3.iMaster + mem3.szMaster - nBlock;
00286     assert( newi > mem3.iMaster+1 );
00287     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
00288     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
00289     mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
00290     mem3.szMaster -= nBlock;
00291     mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
00292     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
00293     mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
00294     if( mem3.szMaster < mem3.mnMaster ){
00295       mem3.mnMaster = mem3.szMaster;
00296     }
00297     return (void*)&mem3.aPool[newi];
00298   }
00299 }
00300 
00301 /*
00302 ** *pRoot is the head of a list of free chunks of the same size
00303 ** or same size hash.  In other words, *pRoot is an entry in either
00304 ** mem3.aiSmall[] or mem3.aiHash[].  
00305 **
00306 ** This routine examines all entries on the given list and tries
00307 ** to coalesce each entries with adjacent free chunks.  
00308 **
00309 ** If it sees a chunk that is larger than mem3.iMaster, it replaces 
00310 ** the current mem3.iMaster with the new larger chunk.  In order for
00311 ** this mem3.iMaster replacement to work, the master chunk must be
00312 ** linked into the hash tables.  That is not the normal state of
00313 ** affairs, of course.  The calling routine must link the master
00314 ** chunk before invoking this routine, then must unlink the (possibly
00315 ** changed) master chunk once this routine has finished.
00316 */
00317 static void memsys3Merge(u32 *pRoot){
00318   u32 iNext, prev, size, i, x;
00319 
00320   assert( sqlite3_mutex_held(mem3.mutex) );
00321   for(i=*pRoot; i>0; i=iNext){
00322     iNext = mem3.aPool[i].u.list.next;
00323     size = mem3.aPool[i-1].u.hdr.size4x;
00324     assert( (size&1)==0 );
00325     if( (size&2)==0 ){
00326       memsys3UnlinkFromList(i, pRoot);
00327       assert( i > mem3.aPool[i-1].u.hdr.prevSize );
00328       prev = i - mem3.aPool[i-1].u.hdr.prevSize;
00329       if( prev==iNext ){
00330         iNext = mem3.aPool[prev].u.list.next;
00331       }
00332       memsys3Unlink(prev);
00333       size = i + size/4 - prev;
00334       x = mem3.aPool[prev-1].u.hdr.size4x & 2;
00335       mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
00336       mem3.aPool[prev+size-1].u.hdr.prevSize = size;
00337       memsys3Link(prev);
00338       i = prev;
00339     }else{
00340       size /= 4;
00341     }
00342     if( size>mem3.szMaster ){
00343       mem3.iMaster = i;
00344       mem3.szMaster = size;
00345     }
00346   }
00347 }
00348 
00349 /*
00350 ** Return a block of memory of at least nBytes in size.
00351 ** Return NULL if unable.
00352 **
00353 ** This function assumes that the necessary mutexes, if any, are
00354 ** already held by the caller. Hence "Unsafe".
00355 */
00356 static void *memsys3MallocUnsafe(int nByte){
00357   u32 i;
00358   int nBlock;
00359   int toFree;
00360 
00361   assert( sqlite3_mutex_held(mem3.mutex) );
00362   assert( sizeof(Mem3Block)==8 );
00363   if( nByte<=12 ){
00364     nBlock = 2;
00365   }else{
00366     nBlock = (nByte + 11)/8;
00367   }
00368   assert( nBlock>=2 );
00369 
00370   /* STEP 1:
00371   ** Look for an entry of the correct size in either the small
00372   ** chunk table or in the large chunk hash table.  This is
00373   ** successful most of the time (about 9 times out of 10).
00374   */
00375   if( nBlock <= MX_SMALL ){
00376     i = mem3.aiSmall[nBlock-2];
00377     if( i>0 ){
00378       memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
00379       return memsys3Checkout(i, nBlock);
00380     }
00381   }else{
00382     int hash = nBlock % N_HASH;
00383     for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
00384       if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
00385         memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
00386         return memsys3Checkout(i, nBlock);
00387       }
00388     }
00389   }
00390 
00391   /* STEP 2:
00392   ** Try to satisfy the allocation by carving a piece off of the end
00393   ** of the master chunk.  This step usually works if step 1 fails.
00394   */
00395   if( mem3.szMaster>=nBlock ){
00396     return memsys3FromMaster(nBlock);
00397   }
00398 
00399 
00400   /* STEP 3:  
00401   ** Loop through the entire memory pool.  Coalesce adjacent free
00402   ** chunks.  Recompute the master chunk as the largest free chunk.
00403   ** Then try again to satisfy the allocation by carving a piece off
00404   ** of the end of the master chunk.  This step happens very
00405   ** rarely (we hope!)
00406   */
00407   for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
00408     memsys3OutOfMemory(toFree);
00409     if( mem3.iMaster ){
00410       memsys3Link(mem3.iMaster);
00411       mem3.iMaster = 0;
00412       mem3.szMaster = 0;
00413     }
00414     for(i=0; i<N_HASH; i++){
00415       memsys3Merge(&mem3.aiHash[i]);
00416     }
00417     for(i=0; i<MX_SMALL-1; i++){
00418       memsys3Merge(&mem3.aiSmall[i]);
00419     }
00420     if( mem3.szMaster ){
00421       memsys3Unlink(mem3.iMaster);
00422       if( mem3.szMaster>=nBlock ){
00423         return memsys3FromMaster(nBlock);
00424       }
00425     }
00426   }
00427 
00428   /* If none of the above worked, then we fail. */
00429   return 0;
00430 }
00431 
00432 /*
00433 ** Free an outstanding memory allocation.
00434 **
00435 ** This function assumes that the necessary mutexes, if any, are
00436 ** already held by the caller. Hence "Unsafe".
00437 */
00438 void memsys3FreeUnsafe(void *pOld){
00439   Mem3Block *p = (Mem3Block*)pOld;
00440   int i;
00441   u32 size, x;
00442   assert( sqlite3_mutex_held(mem3.mutex) );
00443   assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
00444   i = p - mem3.aPool;
00445   assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
00446   size = mem3.aPool[i-1].u.hdr.size4x/4;
00447   assert( i+size<=mem3.nPool+1 );
00448   mem3.aPool[i-1].u.hdr.size4x &= ~1;
00449   mem3.aPool[i+size-1].u.hdr.prevSize = size;
00450   mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
00451   memsys3Link(i);
00452 
00453   /* Try to expand the master using the newly freed chunk */
00454   if( mem3.iMaster ){
00455     while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
00456       size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
00457       mem3.iMaster -= size;
00458       mem3.szMaster += size;
00459       memsys3Unlink(mem3.iMaster);
00460       x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
00461       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
00462       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
00463     }
00464     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
00465     while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
00466       memsys3Unlink(mem3.iMaster+mem3.szMaster);
00467       mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
00468       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
00469       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
00470     }
00471   }
00472 }
00473 
00474 /*
00475 ** Return the size of an outstanding allocation, in bytes.  The
00476 ** size returned omits the 8-byte header overhead.  This only
00477 ** works for chunks that are currently checked out.
00478 */
00479 static int memsys3Size(void *p){
00480   Mem3Block *pBlock;
00481   if( p==0 ) return 0;
00482   pBlock = (Mem3Block*)p;
00483   assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
00484   return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
00485 }
00486 
00487 /*
00488 ** Round up a request size to the next valid allocation size.
00489 */
00490 static int memsys3Roundup(int n){
00491   if( n<=12 ){
00492     return 12;
00493   }else{
00494     return ((n+11)&~7) - 4;
00495   }
00496 }
00497 
00498 /*
00499 ** Allocate nBytes of memory.
00500 */
00501 static void *memsys3Malloc(int nBytes){
00502   sqlite3_int64 *p;
00503   assert( nBytes>0 );          /* malloc.c filters out 0 byte requests */
00504   memsys3Enter();
00505   p = memsys3MallocUnsafe(nBytes);
00506   memsys3Leave();
00507   return (void*)p; 
00508 }
00509 
00510 /*
00511 ** Free memory.
00512 */
00513 void memsys3Free(void *pPrior){
00514   assert( pPrior );
00515   memsys3Enter();
00516   memsys3FreeUnsafe(pPrior);
00517   memsys3Leave();
00518 }
00519 
00520 /*
00521 ** Change the size of an existing memory allocation
00522 */
00523 void *memsys3Realloc(void *pPrior, int nBytes){
00524   int nOld;
00525   void *p;
00526   if( pPrior==0 ){
00527     return sqlite3_malloc(nBytes);
00528   }
00529   if( nBytes<=0 ){
00530     sqlite3_free(pPrior);
00531     return 0;
00532   }
00533   nOld = memsys3Size(pPrior);
00534   if( nBytes<=nOld && nBytes>=nOld-128 ){
00535     return pPrior;
00536   }
00537   memsys3Enter();
00538   p = memsys3MallocUnsafe(nBytes);
00539   if( p ){
00540     if( nOld<nBytes ){
00541       memcpy(p, pPrior, nOld);
00542     }else{
00543       memcpy(p, pPrior, nBytes);
00544     }
00545     memsys3FreeUnsafe(pPrior);
00546   }
00547   memsys3Leave();
00548   return p;
00549 }
00550 
00551 /*
00552 ** Initialize this module.
00553 */
00554 static int memsys3Init(void *NotUsed){
00555   if( !sqlite3GlobalConfig.pHeap ){
00556     return SQLITE_ERROR;
00557   }
00558 
00559   /* Store a pointer to the memory block in global structure mem3. */
00560   assert( sizeof(Mem3Block)==8 );
00561   mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
00562   mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
00563 
00564   /* Initialize the master block. */
00565   mem3.szMaster = mem3.nPool;
00566   mem3.mnMaster = mem3.szMaster;
00567   mem3.iMaster = 1;
00568   mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
00569   mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
00570   mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
00571 
00572   return SQLITE_OK;
00573 }
00574 
00575 /*
00576 ** Deinitialize this module.
00577 */
00578 static void memsys3Shutdown(void *NotUsed){
00579   return;
00580 }
00581 
00582 
00583 
00584 /*
00585 ** Open the file indicated and write a log of all unfreed memory 
00586 ** allocations into that log.
00587 */
00588 void sqlite3Memsys3Dump(const char *zFilename){
00589 #ifdef SQLITE_DEBUG
00590   FILE *out;
00591   int i, j;
00592   u32 size;
00593   if( zFilename==0 || zFilename[0]==0 ){
00594     out = stdout;
00595   }else{
00596     out = fopen(zFilename, "w");
00597     if( out==0 ){
00598       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
00599                       zFilename);
00600       return;
00601     }
00602   }
00603   memsys3Enter();
00604   fprintf(out, "CHUNKS:\n");
00605   for(i=1; i<=mem3.nPool; i+=size/4){
00606     size = mem3.aPool[i-1].u.hdr.size4x;
00607     if( size/4<=1 ){
00608       fprintf(out, "%p size error\n", &mem3.aPool[i]);
00609       assert( 0 );
00610       break;
00611     }
00612     if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
00613       fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
00614       assert( 0 );
00615       break;
00616     }
00617     if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
00618       fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
00619       assert( 0 );
00620       break;
00621     }
00622     if( size&1 ){
00623       fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
00624     }else{
00625       fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
00626                   i==mem3.iMaster ? " **master**" : "");
00627     }
00628   }
00629   for(i=0; i<MX_SMALL-1; i++){
00630     if( mem3.aiSmall[i]==0 ) continue;
00631     fprintf(out, "small(%2d):", i);
00632     for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
00633       fprintf(out, " %p(%d)", &mem3.aPool[j],
00634               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
00635     }
00636     fprintf(out, "\n"); 
00637   }
00638   for(i=0; i<N_HASH; i++){
00639     if( mem3.aiHash[i]==0 ) continue;
00640     fprintf(out, "hash(%2d):", i);
00641     for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
00642       fprintf(out, " %p(%d)", &mem3.aPool[j],
00643               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
00644     }
00645     fprintf(out, "\n"); 
00646   }
00647   fprintf(out, "master=%d\n", mem3.iMaster);
00648   fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
00649   fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
00650   sqlite3_mutex_leave(mem3.mutex);
00651   if( out==stdout ){
00652     fflush(stdout);
00653   }else{
00654     fclose(out);
00655   }
00656 #endif
00657 }
00658 
00659 /*
00660 ** This routine is the only routine in this file with external 
00661 ** linkage.
00662 **
00663 ** Populate the low-level memory allocation function pointers in
00664 ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
00665 ** arguments specify the block of memory to manage.
00666 **
00667 ** This routine is only called by sqlite3_config(), and therefore
00668 ** is not required to be threadsafe (it is not).
00669 */
00670 const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
00671   static const sqlite3_mem_methods mempoolMethods = {
00672      memsys3Malloc,
00673      memsys3Free,
00674      memsys3Realloc,
00675      memsys3Size,
00676      memsys3Roundup,
00677      memsys3Init,
00678      memsys3Shutdown,
00679      0
00680   };
00681   return &mempoolMethods;
00682 }
00683 
00684 #endif /* SQLITE_ENABLE_MEMSYS3 */

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:55 2011 by Doxygen 1.6.1