mirror of
				https://github.com/sqlite/sqlite.git
				synced 2025-10-30 07:05:46 +03:00 
			
		
		
		
	Micro-optimizations and comment fixes on the mem5.c memory allocator module.
FossilOrigin-Name: 8bf5e056eb8beb6e0ed5874fb24d7fe9f0b66d2b
This commit is contained in:
		
							
								
								
									
										29
									
								
								src/mem5.c
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								src/mem5.c
									
									
									
									
									
								
							| @@ -25,7 +25,7 @@ | ||||
| ** | ||||
| ** This memory allocator uses the following algorithm: | ||||
| ** | ||||
| **   1.  All memory allocations sizes are rounded up to a power of 2. | ||||
| **   1.  All memory allocation sizes are rounded up to a power of 2. | ||||
| ** | ||||
| **   2.  If two adjacent free blocks are the halves of a larger block, | ||||
| **       then the two blocks are coalesced into the single larger block. | ||||
| @@ -117,7 +117,7 @@ static SQLITE_WSD struct Mem5Global { | ||||
|   /* | ||||
|   ** Lists of free blocks.  aiFreelist[0] is a list of free blocks of | ||||
|   ** size mem5.szAtom.  aiFreelist[1] holds blocks of size szAtom*2. | ||||
|   ** and so forth. | ||||
|   ** aiFreelist[2] holds free blocks of size szAtom*4.  And so forth. | ||||
|   */ | ||||
|   int aiFreelist[LOGMAX+1]; | ||||
|  | ||||
| @@ -183,9 +183,7 @@ static void memsys5Link(int i, int iLogsize){ | ||||
| } | ||||
|  | ||||
| /* | ||||
| ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex | ||||
| ** will already be held (obtained by code in malloc.c) if | ||||
| ** sqlite3GlobalConfig.bMemStat is true. | ||||
| ** Obtain or release the mutex needed to access global data structures. | ||||
| */ | ||||
| static void memsys5Enter(void){ | ||||
|   sqlite3_mutex_enter(mem5.mutex); | ||||
| @@ -195,9 +193,8 @@ static void memsys5Leave(void){ | ||||
| } | ||||
|  | ||||
| /* | ||||
| ** Return the size of an outstanding allocation, in bytes.  The | ||||
| ** size returned omits the 8-byte header overhead.  This only | ||||
| ** works for chunks that are currently checked out. | ||||
| ** Return the size of an outstanding allocation, in bytes. | ||||
| ** This only works for chunks that are currently checked out. | ||||
| */ | ||||
| static int memsys5Size(void *p){ | ||||
|   int iSize, i; | ||||
| @@ -230,16 +227,12 @@ static void *memsys5MallocUnsafe(int nByte){ | ||||
|   /* Keep track of the maximum allocation request.  Even unfulfilled | ||||
|   ** requests are counted */ | ||||
|   if( (u32)nByte>mem5.maxRequest ){ | ||||
|     /* Abort if the requested allocation size is larger than the largest | ||||
|     ** power of two that we can represent using 32-bit signed integers. */ | ||||
|     if( nByte > 0x40000000 ) return 0; | ||||
|     mem5.maxRequest = nByte; | ||||
|   } | ||||
|  | ||||
|   /* Abort if the requested allocation size is larger than the largest | ||||
|   ** power of two that we can represent using 32-bit signed integers. | ||||
|   */ | ||||
|   if( nByte > 0x40000000 ){ | ||||
|     return 0; | ||||
|   } | ||||
|  | ||||
|   /* Round nByte up to the next valid power of two */ | ||||
|   for(iFullSz=mem5.szAtom,iLogsize=0; iFullSz<nByte; iFullSz*=2,iLogsize++){} | ||||
|  | ||||
| @@ -398,13 +391,11 @@ static void *memsys5Realloc(void *pPrior, int nBytes){ | ||||
|   if( nBytes<=nOld ){ | ||||
|     return pPrior; | ||||
|   } | ||||
|   memsys5Enter(); | ||||
|   p = memsys5MallocUnsafe(nBytes); | ||||
|   p = memsys5Malloc(nBytes); | ||||
|   if( p ){ | ||||
|     memcpy(p, pPrior, nOld); | ||||
|     memsys5FreeUnsafe(pPrior); | ||||
|     memsys5Free(pPrior); | ||||
|   } | ||||
|   memsys5Leave(); | ||||
|   return p; | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user