split IM_INCPATH and IM_LIBPATH with $Config{path_sep}, so they work
[imager.git] / io.c
CommitLineData
02d1d628
AMH
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
2ff8ed30 13#define MAXMAL 102400
02d1d628
AMH
14#define MAXDESC 65
15
a743c0a6
AMH
16#define UNDRRNVAL 10
17#define OVERRNVAL 10
02d1d628
AMH
18
19#define PADBYTE 0xaa
20
21
22static int malloc_need_init = 1;
23
24typedef struct {
fe6163bf 25 void* ptr;
02d1d628 26 size_t size;
fe6163bf 27 char comm[MAXDESC];
02d1d628
AMH
28} malloc_entry;
29
30malloc_entry malloc_pointers[MAXMAL];
31
fe6163bf
AMH
32
33
34
35/* Utility functions */
36
37
38static
39void
aea697ad 40malloc_init(void) {
fe6163bf
AMH
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
48static
49int
50find_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
63static
64void *
65set_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
02d1d628
AMH
76
77
78void
aea697ad 79malloc_state(void) {
fe6163bf
AMH
80 int i, total = 0;
81
cd4b0b20 82 i_clear_error();
02d1d628
AMH
83 mm_log((0,"malloc_state()\n"));
84 bndcheck_all();
fe6163bf
AMH
85 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr != NULL) {
86 mm_log((0,"%d: %d (0x%x) : %s\n", i, malloc_pointers[i].size, malloc_pointers[i].ptr, malloc_pointers[i].comm));
87 total += malloc_pointers[i].size;
02d1d628 88 }
fe6163bf
AMH
89 if (total == 0) mm_log((0,"No memory currently used!\n"))
90 else mm_log((0,"total: %d\n",total));
02d1d628
AMH
91}
92
93
fe6163bf 94
02d1d628 95void*
fe6163bf 96mymalloc_file_line(size_t size, char* file, int line) {
02d1d628
AMH
97 char *buf;
98 int i;
fe6163bf
AMH
99 if (malloc_need_init) malloc_init();
100
101 /* bndcheck_all(); Uncomment for LOTS OF THRASHING */
102
103 if ( (i = find_ptr(NULL)) < 0 ) {
104 mm_log((0,"more than %d segments allocated at %s (%d)\n", MAXMAL, file, line));
105 exit(3);
02d1d628
AMH
106 }
107
fe6163bf
AMH
108 if ( (buf = malloc(size+UNDRRNVAL+OVERRNVAL)) == NULL ) {
109 mm_log((1,"Unable to allocate %i for %s (%i)\n", size, file, line));
110 exit(3);
02d1d628
AMH
111 }
112
fe6163bf
AMH
113 buf = set_entry(i, buf, size, file, line);
114 mm_log((1,"mymalloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, size, buf, file, line));
115 return buf;
02d1d628
AMH
116}
117
118
119
120
02d1d628 121
fe6163bf
AMH
122
123
02d1d628 124void*
fe6163bf
AMH
125myrealloc_file_line(void *ptr, size_t newsize, char* file, int line) {
126 char *buf;
02d1d628 127 int i;
fe6163bf
AMH
128
129 if (malloc_need_init) malloc_init();
130 /* bndcheck_all(); ACTIVATE FOR LOTS OF THRASHING */
131
132 if (!ptr) {
133 mm_log((1, "realloc called with ptr = NULL, sending request to malloc\n"));
134 return mymalloc_file_line(newsize, file, line);
02d1d628
AMH
135 }
136
fe6163bf
AMH
137 if (!newsize) {
138 mm_log((1, "newsize = 0, sending request to free\n"));
139 myfree_file_line(ptr, file, line);
140 return NULL;
141 }
02d1d628 142
fe6163bf
AMH
143 if ( (i = find_ptr(ptr)) == -1) {
144 mm_log((0, "Unable to find %p in realloc for %s (%i)\n", ptr, file, line));
145 exit(3);
146 }
147
aea697ad 148 if ( (buf = realloc(((char *)ptr)-UNDRRNVAL, UNDRRNVAL+OVERRNVAL+newsize)) == NULL ) {
fe6163bf
AMH
149 mm_log((1,"Unable to reallocate %i bytes at %p for %s (%i)\n", newsize, ptr, file, line));
150 exit(3);
02d1d628 151 }
fe6163bf
AMH
152
153 buf = set_entry(i, buf, newsize, file, line);
154 mm_log((1,"realloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, newsize, buf, file, line));
155 return buf;
02d1d628 156}
fe6163bf
AMH
157
158
159
02d1d628
AMH
160
161static
162void
163bndcheck(int idx) {
164 int i;
165 size_t s = malloc_pointers[idx].size;
fe6163bf 166 unsigned char *pp = malloc_pointers[idx].ptr;
02d1d628
AMH
167 if (!pp) {
168 mm_log((1, "bndcheck: No pointer in slot %d\n", idx));
169 return;
170 }
171
172 for(i=0;i<UNDRRNVAL;i++)
fe6163bf
AMH
173 if (pp[-(1+i)] != PADBYTE)
174 mm_log((1,"bndcheck: UNDERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
02d1d628 175
fe6163bf 176 for(i=0;i<OVERRNVAL;i++)
02d1d628
AMH
177 if (pp[s+i] != PADBYTE)
178 mm_log((1,"bndcheck: OVERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
179}
180
181void
182bndcheck_all() {
183 int idx;
184 mm_log((1, "bndcheck_all()\n"));
76ff75b8 185 for(idx=0; idx<MAXMAL; idx++)
fe6163bf 186 if (malloc_pointers[idx].ptr)
02d1d628
AMH
187 bndcheck(idx);
188}
189
190
191
192
193
194void
195myfree_file_line(void *p, char *file, int line) {
196 char *pp = p;
197 int match = 0;
198 int i;
199
fe6163bf 200 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr == p) {
02d1d628
AMH
201 mm_log((1,"myfree_file_line: pointer %i (%s) freed at %s (%i)\n", i, malloc_pointers[i].comm, file, line));
202 bndcheck(i);
fe6163bf 203 malloc_pointers[i].ptr = NULL;
02d1d628
AMH
204 match++;
205 }
206
207 if (match != 1) {
f1ac5027 208 mm_log((1, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line));
aea697ad 209 fprintf(stderr, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line);
a73aeb5f 210 exit(255);
02d1d628
AMH
211 }
212
a73aeb5f 213 mm_log((1, "myfree_file_line: freeing address %p (real %p)\n", pp, pp-UNDRRNVAL));
02d1d628
AMH
214
215 free(pp-UNDRRNVAL);
216}
217
218#else
219
220#define malloc_comm(a,b) (mymalloc(a))
221
222void
223malloc_state() {
224 printf("malloc_state: not in debug mode\n");
225}
226
227void*
228mymalloc(int size) {
229 void *buf;
230
a743c0a6 231 if ( (buf = malloc(size)) == NULL ) {
faa9b3e7 232 mm_log((1, "mymalloc: unable to malloc %d\n", size));
cc6483e0 233 fprintf(stderr,"Unable to malloc %d.\n", size); exit(3);
02d1d628 234 }
f1ac5027 235 mm_log((1, "mymalloc(size %d) -> %p\n", size, buf));
02d1d628
AMH
236 return buf;
237}
238
239void
240myfree(void *p) {
dd55acc8 241 mm_log((1, "myfree(p %p)\n", p));
02d1d628
AMH
242 free(p);
243}
244
faa9b3e7
TC
245void *
246myrealloc(void *block, size_t size) {
247 void *result;
248
249 mm_log((1, "myrealloc(block %p, size %u)\n", block, size));
250 if ((result = realloc(block, size)) == NULL) {
251 mm_log((1, "myrealloc: out of memory\n"));
252 fprintf(stderr, "Out of memory.\n");
253 exit(3);
254 }
255 return result;
256}
257
02d1d628
AMH
258#endif /* IMAGER_MALLOC_DEBUG */
259
260
261
262
263
264
5473b91d 265/* Should these really be here? */
02d1d628
AMH
266
267#undef min
268#undef max
269
270int
271min(int a,int b) {
272 if (a<b) return a; else return b;
273}
274
275int
276max(int a,int b) {
277 if (a>b) return a; else return b;
278}
279