3 * (C) Copyright Apr 15 1995, Edmond J. Breen.
5 * This code may be copied for personal, non-profit use only.
22 size_t nbytes; /*sizeof allocated area*/
23 void * p; /*pointer to allocated area*/
24 char mark; /*memory mark: used by garbage collector*/
25 char * crt_file; /*Name of file which asked to creat the memory*/
26 int crt_lineno; /*Line no from which the creation call was made*/
27 unsigned long alloc_num; /*allocation entry number*/
40 memtab_t MTAB[MTABSZ];
41 #define BNO(p) ((unsigned long)p % MTABSZ)
44 #define STDMSG ("line %d in file %s: "\
45 "tot_seen %lu\n",lineno,file,(unsigned long)tot_seen);
49 unsigned long EiC_tot_memory = 0L; /*total amount of memory allocated in bytes*/
50 size_t EiC_tot_alloc = 0; /*total number of current memory allocations*/
51 size_t tot_seen = 0; /*total number of allocations made */
54 int EiC_getMemMark(unsigned long item)
56 /* given an item number, return its mark value */
59 for(i=0;i<MTAB[k].dbuf_no;++i)
60 if(MTAB[k].dbuf[i].alloc_num == item)
61 return MTAB[k].dbuf[i].mark;
65 void EiC_freeMemItem(unsigned long item)
69 for(i=0;i<MTAB[k].dbuf_no;++i)
70 if(MTAB[k].dbuf[i].alloc_num == item) {
71 xfree(MTAB[k].dbuf[i].p);
76 void EiC_ydumpnonmark(char *outfile, char mark)
80 for(i=0;i<MTAB[k].dbuf_no;++i)
81 if(MTAB[k].dbuf[i].mark >= 0) {
82 if(MTAB[k].dbuf[i].mark != mark) {
83 printf("item %ld Create line %d file %s nbytes %lu\n",
84 MTAB[k].dbuf[i].alloc_num,
85 MTAB[k].dbuf[i].crt_lineno,
86 MTAB[k].dbuf[i].crt_file,
87 (unsigned long)MTAB[k].dbuf[i].nbytes);
88 MTAB[k].dbuf[i].mark = MEM_LEAK;
90 MTAB[k].dbuf[i].mark = 0;
94 size_t EiC_xalloc_NextEntryNum(void)
99 void EiC_xalloc_CleanUp(size_t bot, size_t top)
103 for(k=0;k<MTABSZ;k++)
104 for(i=0;i<MTAB[k].dbuf_no;++i)
105 if(MTAB[k].dbuf[i].p)
106 if(MTAB[k].dbuf[i].alloc_num >= bot &&
107 MTAB[k].dbuf[i].alloc_num < top &&
108 MTAB[k].dbuf[i].mark != eicstay)
109 xfree(MTAB[k].dbuf[i].p);
114 void EiC_xfreemark(char mark)
118 for(k=0;k<MTABSZ;k++)
119 for(i=0;i<MTAB[k].dbuf_no;++i)
120 if(MTAB[k].dbuf[i].mark == mark)
121 xfree(MTAB[k].dbuf[i].p);
124 int EiC_ymark(char *file,
125 int lineno, void *p, char mark)
129 assertp(found < 0,STDMSG);
130 MTAB[BNO(p)].dbuf[found].mark = mark;
135 static int install(char *file,
142 extern int EiC_memtraceON;
147 for(i=0;i<MTAB[bno].dbuf_no;++i) { /* search for empty slot */
148 if(MTAB[bno].dbuf[i].p == NULL)
152 if(i >= MTAB[bno].top) {
153 MTAB[bno].top += BUFINC;
155 MTAB[bno].dbuf = (XALLOC*)(calloc)(sizeof(XALLOC),BUFINC + 1);
157 MTAB[bno].dbuf = (XALLOC*)realloc(MTAB[bno].dbuf,
158 sizeof(XALLOC) * (MTAB[bno].top+1));
161 assertp(MTAB[bno].dbuf == NULL,("Out of Memory"));
163 MTAB[bno].dbuf[i].p = p;
164 MTAB[bno].dbuf[i].nbytes = nbytes;
165 MTAB[bno].dbuf[i].mark = XGMARK;
166 MTAB[bno].dbuf[i].crt_file = file;
167 MTAB[bno].dbuf[i].crt_lineno = lineno;
168 EiC_tot_memory += nbytes;
170 if(i>=MTAB[bno].dbuf_no)
173 MTAB[bno].dbuf[i].alloc_num = ++tot_seen;
175 printf("%lu ",(unsigned long)tot_seen);
181 unsigned i,bno = BNO(p);
183 for(i=0; i< MTAB[bno].dbuf_no;i++)
184 if(MTAB[bno].dbuf[i].mark >= 0) {
185 if(MTAB[bno].dbuf[i].p == p)
191 void * EiC_ymalloc(char *file, int lineno, size_t nbytes)
195 pheap = (malloc)(nbytes);
196 assertp(pheap==NULL,STDMSG);
197 install(file,lineno,pheap,nbytes);
202 void * EiC_ycalloc(char *file,
208 pheap = EiC_ymalloc(file, lineno,nelems * elems);
210 memset(pheap,0,nelems * elems);
214 void * EiC_yrealloc(char *file, int lineno, void *oldp, size_t nbytes)
220 found = xlookup(oldp);
221 assertp(found < 0,STDMSG);
224 newp = realloc(oldp,nbytes);
226 assertp(nbytes && newp == NULL,("line %d in file %s\n",lineno,file));
230 d = nbytes - MTAB[bno].dbuf[found].nbytes;
231 if(bno != BNO(newp)) {
235 MTAB[bno].dbuf[found].p = NULL;
236 MTAB[bno].dbuf[found].mark = freemark;
237 i = install(file,lineno,newp,nbytes);
238 /* retain creation time stamp */
239 MTAB[BNO(newp)].dbuf[i].alloc_num
240 = MTAB[bno].dbuf[found].alloc_num;
243 MTAB[bno].dbuf[found].p = newp;
244 MTAB[bno].dbuf[found].nbytes = nbytes;
245 MTAB[bno].dbuf[found].crt_file = file;
246 MTAB[bno].dbuf[found].crt_lineno = lineno;
250 install(file,lineno,newp,nbytes);
256 void EiC_yfree(char *file, int lineno, void * p)
258 int found,bno = BNO(p);
261 /*EiC_warningerror("free non-xalloc ptr: from %s line %d", file, lineno);*/
264 EiC_tot_memory -= MTAB[bno].dbuf[found].nbytes;
267 MTAB[bno].dbuf[found].p = NULL;
268 MTAB[bno].dbuf[found].mark = freemark;