8 /* FIXME: make allocation dynamic */
11 #ifdef IMAGER_DEBUG_MALLOC
22 static int malloc_need_init = 1;
30 malloc_entry malloc_pointers[MAXMAL];
33 #define mymalloc(x) (mymalloc_file_line(x,__FILE__,__LINE__))
41 mm_log((0,"malloc_state()\n"));
43 for(i=0;i<MAXMAL;i++) if (malloc_pointers[i].point!=NULL) {
44 mm_log((0,"%d: %d (0x%x) : %s\n",i,malloc_pointers[i].size,malloc_pointers[i].point,malloc_pointers[i].comm));
45 total+=malloc_pointers[i].size;
47 if (total==0 ) mm_log((0,"No memory currently used!\n"))
48 else mm_log((0,"total: %d\n",total));
53 mymalloc_file_line(int size, char* file, int line) {
56 if (malloc_need_init) {
57 for(i=0; i<MAXMAL; i++) malloc_pointers[i].point = NULL;
62 /* bndcheck_all(); ACTIVATE FOR LOTS OF THRASHING */
64 if ( (buf = malloc(size+UNDRRNVAL+OVERRNVAL)) == NULL ) { mm_log((1,"Unable to allocate %i for %s (%i)\n", size, file, line)); exit(3); }
65 memset( buf, PADBYTE, UNDRRNVAL );
66 memset( &buf[UNDRRNVAL+size], PADBYTE, OVERRNVAL );
68 mm_log((1,"mymalloc_file_line: real address %p\n", buf));
69 buf += UNDRRNVAL; /* Do this now so we will see the returned address in the logs. */
71 for(i=0;i<MAXMAL;i++) if (malloc_pointers[i].point == NULL) {
72 malloc_pointers[i].size = size;
73 sprintf(malloc_pointers[i].comm,"%s (%d)", file, line);
74 malloc_pointers[i].point = buf;
75 mm_log((1,"mymalloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, size, buf, file, line));
79 mm_log((0,"more than %d segments allocated at %s (%d)\n", MAXMAL, file, line));
87 /* This function not maintained for now */
91 mymalloc_comm(int size,char *comm) {
94 if (malloc_need_init) {
95 for(i=0;i<MAXMAL;i++) malloc_pointers[i].point=NULL;
99 if ((buf=malloc(size))==NULL) { mm_log((1,"Unable to malloc.\n")); exit(3); }
101 for(i=0;i<MAXMAL;i++) if (malloc_pointers[i].point==NULL) {
102 malloc_pointers[i].point=buf;
103 malloc_pointers[i].size=size;
104 strncpy(malloc_pointers[i].comm,comm,MAXDESC-1);
107 mm_log((0,"more than %d segments malloced\n",MAXMAL));
117 size_t s = malloc_pointers[idx].size;
118 unsigned char *pp = malloc_pointers[idx].point;
120 mm_log((1, "bndcheck: No pointer in slot %d\n", idx));
124 for(i=0;i<UNDRRNVAL;i++)
125 if (pp[-(1+i)] != PADBYTE)
126 mm_log((1,"bndcheck: UNDERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
128 for(i=0;i<OVERRNVAL;i++)
129 if (pp[s+i] != PADBYTE)
130 mm_log((1,"bndcheck: OVERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
136 mm_log((1, "bndcheck_all()\n"));
137 for(idx=0; idx<MAXMAL; idx++)
138 if (malloc_pointers[idx].point)
147 myfree_file_line(void *p, char *file, int line) {
152 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].point == p) {
153 mm_log((1,"myfree_file_line: pointer %i (%s) freed at %s (%i)\n", i, malloc_pointers[i].comm, file, line));
155 malloc_pointers[i].point = NULL;
160 mm_log((1, "myfree_file_line: inconsistent refcount %d\n", match));
163 mm_log((1, "myfree_file_line: freeing address %p\n", pp-UNDRRNVAL));
170 #define malloc_comm(a,b) (mymalloc(a))
174 printf("malloc_state: not in debug mode\n");
181 mm_log((1, "mymalloc(size %d)\n", size));
182 if ( (buf=malloc(size)) == NULL ) {
183 mm_log((1, "mymalloc: unable to malloc\n", size));
184 fprintf(stderr,"Unable to malloc.\n"); exit(3);
194 #endif /* IMAGER_MALLOC_DEBUG */
208 if (a<b) return a; else return b;
213 if (a>b) return a; else return b;
217 myread(int fd,void *buf,int len) {
220 bufc = (unsigned char*)buf;
222 while( ((rc=read(fd,bufc+bc,len-bc))>0 ) && (bc!=len) ) bc+=rc;
223 if ( rc < 0 ) return rc;
228 mywrite(int fd,void *buf,int len) {
231 bufc=(unsigned char*)buf;
233 while(((rc=write(fd,bufc+bc,len-bc))>0) && (bc!=len)) bc+=rc;
239 interleave(unsigned char *inbuffer,unsigned char *outbuffer,int rowsize,int channels) {
242 if ( inbuffer==outbuffer ) return; /* Check if data is already in interleaved format */
243 for( ind=0; ind<rowsize; ind++) for (ch=0; ch<channels; ch++) outbuffer[i++] = inbuffer[rowsize*ch+ind];