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