No-op the sqlite3_memory_alarm() interface in a different way, that does

not break legacy memory behavior.  This is a re-do of
check-in [5d3f5df4da9f40d5].

FossilOrigin-Name: 8250e2a487ee12c9a2dea5603ab60aed51e5dc7b
This commit is contained in:
drh 2015-09-10 01:22:09 +00:00
parent f5eac36a64
commit 5fb72e5f3e
3 changed files with 52 additions and 68 deletions

View File

@ -1,5 +1,5 @@
C Fix\sharmless\scompiler\swarning\sin\sFTS5.
D 2015-09-09T23:54:46.415
C No-op\sthe\ssqlite3_memory_alarm()\sinterface\sin\sa\sdifferent\sway,\sthat\sdoes\nnot\sbreak\slegacy\smemory\sbehavior.\s\sThis\sis\sa\sre-do\sof\ncheck-in\s[5d3f5df4da9f40d5].
D 2015-09-10T01:22:09.178
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in f85066ce844a28b671aaeeff320921cd0ce36239
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@ -305,7 +305,7 @@ F src/legacy.c ba1863ea58c4c840335a84ec276fc2b25e22bc4e
F src/lempar.c d344a95d60c24e2f490ee59db9784b1b17439012
F src/loadext.c dfcee8c7c032cd0fd55af3e0fc1fcfb01e426df2
F src/main.c e17fcffae4306a9b8334faf3bac80d7396850b54
F src/malloc.c 021012e28a81ffdabf4c30ec3df6ce1f6cc93f1d
F src/malloc.c 3a37ce6979a40f499d8cea9e9ab4e8517854d35d
F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645
F src/mem1.c abe6ee469b6c5a35c7f22bfeb9c9bac664a1c987
F src/mem2.c f1940d9e91948dd6a908fbb9ce3835c36b5d83c3
@ -1383,7 +1383,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P 8d2ed150a7a15626965cf994ef48c3ab61eca6ec
R 9c684262bd2bed914ee53743d3728f7e
U mistachkin
Z 5aaf25a787ca8169e60e163e7c3cad6b
P 86146a731d75eb25279c0e072c0bdda593de905d
R 7c5553c63d5721a9d16f1642ebb06bea
U drh
Z 0fce0dbaade5235ccf22a67355771bfc

View File

@ -1 +1 @@
86146a731d75eb25279c0e072c0bdda593de905d
8250e2a487ee12c9a2dea5603ab60aed51e5dc7b

View File

@ -45,7 +45,7 @@ typedef struct ScratchFreeslot {
*/
static SQLITE_WSD struct Mem0Global {
sqlite3_mutex *mutex; /* Mutex to serialize access */
sqlite3_int64 alarmThreshold; /* The soft heap limit */
sqlite3_int64 alarmThreshold; /* The soft heap limit */
/*
** Pointers to the end of sqlite3GlobalConfig.pScratch memory
@ -73,59 +73,20 @@ sqlite3_mutex *sqlite3MallocMutex(void){
return mem0.mutex;
}
/*
** Return the amount of memory currently in use.
*/
static sqlite3_int64 memInUse(void){
assert( sqlite3_mutex_held(mem0.mutex) );
return sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
}
/*
** Called when the soft heap limit is exceeded for an allocation
** of nBytes.
*/
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
static void sqlite3HeapLimitExceeded(int nByte){
sqlite3_int64 excess = memInUse() + nByte - mem0.alarmThreshold;
sqlite3_mutex_leave(mem0.mutex);
sqlite3_release_memory((int)(excess & 0x7fffffff));
sqlite3_mutex_enter(mem0.mutex);
}
#else
# define sqlite3HeapLimitExceeded(X) /* no-op */
#endif
/*
** Check to see if increasing the total memory usage by nNew bytes
** will exceed the soft heap limit.
**
** If the soft heap limit is exceeded, set the mem0.nearlyFull flag
** and invoke sqlite3HeapLimitExceeded() to try to free up some
** memory.
*/
static void sqlite3CheckSoftHeapLimit(int nNew){
assert( sqlite3_mutex_held(mem0.mutex) );
if( mem0.alarmThreshold>0 ){
if( mem0.alarmThreshold-nNew >= memInUse() ){
mem0.nearlyFull = 1;
sqlite3HeapLimitExceeded(nNew);
}else{
mem0.nearlyFull = 0;
}
}
}
#ifndef SQLITE_OMIT_DEPRECATED
/*
** Deprecated external interface. First deprecated 2007-11-05. Changed
** into a no-op on 2015-09-02.
** Deprecated external interface. It used to set an alarm callback
** that was invoked when memory usage grew too large. Now it is a
** no-op.
*/
int sqlite3_memory_alarm(
void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
void *pArg,
sqlite3_int64 iThreshold
){
(void)xCallback;
(void)pArg;
(void)iThreshold;
return SQLITE_OK;
}
#endif
@ -136,20 +97,24 @@ int sqlite3_memory_alarm(
*/
sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
sqlite3_int64 priorLimit;
sqlite3_int64 excess;
sqlite3_int64 nUsed;
#ifndef SQLITE_OMIT_AUTOINIT
int rc = sqlite3_initialize();
if( rc ) return -1;
#endif
sqlite3_mutex_enter(mem0.mutex);
priorLimit = mem0.alarmThreshold;
if( n>0 ){
mem0.alarmThreshold = n;
sqlite3CheckSoftHeapLimit(0);
}else if( n==0 ){
mem0.alarmThreshold = 0;
mem0.nearlyFull = 0;
if( n<0 ){
sqlite3_mutex_leave(mem0.mutex);
return priorLimit;
}
mem0.alarmThreshold = n;
nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
mem0.nearlyFull = (n>0 && n<=nUsed);
sqlite3_mutex_leave(mem0.mutex);
excess = sqlite3_memory_used() - n;
if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
return priorLimit;
}
void sqlite3_soft_heap_limit(int n){
@ -240,6 +205,16 @@ sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
return mx;
}
/*
** Trigger the alarm
*/
static void sqlite3MallocAlarm(int nByte){
if( mem0.alarmThreshold<=0 ) return;
sqlite3_mutex_leave(mem0.mutex);
sqlite3_release_memory(nByte);
sqlite3_mutex_enter(mem0.mutex);
}
/*
** Do a memory allocation with statistics and alarms. Assume the
** lock is already held.
@ -250,11 +225,19 @@ static int mallocWithAlarm(int n, void **pp){
assert( sqlite3_mutex_held(mem0.mutex) );
nFull = sqlite3GlobalConfig.m.xRoundup(n);
sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
sqlite3CheckSoftHeapLimit(nFull);
if( mem0.alarmThreshold>0 ){
sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
if( nUsed >= mem0.alarmThreshold - nFull ){
mem0.nearlyFull = 1;
sqlite3MallocAlarm(nFull);
}else{
mem0.nearlyFull = 0;
}
}
p = sqlite3GlobalConfig.m.xMalloc(nFull);
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
if( p==0 && mem0.alarmThreshold ){
sqlite3HeapLimitExceeded(nFull);
if( p==0 && mem0.alarmThreshold>0 ){
sqlite3MallocAlarm(nFull);
p = sqlite3GlobalConfig.m.xMalloc(nFull);
}
#endif
@ -537,14 +520,15 @@ void *sqlite3Realloc(void *pOld, u64 nBytes){
sqlite3_mutex_enter(mem0.mutex);
sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
nDiff = nNew - nOld;
sqlite3CheckSoftHeapLimit(nDiff);
if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
mem0.alarmThreshold-nDiff ){
sqlite3MallocAlarm(nDiff);
}
pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
if( pNew==0 && mem0.alarmThreshold ){
sqlite3HeapLimitExceeded((int)nBytes);
if( pNew==0 && mem0.alarmThreshold>0 ){
sqlite3MallocAlarm((int)nBytes);
pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
}
#endif
if( pNew ){
nNew = sqlite3MallocSize(pNew);
sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);