#include "sqliteInt.h"
#ifdef SQLITE_ENABLE_MEMSYS5
typedef struct Mem5Link Mem5Link;
struct Mem5Link {
int next;
int prev;
};
#define LOGMAX 30
#define CTRL_LOGSIZE 0x1f
#define CTRL_FREE 0x20
static SQLITE_WSD struct Mem5Global {
int szAtom;
int nBlock;
u8 *zPool;
sqlite3_mutex *mutex;
#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
u64 nAlloc;
u64 totalAlloc;
u64 totalExcess;
u32 currentOut;
u32 currentCount;
u32 maxOut;
u32 maxCount;
u32 maxRequest;
#endif
int aiFreelist[LOGMAX+1];
u8 *aCtrl;
} mem5;
#define mem5 GLOBAL(struct Mem5Global, mem5)
#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
static void memsys5Unlink(int i, int iLogsize){
int next, prev;
assert( i>=0 && i<mem5.nBlock );
assert( iLogsize>=0 && iLogsize<=LOGMAX );
assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
next = MEM5LINK(i)->next;
prev = MEM5LINK(i)->prev;
if( prev<0 ){
mem5.aiFreelist[iLogsize] = next;
}else{
MEM5LINK(prev)->next = next;
}
if( next>=0 ){
MEM5LINK(next)->prev = prev;
}
}
static void memsys5Link(int i, int iLogsize){
int x;
assert( sqlite3_mutex_held(mem5.mutex) );
assert( i>=0 && i<mem5.nBlock );
assert( iLogsize>=0 && iLogsize<=LOGMAX );
assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
MEM5LINK(i)->prev = -1;
if( x>=0 ){
assert( x<mem5.nBlock );
MEM5LINK(x)->prev = i;
}
mem5.aiFreelist[iLogsize] = i;
}
static void memsys5Enter(void){
sqlite3_mutex_enter(mem5.mutex);
}
static void memsys5Leave(void){
sqlite3_mutex_leave(mem5.mutex);
}
static int memsys5Size(void *p){
int iSize, i;
assert( p!=0 );
i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
assert( i>=0 && i<mem5.nBlock );
iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
return iSize;
}
static void *memsys5MallocUnsafe(int nByte){
int i;
int iBin;
int iFullSz;
int iLogsize;
assert( nByte>0 );
if( nByte > 0x40000000 ) return 0;
#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
if( (u32)nByte>mem5.maxRequest ){
mem5.maxRequest = nByte;
}
#endif
for(iFullSz=mem5.szAtom,iLogsize=0; iFullSz<nByte; iFullSz*=2,iLogsize++){}
for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
if( iBin>LOGMAX ){
testcase( sqlite3GlobalConfig.xLog!=0 );
sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
return 0;
}
i = mem5.aiFreelist[iBin];
memsys5Unlink(i, iBin);
while( iBin>iLogsize ){
int newSize;
iBin--;
newSize = 1 << iBin;
mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
memsys5Link(i+newSize, iBin);
}
mem5.aCtrl[i] = iLogsize;
#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
mem5.nAlloc++;
mem5.totalAlloc += iFullSz;
mem5.totalExcess += iFullSz - nByte;
mem5.currentCount++;
mem5.currentOut += iFullSz;
if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
#endif
#ifdef SQLITE_DEBUG
memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
#endif
return (void*)&mem5.zPool[i*mem5.szAtom];
}
static void memsys5FreeUnsafe(void *pOld){
u32 size, iLogsize;
int iBlock;
iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
assert( iBlock>=0 && iBlock<mem5.nBlock );
assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
size = 1<<iLogsize;
assert( iBlock+size-1<(u32)mem5.nBlock );
mem5.aCtrl[iBlock] |= CTRL_FREE;
mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
assert( mem5.currentCount>0 );
assert( mem5.currentOut>=(size*mem5.szAtom) );
mem5.currentCount--;
mem5.currentOut -= size*mem5.szAtom;
assert( mem5.currentOut>0 || mem5.currentCount==0 );
assert( mem5.currentCount>0 || mem5.currentOut==0 );
#endif
mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
while( ALWAYS(iLogsize<LOGMAX) ){
int iBuddy;
if( (iBlock>>iLogsize) & 1 ){
iBuddy = iBlock - size;
assert( iBuddy>=0 );
}else{
iBuddy = iBlock + size;
if( iBuddy>=mem5.nBlock ) break;
}
if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
memsys5Unlink(iBuddy, iLogsize);
iLogsize++;
if( iBuddy<iBlock ){
mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
mem5.aCtrl[iBlock] = 0;
iBlock = iBuddy;
}else{
mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
mem5.aCtrl[iBuddy] = 0;
}
size *= 2;
}
#ifdef SQLITE_DEBUG
memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
#endif
memsys5Link(iBlock, iLogsize);
}
static void *memsys5Malloc(int nBytes){
sqlite3_int64 *p = 0;
if( nBytes>0 ){
memsys5Enter();
p = memsys5MallocUnsafe(nBytes);
memsys5Leave();
}
return (void*)p;
}
static void memsys5Free(void *pPrior){
assert( pPrior!=0 );
memsys5Enter();
memsys5FreeUnsafe(pPrior);
memsys5Leave();
}
static void *memsys5Realloc(void *pPrior, int nBytes){
int nOld;
void *p;
assert( pPrior!=0 );
assert( (nBytes&(nBytes-1))==0 );
assert( nBytes>=0 );
if( nBytes==0 ){
return 0;
}
nOld = memsys5Size(pPrior);
if( nBytes<=nOld ){
return pPrior;
}
p = memsys5Malloc(nBytes);
if( p ){
memcpy(p, pPrior, nOld);
memsys5Free(pPrior);
}
return p;
}
static int memsys5Roundup(int n){
int iFullSz;
if( n<=mem5.szAtom*2 ){
if( n<=mem5.szAtom ) return mem5.szAtom;
return mem5.szAtom*2;
}
if( n>0x10000000 ){
if( n>0x40000000 ) return 0;
if( n>0x20000000 ) return 0x40000000;
return 0x20000000;
}
for(iFullSz=mem5.szAtom*8; iFullSz<n; iFullSz *= 4);
if( (iFullSz/2)>=(i64)n ) return iFullSz/2;
return iFullSz;
}
static int memsys5Log(int iValue){
int iLog;
for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
return iLog;
}
static int memsys5Init(void *NotUsed){
int ii;
int nByte;
u8 *zByte;
int nMinLog;
int iOffset;
UNUSED_PARAMETER(NotUsed);
mem5.mutex = 0;
assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
nByte = sqlite3GlobalConfig.nHeap;
zByte = (u8*)sqlite3GlobalConfig.pHeap;
assert( zByte!=0 );
nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
mem5.szAtom = (1<<nMinLog);
while( (int)sizeof(Mem5Link)>mem5.szAtom ){
mem5.szAtom = mem5.szAtom << 1;
}
mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
mem5.zPool = zByte;
mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
for(ii=0; ii<=LOGMAX; ii++){
mem5.aiFreelist[ii] = -1;
}
iOffset = 0;
for(ii=LOGMAX; ii>=0; ii--){
int nAlloc = (1<<ii);
if( (iOffset+nAlloc)<=mem5.nBlock ){
mem5.aCtrl[iOffset] = ii | CTRL_FREE;
memsys5Link(iOffset, ii);
iOffset += nAlloc;
}
assert((iOffset+nAlloc)>mem5.nBlock);
}
if( sqlite3GlobalConfig.bMemstat==0 ){
mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
}
return SQLITE_OK;
}
static void memsys5Shutdown(void *NotUsed){
UNUSED_PARAMETER(NotUsed);
mem5.mutex = 0;
return;
}
#ifdef SQLITE_TEST
void sqlite3Memsys5Dump(const char *zFilename){
FILE *out;
int i, j, n;
int nMinLog;
if( zFilename==0 || zFilename[0]==0 ){
out = stdout;
}else{
out = fopen(zFilename, "w");
if( out==0 ){
fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
zFilename);
return;
}
}
memsys5Enter();
nMinLog = memsys5Log(mem5.szAtom);
for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
}
fprintf(out, "mem5.nAlloc = %llu\n", mem5.nAlloc);
fprintf(out, "mem5.totalAlloc = %llu\n", mem5.totalAlloc);
fprintf(out, "mem5.totalExcess = %llu\n", mem5.totalExcess);
fprintf(out, "mem5.currentOut = %u\n", mem5.currentOut);
fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
fprintf(out, "mem5.maxOut = %u\n", mem5.maxOut);
fprintf(out, "mem5.maxCount = %u\n", mem5.maxCount);
fprintf(out, "mem5.maxRequest = %u\n", mem5.maxRequest);
memsys5Leave();
if( out==stdout ){
fflush(stdout);
}else{
fclose(out);
}
}
#endif
const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
static const sqlite3_mem_methods memsys5Methods = {
memsys5Malloc,
memsys5Free,
memsys5Realloc,
memsys5Size,
memsys5Roundup,
memsys5Init,
memsys5Shutdown,
0
};
return &memsys5Methods;
}
#endif