mirror of
https://github.com/sqlite/sqlite.git
synced 2025-08-08 14:02:16 +03:00
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
This commit is contained in:
12
src/malloc.c
12
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);
|
||||
|
Reference in New Issue
Block a user