Not yet working stack checking (symbol STACKCHECK)
[gnokii.git] / gnokii / cleanup.c
index e11b7c2..1900733 100644 (file)
@@ -1,4 +1,48 @@
+#ifdef GNOKII_MAIN
+#include "config.h"
+#endif
+
+#include <stdio.h>
+
 void _cleanup(void)
 {
        /* hack for pic32 gcc */
 }
+
+#ifdef STACKCHECK
+
+static volatile int live_check_disable=0;
+
+void live_check(const char *file,int line)
+{
+static volatile char *top=NULL,*top_printed=NULL;
+static volatile size_t largest=0,largest_printed=0;
+volatile char mark;
+
+       if (&mark > top) 
+               top=&mark;
+       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 (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 (live_check_disable<=0)
+                       puts("live_check - aborting!");
+               _exit(99);
+               }
+}
+
+void live_disable(int how)
+{
+       live_check_disable+=how;
+}
+
+#endif /* STACKCHECK */