File: error.c

package info (click to toggle)
links 0.84-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,184 kB
  • ctags: 2,048
  • sloc: ansic: 16,116; sh: 395; makefile: 49; awk: 33
file content (513 lines) | stat: -rw-r--r-- 11,877 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#include "links.h"

#ifdef LEAK_DEBUG
long mem_amount = 0;
long last_mem_amount = -1;
#ifdef MAX_DEBUG_SIZE
long debug_sizes[MAX_DEBUG_SIZE];
long last_debug_sizes[MAX_DEBUG_SIZE];
#endif
#ifdef MAX_LIST_SIZE
struct md memory_list[MAX_LIST_SIZE];
struct md last_memory_list[MAX_LIST_SIZE];
#endif
#endif

static inline void force_dump()
{
	fprintf(stderr, "\n\033[1mForcing core dump\033[0m\n");
	raise(SIGSEGV);
}

void check_memory_leaks()
{
#ifdef LEAK_DEBUG
	if (mem_amount) {
		fprintf(stderr, "\n\033[1mMemory leak by %ld bytes\033[0m\n", mem_amount);
#ifdef MAX_DEBUG_SIZE
		{
			int i;
			int r = 0;
			fprintf(stderr, "Differences: ");
			for (i = 0; i < MAX_DEBUG_SIZE; i++) if (debug_sizes[i])
				fprintf(stderr, "%s%ld x %d", r ? ", " : "", debug_sizes[i], i), r = 1;
			fprintf(stderr, "\n");
		}
#ifdef MAX_LIST_SIZE
		{
			int i;
			int r = 0;
			fprintf(stderr, "List: ");
			for (i = 0; i < MAX_LIST_SIZE; i++) if (memory_list[i].p) {
				fprintf(stderr, "%s%p:%d @ %s:%d", r ? ", " : "", memory_list[i].p, (int)memory_list[i].size, memory_list[i].file, memory_list[i].line), r = 1;
				if (memory_list[i].comment) fprintf(stderr, ":\"%s\"", memory_list[i].comment);
			}
			fprintf(stderr, "\n");
		}
#endif
#endif
		force_dump();
	}
#endif
}

void err(int b, unsigned char *m, va_list l)
{
	if (b) fprintf(stderr, "%c", (char)7);
	vfprintf(stderr, m, l);
	fprintf(stderr, "\n");
	sleep(1);
}

void error(unsigned char *m, ...)
{
	va_list l;
	va_start(l, m);
	err(1, m, l);
}

int errline;
unsigned char *errfile;

unsigned char errbuf[4096];

void int_error(unsigned char *m, ...)
{
	va_list l;
	va_start(l, m);
	sprintf(errbuf, "\033[1mINTERNAL ERROR\033[0m at %s:%d: ", errfile, errline);
	strcat(errbuf, m);
	err(1, errbuf, l);
	force_dump();
}

void debug_msg(unsigned char *m, ...)
{
	va_list l;
	va_start(l, m);
	sprintf(errbuf, "DEBUG MESSAGE at %s:%d: ", errfile, errline);
	strcat(errbuf, m);
	err(0, errbuf, l);
}

#ifdef LEAK_DEBUG

void *debug_mem_alloc(unsigned char *file, int line, size_t size)
{
	void *p;
	if (!size) return DUMMY;
#ifdef LEAK_DEBUG
	mem_amount += size;
#ifdef MAX_DEBUG_SIZE
	if (size < MAX_DEBUG_SIZE) debug_sizes[size]++;
#endif
	size += L_D_S;
#endif
	if (!(p = xmalloc(size))) {
		error("ERROR: out of memory (malloc returned NULL)\n");
		return NULL;
	}
#ifdef LEAK_DEBUG
	*(size_t *)p = size - L_D_S;
	p = (char *)p + L_D_S;
#ifdef MAX_LIST_SIZE
	{
		long i;
		for (i = 0; i < MAX_LIST_SIZE; i++) if (!memory_list[i].p) {
			memory_list[i].p = p;
			memory_list[i].size = size;
			memory_list[i].file = file;
			memory_list[i].line = line;
			memory_list[i].comment = NULL;
			goto b;
		}
		debug("warning: memory list overflow");
		b:;
	}
#endif
#endif
	return p;
}

void debug_mem_free(unsigned char *file, int line, void *p)
{
	if (p == DUMMY) return;
	if (!p) {
		errfile = file, errline = line, int_error("mem_free(NULL)");
		return;
	}
#ifdef LEAK_DEBUG
#ifdef MAX_LIST_SIZE
	{
		long i;
		for (i = 0; i < MAX_LIST_SIZE; i++) if (memory_list[i].p == p) {
			memory_list[i].p = NULL;
			goto b;
		}
		errfile = file, errline = line, int_error("free: address %p not from malloc", p);
		b:;
	}
#endif
	p = (char *)p - L_D_S;
	mem_amount -= *(size_t *)p;
#ifdef MAX_DEBUG_SIZE
	if (*(int *)p < MAX_DEBUG_SIZE) debug_sizes[*(int *)p]--;
#endif
#endif
	xfree(p);
}

void *debug_mem_realloc(unsigned char *file, int line, void *p, size_t size)
{
#ifdef LEAK_DEBUG
#ifdef MAX_LIST_SIZE
	void *q = p;
#endif
#endif
	if (p == DUMMY) return debug_mem_alloc(file, line, size);
	if (!p) {
		errfile = file, errline = line, int_error("mem_realloc(NULL, %d)", size);
		return NULL;
	}
	if (!size) {
		debug_mem_free(file, line, p);
		return DUMMY;
	}
#ifdef LEAK_DEBUG
	p = (char *)p - L_D_S;
	mem_amount += size - *(size_t *)p;
#ifdef MAX_DEBUG_SIZE
	if (size < MAX_DEBUG_SIZE) debug_sizes[size]++;
	if (*(int *)p < MAX_DEBUG_SIZE) debug_sizes[*(int *)p]--;
#endif
	*(size_t *)p = size;
	size += L_D_S;
#endif
	if (!(p = xrealloc(p, size))) {
		error("ERROR: out of memory (realloc returned NULL)\n");
		return NULL;
	}
#ifdef LEAK_DEBUG
	p = (char *)p + L_D_S;
#ifdef MAX_LIST_SIZE
	{
		long i;
		for (i = 0; i < MAX_LIST_SIZE; i++) if (memory_list[i].p == q) {
			memory_list[i].p = p;
			memory_list[i].size = size;
			goto b;
		}
		errfile = file, errline = line, int_error("realloc: address %p not from malloc", p);
		b:;
	}
#endif
#endif
	return p;
}

void set_mem_comment(void *p, unsigned char *c, int l)
{
#ifdef MAX_LIST_SIZE
	long i;
	for (i = 0; i < MAX_LIST_SIZE; i++) if (memory_list[i].p == p) {
		if ((memory_list[i].comment = malloc(l + 1)))
			memcpy(memory_list[i].comment, c, l),
			memory_list[i].comment[l] = 0;
		goto b;
	}
	internal("set_mem_comment: not allocated");
	b:
#endif
}

#endif

#ifdef SPECIAL_MALLOC

struct malloc_chunk {
	struct malloc_block *ptr;
	int free;
};

struct malloc_block {
	int next:16;
#ifdef DEBUG
	int size:15;
	int free:1;
#else
	int size:16;
#endif
};

struct malloc_chunk *chunks = DUMMY;
int n_chunks = 0;
struct malloc_chunk *last_chunk = NULL;
int last_f_chunk;

#ifdef DEBUG

void sp_check()
{
	int i;
	for (i = 0; i < n_chunks; i++) {
		struct malloc_block *pr = chunks[i].ptr;
		while (pr->next) {
			pr = (struct malloc_block *)((char *)pr + pr->next);
			if (pr->size & (sizeof(struct malloc_block)-1)) internal("unaligned block; chunk %d, block %p", i, pr);
			if (pr->next & (sizeof(struct malloc_block)-1)) internal("unaligned block; chunk %d, block %p", i, pr);
			if (!pr->free) internal("block on freelist is not free; chunk %d, block %p", i, pr);
			if (pr < chunks[i].ptr || (void *)pr > (void *)((char *)chunks[i].ptr + CHUNK_SIZE)) internal("out of chunk %d, pointer %p @ 1", i, pr);
		}
		pr = chunks[i].ptr;
		while (pr->next) {
			pr = (struct malloc_block *)((char *)pr + pr->size);
			if (pr->size & (sizeof(struct malloc_block)-1)) internal("unaligned block; chunk %d, block %p", i, pr);
			if (pr->free && ((pr->next & sizeof(struct malloc_block)-1))) internal("unaligned block; chunk %d, block %p", i, pr);
			if (pr < chunks[i].ptr || (void *)pr > (void *)((char *)chunks[i].ptr + CHUNK_SIZE)) internal("out of chunk %d, pointer %p @ 1", i, pr);
		}
	}
}

#else

inline void sp_check() {}

#endif

void new_chunk()
{
	struct malloc_block *p;
	if (!(p = malloc(CHUNK_SIZE))) return;
	if (!(n_chunks & ALLOC_GR)) {
		struct malloc_chunk *ch;
		if (n_chunks) ch = realloc(chunks, (n_chunks + ALLOC_GR) * sizeof(struct malloc_chunk));
		else ch = malloc(ALLOC_GR * sizeof(struct malloc_chunk));
		if (!ch) {
			free(p);
			return;
		}
		chunks = ch;
	}
	last_chunk = &chunks[n_chunks++];
	last_chunk->ptr = p;
	last_chunk->free = CHUNK_SIZE - sizeof(struct malloc_block);
	p->next = sizeof(struct malloc_block);
	p->size = sizeof(struct malloc_block);;
#ifdef DEBUG
	p->free = 0;
#endif
	p++;
	p->next = 0;
	p->size = CHUNK_SIZE - sizeof(struct malloc_block);
#ifdef DEBUG
	p->free = 1;
#endif
	sp_check();
}

void *alloc_in_chunk(size_t size)
{
	struct malloc_block *pr, *b, *n;
	if (!last_chunk) return NULL;
	size = (size + 2 * sizeof(struct malloc_block) - 1) & ~(sizeof(struct malloc_block) - 1);
	pr = last_chunk->ptr;
	while (pr->next) {
		b = (struct malloc_block *)((char *)pr + pr->next);
#ifdef DEBUG
		if (!b->free) {
			internal("block on freelist is not free");
			pr = b;
			continue;
		}
#endif
		if (b->size >= size) goto found_it;
		pr = b;
	}
	return NULL;
	found_it:
	if (b->size <= size + sizeof(struct malloc_block)) {
#ifdef DEBUG
		b->free = 0;
#endif
		if (!b->next) pr->next = 0;
		else pr->next = (char *)b + b->next - (char *)pr;
		last_chunk->free -= b->size;
		sp_check();
		return b + 1;
	}
	n = (struct malloc_block *)((char *)b + size);
	if (n->next = b->next) n->next -= size;
	n->size = b->size - size;
#ifdef DEBUG
	n->free = 1;
#endif
	b->size = size;
#ifdef DEBUG
	b->free = 0;
#endif
	pr->next += size;
	last_chunk->free -= size;
	sp_check();
	return b + 1;
}

void *sp_malloc(size_t size)
{
	void *p;
	struct malloc_chunk *c, *cc;
	int f;
	if (size >= SMALL_MALLOC) {
		struct malloc_block *m = malloc(size + sizeof(struct malloc_block));
		if (m) {
			m->size = 0;
			return m + 1;
		}
		return NULL;
	}
	if (last_chunk) if ((p = alloc_in_chunk(size))) return p;
	f = 0;
	for (c = chunks; c < chunks + n_chunks; c++) if (c->free >= MIN_CHUNK_FREE && c->free > f) {
		cc = c;
		f = c->free;
	}
	if (f > 0) {
		last_chunk = cc;
		if ((p = alloc_in_chunk(size))) return p;
	}
	new_chunk();
	return alloc_in_chunk(size);
}

void sp_free(void *p)
{
	struct malloc_block *pr, *b, *f;
	int c;
	f = (struct malloc_block *)p - 1;
	if (!f->size) {
		free(f);
		return;
	}
	if (last_f_chunk < n_chunks && p >= (void *)chunks[last_f_chunk].ptr && p < (void *)((char *)chunks[last_f_chunk].ptr + CHUNK_SIZE)) {
		f:
		chunks[last_f_chunk].free += f->size;
		pr = chunks[last_f_chunk].ptr;
		while (pr->next) {
			b = (struct malloc_block *)((char *)pr + pr->next);
#ifdef DEBUG
			if (!b->free) {
				internal("block on freelist is not free");
				pr = b;
				continue;
			}
#endif
			if ((char *)b + b->size == (char *)f) {
				b->size += f->size;
				sp_check();
				return;
			}
			if (b < f) {
				pr = b;
				continue;
			}
			pr->next = (char *)f - (char *)pr;
			if ((char *)f + f->size != (char *)b) {
				f->next = (char *)b - (char *)f;
#ifdef DEBUG
				f->free = 1;
#endif
				sp_check();
				return;
			}
			if ((f->next = b->next)) f->next += f->size;
			f->size += b->size;
#ifdef DEBUG
			f->free = 1;
#endif
			sp_check();
			return;
		}
		b->next = (char *)f - (char *)b;
		f->next = 0;
#ifdef DEBUG
		f->free = 1;
#endif
	}
	for (last_f_chunk = 0; last_f_chunk < n_chunks; last_f_chunk++)
		if (p >= (void *)chunks[last_f_chunk].ptr && p < (void *)((char *)chunks[last_f_chunk].ptr + CHUNK_SIZE)) goto f;
	internal("sp_free: address %p not from sp_malloc", p);
}

void *sp_realloc(void *p, size_t psize)
{
	struct malloc_block *pr, *b, *f, *n;
	void *np;
	int c;
	int size = (psize + 2 * sizeof(struct malloc_block) - 1) & ~(sizeof(struct malloc_block) - 1);
	f = (struct malloc_block *)p - 1;
	if (!f->size) {
		b = realloc(f, size);
		if (b) return b + 1;
		return NULL;
	}
	if (f->size > size) goto shrink;
	if (last_f_chunk < n_chunks && p >= (void *)chunks[last_f_chunk].ptr && p < (void *)((char *)chunks[last_f_chunk].ptr + CHUNK_SIZE)) {
		f:
		pr = chunks[last_f_chunk].ptr;
		while (pr->next) {
			b = (struct malloc_block *)((char *)pr + pr->next);
#ifdef DEBUG
			if (!b->free) {
				internal("block on freelist is not free");
				break;
			}
#endif
			if ((char *)f + f->size != (char *)b)
				if (b > f) break;
				else {
					pr = b;
					continue;
				}
			if ((char *)f + size > (char *)b + b->size) break;
			if ((char *)f + size >= (char *)b + b->size - sizeof(struct malloc_block)) {
				if (!b->next) pr->next = 0;
				else pr->next = (char *)b + b->next - (char *)pr;
				last_chunk->free -= b->size;
				f->size += b->size;
				sp_check();
				return f + 1;
			}
			n = (struct malloc_block *)((char *)f + size);
			pr->next += (char *)n - (char *)b;
			if ((n->next = b->next)) n->next -= size - f->size;
			n->size = b->size - size + f->size;
#ifdef DEBUG
			n->free = 1;
#endif
			last_chunk->free -= size - f->size;
			f->size = size;
			sp_check();
			return f + 1;
		}
		if ((np = sp_malloc(psize))) {
			memcpy(np, p, f->size - sizeof(struct malloc_block));
			sp_free(p);
		}
		sp_check();
		return np;
	}
	for (last_f_chunk = 0; last_f_chunk < n_chunks; last_f_chunk++)
		if (p >= (void *)chunks[last_f_chunk].ptr && p < (void *)((char *)chunks[last_f_chunk].ptr + CHUNK_SIZE)) goto f;
	internal("sp_free: address %p not from sp_malloc", p);
	return p;

	shrink:
	if (size >= f->size - sizeof(struct malloc_block)) return p;
	n = (struct malloc_block *)((char *)f + size);
	n->size = f->size - size;
	f->size = size;
	sp_check();
	sp_free(n + 1);
	return p;
}

#endif