e93075db48c8464053afe0946a39022f751fcbaf
[gnokii.git] / gnokii / cleanup.c
1 #ifdef GNOKII_MAIN
2 #include "config.h"
3 #endif
4
5 #include <stdio.h>
6
7 void _cleanup(void)
8 {
9         /* hack for pic32 gcc */
10
11 #if 0
12 #ifdef LIVE
13         LIVE;
14 #endif /* LIVE */
15 #endif
16 }
17
18 #ifdef STACKCHECK
19
20 static volatile int live_check_disable=0;
21
22 void live_check(const char *file,int line,int alloc)
23 {
24 static volatile char *top=NULL,*top_printed=NULL;
25 static volatile size_t largest=0,largest_printed=0;
26 volatile char mark;
27 volatile char *will=&mark-(alloc==-1 ? 0 : alloc);
28
29         if (will > top) 
30                 top=will;
31         if (live_check_disable<=0 && top!=top_printed) {
32                 printf("live_check [%s:%d]: new top=0x%08lX\n",file,line,(long)top);
33                 fflush(stdout);
34                 top_printed=top;
35                 }
36         if (largest < top-will)
37                 largest = top-will;
38         if (live_check_disable<=0 && largest!=largest_printed) {
39                 printf("live_check [%s:%d]: largest=%ld\n",file,line,(long)largest);
40                 fflush(stdout);
41                 largest_printed=largest;
42                 }
43         if (largest >= STACKCHECK) {
44                 if (live_check_disable<=0)
45                         printf("live_check - aborting: limit=%d < largest=%ld!\n",STACKCHECK,(long)largest);
46                 _exit(99);
47                 }
48 }
49
50 void live_disable(int how)
51 {
52         live_check_disable+=how;
53 }
54
55 #define STACKCHECK_HEADER_PC_OFFSET 16
56
57 void stackcheck(void)
58 {
59 volatile static inside=0;
60 volatile void *func;
61 volatile int fp_alloc;
62
63         asm("movel %%d0,%0" : "=r" (fp_alloc) : /* input */ : "d0");
64         asm("movel %%sp@,%0" : "=r" (func) : /* input */);
65
66         if (inside)
67                 return;
68         inside++;
69
70         if (live_check_disable<=0)
71                 printf("mcount(func=0x%08lX,fp_alloc=%d)\n",((long)func)-STACKCHECK_HEADER_PC_OFFSET,-fp_alloc);
72 #ifdef LIVE_ALLOC
73         LIVE_ALLOC((-fp_alloc)+4);
74 #endif /* LIVE_ALLOC */
75
76         inside--;
77 }
78
79 #endif /* STACKCHECK */