Update the changelog
[opencv] / apps / cvenv / EiC / ymem.c
1 /* ymem.c
2  *
3  *      (C) Copyright Apr 15 1995, Edmond J. Breen.
4  *                 ALL RIGHTS RESERVED.
5  * This code may be copied for personal, non-profit use only.
6  *
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include "xalloc.h"
13 #include "assertp.h"
14 #include "global.h"
15
16 char XGMARK = 0;
17
18 #define freemark -1
19
20 /*CUT XALLOC_struct*/
21 typedef struct {
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*/
28 }XALLOC;
29 /*END CUT*/
30
31
32
33 typedef struct {
34     int dbuf_no;
35     int top;
36     XALLOC *dbuf;
37 }memtab_t;
38
39 #define  MTABSZ  203
40 memtab_t MTAB[MTABSZ];
41 #define  BNO(p) ((unsigned long)p % MTABSZ)  
42 #define  BUFINC  10
43
44 #define STDMSG ("line %d in file %s: "\
45                 "tot_seen  %lu\n",lineno,file,(unsigned long)tot_seen);
46
47 /*CUT XALLOC_array*/
48
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 */
52 /*END CUT*/
53
54 int EiC_getMemMark(unsigned long item)
55 {
56     /* given an item number, return its mark value */
57     size_t i,k;
58     for(k=0;k<MTABSZ;k++)
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;
62     return -1;
63 }
64
65 void EiC_freeMemItem(unsigned long item)
66 {
67     size_t i,k;
68     for(k=0;k<MTABSZ;k++)
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);
72                 return ;
73             }
74 }
75
76 void EiC_ydumpnonmark(char *outfile, char mark)
77 {
78     size_t i,k;
79     for(k=0;k<MTABSZ;k++) 
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;
89                 } else
90                     MTAB[k].dbuf[i].mark = 0;
91             }
92 }
93
94 size_t EiC_xalloc_NextEntryNum(void)
95 {
96     return tot_seen + 1;
97 }
98
99 void EiC_xalloc_CleanUp(size_t bot, size_t top)
100 {
101     size_t i,k;
102
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);
110 }
111
112
113
114 void EiC_xfreemark(char mark)
115 {
116     size_t i,k;
117
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);
122 }
123
124 int EiC_ymark(char *file,
125           int lineno, void *p, char mark)
126 {
127     int found;
128     found = xlookup(p);
129     assertp(found < 0,STDMSG);
130     MTAB[BNO(p)].dbuf[found].mark = mark;
131
132     return 1;
133 }
134
135 static int install(char *file,
136                    int lineno,
137                    void *p,
138                    size_t nbytes)
139 {
140     int bno;
141     unsigned i;
142     extern int EiC_memtraceON;
143
144
145     bno = BNO(p);
146     
147     for(i=0;i<MTAB[bno].dbuf_no;++i) {  /* search for empty slot */
148         if(MTAB[bno].dbuf[i].p == NULL)
149             break;
150     }
151     
152     if(i >= MTAB[bno].top) {
153         MTAB[bno].top += BUFINC;
154         if(!MTAB[bno].dbuf)
155             MTAB[bno].dbuf = (XALLOC*)(calloc)(sizeof(XALLOC),BUFINC + 1);
156         else
157             MTAB[bno].dbuf = (XALLOC*)realloc(MTAB[bno].dbuf,
158                                    sizeof(XALLOC) * (MTAB[bno].top+1));
159     }
160     
161     assertp(MTAB[bno].dbuf == NULL,("Out of Memory"));
162     
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;
169     EiC_tot_alloc++;
170     if(i>=MTAB[bno].dbuf_no)
171         MTAB[bno].dbuf_no++;
172     
173     MTAB[bno].dbuf[i].alloc_num = ++tot_seen;
174     if(EiC_memtraceON)
175         printf("%lu ",(unsigned long)tot_seen);
176     return i;
177 }
178
179 int xlookup(void *p)
180 {
181     unsigned i,bno = BNO(p);
182     
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)
186                 return i;
187         }
188     return -1;
189 }
190
191 void * EiC_ymalloc(char *file, int lineno, size_t nbytes)
192 {
193     void * pheap;
194
195     pheap = (malloc)(nbytes);
196     assertp(pheap==NULL,STDMSG);
197     install(file,lineno,pheap,nbytes);
198
199     return pheap;
200 }       
201
202 void * EiC_ycalloc(char *file,
203                int lineno,
204                size_t nelems,
205                size_t elems)
206 {
207     void * pheap;
208     pheap = EiC_ymalloc(file, lineno,nelems * elems);
209     if(pheap)
210         memset(pheap,0,nelems * elems);
211     return pheap;
212 }
213
214 void * EiC_yrealloc(char *file, int lineno, void *oldp, size_t nbytes)
215 {
216     void *newp;
217     int found, d;
218     
219     if(oldp != NULL) {
220         found =  xlookup(oldp);
221         assertp(found < 0,STDMSG);
222     }
223     
224     newp = realloc(oldp,nbytes);
225     
226     assertp(nbytes && newp == NULL,("line %d in file %s\n",lineno,file));
227     
228     if(oldp) {
229         int bno = BNO(oldp);
230         d = nbytes - MTAB[bno].dbuf[found].nbytes;
231         if(bno != BNO(newp)) {
232
233             int i;
234             
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;
241
242         } else {
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;
247         }
248         EiC_tot_memory += d;
249     } else
250         install(file,lineno,newp,nbytes);
251     
252
253     return newp;
254 }
255
256 void EiC_yfree(char *file, int lineno, void * p)
257 {
258     int found,bno = BNO(p);
259     found = xlookup(p);
260     if(found < 0) {
261         /*EiC_warningerror("free non-xalloc ptr: from %s line %d", file, lineno);*/
262         (free)(p);
263     } else {
264         EiC_tot_memory -= MTAB[bno].dbuf[found].nbytes;
265         EiC_tot_alloc--;
266         (free)(p);
267         MTAB[bno].dbuf[found].p = NULL;
268         MTAB[bno].dbuf[found].mark = freemark;
269     }
270 }