int EiC_ymark(char *file, int lineno, void *p, char mark) { int found; found = xlookup(p); assertp(found < 0,STDMSG); MTAB[BNO(p)].dbuf[found].mark = mark; return 1; }
int xlookup(void *p) { unsigned i,bno = BNO(p); for(i=0; i< MTAB[bno].dbuf_no;i++) if(MTAB[bno].dbuf[i].mark >= 0) { if(MTAB[bno].dbuf[i].p == p) return i; } return -1; }
void * EiC_yrealloc(char *file, int lineno, void *oldp, size_t nbytes) { void *newp; int found, d; if(oldp != NULL) { found = xlookup(oldp); assertp(found < 0,STDMSG); } newp = realloc(oldp,nbytes); assertp(nbytes && newp == NULL,("line %d in file %s\n",lineno,file)); if(oldp) { int bno = BNO(oldp); d = nbytes - MTAB[bno].dbuf[found].nbytes; if(bno != BNO(newp)) { int i; MTAB[bno].dbuf[found].p = NULL; MTAB[bno].dbuf[found].mark = freemark; i = install(file,lineno,newp,nbytes); /* retain creation time stamp */ MTAB[BNO(newp)].dbuf[i].alloc_num = MTAB[bno].dbuf[found].alloc_num; } else { MTAB[bno].dbuf[found].p = newp; MTAB[bno].dbuf[found].nbytes = nbytes; MTAB[bno].dbuf[found].crt_file = file; MTAB[bno].dbuf[found].crt_lineno = lineno; } EiC_tot_memory += d; } else install(file,lineno,newp,nbytes); return newp; }
static int install(char *file, int lineno, void *p, size_t nbytes) { int bno; unsigned i; extern int EiC_memtraceON; bno = BNO(p); for(i=0;i<MTAB[bno].dbuf_no;++i) { /* search for empty slot */ if(MTAB[bno].dbuf[i].p == NULL) break; } if(i >= MTAB[bno].top) { MTAB[bno].top += BUFINC; if(!MTAB[bno].dbuf) { MTAB[bno].dbuf = (XALLOC*)(calloc)(sizeof(XALLOC),BUFINC + 1); } else { MTAB[bno].dbuf = (XALLOC*)realloc(MTAB[bno].dbuf, sizeof(XALLOC) * (MTAB[bno].top+1)); } } assertp(MTAB[bno].dbuf == NULL,("Out of Memory")); MTAB[bno].dbuf[i].p = p; MTAB[bno].dbuf[i].nbytes = nbytes; MTAB[bno].dbuf[i].mark = XGMARK; MTAB[bno].dbuf[i].crt_file = file; MTAB[bno].dbuf[i].crt_lineno = lineno; EiC_tot_memory += nbytes; EiC_tot_alloc++; if(i>=MTAB[bno].dbuf_no) MTAB[bno].dbuf_no++; MTAB[bno].dbuf[i].alloc_num = ++tot_seen; if(EiC_memtraceON) printf("%lu ",(unsigned long)tot_seen); return i; }
void EiC_yfree(char *file, int lineno, void * p) { int found,bno = BNO(p); found = xlookup(p); if(found < 0) { /*EiC_warningerror("free non-xalloc ptr: from %s line %d", file, lineno);*/ (free)(p); } else { EiC_tot_memory -= MTAB[bno].dbuf[found].nbytes; EiC_tot_alloc--; (free)(p); MTAB[bno].dbuf[found].p = NULL; MTAB[bno].dbuf[found].mark = freemark; } }
void * EiC_yrealloc(char *file, int lineno, void *oldp, size_t nbytes) { void *newp; int found = 0, d, bOld = 0, bno = 0; //maks if(nbytes <= 0 && oldp) { //maks: realloc definition EiC_yfree(file, lineno, oldp); return 0; } if(oldp != NULL) { found = xlookup(oldp); assertp(found < 0,STDMSG); bOld = 1; //maks bno = BNO(oldp); } if(GetGameMode()) { unsigned long total = EiC_tot_memory + nbytes; if(total > gedMaxGameMem) //maks { //Don't alloc more then gedMaxGameMem EiC_error("Attempt to access %lu bytes.\n If is correct, set the variable gedMaxGameMem=%lu or more\n in an action before the allocation", nbytes, total); nbytes = 4; } } realAllocatedMem = nbytes; newp = realloc(oldp,nbytes); assertp(nbytes && newp == NULL,("line %d in file %s\n",lineno,file)); if(bOld) //maks { d = nbytes - MTAB[bno].dbuf[found].nbytes; EiC_tot_memory -= MTAB[bno].dbuf[found].nbytes; //maks if(bno != BNO(newp)) { int i; MTAB[bno].dbuf[found].p = NULL; MTAB[bno].dbuf[found].mark = freemark; i = install(file,lineno,newp,nbytes); /* retain creation time stamp */ MTAB[BNO(newp)].dbuf[i].alloc_num = MTAB[bno].dbuf[found].alloc_num; } else { MTAB[bno].dbuf[found].p = newp; MTAB[bno].dbuf[found].nbytes = nbytes; MTAB[bno].dbuf[found].crt_file = file; MTAB[bno].dbuf[found].crt_lineno = lineno; EiC_tot_memory += nbytes; //maks } } else { install(file,lineno,newp,nbytes); } return newp; }