Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 308 lines (220 sloc) 6.282 kB
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
1 #include "io.h"
2 #include <stdlib.h>
3 #ifndef _MSC_VER
4 #include <unistd.h>
5 #endif
6
7
8 /* FIXME: make allocation dynamic */
9
10
11 #ifdef IMAGER_DEBUG_MALLOC
12
2ff8ed3 Added more logging to quant.c. Removed global variables from gif.c.
Arnar Mar Hrafnkelsson authored
13 #define MAXMAL 102400
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
14 #define MAXDESC 65
15
a743c0a Removed a bunch of unused variables and fixed an attempt to print out a
Arnar Mar Hrafnkelsson authored
16 #define UNDRRNVAL 10
17 #define OVERRNVAL 10
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
18
19 #define PADBYTE 0xaa
20
21
22 static int malloc_need_init = 1;
23
24 typedef struct {
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
25 void* ptr;
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
26 size_t size;
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
27 char comm[MAXDESC];
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
28 } malloc_entry;
29
30 malloc_entry malloc_pointers[MAXMAL];
31
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
32
33
34
35 /* Utility functions */
36
37
38 static
39 void
40 malloc_init() {
41 int i;
42 for(i=0; i<MAXMAL; i++) malloc_pointers[i].ptr = NULL;
43 malloc_need_init = 0;
44 atexit(malloc_state);
45 }
46
47
48 static
49 int
50 find_ptr(void *p) {
51 int i;
52 for(i=0;i<MAXMAL;i++)
53 if (malloc_pointers[i].ptr == p)
54 return i;
55 return -1;
56 }
57
58
59 /* Takes a pointer to real start of array,
60 * sets the entries in the table, returns
61 * the offset corrected pointer */
62
63 static
64 void *
65 set_entry(int i, char *buf, size_t size, char *file, int line) {
66 memset( buf, PADBYTE, UNDRRNVAL );
67 memset( &buf[UNDRRNVAL+size], PADBYTE, OVERRNVAL );
68 buf += UNDRRNVAL;
69 malloc_pointers[i].ptr = buf;
70 malloc_pointers[i].size = size;
71 sprintf(malloc_pointers[i].comm,"%s (%d)", file, line);
72 return buf;
73 }
74
75
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
76
77
78 void
79 malloc_state() {
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
80 int i, total = 0;
81
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
82 mm_log((0,"malloc_state()\n"));
83 bndcheck_all();
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
84 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr != NULL) {
85 mm_log((0,"%d: %d (0x%x) : %s\n", i, malloc_pointers[i].size, malloc_pointers[i].ptr, malloc_pointers[i].comm));
86 total += malloc_pointers[i].size;
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
87 }
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
88 if (total == 0) mm_log((0,"No memory currently used!\n"))
89 else mm_log((0,"total: %d\n",total));
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
90 }
91
92
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
93
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
94 void*
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
95 mymalloc_file_line(size_t size, char* file, int line) {
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
96 char *buf;
97 int i;
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
98 if (malloc_need_init) malloc_init();
99
100 /* bndcheck_all(); Uncomment for LOTS OF THRASHING */
101
102 if ( (i = find_ptr(NULL)) < 0 ) {
103 mm_log((0,"more than %d segments allocated at %s (%d)\n", MAXMAL, file, line));
104 exit(3);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
105 }
106
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
107 if ( (buf = malloc(size+UNDRRNVAL+OVERRNVAL)) == NULL ) {
108 mm_log((1,"Unable to allocate %i for %s (%i)\n", size, file, line));
109 exit(3);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
110 }
111
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
112 buf = set_entry(i, buf, size, file, line);
113 mm_log((1,"mymalloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, size, buf, file, line));
114 return buf;
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
115 }
116
117
118
119
120
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
121
122
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
123 void*
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
124 myrealloc_file_line(void *ptr, size_t newsize, char* file, int line) {
125 char *buf;
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
126 int i;
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
127
128 if (malloc_need_init) malloc_init();
129 /* bndcheck_all(); ACTIVATE FOR LOTS OF THRASHING */
130
131 if (!ptr) {
132 mm_log((1, "realloc called with ptr = NULL, sending request to malloc\n"));
133 return mymalloc_file_line(newsize, file, line);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
134 }
135
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
136 if (!newsize) {
137 mm_log((1, "newsize = 0, sending request to free\n"));
138 myfree_file_line(ptr, file, line);
139 return NULL;
140 }
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
141
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
142 if ( (i = find_ptr(ptr)) == -1) {
143 mm_log((0, "Unable to find %p in realloc for %s (%i)\n", ptr, file, line));
144 exit(3);
145 }
146
147 if ( (buf = realloc(ptr-UNDRRNVAL, UNDRRNVAL+OVERRNVAL+newsize) == NULL) ) {
148 mm_log((1,"Unable to reallocate %i bytes at %p for %s (%i)\n", newsize, ptr, file, line));
149 exit(3);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
150 }
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
151
152 buf = set_entry(i, buf, newsize, file, line);
153 mm_log((1,"realloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, newsize, buf, file, line));
154 return buf;
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
155 }
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
156
157
158
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
159
160 static
161 void
162 bndcheck(int idx) {
163 int i;
164 size_t s = malloc_pointers[idx].size;
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
165 unsigned char *pp = malloc_pointers[idx].ptr;
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
166 if (!pp) {
167 mm_log((1, "bndcheck: No pointer in slot %d\n", idx));
168 return;
169 }
170
171 for(i=0;i<UNDRRNVAL;i++)
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
172 if (pp[-(1+i)] != PADBYTE)
173 mm_log((1,"bndcheck: UNDERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
174
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
175 for(i=0;i<OVERRNVAL;i++)
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
176 if (pp[s+i] != PADBYTE)
177 mm_log((1,"bndcheck: OVERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
178 }
179
180 void
181 bndcheck_all() {
182 int idx;
183 mm_log((1, "bndcheck_all()\n"));
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
184 for(idx=0; idx<MAXMAL; idx++)
185 if (malloc_pointers[idx].ptr)
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
186 bndcheck(idx);
187 }
188
189
190
191
192
193 void
194 myfree_file_line(void *p, char *file, int line) {
195 char *pp = p;
196 int match = 0;
197 int i;
198
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
199 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr == p) {
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
200 mm_log((1,"myfree_file_line: pointer %i (%s) freed at %s (%i)\n", i, malloc_pointers[i].comm, file, line));
201 bndcheck(i);
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored
202 malloc_pointers[i].ptr = NULL;
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
203 match++;
204 }
205
206 if (match != 1) {
f1ac502 support for generic fills for box and arc, with solid, hatched
Tony Cook authored
207 mm_log((1, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line));
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
208 }
209
210 mm_log((1, "myfree_file_line: freeing address %p\n", pp-UNDRRNVAL));
211
212 free(pp-UNDRRNVAL);
213 }
214
215 #else
216
217 #define malloc_comm(a,b) (mymalloc(a))
218
219 void
220 malloc_state() {
221 printf("malloc_state: not in debug mode\n");
222 }
223
224 void*
225 mymalloc(int size) {
226 void *buf;
227
a743c0a Removed a bunch of unused variables and fixed an attempt to print out a
Arnar Mar Hrafnkelsson authored
228 if ( (buf = malloc(size)) == NULL ) {
faa9b3e Egads
Tony Cook authored
229 mm_log((1, "mymalloc: unable to malloc %d\n", size));
cc6483e an extra stipple
Tony Cook authored
230 fprintf(stderr,"Unable to malloc %d.\n", size); exit(3);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
231 }
f1ac502 support for generic fills for box and arc, with solid, hatched
Tony Cook authored
232 mm_log((1, "mymalloc(size %d) -> %p\n", size, buf));
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
233 return buf;
234 }
235
236 void
237 myfree(void *p) {
dd55acc Switched all of jpeg to iolayer functions, removed older functions an…
Arnar Mar Hrafnkelsson authored
238 mm_log((1, "myfree(p %p)\n", p));
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
239 free(p);
240 }
241
faa9b3e Egads
Tony Cook authored
242 void *
243 myrealloc(void *block, size_t size) {
244 void *result;
245
246 mm_log((1, "myrealloc(block %p, size %u)\n", block, size));
247 if ((result = realloc(block, size)) == NULL) {
248 mm_log((1, "myrealloc: out of memory\n"));
249 fprintf(stderr, "Out of memory.\n");
250 exit(3);
251 }
252 return result;
253 }
254
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
255 #endif /* IMAGER_MALLOC_DEBUG */
256
257
258
259
260
261
262
263
264 #undef min
265 #undef max
266
267 int
268 min(int a,int b) {
269 if (a<b) return a; else return b;
270 }
271
272 int
273 max(int a,int b) {
274 if (a>b) return a; else return b;
275 }
276
277 int
278 myread(int fd,void *buf,int len) {
279 unsigned char* bufc;
280 int bc,rc;
281 bufc = (unsigned char*)buf;
282 bc=0;
283 while( ((rc=read(fd,bufc+bc,len-bc))>0 ) && (bc!=len) ) bc+=rc;
284 if ( rc < 0 ) return rc;
285 else return bc;
286 }
287
288 int
289 mywrite(int fd,void *buf,int len) {
290 unsigned char* bufc;
291 int bc,rc;
292 bufc=(unsigned char*)buf;
293 bc=0;
294 while(((rc=write(fd,bufc+bc,len-bc))>0) && (bc!=len)) bc+=rc;
295 if (rc<0) return rc;
296 else return bc;
297 }
298
299 void
300 interleave(unsigned char *inbuffer,unsigned char *outbuffer,int rowsize,int channels) {
301 int ch,ind,i;
302 i=0;
303 if ( inbuffer==outbuffer ) return; /* Check if data is already in interleaved format */
304 for( ind=0; ind<rowsize; ind++) for (ch=0; ch<channels; ch++) outbuffer[i++] = inbuffer[rowsize*ch+ind];
305 }
306
307
Something went wrong with that request. Please try again.