m68k stack checking implemented, not tested yet on uClinux
[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 #ifdef LIVE
12         LIVE;
13 #endif /* LIVE */
14 }
15
16 #ifdef STACKCHECK
17
18 static volatile int live_check_disable=0;
19
20 void live_check(const char *file,int line,int alloc)
21 {
22 static volatile char *top=NULL,*top_printed=NULL;
23 static volatile size_t largest=0,largest_printed=0;
24 volatile char mark;
25 volatile char *will=&mark-(alloc==-1 ? 0 : alloc);
26
27         if (will > top) 
28                 top=will;
29         if (live_check_disable<=0 && top!=top_printed) {
30                 printf("live_check [%s:%d]: new top=0x%08lX\n",file,line,(long)top);
31                 fflush(stdout);
32                 top_printed=top;
33                 }
34         if (largest < top-will)
35                 largest = top-will;
36         if (live_check_disable<=0 && largest!=largest_printed) {
37                 printf("live_check [%s:%d]: largest=%ld\n",file,line,(long)largest);
38                 fflush(stdout);
39                 largest_printed=largest;
40                 }
41         if (largest >= STACKCHECK) {
42                 if (live_check_disable<=0)
43                         printf("live_check - aborting: limit=%d < largest=%ld!\n",STACKCHECK,(long)largest);
44                 _exit(99);
45                 }
46 }
47
48 void live_disable(int how)
49 {
50         live_check_disable+=how;
51 }
52
53 #define STACKCHECK_HEADER_PC_OFFSET 16
54
55 void stackcheck(void)
56 {
57 volatile void *func;
58 volatile int fp_alloc;
59
60         asm("movel %%d0,%0" : "=r" (fp_alloc) : /* input */ : "d0");
61         asm("movel %%sp@,%0" : "=r" (func) : /* input */);
62
63         if (live_check_disable<=0)
64                 printf("mcount(func=0x%08lX,fp_alloc=%d)\n",((long)func)-STACKCHECK_HEADER_PC_OFFSET,-fp_alloc);
65 #ifdef LIVE_ALLOC
66         LIVE_ALLOC((-fp_alloc)+4);
67 #endif /* LIVE_ALLOC */
68 }
69
70 #endif /* STACKCHECK */