Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/src/mem5.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / src / mem5.c` 585 lines ยท 17384 bytes Raw Blame History 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 application gives SQLite 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_MEMSYS5 is defined. 25 ** 26 ** This memory allocator uses the following algorithm: 27 ** 28 ** 1. All memory allocation sizes are rounded up to a power of 2. 29 ** 30 ** 2. If two adjacent free blocks are the halves of a larger block, 31 ** then the two blocks are coalesced into the single larger block. 32 ** 33 ** 3. New memory is allocated from the first available free block. 34 ** 35 ** This algorithm is described in: J. M. Robson. "Bounds for Some Functions 36 ** Concerning Dynamic Storage Allocation". Journal of the Association for 37 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499. 38 ** 39 ** Let n be the size of the largest allocation divided by the minimum 40 ** allocation size (after rounding all sizes up to a power of 2.) Let M 41 ** be the maximum amount of memory ever outstanding at one time. Let 42 ** N be the total amount of memory available for allocation. Robson 43 ** proved that this memory allocator will never breakdown due to 44 ** fragmentation as long as the following constraint holds: 45 ** 46 ** N >= M*(1 + log2(n)/2) - n + 1 47 ** 48 ** The sqlite3_status() logic tracks the maximum values of n and M so 49 ** that an application can, at any time, verify this constraint. 50 */ 51 #include "sqliteInt.h" 52 53 /* 54 ** This version of the memory allocator is used only when 55 ** SQLITE_ENABLE_MEMSYS5 is defined. 56 */ 57 #ifdef SQLITE_ENABLE_MEMSYS5 58 59 /* 60 ** A minimum allocation is an instance of the following structure. 61 ** Larger allocations are an array of these structures where the 62 ** size of the array is a power of 2. 63 ** 64 ** The size of this object must be a power of two. That fact is 65 ** verified in memsys5Init(). 66 */ 67 typedef struct Mem5Link Mem5Link; 68 struct Mem5Link { 69 int next; /* Index of next free chunk */ 70 int prev; /* Index of previous free chunk */ 71 }; 72 73 /* 74 ** Maximum size of any allocation is ((1<=0 && i=0 && iLogsize<=LOGMAX ); 153 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize ); 154 155 next = MEM5LINK(i)->next; 156 prev = MEM5LINK(i)->prev; 157 if( prev<0 ){ 158 mem5.aiFreelist[iLogsize] = next; 159 }else{ 160 MEM5LINK(prev)->next = next; 161 } 162 if( next>=0 ){ 163 MEM5LINK(next)->prev = prev; 164 } 165 } 166 167 /* 168 ** Link the chunk at mem5.aPool[i] so that is on the iLogsize 169 ** free list. 170 */ 171 static void memsys5Link(int i, int iLogsize){ 172 int x; 173 assert( sqlite3_mutex_held(mem5.mutex) ); 174 assert( i>=0 && i=0 && iLogsize<=LOGMAX ); 176 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize ); 177 178 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize]; 179 MEM5LINK(i)->prev = -1; 180 if( x>=0 ){ 181 assert( xprev = i; 183 } 184 mem5.aiFreelist[iLogsize] = i; 185 } 186 187 /* 188 ** Obtain or release the mutex needed to access global data structures. 189 */ 190 static void memsys5Enter(void){ 191 sqlite3_mutex_enter(mem5.mutex); 192 } 193 static void memsys5Leave(void){ 194 sqlite3_mutex_leave(mem5.mutex); 195 } 196 197 /* 198 ** Return the size of an outstanding allocation, in bytes. 199 ** This only works for chunks that are currently checked out. 200 */ 201 static int memsys5Size(void *p){ 202 int iSize, i; 203 assert( p!=0 ); 204 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom); 205 assert( i>=0 && i0 ); 228 229 /* No more than 1GiB per allocation */ 230 if( nByte > 0x40000000 ) return 0; 231 232 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) 233 /* Keep track of the maximum allocation request. Even unfulfilled 234 ** requests are counted */ 235 if( (u32)nByte>mem5.maxRequest ){ 236 mem5.maxRequest = nByte; 237 } 238 #endif 239 240 241 /* Round nByte up to the next valid power of two */ 242 for(iFullSz=mem5.szAtom,iLogsize=0; iFullSzLOGMAX ){ 250 testcase( sqlite3GlobalConfig.xLog!=0 ); 251 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte); 252 return 0; 253 } 254 i = mem5.aiFreelist[iBin]; 255 memsys5Unlink(i, iBin); 256 while( iBin>iLogsize ){ 257 int newSize; 258 259 iBin--; 260 newSize = 1 << iBin; 261 mem5.aCtrl[i+newSize] = CTRL_FREE | iBin; 262 memsys5Link(i+newSize, iBin); 263 } 264 mem5.aCtrl[i] = iLogsize; 265 266 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) 267 /* Update allocator performance statistics. */ 268 mem5.nAlloc++; 269 mem5.totalAlloc += iFullSz; 270 mem5.totalExcess += iFullSz - nByte; 271 mem5.currentCount++; 272 mem5.currentOut += iFullSz; 273 if( mem5.maxCount=0 && iBlock0 ); 313 assert( mem5.currentOut>=(size*mem5.szAtom) ); 314 mem5.currentCount--; 315 mem5.currentOut -= size*mem5.szAtom; 316 assert( mem5.currentOut>0 || mem5.currentCount==0 ); 317 assert( mem5.currentCount>0 || mem5.currentOut==0 ); 318 #endif 319 320 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize; 321 while( ALWAYS(iLogsize>iLogsize) & 1 ){ 324 iBuddy = iBlock - size; 325 assert( iBuddy>=0 ); 326 }else{ 327 iBuddy = iBlock + size; 328 if( iBuddy>=mem5.nBlock ) break; 329 } 330 if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break; 331 memsys5Unlink(iBuddy, iLogsize); 332 iLogsize++; 333 if( iBuddy0 ){ 359 memsys5Enter(); 360 p = memsys5MallocUnsafe(nBytes); 361 memsys5Leave(); 362 } 363 return (void*)p; 364 } 365 366 /* 367 ** Free memory. 368 ** 369 ** The outer layer memory allocator prevents this routine from 370 ** being called with pPrior==0. 371 */ 372 static void memsys5Free(void *pPrior){ 373 assert( pPrior!=0 ); 374 memsys5Enter(); 375 memsys5FreeUnsafe(pPrior); 376 memsys5Leave(); 377 } 378 379 /* 380 ** Change the size of an existing memory allocation. 381 ** 382 ** The outer layer memory allocator prevents this routine from 383 ** being called with pPrior==0. 384 ** 385 ** nBytes is always a value obtained from a prior call to 386 ** memsys5Round(). Hence nBytes is always a non-negative power 387 ** of two. If nBytes==0 that means that an oversize allocation 388 ** (an allocation larger than 0x40000000) was requested and this 389 ** routine should return 0 without freeing pPrior. 390 */ 391 static void *memsys5Realloc(void *pPrior, int nBytes){ 392 int nOld; 393 void *p; 394 assert( pPrior!=0 ); 395 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */ 396 assert( nBytes>=0 ); 397 if( nBytes==0 ){ 398 return 0; 399 } 400 nOld = memsys5Size(pPrior); 401 if( nBytes<=nOld ){ 402 return pPrior; 403 } 404 p = memsys5Malloc(nBytes); 405 if( p ){ 406 memcpy(p, pPrior, nOld); 407 memsys5Free(pPrior); 408 } 409 return p; 410 } 411 412 /* 413 ** Round up a request size to the next valid allocation size. If 414 ** the allocation is too large to be handled by this allocation system, 415 ** return 0. 416 ** 417 ** All allocations must be a power of two and must be expressed by a 418 ** 32-bit signed integer. Hence the largest allocation is 0x40000000 419 ** or `1073741824` bytes. 420 */ 421 static int memsys5Roundup(int n){ 422 int iFullSz; 423 if( n<=mem5.szAtom*2 ){ 424 if( n<=mem5.szAtom ) return mem5.szAtom; 425 return mem5.szAtom*2; 426 } 427 if( n>0x10000000 ){ 428 if( n>0x40000000 ) return 0; 429 if( n>0x20000000 ) return 0x40000000; 430 return 0x20000000; 431 } 432 for(iFullSz=mem5.szAtom*8; iFullSz=(i64)n ) return iFullSz/2; 434 return iFullSz; 435 } 436 437 /* 438 ** Return the ceiling of the logarithm base 2 of iValue. 439 ** 440 ** Examples: memsys5Log(1) -> 0 441 ** memsys5Log(2) -> 1 442 ** memsys5Log(4) -> 2 443 ** memsys5Log(5) -> 3 444 ** memsys5Log(8) -> 3 445 ** memsys5Log(9) -> 4 446 */ 447 static int memsys5Log(int iValue){ 448 int iLog; 449 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<mem5.szAtom ){ 484 mem5.szAtom = mem5.szAtom << 1; 485 } 486 487 mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8))); 488 mem5.zPool = zByte; 489 mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom]; 490 491 for(ii=0; ii<=LOGMAX; ii++){ 492 mem5.aiFreelist[ii] = -1; 493 } 494 495 iOffset = 0; 496 for(ii=LOGMAX; ii>=0; ii--){ 497 int nAlloc = (1<mem5.nBlock); 504 } 505 506 /* If a mutex is required for normal operation, allocate one */ 507 if( sqlite3GlobalConfig.bMemstat==0 ){ 508 mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); 509 } 510 511 return SQLITE_OK; 512 } 513 514 /* 515 ** Deinitialize this module. 516 */ 517 static void memsys5Shutdown(void *NotUsed){ 518 UNUSED_PARAMETER(NotUsed); 519 mem5.mutex = 0; 520 return; 521 } 522 523 #ifdef SQLITE_TEST 524 /* 525 ** Open the file indicated and write a log of all unfreed memory 526 ** allocations into that log. 527 */ 528 void sqlite3Memsys5Dump(const char *zFilename){ 529 FILE *out; 530 int i, j, n; 531 int nMinLog; 532 533 if( zFilename==0 || zFilename[0]==0 ){ 534 out = stdout; 535 }else{ 536 out = fopen(zFilename, "w"); 537 if( out==0 ){ 538 fprintf(stderr, "** Unable to output memory debug output log: %s **\n", 539 zFilename); 540 return; 541 } 542 } 543 memsys5Enter(); 544 nMinLog = memsys5Log(mem5.szAtom); 545 for(i=0; i<=LOGMAX && i+nMinLog<32; i++){ 546 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){} 547 fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n); 548 } 549 fprintf(out, "mem5.nAlloc = %llu\n", mem5.nAlloc); 550 fprintf(out, "mem5.totalAlloc = %llu\n", mem5.totalAlloc); 551 fprintf(out, "mem5.totalExcess = %llu\n", mem5.totalExcess); 552 fprintf(out, "mem5.currentOut = %u\n", mem5.currentOut); 553 fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount); 554 fprintf(out, "mem5.maxOut = %u\n", mem5.maxOut); 555 fprintf(out, "mem5.maxCount = %u\n", mem5.maxCount); 556 fprintf(out, "mem5.maxRequest = %u\n", mem5.maxRequest); 557 memsys5Leave(); 558 if( out==stdout ){ 559 fflush(stdout); 560 }else{ 561 fclose(out); 562 } 563 } 564 #endif 565 566 /* 567 ** This routine is the only routine in this file with external 568 ** linkage. It returns a pointer to a static sqlite3_mem_methods 569 ** struct populated with the memsys5 methods. 570 */ 571 const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){ 572 static const sqlite3_mem_methods memsys5Methods = { 573 memsys5Malloc, 574 memsys5Free, 575 memsys5Realloc, 576 memsys5Size, 577 memsys5Roundup, 578 memsys5Init, 579 memsys5Shutdown, 580 0 581 }; 582 return &memsys5Methods; 583 } 584 585 #endif /* SQLITE_ENABLE_MEMSYS5 */