diff options
author | drh <drh@noemail.net> | 2017-03-10 15:55:54 +0000 |
---|---|---|
committer | drh <drh@noemail.net> | 2017-03-10 15:55:54 +0000 |
commit | e43d6aee45208884330cd23a64d2e38771b8f5e4 (patch) | |
tree | a872e979834fb2792118cba0e83ddf4ee84c0488 /src/malloc.c | |
parent | a2df53bd619520ce4ea8be314bed25d998a13947 (diff) | |
download | sqlite-e43d6aee45208884330cd23a64d2e38771b8f5e4.tar.gz sqlite-e43d6aee45208884330cd23a64d2e38771b8f5e4.zip |
Add the SQLITE_MAX_MEMORY compile-time option that provides a hard upper bound
on the amount of memory that SQLite will use, per process.
FossilOrigin-Name: 77dfe2abdae88dea81217f352d87e5ba2c822715
Diffstat (limited to 'src/malloc.c')
-rw-r--r-- | src/malloc.c | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/malloc.c b/src/malloc.c index 0fdc8d73f..b942f47b2 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -217,7 +217,7 @@ static void sqlite3MallocAlarm(int nByte){ ** Do a memory allocation with statistics and alarms. Assume the ** lock is already held. */ -static void mallocWithAlarm(int n, void **pp){ +static void *mallocWithAlarm(int n){ void *p; int nFull; assert( sqlite3_mutex_held(mem0.mutex) ); @@ -230,6 +230,12 @@ static void mallocWithAlarm(int n, void **pp){ ** following xRoundup() call. */ nFull = sqlite3GlobalConfig.m.xRoundup(n); +#ifdef SQLITE_MAX_MEMORY + if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nFull>SQLITE_MAX_MEMORY ){ + return 0; + } +#endif + sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n); if( mem0.alarmThreshold>0 ){ sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); @@ -252,7 +258,7 @@ static void mallocWithAlarm(int n, void **pp){ sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull); sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1); } - *pp = p; + return p; } /* @@ -270,7 +276,7 @@ void *sqlite3Malloc(u64 n){ p = 0; }else if( sqlite3GlobalConfig.bMemstat ){ sqlite3_mutex_enter(mem0.mutex); - mallocWithAlarm((int)n, &p); + p = mallocWithAlarm((int)n); sqlite3_mutex_leave(mem0.mutex); }else{ p = sqlite3GlobalConfig.m.xMalloc((int)n); |