57 #ifdef SQLITE_ENABLE_MEMSYS5 83 #define CTRL_LOGSIZE 0x1f 84 #define CTRL_FREE 0x20 135 #define mem5 GLOBAL(struct Mem5Global, mem5) 141 #define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom])) 147 static void memsys5Unlink(
int i,
int iLogsize){
156 mem5.aiFreelist[iLogsize] = next;
169 static void memsys5Link(
int i,
int iLogsize){
182 mem5.aiFreelist[iLogsize] = i;
190 static void memsys5Enter(
void){
193 static void memsys5Leave(
void){
202 static int memsys5Size(
void *p){
205 int i = (int)(((
u8 *)p-
mem5.zPool)/
mem5.szAtom);
233 if( (
u32)nByte>
mem5.maxRequest ){
234 mem5.maxRequest = nByte;
240 if( nByte > 0x40000000 ){
245 for(iFullSz=
mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
251 for(iBin=iLogsize; iBin<=
LOGMAX &&
mem5.aiFreelist[iBin]<0; iBin++){}
254 sqlite3_log(SQLITE_NOMEM,
"failed to allocate %u bytes", nByte);
257 i =
mem5.aiFreelist[iBin];
258 memsys5Unlink(i, iBin);
259 while( iBin>iLogsize ){
265 memsys5Link(i+newSize, iBin);
267 mem5.aCtrl[i] = iLogsize;
271 mem5.totalAlloc += iFullSz;
272 mem5.totalExcess += iFullSz - nByte;
274 mem5.currentOut += iFullSz;
285 return (
void*)&
mem5.zPool[i*
mem5.szAtom];
298 iBlock = (int)(((
u8 *)pOld-
mem5.zPool)/
mem5.szAtom);
314 mem5.currentOut -= size*
mem5.szAtom;
321 if( (iBlock>>iLogsize) & 1 ){
322 iBuddy = iBlock - size;
324 iBuddy = iBlock + size;
327 if( (iBuddy+(1<<iLogsize))>
mem5.nBlock )
break;
329 memsys5Unlink(iBuddy, iLogsize);
333 mem5.aCtrl[iBlock] = 0;
337 mem5.aCtrl[iBuddy] = 0;
348 memsys5Link(iBlock, iLogsize);
354 static void *memsys5Malloc(
int nBytes){
370 static void memsys5Free(
void *pPrior){
393 assert( (nBytes&(nBytes-1))==0 );
398 nOld = memsys5Size(pPrior);
423 if( n > 0x40000000 )
return 0;
424 for(iFullSz=
mem5.szAtom; iFullSz<n; iFullSz *= 2);
438 static int memsys5Log(
int iValue){
440 for(iLog=0; (iLog<(int)((
sizeof(
int)*8)-1)) && (1<<iLog)<iValue; iLog++);
473 mem5.szAtom = (1<<nMinLog);
478 mem5.nBlock = (nByte / (
mem5.szAtom+
sizeof(
u8)));
482 for(ii=0; ii<=
LOGMAX; ii++){
483 mem5.aiFreelist[ii] = -1;
487 for(ii=
LOGMAX; ii>=0; ii--){
488 int nAlloc = (1<<ii);
489 if( (iOffset+nAlloc)<=
mem5.nBlock ){
491 memsys5Link(iOffset, ii);
508 static void memsys5Shutdown(
void *NotUsed){
519 void sqlite3Memsys5Dump(
const char *zFilename){
524 if( zFilename==0 || zFilename[0]==0 ){
527 out = fopen(zFilename,
"w");
529 fprintf(stderr,
"** Unable to output memory debug output log: %s **\n",
535 nMinLog = memsys5Log(
mem5.szAtom);
536 for(i=0; i<=
LOGMAX && i+nMinLog<32; i++){
537 for(n=0, j=
mem5.aiFreelist[i]; j>=0; j =
MEM5LINK(j)->next, n++){}
538 fprintf(out,
"freelist items of size %d: %d\n",
mem5.szAtom << i, n);
540 fprintf(out,
"mem5.nAlloc = %llu\n",
mem5.nAlloc);
541 fprintf(out,
"mem5.totalAlloc = %llu\n",
mem5.totalAlloc);
542 fprintf(out,
"mem5.totalExcess = %llu\n",
mem5.totalExcess);
543 fprintf(out,
"mem5.currentOut = %u\n",
mem5.currentOut);
544 fprintf(out,
"mem5.currentCount = %u\n",
mem5.currentCount);
545 fprintf(out,
"mem5.maxOut = %u\n",
mem5.maxOut);
546 fprintf(out,
"mem5.maxCount = %u\n",
mem5.maxCount);
547 fprintf(out,
"mem5.maxRequest = %u\n",
mem5.maxRequest);
573 return &memsys5Methods;
void * memset(void *b, int c, size_t len)
void ** sqlite3_mem_methods
void * memsys5Realloc(void *pPrior, int nBytes)
const sqlite3_mem_methods * sqlite3MemGetMemsys5(void)
void * memsys5MallocUnsafe(int nByte)
void memsys5FreeUnsafe(void *pOld)
#define sqlite3_mutex_enter(x)
#define sqlite3_mutex_leave(x)
int memsys5Init(void *NotUsed)
#define UNUSED_PARAMETER(x)
#define sqlite3GlobalConfig
int memsys5Roundup(int n)
#define sqlite3MutexAlloc(x)
void * memcpy(void *dst, const void *src, size_t n)