void *sqlite3ScratchMalloc(int n){ void *p; assert( n>0 ); sqlite3_mutex_enter(mem0.mutex); if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){ p = mem0.pScratchFree; mem0.pScratchFree = mem0.pScratchFree->pNext; mem0.nScratchFree--; sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); sqlite3_mutex_leave(mem0.mutex); }else{ if( sqlite3GlobalConfig.bMemstat ){ sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); n = mallocWithAlarm(n, &p); if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); sqlite3_mutex_leave(mem0.mutex); }else{ sqlite3_mutex_leave(mem0.mutex); p = sqlite3GlobalConfig.m.xMalloc(n); } sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); } assert( sqlite3_mutex_notheld(mem0.mutex) ); #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) assert( scratchAllocOut<=1 ); if( p ) scratchAllocOut++; #endif return p; }
/* ** Allocate memory that is to be used and released right away. ** This routine is similar to alloca() in that it is not intended ** for situations where the memory might be held long-term. This ** routine is intended to get memory to old large transient data ** structures that would not normally fit on the stack of an ** embedded processor. */ void *sqlite3ScratchMalloc(int n){ void *p; assert( n>0 ); #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) /* Verify that no more than two scratch allocation per thread ** is outstanding at one time. (This is only checked in the ** single-threaded case since checking in the multi-threaded case ** would be much more complicated.) */ assert( scratchAllocOut<=1 ); #endif if( sqlite3GlobalConfig.szScratch<n ){ goto scratch_overflow; }else{ sqlite3_mutex_enter(mem0.mutex); if( mem0.nScratchFree==0 ){ sqlite3_mutex_leave(mem0.mutex); goto scratch_overflow; }else{ int i; i = mem0.aScratchFree[--mem0.nScratchFree]; i *= sqlite3GlobalConfig.szScratch; sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); sqlite3_mutex_leave(mem0.mutex); p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i]; assert( (((u8*)p - (u8*)0) & 7)==0 ); } } #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) scratchAllocOut = p!=0; #endif return p; scratch_overflow: if( sqlite3GlobalConfig.bMemstat ){ sqlite3_mutex_enter(mem0.mutex); sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); n = mallocWithAlarm(n, &p); if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); sqlite3_mutex_leave(mem0.mutex); }else{ p = sqlite3GlobalConfig.m.xMalloc(n); } sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) scratchAllocOut = p!=0; #endif return p; }
/* ** Allocate memory. This routine is like sqlite3_malloc() except that it ** assumes the memory subsystem has already been initialized. */ void *sqlite3Malloc(int n){ void *p; if( n<=0 ){ p = 0; }else if( sqlite3GlobalConfig.bMemstat ){ sqlite3_mutex_enter(mem0.mutex); mallocWithAlarm(n, &p); sqlite3_mutex_leave(mem0.mutex); }else{ p = sqlite3GlobalConfig.m.xMalloc(n); } return p; }
void *sqlite3Malloc(int n){ void *p; if( n<=0 || n>=0x7fffff00 ){ p = 0; }else if( sqlite3GlobalConfig.bMemstat ){ sqlite3_mutex_enter(mem0.mutex); mallocWithAlarm(n, &p); sqlite3_mutex_leave(mem0.mutex); }else{ p = sqlite3GlobalConfig.m.xMalloc(n); } assert( EIGHT_BYTE_ALIGNMENT(p) ); return p; }
/* ** Allocate memory. This routine is like sqlite3_malloc() except that it ** assumes the memory subsystem has already been initialized. */ void *sqlite3Malloc(int n){ void *p; if( n<=0 || n>=0x7fffff00 ){ /* A memory allocation of a number of bytes which is near the maximum ** signed integer value might cause an integer overflow inside of the ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving ** 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); mallocWithAlarm(n, &p); sqlite3_mutex_leave(mem0.mutex); }else{ p = sqlite3GlobalConfig.m.xMalloc(n); } return p; }
/* ** Allocate memory. This routine is like system_malloc() except that it assumes the memory subsystem has already been initialized. */ void *systemMalloc(int n) { void *p; if (n <= 0|| n >= 0x7fffff00) /* IMP: R-65312-04917 */ /* A memory allocation of a number of bytes which is near the maximum signed integer value might cause an integer overflow inside of the ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving 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 (systemGlobalConfig.bMemstat) { system_mutex_enter(mem0.mutex); mallocWithAlarm(n, &p); system_mutex_leave(mem0.mutex); } else p = systemGlobalConfig.m.xMalloc(n); assert(EIGHT_BYTE_ALIGNMENT(p)); /* IMP: R-04675-44850 */ return p; }
/* ** Allocate memory that is to be used and released right away. This routine is similar to alloca() in that it is not intended ** for situations where the memory might be held long-term. This routine is intended to get memory to old large transient data ** structures that would not normally fit on the stack of an embedded processor. */ void *systemScratchMalloc(int n) { void *p; assert(n > 0); system_mutex_enter(mem0.mutex); if (mem0.nScratchFree && systemGlobalConfig.szScratch >= n) { p = mem0.pScratchFree; mem0.pScratchFree = mem0.pScratchFree->pNext; mem0.nScratchFree--; systemStatusAdd(SYSTEM_MEMSTATUS_SCRATCH_USED, 1); systemStatusSet(SYSTEM_MEMSTATUS_SCRATCH_SIZE, n); system_mutex_leave(mem0.mutex); } else { if (systemGlobalConfig.bMemstat) { systemStatusSet(SYSTEM_MEMSTATUS_SCRATCH_SIZE, n); n = mallocWithAlarm(n, &p); if (p) systemStatusAdd(SYSTEM_MEMSTATUS_SCRATCH_OVERFLOW, n); system_mutex_leave(mem0.mutex); } else { system_mutex_leave(mem0.mutex); p = systemGlobalConfig.m.xMalloc(n); } systemMemdebugSetType(p, MEMTYPE_SCRATCH); } assert(system_mutex_notheld(mem0.mutex)); #if SYSTEM_THREADSAFE==0 && !defined(NDEBUG) /* Verify that no more than two scratch allocations per thread are outstanding at one time. (This is only checked in the ** single-threaded case since checking in the multi-threaded case would be much more complicated.) */ assert(scratchAllocOut <= 1); if (p) scratchAllocOut++; #endif return p; }
/* ** Allocate memory. This routine is like sqlite3_malloc() except that it ** assumes the memory subsystem has already been initialized. */ void *sqlite3Malloc(int n){ void *p; if( n<=0 || NEVER(n>=0x7fffff00) ){ /* The NEVER(n>=0x7fffff00) term is added out of paranoia. We want to make ** absolutely sure that there is nothing within SQLite that can cause a ** memory allocation of a number of bytes which is near the maximum signed ** integer value and thus cause an integer overflow inside of the xMalloc() ** implementation. The n>=0x7fffff00 gives us 255 bytes of headroom. The ** test should never be true because SQLITE_MAX_LENGTH should be much ** less than 0x7fffff00 and it should catch large memory allocations ** before they reach this point. */ p = 0; }else if( sqlite3GlobalConfig.bMemstat ){ sqlite3_mutex_enter(mem0.mutex); mallocWithAlarm(n, &p); sqlite3_mutex_leave(mem0.mutex); }else{ p = sqlite3GlobalConfig.m.xMalloc(n); } return p; }
void *sqlite3PageMalloc(int n){ void *p; assert( n>0 ); assert( (n & (n-1))==0 ); assert( n>=512 && n<=32768 ); if( sqlite3GlobalConfig.szPage<n ){ goto page_overflow; }else{ sqlite3_mutex_enter(mem0.mutex); if( mem0.nPageFree==0 ){ sqlite3_mutex_leave(mem0.mutex); goto page_overflow; }else{ int i; i = mem0.aPageFree[--mem0.nPageFree]; sqlite3_mutex_leave(mem0.mutex); i *= sqlite3GlobalConfig.szPage; sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n); sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1); p = (void*)&((char*)sqlite3GlobalConfig.pPage)[i]; } } return p; page_overflow: if( sqlite3GlobalConfig.bMemstat ){ sqlite3_mutex_enter(mem0.mutex); sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n); n = mallocWithAlarm(n, &p); if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n); sqlite3_mutex_leave(mem0.mutex); }else{ p = sqlite3GlobalConfig.m.xMalloc(n); } return p; }