xref: /sqlite-3.40.0/src/mem3.c (revision 7830cd41)
1 /*
2 ** 2007 October 14
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** This file contains the C functions that implement a memory
13 ** allocation subsystem for use by SQLite.
14 **
15 ** This version of the memory allocation subsystem omits all
16 ** use of malloc(). The SQLite user supplies a block of memory
17 ** before calling sqlite3_initialize() from which allocations
18 ** are made and returned by the xMalloc() and xRealloc()
19 ** implementations. Once sqlite3_initialize() has been called,
20 ** the amount of memory available to SQLite is fixed and cannot
21 ** be changed.
22 **
23 ** This version of the memory allocation subsystem is included
24 ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
25 **
26 ** $Id: mem3.c,v 1.19 2008/07/16 12:25:32 drh Exp $
27 */
28 #include "sqliteInt.h"
29 
30 /*
31 ** This version of the memory allocator is only built into the library
32 ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
33 ** mean that the library will use a memory-pool by default, just that
34 ** it is available. The mempool allocator is activated by calling
35 ** sqlite3_config().
36 */
37 #ifdef SQLITE_ENABLE_MEMSYS3
38 
39 /*
40 ** Maximum size (in Mem3Blocks) of a "small" chunk.
41 */
42 #define MX_SMALL 10
43 
44 
45 /*
46 ** Number of freelist hash slots
47 */
48 #define N_HASH  61
49 
50 /*
51 ** A memory allocation (also called a "chunk") consists of two or
52 ** more blocks where each block is 8 bytes.  The first 8 bytes are
53 ** a header that is not returned to the user.
54 **
55 ** A chunk is two or more blocks that is either checked out or
56 ** free.  The first block has format u.hdr.  u.hdr.size4x is 4 times the
57 ** size of the allocation in blocks if the allocation is free.
58 ** The u.hdr.size4x&1 bit is true if the chunk is checked out and
59 ** false if the chunk is on the freelist.  The u.hdr.size4x&2 bit
60 ** is true if the previous chunk is checked out and false if the
61 ** previous chunk is free.  The u.hdr.prevSize field is the size of
62 ** the previous chunk in blocks if the previous chunk is on the
63 ** freelist. If the previous chunk is checked out, then
64 ** u.hdr.prevSize can be part of the data for that chunk and should
65 ** not be read or written.
66 **
67 ** We often identify a chunk by its index in mem3.aPool[].  When
68 ** this is done, the chunk index refers to the second block of
69 ** the chunk.  In this way, the first chunk has an index of 1.
70 ** A chunk index of 0 means "no such chunk" and is the equivalent
71 ** of a NULL pointer.
72 **
73 ** The second block of free chunks is of the form u.list.  The
74 ** two fields form a double-linked list of chunks of related sizes.
75 ** Pointers to the head of the list are stored in mem3.aiSmall[]
76 ** for smaller chunks and mem3.aiHash[] for larger chunks.
77 **
78 ** The second block of a chunk is user data if the chunk is checked
79 ** out.  If a chunk is checked out, the user data may extend into
80 ** the u.hdr.prevSize value of the following chunk.
81 */
82 typedef struct Mem3Block Mem3Block;
83 struct Mem3Block {
84   union {
85     struct {
86       u32 prevSize;   /* Size of previous chunk in Mem3Block elements */
87       u32 size4x;     /* 4x the size of current chunk in Mem3Block elements */
88     } hdr;
89     struct {
90       u32 next;       /* Index in mem3.aPool[] of next free chunk */
91       u32 prev;       /* Index in mem3.aPool[] of previous free chunk */
92     } list;
93   } u;
94 };
95 
96 /*
97 ** All of the static variables used by this module are collected
98 ** into a single structure named "mem3".  This is to keep the
99 ** static variables organized and to reduce namespace pollution
100 ** when this module is combined with other in the amalgamation.
101 */
102 static struct {
103   /*
104   ** True if we are evaluating an out-of-memory callback.
105   */
106   int alarmBusy;
107 
108   /*
109   ** Mutex to control access to the memory allocation subsystem.
110   */
111   sqlite3_mutex *mutex;
112 
113   /*
114   ** The minimum amount of free space that we have seen.
115   */
116   u32 mnMaster;
117 
118   /*
119   ** iMaster is the index of the master chunk.  Most new allocations
120   ** occur off of this chunk.  szMaster is the size (in Mem3Blocks)
121   ** of the current master.  iMaster is 0 if there is not master chunk.
122   ** The master chunk is not in either the aiHash[] or aiSmall[].
123   */
124   u32 iMaster;
125   u32 szMaster;
126 
127   /*
128   ** Array of lists of free blocks according to the block size
129   ** for smaller chunks, or a hash on the block size for larger
130   ** chunks.
131   */
132   u32 aiSmall[MX_SMALL-1];   /* For sizes 2 through MX_SMALL, inclusive */
133   u32 aiHash[N_HASH];        /* For sizes MX_SMALL+1 and larger */
134 
135   /*
136   ** Memory available for allocation. nPool is the size of the array
137   ** (in Mem3Blocks) pointed to by aPool less 2.
138   */
139   u32 nPool;
140   Mem3Block *aPool;
141   /* Mem3Block aPool[SQLITE_MEMORY_SIZE/sizeof(Mem3Block)+2]; */
142 } mem3;
143 
144 /*
145 ** Unlink the chunk at mem3.aPool[i] from list it is currently
146 ** on.  *pRoot is the list that i is a member of.
147 */
148 static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
149   u32 next = mem3.aPool[i].u.list.next;
150   u32 prev = mem3.aPool[i].u.list.prev;
151   assert( sqlite3_mutex_held(mem3.mutex) );
152   if( prev==0 ){
153     *pRoot = next;
154   }else{
155     mem3.aPool[prev].u.list.next = next;
156   }
157   if( next ){
158     mem3.aPool[next].u.list.prev = prev;
159   }
160   mem3.aPool[i].u.list.next = 0;
161   mem3.aPool[i].u.list.prev = 0;
162 }
163 
164 /*
165 ** Unlink the chunk at index i from
166 ** whatever list is currently a member of.
167 */
168 static void memsys3Unlink(u32 i){
169   u32 size, hash;
170   assert( sqlite3_mutex_held(mem3.mutex) );
171   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
172   assert( i>=1 );
173   size = mem3.aPool[i-1].u.hdr.size4x/4;
174   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
175   assert( size>=2 );
176   if( size <= MX_SMALL ){
177     memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
178   }else{
179     hash = size % N_HASH;
180     memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
181   }
182 }
183 
184 /*
185 ** Link the chunk at mem3.aPool[i] so that is on the list rooted
186 ** at *pRoot.
187 */
188 static void memsys3LinkIntoList(u32 i, u32 *pRoot){
189   assert( sqlite3_mutex_held(mem3.mutex) );
190   mem3.aPool[i].u.list.next = *pRoot;
191   mem3.aPool[i].u.list.prev = 0;
192   if( *pRoot ){
193     mem3.aPool[*pRoot].u.list.prev = i;
194   }
195   *pRoot = i;
196 }
197 
198 /*
199 ** Link the chunk at index i into either the appropriate
200 ** small chunk list, or into the large chunk hash table.
201 */
202 static void memsys3Link(u32 i){
203   u32 size, hash;
204   assert( sqlite3_mutex_held(mem3.mutex) );
205   assert( i>=1 );
206   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
207   size = mem3.aPool[i-1].u.hdr.size4x/4;
208   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
209   assert( size>=2 );
210   if( size <= MX_SMALL ){
211     memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
212   }else{
213     hash = size % N_HASH;
214     memsys3LinkIntoList(i, &mem3.aiHash[hash]);
215   }
216 }
217 
218 /*
219 ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
220 ** will already be held (obtained by code in malloc.c) if
221 ** sqlite3Config.bMemStat is true.
222 */
223 static void memsys3Enter(void){
224   if( sqlite3Config.bMemstat==0 && mem3.mutex==0 ){
225     mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
226   }
227   sqlite3_mutex_enter(mem3.mutex);
228 }
229 static void memsys3Leave(void){
230   sqlite3_mutex_leave(mem3.mutex);
231 }
232 
233 /*
234 ** Called when we are unable to satisfy an allocation of nBytes.
235 */
236 static void memsys3OutOfMemory(int nByte){
237   if( !mem3.alarmBusy ){
238     mem3.alarmBusy = 1;
239     assert( sqlite3_mutex_held(mem3.mutex) );
240     sqlite3_mutex_leave(mem3.mutex);
241     sqlite3_release_memory(nByte);
242     sqlite3_mutex_enter(mem3.mutex);
243     mem3.alarmBusy = 0;
244   }
245 }
246 
247 
248 /*
249 ** Chunk i is a free chunk that has been unlinked.  Adjust its
250 ** size parameters for check-out and return a pointer to the
251 ** user portion of the chunk.
252 */
253 static void *memsys3Checkout(u32 i, int nBlock){
254   u32 x;
255   assert( sqlite3_mutex_held(mem3.mutex) );
256   assert( i>=1 );
257   assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
258   assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
259   x = mem3.aPool[i-1].u.hdr.size4x;
260   mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
261   mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
262   mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
263   return &mem3.aPool[i];
264 }
265 
266 /*
267 ** Carve a piece off of the end of the mem3.iMaster free chunk.
268 ** Return a pointer to the new allocation.  Or, if the master chunk
269 ** is not large enough, return 0.
270 */
271 static void *memsys3FromMaster(int nBlock){
272   assert( sqlite3_mutex_held(mem3.mutex) );
273   assert( mem3.szMaster>=nBlock );
274   if( nBlock>=mem3.szMaster-1 ){
275     /* Use the entire master */
276     void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
277     mem3.iMaster = 0;
278     mem3.szMaster = 0;
279     mem3.mnMaster = 0;
280     return p;
281   }else{
282     /* Split the master block.  Return the tail. */
283     u32 newi, x;
284     newi = mem3.iMaster + mem3.szMaster - nBlock;
285     assert( newi > mem3.iMaster+1 );
286     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
287     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
288     mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
289     mem3.szMaster -= nBlock;
290     mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
291     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
292     mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
293     if( mem3.szMaster < mem3.mnMaster ){
294       mem3.mnMaster = mem3.szMaster;
295     }
296     return (void*)&mem3.aPool[newi];
297   }
298 }
299 
300 /*
301 ** *pRoot is the head of a list of free chunks of the same size
302 ** or same size hash.  In other words, *pRoot is an entry in either
303 ** mem3.aiSmall[] or mem3.aiHash[].
304 **
305 ** This routine examines all entries on the given list and tries
306 ** to coalesce each entries with adjacent free chunks.
307 **
308 ** If it sees a chunk that is larger than mem3.iMaster, it replaces
309 ** the current mem3.iMaster with the new larger chunk.  In order for
310 ** this mem3.iMaster replacement to work, the master chunk must be
311 ** linked into the hash tables.  That is not the normal state of
312 ** affairs, of course.  The calling routine must link the master
313 ** chunk before invoking this routine, then must unlink the (possibly
314 ** changed) master chunk once this routine has finished.
315 */
316 static void memsys3Merge(u32 *pRoot){
317   u32 iNext, prev, size, i, x;
318 
319   assert( sqlite3_mutex_held(mem3.mutex) );
320   for(i=*pRoot; i>0; i=iNext){
321     iNext = mem3.aPool[i].u.list.next;
322     size = mem3.aPool[i-1].u.hdr.size4x;
323     assert( (size&1)==0 );
324     if( (size&2)==0 ){
325       memsys3UnlinkFromList(i, pRoot);
326       assert( i > mem3.aPool[i-1].u.hdr.prevSize );
327       prev = i - mem3.aPool[i-1].u.hdr.prevSize;
328       if( prev==iNext ){
329         iNext = mem3.aPool[prev].u.list.next;
330       }
331       memsys3Unlink(prev);
332       size = i + size/4 - prev;
333       x = mem3.aPool[prev-1].u.hdr.size4x & 2;
334       mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
335       mem3.aPool[prev+size-1].u.hdr.prevSize = size;
336       memsys3Link(prev);
337       i = prev;
338     }else{
339       size /= 4;
340     }
341     if( size>mem3.szMaster ){
342       mem3.iMaster = i;
343       mem3.szMaster = size;
344     }
345   }
346 }
347 
348 /*
349 ** Return a block of memory of at least nBytes in size.
350 ** Return NULL if unable.
351 **
352 ** This function assumes that the necessary mutexes, if any, are
353 ** already held by the caller. Hence "Unsafe".
354 */
355 static void *memsys3MallocUnsafe(int nByte){
356   u32 i;
357   int nBlock;
358   int toFree;
359 
360   assert( sqlite3_mutex_held(mem3.mutex) );
361   assert( sizeof(Mem3Block)==8 );
362   if( nByte<=12 ){
363     nBlock = 2;
364   }else{
365     nBlock = (nByte + 11)/8;
366   }
367   assert( nBlock>=2 );
368 
369   /* STEP 1:
370   ** Look for an entry of the correct size in either the small
371   ** chunk table or in the large chunk hash table.  This is
372   ** successful most of the time (about 9 times out of 10).
373   */
374   if( nBlock <= MX_SMALL ){
375     i = mem3.aiSmall[nBlock-2];
376     if( i>0 ){
377       memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
378       return memsys3Checkout(i, nBlock);
379     }
380   }else{
381     int hash = nBlock % N_HASH;
382     for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
383       if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
384         memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
385         return memsys3Checkout(i, nBlock);
386       }
387     }
388   }
389 
390   /* STEP 2:
391   ** Try to satisfy the allocation by carving a piece off of the end
392   ** of the master chunk.  This step usually works if step 1 fails.
393   */
394   if( mem3.szMaster>=nBlock ){
395     return memsys3FromMaster(nBlock);
396   }
397 
398 
399   /* STEP 3:
400   ** Loop through the entire memory pool.  Coalesce adjacent free
401   ** chunks.  Recompute the master chunk as the largest free chunk.
402   ** Then try again to satisfy the allocation by carving a piece off
403   ** of the end of the master chunk.  This step happens very
404   ** rarely (we hope!)
405   */
406   for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
407     memsys3OutOfMemory(toFree);
408     if( mem3.iMaster ){
409       memsys3Link(mem3.iMaster);
410       mem3.iMaster = 0;
411       mem3.szMaster = 0;
412     }
413     for(i=0; i<N_HASH; i++){
414       memsys3Merge(&mem3.aiHash[i]);
415     }
416     for(i=0; i<MX_SMALL-1; i++){
417       memsys3Merge(&mem3.aiSmall[i]);
418     }
419     if( mem3.szMaster ){
420       memsys3Unlink(mem3.iMaster);
421       if( mem3.szMaster>=nBlock ){
422         return memsys3FromMaster(nBlock);
423       }
424     }
425   }
426 
427   /* If none of the above worked, then we fail. */
428   return 0;
429 }
430 
431 /*
432 ** Free an outstanding memory allocation.
433 **
434 ** This function assumes that the necessary mutexes, if any, are
435 ** already held by the caller. Hence "Unsafe".
436 */
437 void memsys3FreeUnsafe(void *pOld){
438   Mem3Block *p = (Mem3Block*)pOld;
439   int i;
440   u32 size, x;
441   assert( sqlite3_mutex_held(mem3.mutex) );
442   assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
443   i = p - mem3.aPool;
444   assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
445   size = mem3.aPool[i-1].u.hdr.size4x/4;
446   assert( i+size<=mem3.nPool+1 );
447   mem3.aPool[i-1].u.hdr.size4x &= ~1;
448   mem3.aPool[i+size-1].u.hdr.prevSize = size;
449   mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
450   memsys3Link(i);
451 
452   /* Try to expand the master using the newly freed chunk */
453   if( mem3.iMaster ){
454     while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
455       size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
456       mem3.iMaster -= size;
457       mem3.szMaster += size;
458       memsys3Unlink(mem3.iMaster);
459       x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
460       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
461       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
462     }
463     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
464     while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
465       memsys3Unlink(mem3.iMaster+mem3.szMaster);
466       mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
467       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
468       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
469     }
470   }
471 }
472 
473 /*
474 ** Allocate nBytes of memory.
475 */
476 static void *memsys3Malloc(int nBytes){
477   sqlite3_int64 *p;
478   assert( nBytes>0 );          /* malloc.c filters out 0 byte requests */
479   memsys3Enter();
480   p = memsys3MallocUnsafe(nBytes);
481   memsys3Leave();
482   return (void*)p;
483 }
484 
485 /*
486 ** Free memory.
487 */
488 void memsys3Free(void *pPrior){
489   assert( pPrior );
490   memsys3Enter();
491   memsys3FreeUnsafe(pPrior);
492   memsys3Leave();
493 }
494 
495 /*
496 ** Return the size of an outstanding allocation, in bytes.  The
497 ** size returned omits the 8-byte header overhead.  This only
498 ** works for chunks that are currently checked out.
499 */
500 static int memsys3Size(void *p){
501   Mem3Block *pBlock;
502   if( p==0 ) return 0;
503   pBlock = (Mem3Block*)p;
504   assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
505   return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
506 }
507 
508 /*
509 ** Change the size of an existing memory allocation
510 */
511 void *memsys3Realloc(void *pPrior, int nBytes){
512   int nOld;
513   void *p;
514   if( pPrior==0 ){
515     return sqlite3_malloc(nBytes);
516   }
517   if( nBytes<=0 ){
518     sqlite3_free(pPrior);
519     return 0;
520   }
521   nOld = memsys3Size(pPrior);
522   if( nBytes<=nOld && nBytes>=nOld-128 ){
523     return pPrior;
524   }
525   memsys3Enter();
526   p = memsys3MallocUnsafe(nBytes);
527   if( p ){
528     if( nOld<nBytes ){
529       memcpy(p, pPrior, nOld);
530     }else{
531       memcpy(p, pPrior, nBytes);
532     }
533     memsys3FreeUnsafe(pPrior);
534   }
535   memsys3Leave();
536   return p;
537 }
538 
539 /*
540 ** Round up a request size to the next valid allocation size.
541 */
542 static int memsys3Roundup(int n){
543   return (n+7) & ~7;
544 }
545 
546 /*
547 ** Initialize this module.
548 */
549 static int memsys3Init(void *NotUsed){
550   if( !sqlite3Config.pHeap ){
551     return SQLITE_ERROR;
552   }
553 
554   /* Store a pointer to the memory block in global structure mem3. */
555   assert( sizeof(Mem3Block)==8 );
556   mem3.aPool = (Mem3Block *)sqlite3Config.pHeap;
557   mem3.nPool = (sqlite3Config.nHeap / sizeof(Mem3Block)) - 2;
558 
559   /* Initialize the master block. */
560   mem3.szMaster = mem3.nPool;
561   mem3.mnMaster = mem3.szMaster;
562   mem3.iMaster = 1;
563   mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
564   mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
565   mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
566 
567   return SQLITE_OK;
568 }
569 
570 /*
571 ** Deinitialize this module.
572 */
573 static void memsys3Shutdown(void *NotUsed){
574   return;
575 }
576 
577 
578 
579 /*
580 ** Open the file indicated and write a log of all unfreed memory
581 ** allocations into that log.
582 */
583 #ifdef SQLITE_DEBUG
584 void sqlite3Memsys3Dump(const char *zFilename){
585   FILE *out;
586   int i, j;
587   u32 size;
588   if( zFilename==0 || zFilename[0]==0 ){
589     out = stdout;
590   }else{
591     out = fopen(zFilename, "w");
592     if( out==0 ){
593       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
594                       zFilename);
595       return;
596     }
597   }
598   memsys3Enter();
599   fprintf(out, "CHUNKS:\n");
600   for(i=1; i<=mem3.nPool; i+=size/4){
601     size = mem3.aPool[i-1].u.hdr.size4x;
602     if( size/4<=1 ){
603       fprintf(out, "%p size error\n", &mem3.aPool[i]);
604       assert( 0 );
605       break;
606     }
607     if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
608       fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
609       assert( 0 );
610       break;
611     }
612     if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
613       fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
614       assert( 0 );
615       break;
616     }
617     if( size&1 ){
618       fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
619     }else{
620       fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
621                   i==mem3.iMaster ? " **master**" : "");
622     }
623   }
624   for(i=0; i<MX_SMALL-1; i++){
625     if( mem3.aiSmall[i]==0 ) continue;
626     fprintf(out, "small(%2d):", i);
627     for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
628       fprintf(out, " %p(%d)", &mem3.aPool[j],
629               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
630     }
631     fprintf(out, "\n");
632   }
633   for(i=0; i<N_HASH; i++){
634     if( mem3.aiHash[i]==0 ) continue;
635     fprintf(out, "hash(%2d):", i);
636     for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
637       fprintf(out, " %p(%d)", &mem3.aPool[j],
638               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
639     }
640     fprintf(out, "\n");
641   }
642   fprintf(out, "master=%d\n", mem3.iMaster);
643   fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
644   fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
645   sqlite3_mutex_leave(mem3.mutex);
646   if( out==stdout ){
647     fflush(stdout);
648   }else{
649     fclose(out);
650   }
651 }
652 #endif
653 
654 /*
655 ** This routine is the only routine in this file with external
656 ** linkage.
657 **
658 ** Populate the low-level memory allocation function pointers in
659 ** sqlite3Config.m with pointers to the routines in this file. The
660 ** arguments specify the block of memory to manage.
661 **
662 ** This routine is only called by sqlite3_config(), and therefore
663 ** is not required to be threadsafe (it is not).
664 */
665 const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
666   static const sqlite3_mem_methods mempoolMethods = {
667      memsys3Malloc,
668      memsys3Free,
669      memsys3Realloc,
670      memsys3Size,
671      memsys3Roundup,
672      memsys3Init,
673      memsys3Shutdown,
674      0
675   };
676   return &mempoolMethods;
677 }
678 
679 #endif /* SQLITE_ENABLE_MEMSYS3 */
680