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