Some stackchecking fixes
[gnokii.git] / gnokii / cleanup.c
index e11b7c2..e93075d 100644 (file)
@@ -1,4 +1,79 @@
+#ifdef GNOKII_MAIN
+#include "config.h"
+#endif
+
+#include <stdio.h>
+
 void _cleanup(void)
 {
        /* hack for pic32 gcc */
+
+#if 0
+#ifdef LIVE
+       LIVE;
+#endif /* LIVE */
+#endif
 }
+
+#ifdef STACKCHECK
+
+static volatile int live_check_disable=0;
+
+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;
+volatile char *will=&mark-(alloc==-1 ? 0 : alloc);
+
+       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 (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 (largest >= STACKCHECK) {
+               if (live_check_disable<=0)
+                       printf("live_check - aborting: limit=%d < largest=%ld!\n",STACKCHECK,(long)largest);
+               _exit(99);
+               }
+}
+
+void live_disable(int how)
+{
+       live_check_disable+=how;
+}
+
+#define STACKCHECK_HEADER_PC_OFFSET 16
+
+void stackcheck(void)
+{
+volatile static inside=0;
+volatile void *func;
+volatile int fp_alloc;
+
+       asm("movel %%d0,%0" : "=r" (fp_alloc) : /* input */ : "d0");
+       asm("movel %%sp@,%0" : "=r" (func) : /* input */);
+
+       if (inside)
+               return;
+       inside++;
+
+       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 */
+
+       inside--;
+}
+
+#endif /* STACKCHECK */