xref: /sqlite-3.40.0/src/mem3.c (revision 32155ef0)
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.16 2008/06/25 10:34:35 danielk1977 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 ** Enter the mutex mem3.mutex. Allocate it if it is not already allocated.
220 **
221 ** Also:  Initialize the memory allocation subsystem the first time
222 ** this routine is called.
223 */
224 static void memsys3Enter(void){
225 #if 0
226   if( mem3.mutex==0 ){
227     mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
228   }
229   sqlite3_mutex_enter(mem3.mutex);
230 #endif
231 }
232 static void memsys3Leave(void){
233 }
234 
235 /*
236 ** Called when we are unable to satisfy an allocation of nBytes.
237 */
238 static void memsys3OutOfMemory(int nByte){
239   if( !mem3.alarmBusy ){
240     mem3.alarmBusy = 1;
241     assert( sqlite3_mutex_held(mem3.mutex) );
242     sqlite3_mutex_leave(mem3.mutex);
243     sqlite3_release_memory(nByte);
244     sqlite3_mutex_enter(mem3.mutex);
245     mem3.alarmBusy = 0;
246   }
247 }
248 
249 
250 /*
251 ** Chunk i is a free chunk that has been unlinked.  Adjust its
252 ** size parameters for check-out and return a pointer to the
253 ** user portion of the chunk.
254 */
255 static void *memsys3Checkout(u32 i, int nBlock){
256   u32 x;
257   assert( sqlite3_mutex_held(mem3.mutex) );
258   assert( i>=1 );
259   assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
260   assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
261   x = mem3.aPool[i-1].u.hdr.size4x;
262   mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
263   mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
264   mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
265   return &mem3.aPool[i];
266 }
267 
268 /*
269 ** Carve a piece off of the end of the mem3.iMaster free chunk.
270 ** Return a pointer to the new allocation.  Or, if the master chunk
271 ** is not large enough, return 0.
272 */
273 static void *memsys3FromMaster(int nBlock){
274   assert( sqlite3_mutex_held(mem3.mutex) );
275   assert( mem3.szMaster>=nBlock );
276   if( nBlock>=mem3.szMaster-1 ){
277     /* Use the entire master */
278     void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
279     mem3.iMaster = 0;
280     mem3.szMaster = 0;
281     mem3.mnMaster = 0;
282     return p;
283   }else{
284     /* Split the master block.  Return the tail. */
285     u32 newi, x;
286     newi = mem3.iMaster + mem3.szMaster - nBlock;
287     assert( newi > mem3.iMaster+1 );
288     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
289     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
290     mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
291     mem3.szMaster -= nBlock;
292     mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
293     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
294     mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
295     if( mem3.szMaster < mem3.mnMaster ){
296       mem3.mnMaster = mem3.szMaster;
297     }
298     return (void*)&mem3.aPool[newi];
299   }
300 }
301 
302 /*
303 ** *pRoot is the head of a list of free chunks of the same size
304 ** or same size hash.  In other words, *pRoot is an entry in either
305 ** mem3.aiSmall[] or mem3.aiHash[].
306 **
307 ** This routine examines all entries on the given list and tries
308 ** to coalesce each entries with adjacent free chunks.
309 **
310 ** If it sees a chunk that is larger than mem3.iMaster, it replaces
311 ** the current mem3.iMaster with the new larger chunk.  In order for
312 ** this mem3.iMaster replacement to work, the master chunk must be
313 ** linked into the hash tables.  That is not the normal state of
314 ** affairs, of course.  The calling routine must link the master
315 ** chunk before invoking this routine, then must unlink the (possibly
316 ** changed) master chunk once this routine has finished.
317 */
318 static void memsys3Merge(u32 *pRoot){
319   u32 iNext, prev, size, i, x;
320 
321   assert( sqlite3_mutex_held(mem3.mutex) );
322   for(i=*pRoot; i>0; i=iNext){
323     iNext = mem3.aPool[i].u.list.next;
324     size = mem3.aPool[i-1].u.hdr.size4x;
325     assert( (size&1)==0 );
326     if( (size&2)==0 ){
327       memsys3UnlinkFromList(i, pRoot);
328       assert( i > mem3.aPool[i-1].u.hdr.prevSize );
329       prev = i - mem3.aPool[i-1].u.hdr.prevSize;
330       if( prev==iNext ){
331         iNext = mem3.aPool[prev].u.list.next;
332       }
333       memsys3Unlink(prev);
334       size = i + size/4 - prev;
335       x = mem3.aPool[prev-1].u.hdr.size4x & 2;
336       mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
337       mem3.aPool[prev+size-1].u.hdr.prevSize = size;
338       memsys3Link(prev);
339       i = prev;
340     }else{
341       size /= 4;
342     }
343     if( size>mem3.szMaster ){
344       mem3.iMaster = i;
345       mem3.szMaster = size;
346     }
347   }
348 }
349 
350 /*
351 ** Return a block of memory of at least nBytes in size.
352 ** Return NULL if unable.
353 **
354 ** This function assumes that the necessary mutexes, if any, are
355 ** already held by the caller. Hence "Unsafe".
356 */
357 static void *memsys3MallocUnsafe(int nByte){
358   u32 i;
359   int nBlock;
360   int toFree;
361 
362   assert( sqlite3_mutex_held(mem3.mutex) );
363   assert( sizeof(Mem3Block)==8 );
364   if( nByte<=12 ){
365     nBlock = 2;
366   }else{
367     nBlock = (nByte + 11)/8;
368   }
369   assert( nBlock>=2 );
370 
371   /* STEP 1:
372   ** Look for an entry of the correct size in either the small
373   ** chunk table or in the large chunk hash table.  This is
374   ** successful most of the time (about 9 times out of 10).
375   */
376   if( nBlock <= MX_SMALL ){
377     i = mem3.aiSmall[nBlock-2];
378     if( i>0 ){
379       memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
380       return memsys3Checkout(i, nBlock);
381     }
382   }else{
383     int hash = nBlock % N_HASH;
384     for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
385       if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
386         memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
387         return memsys3Checkout(i, nBlock);
388       }
389     }
390   }
391 
392   /* STEP 2:
393   ** Try to satisfy the allocation by carving a piece off of the end
394   ** of the master chunk.  This step usually works if step 1 fails.
395   */
396   if( mem3.szMaster>=nBlock ){
397     return memsys3FromMaster(nBlock);
398   }
399 
400 
401   /* STEP 3:
402   ** Loop through the entire memory pool.  Coalesce adjacent free
403   ** chunks.  Recompute the master chunk as the largest free chunk.
404   ** Then try again to satisfy the allocation by carving a piece off
405   ** of the end of the master chunk.  This step happens very
406   ** rarely (we hope!)
407   */
408   for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
409     memsys3OutOfMemory(toFree);
410     if( mem3.iMaster ){
411       memsys3Link(mem3.iMaster);
412       mem3.iMaster = 0;
413       mem3.szMaster = 0;
414     }
415     for(i=0; i<N_HASH; i++){
416       memsys3Merge(&mem3.aiHash[i]);
417     }
418     for(i=0; i<MX_SMALL-1; i++){
419       memsys3Merge(&mem3.aiSmall[i]);
420     }
421     if( mem3.szMaster ){
422       memsys3Unlink(mem3.iMaster);
423       if( mem3.szMaster>=nBlock ){
424         return memsys3FromMaster(nBlock);
425       }
426     }
427   }
428 
429   /* If none of the above worked, then we fail. */
430   return 0;
431 }
432 
433 /*
434 ** Free an outstanding memory allocation.
435 **
436 ** This function assumes that the necessary mutexes, if any, are
437 ** already held by the caller. Hence "Unsafe".
438 */
439 void memsys3FreeUnsafe(void *pOld){
440   Mem3Block *p = (Mem3Block*)pOld;
441   int i;
442   u32 size, x;
443   assert( sqlite3_mutex_held(mem3.mutex) );
444   assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
445   i = p - mem3.aPool;
446   assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
447   size = mem3.aPool[i-1].u.hdr.size4x/4;
448   assert( i+size<=mem3.nPool+1 );
449   mem3.aPool[i-1].u.hdr.size4x &= ~1;
450   mem3.aPool[i+size-1].u.hdr.prevSize = size;
451   mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
452   memsys3Link(i);
453 
454   /* Try to expand the master using the newly freed chunk */
455   if( mem3.iMaster ){
456     while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
457       size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
458       mem3.iMaster -= size;
459       mem3.szMaster += size;
460       memsys3Unlink(mem3.iMaster);
461       x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
462       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
463       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
464     }
465     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
466     while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
467       memsys3Unlink(mem3.iMaster+mem3.szMaster);
468       mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
469       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
470       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
471     }
472   }
473 }
474 
475 /*
476 ** Allocate nBytes of memory.
477 */
478 static void *memsys3Malloc(int nBytes){
479   sqlite3_int64 *p;
480   assert( nBytes>0 );          /* malloc.c filters out 0 byte requests */
481   memsys3Enter();
482   p = memsys3MallocUnsafe(nBytes);
483   memsys3Leave();
484   return (void*)p;
485 }
486 
487 /*
488 ** Free memory.
489 */
490 void memsys3Free(void *pPrior){
491   assert( pPrior );
492   memsys3Enter();
493   memsys3FreeUnsafe(pPrior);
494   memsys3Leave();
495 }
496 
497 /*
498 ** Return the size of an outstanding allocation, in bytes.  The
499 ** size returned omits the 8-byte header overhead.  This only
500 ** works for chunks that are currently checked out.
501 */
502 static int memsys3Size(void *p){
503   Mem3Block *pBlock = (Mem3Block*)p;
504   assert( pBlock );
505   assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
506   return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
507 }
508 
509 /*
510 ** Change the size of an existing memory allocation
511 */
512 void *memsys3Realloc(void *pPrior, int nBytes){
513   int nOld;
514   void *p;
515   if( pPrior==0 ){
516     return sqlite3_malloc(nBytes);
517   }
518   if( nBytes<=0 ){
519     sqlite3_free(pPrior);
520     return 0;
521   }
522   nOld = memsys3Size(pPrior);
523   if( nBytes<=nOld && nBytes>=nOld-128 ){
524     return pPrior;
525   }
526   memsys3Enter();
527   p = memsys3MallocUnsafe(nBytes);
528   if( p ){
529     if( nOld<nBytes ){
530       memcpy(p, pPrior, nOld);
531     }else{
532       memcpy(p, pPrior, nBytes);
533     }
534     memsys3FreeUnsafe(pPrior);
535   }
536   memsys3Leave();
537   return p;
538 }
539 
540 /*
541 ** Round up a request size to the next valid allocation size.
542 */
543 static int memsys3Roundup(int n){
544   return (n+7) & ~7;
545 }
546 
547 /*
548 ** Initialize this module.
549 */
550 static int memsys3Init(void *NotUsed){
551   return SQLITE_OK;
552 }
553 
554 /*
555 ** Deinitialize this module.
556 */
557 static void memsys3Shutdown(void *NotUsed){
558   return;
559 }
560 
561 
562 
563 /*
564 ** Open the file indicated and write a log of all unfreed memory
565 ** allocations into that log.
566 */
567 #ifdef SQLITE_DEBUG
568 void sqlite3Memsys3Dump(const char *zFilename){
569   FILE *out;
570   int i, j;
571   u32 size;
572   if( zFilename==0 || zFilename[0]==0 ){
573     out = stdout;
574   }else{
575     out = fopen(zFilename, "w");
576     if( out==0 ){
577       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
578                       zFilename);
579       return;
580     }
581   }
582   memsys3Enter();
583   fprintf(out, "CHUNKS:\n");
584   for(i=1; i<=mem3.nPool; i+=size/4){
585     size = mem3.aPool[i-1].u.hdr.size4x;
586     if( size/4<=1 ){
587       fprintf(out, "%p size error\n", &mem3.aPool[i]);
588       assert( 0 );
589       break;
590     }
591     if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
592       fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
593       assert( 0 );
594       break;
595     }
596     if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
597       fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
598       assert( 0 );
599       break;
600     }
601     if( size&1 ){
602       fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
603     }else{
604       fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
605                   i==mem3.iMaster ? " **master**" : "");
606     }
607   }
608   for(i=0; i<MX_SMALL-1; i++){
609     if( mem3.aiSmall[i]==0 ) continue;
610     fprintf(out, "small(%2d):", i);
611     for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
612       fprintf(out, " %p(%d)", &mem3.aPool[j],
613               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
614     }
615     fprintf(out, "\n");
616   }
617   for(i=0; i<N_HASH; i++){
618     if( mem3.aiHash[i]==0 ) continue;
619     fprintf(out, "hash(%2d):", i);
620     for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
621       fprintf(out, " %p(%d)", &mem3.aPool[j],
622               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
623     }
624     fprintf(out, "\n");
625   }
626   fprintf(out, "master=%d\n", mem3.iMaster);
627   fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
628   fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
629   sqlite3_mutex_leave(mem3.mutex);
630   if( out==stdout ){
631     fflush(stdout);
632   }else{
633     fclose(out);
634   }
635 }
636 #endif
637 
638 /*
639 ** This routine is the only routine in this file with external
640 ** linkage.
641 **
642 ** Populate the low-level memory allocation function pointers in
643 ** sqlite3Config.m with pointers to the routines in this file. The
644 ** arguments specify the block of memory to manage.
645 **
646 ** This routine is only called by sqlite3_config(), and therefore
647 ** is not required to be threadsafe (it is not).
648 */
649 void sqlite3MemSetMemsys3(u8 *pBlock, int nBlock){
650   static const sqlite3_mem_methods mempoolMethods = {
651      memsys3Malloc,
652      memsys3Free,
653      memsys3Realloc,
654      memsys3Size,
655      memsys3Roundup,
656      memsys3Init,
657      memsys3Shutdown,
658      0
659   };
660 
661   /* Configure the functions to call to allocate memory. */
662   sqlite3_config(SQLITE_CONFIG_MALLOC, &mempoolMethods);
663 
664   /* Store a pointer to the memory block in global structure mem3. */
665   assert( sizeof(Mem3Block)==8 );
666   mem3.aPool = (Mem3Block *)pBlock;
667   mem3.nPool = (nBlock / sizeof(Mem3Block)) - 2;
668 
669   /* Initialize the master block. */
670   mem3.szMaster = mem3.nPool;
671   mem3.mnMaster = mem3.szMaster;
672   mem3.iMaster = 1;
673   mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
674   mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
675   mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
676 }
677 
678 #endif /* SQLITE_ENABLE_MEMSYS3 */
679