m68k stack checking implemented, not tested yet on uClinux
[gnokii.git] / gnokii / cleanup.c
index 1900733..2a0b51e 100644 (file)
@@ -7,35 +7,40 @@
 void _cleanup(void)
 {
        /* hack for pic32 gcc */
 void _cleanup(void)
 {
        /* hack for pic32 gcc */
+
+#ifdef LIVE
+       LIVE;
+#endif /* LIVE */
 }
 
 #ifdef STACKCHECK
 
 static volatile int live_check_disable=0;
 
 }
 
 #ifdef STACKCHECK
 
 static volatile int live_check_disable=0;
 
-void live_check(const char *file,int line)
+void live_check(const char *file,int line,int alloc)
 {
 static volatile char *top=NULL,*top_printed=NULL;
 static volatile size_t largest=0,largest_printed=0;
 volatile char mark;
 {
 static volatile char *top=NULL,*top_printed=NULL;
 static volatile size_t largest=0,largest_printed=0;
 volatile char mark;
+volatile char *will=&mark-(alloc==-1 ? 0 : alloc);
 
 
-       if (&mark > top) 
-               top=&mark;
+       if (will > top) 
+               top=will;
        if (live_check_disable<=0 && top!=top_printed) {
                printf("live_check [%s:%d]: new top=0x%08lX\n",file,line,(long)top);
                fflush(stdout);
                top_printed=top;
                }
        if (live_check_disable<=0 && top!=top_printed) {
                printf("live_check [%s:%d]: new top=0x%08lX\n",file,line,(long)top);
                fflush(stdout);
                top_printed=top;
                }
-       if (largest < top-&mark)
-               largest = top-&mark;
+       if (largest < top-will)
+               largest = top-will;
        if (live_check_disable<=0 && largest!=largest_printed) {
                printf("live_check [%s:%d]: largest=%ld\n",file,line,(long)largest);
                fflush(stdout);
                largest_printed=largest;
                }
        if (live_check_disable<=0 && largest!=largest_printed) {
                printf("live_check [%s:%d]: largest=%ld\n",file,line,(long)largest);
                fflush(stdout);
                largest_printed=largest;
                }
-       if (largest >= 12000) {
+       if (largest >= STACKCHECK) {
                if (live_check_disable<=0)
                if (live_check_disable<=0)
-                       puts("live_check - aborting!");
+                       printf("live_check - aborting: limit=%d < largest=%ld!\n",STACKCHECK,(long)largest);
                _exit(99);
                }
 }
                _exit(99);
                }
 }
@@ -45,4 +50,21 @@ void live_disable(int how)
        live_check_disable+=how;
 }
 
        live_check_disable+=how;
 }
 
+#define STACKCHECK_HEADER_PC_OFFSET 16
+
+void stackcheck(void)
+{
+volatile void *func;
+volatile int fp_alloc;
+
+       asm("movel %%d0,%0" : "=r" (fp_alloc) : /* input */ : "d0");
+       asm("movel %%sp@,%0" : "=r" (func) : /* input */);
+
+       if (live_check_disable<=0)
+               printf("mcount(func=0x%08lX,fp_alloc=%d)\n",((long)func)-STACKCHECK_HEADER_PC_OFFSET,-fp_alloc);
+#ifdef LIVE_ALLOC
+       LIVE_ALLOC((-fp_alloc)+4);
+#endif /* LIVE_ALLOC */
+}
+
 #endif /* STACKCHECK */
 #endif /* STACKCHECK */