From: dan Date: Thu, 19 Jul 2012 20:27:14 +0000 (+0000) Subject: Further tweaks to malloc paths. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fheads%2Fcalloc;p=thirdparty%2Fsqlite.git Further tweaks to malloc paths. FossilOrigin-Name: 0a33444105e29d79475e267021d33c1a006b85a3 --- diff --git a/manifest b/manifest index 64151250ab..de219ac256 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Remove\sanother\sbranch\sfrom\sthe\sDbMalloc()\spaths. -D 2012-07-18T16:07:24.009 +C Further\stweaks\sto\smalloc\spaths. +D 2012-07-19T20:27:14.139 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 8f6d858bf3df9978ba43df19985146a1173025e4 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -146,9 +146,9 @@ F src/legacy.c a199d7683d60cef73089e892409113e69c23a99f F src/lempar.c 0ee69fca0be54cd93939df98d2aca4ca46f44416 F src/loadext.c f20382fbaeec832438a1ba7797bee3d3c8a6d51d F src/main.c 6241183d894ff51a2049bd6bf246aea877d648b5 -F src/malloc.c 9321b18a9224a4e4823129bd3e1e34cb3c18a7cb +F src/malloc.c 57121e3ef46431b95a73651f9b5653f96867e358 F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645 -F src/mem1.c 5ac4fb90adf0aa2beb912abafc67100a6ce126dc +F src/mem1.c 7ed7d5cd9a6ec97cc881ea80d996f6a16e8b7426 F src/mem2.c e307323e86b5da1853d7111b68fd6b84ad6f09cf F src/mem3.c 61c9d47b792908c532ca3a62b999cf21795c6534 F src/mem5.c c2c63b7067570b00bf33d751c39af24182316f7f @@ -1005,7 +1005,7 @@ F tool/tostr.awk e75472c2f98dd76e06b8c9c1367f4ab07e122d06 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381 -P 75c095ee463c0c76e9637edccd33e807f837eb4e -R 4b3f5fc3616c1d2a9a882bbf6ef4a9dd +P defd828e90a5494e58cc2cd14c8f4cce7fd2bb2b +R 62a7b8739dbc0504b7043c6629065afd U dan -Z d1a34a26ab7e5c2fc76d0e075bca051a +Z f8c7a516451a2fad10c61fa887e0e873 diff --git a/manifest.uuid b/manifest.uuid index 46f3e9cf56..215799ae9f 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -defd828e90a5494e58cc2cd14c8f4cce7fd2bb2b \ No newline at end of file +0a33444105e29d79475e267021d33c1a006b85a3 \ No newline at end of file diff --git a/src/malloc.c b/src/malloc.c index ee2d67ffd1..4b118d5c81 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -263,17 +263,23 @@ static void *mallocWithAlarm( void *(*xAlloc)(int), /* Memory allocation function */ int n /* Bytes of memory to allocate */ ){ - int nFull; - void *p; + void *p; /* Pointer to allocated memory */ + int nFull; /* Value returned by xRoundup (if required) */ assert( sqlite3_mutex_held(mem0.mutex) ); assert( xAlloc==sqlite3GlobalConfig.m.xMalloc || xAlloc==sqlite3GlobalConfig.m.xCalloc ); - nFull = sqlite3GlobalConfig.m.xRoundup(n); + + /* Note: At one point this function would call xRoundup() and use the + ** resulting value as the argument to xAlloc(). However, this is not + ** required. And it is a (small) drag on performance. */ + /* nFull = sqlite3GlobalConfig.m.xRoundup(n); */ + sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); - if( mem0.alarmCallback!=0 ){ + if( mem0.alarmCallback ){ int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); + nFull = sqlite3GlobalConfig.m.xRoundup(n); if( nUsed >= mem0.alarmThreshold - nFull ){ mem0.nearlyFull = 1; sqlite3MallocAlarm(nFull); @@ -281,7 +287,8 @@ static void *mallocWithAlarm( mem0.nearlyFull = 0; } } - p = xAlloc(nFull); + + p = xAlloc(n); #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT if( p==0 && mem0.alarmCallback ){ sqlite3MallocAlarm(nFull); @@ -289,9 +296,11 @@ static void *mallocWithAlarm( } #endif if( p ){ - nFull = sqlite3MallocSize(p); - sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); + sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p)); sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1); + }else{ + testcase( sqlite3GlobalConfig.xLog!=0 ); + sqlite3_log(SQLITE_NOMEM, "failed to allocate %d bytes of memory", n); } return p; @@ -301,6 +310,7 @@ static void *mallocWithAlarm( ** Use allocator function xAlloc to allocate n bytes of memory. */ static void *memAllocate( + sqlite3 *db, void *(*xAlloc)(int), /* Memory allocation function */ int n /* Bytes of space to allocate */ ){ @@ -314,14 +324,23 @@ static void *memAllocate( ** 255 bytes of overhead. SQLite itself will never use anything near ** this amount. The only way to reach the limit is with sqlite3_malloc() */ p = 0; - }else if( sqlite3GlobalConfig.bMemstat ){ - sqlite3_mutex_enter(mem0.mutex); - p = mallocWithAlarm(xAlloc, n); - sqlite3_mutex_leave(mem0.mutex); }else{ - p = xAlloc(n); + if( sqlite3GlobalConfig.bMemstat ){ + sqlite3_mutex_enter(mem0.mutex); + p = mallocWithAlarm(xAlloc, n); + sqlite3_mutex_leave(mem0.mutex); + }else{ + p = xAlloc(n); + } + if( !p && db ) db->mallocFailed = 1; } + assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-04675-44850 */ + if( db ){ + sqlite3MemdebugSetType(p, MEMTYPE_DB | + ((db && db->lookaside.sz) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP) + ); + } return p; } @@ -331,14 +350,14 @@ static void *memAllocate( ** assumes the memory subsystem has already been initialized. */ void *sqlite3Malloc(int n){ - return memAllocate(sqlite3GlobalConfig.m.xMalloc, n); + return memAllocate(0, sqlite3GlobalConfig.m.xMalloc, n); } /* ** Allocate and zero memory. */ void *sqlite3MallocZero(int n){ - return memAllocate(sqlite3GlobalConfig.m.xCalloc, n); + return memAllocate(0, sqlite3GlobalConfig.m.xCalloc, n); } /* @@ -622,6 +641,7 @@ static void *lookasideAlloc(sqlite3 *db, int n){ db->lookaside.mxOut = db->lookaside.nOut; } } + sqlite3MemdebugSetType((void *)pBuf, MEMTYPE_DB | MEMTYPE_LOOKASIDE); return (void*)pBuf; } return 0; @@ -650,22 +670,15 @@ static void *lookasideAlloc(sqlite3 *db, int n){ */ void *sqlite3DbMallocZero(sqlite3 *db, int n){ void *p; - if( db==0 ){ - p = memAllocate(sqlite3GlobalConfig.m.xCalloc, n); - }else if( db->mallocFailed ){ - p = 0; - }else{ + if( db ){ + if( db->mallocFailed ) return 0; if( (p = lookasideAlloc(db, n)) ){ memset(p, 0, n); - }else{ - p = memAllocate(sqlite3GlobalConfig.m.xCalloc, n); - if( !p ) db->mallocFailed = 1; + return p; } } - sqlite3MemdebugSetType(p, MEMTYPE_DB | - ((db && db->lookaside.sz) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); - return p; + return memAllocate(db, sqlite3GlobalConfig.m.xCalloc, n); } /* @@ -678,21 +691,14 @@ void *sqlite3DbMallocZero(sqlite3 *db, int n){ */ void *sqlite3DbMallocRaw(sqlite3 *db, int n){ void *p; - if( db==0 ){ - p = memAllocate(sqlite3GlobalConfig.m.xMalloc, n); - }else if( db->mallocFailed ){ - p = 0; - }else{ - p = lookasideAlloc(db, n); - if( !p ){ - p = memAllocate(sqlite3GlobalConfig.m.xMalloc, n); - if( !p ) db->mallocFailed = 1; + if( db ){ + if( db->mallocFailed ) return 0; + if( (p = lookasideAlloc(db, n)) ){ + return p; } } - sqlite3MemdebugSetType(p, MEMTYPE_DB | - ((db && db->lookaside.sz) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); - return p; + return memAllocate(db, sqlite3GlobalConfig.m.xMalloc, n); } /* diff --git a/src/mem1.c b/src/mem1.c index 8e4b66ec01..f84a42abae 100644 --- a/src/mem1.c +++ b/src/mem1.c @@ -101,15 +101,6 @@ static malloc_zone_t* _sqliteZone_; #endif /* __APPLE__ or not __APPLE__ */ -/* -** A memory allocation of nByte bytes has failed. Log an error message -** using sqlite3_log(). -*/ -static void logAllocationError(int nByte){ - testcase( sqlite3GlobalConfig.xLog!=0 ); - sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte); -} - /* ** Allocate nByte bytes of memory. ** @@ -118,20 +109,15 @@ static void logAllocationError(int nByte){ ** routines. */ static void *sqlite3MemMalloc(int nByte){ - i64 *p; #ifdef SQLITE_MALLOCSIZE - p = SQLITE_MALLOC(nByte); - if( p==0 ){ + return SQLITE_MALLOC( ROUND8(nByte) ); #else + i64 *p; nByte = ROUND8(nByte); p = SQLITE_MALLOC(nByte+8); - if( p ){ - *(p++) = (i64)nByte; - }else{ -#endif - logAllocationError(nByte); - } + if( p ) *(p++) = (i64)nByte; return (void *)p; +#endif } /* @@ -142,20 +128,15 @@ static void *sqlite3MemMalloc(int nByte){ ** routines. */ static void *sqlite3MemCalloc(int nByte){ - i64 *p; #ifdef SQLITE_MALLOCSIZE - p = SQLITE_CALLOC(nByte); - if( p==0 ){ + return SQLITE_CALLOC( ROUND8(nByte) ); #else + i64 *p; nByte = ROUND8(nByte); p = SQLITE_CALLOC(nByte+8); - if( p ){ - *(p++) = (i64)nByte; - }else{ -#endif - logAllocationError(nByte); - } + if( p ) *(p++) = (i64)nByte; return (void *)p; +#endif } /* @@ -301,7 +282,7 @@ void sqlite3MemSetDefault(void){ 0, sqlite3MemCalloc }; - sqlite3_config(SQLITE_CONFIG_MALLOC2, &defaultMethods); + sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods); } #endif /* SQLITE_SYSTEM_MALLOC */