stack_depth.c

00001 /* Compute and print stack depth of program
00002    Copyright (C) 2001, 2002 Free Software Foundation, Inc.
00003    Written by Stephane Carrez (stcarrez@nerim.fr)       
00004 
00005 This file is free software; you can redistribute it and/or modify it
00006 under the terms of the GNU General Public License as published by the
00007 Free Software Foundation; either version 2, or (at your option) any
00008 later version.
00009 
00010 In addition to the permissions in the GNU General Public License, the
00011 Free Software Foundation gives you unlimited permission to link the
00012 compiled version of this file with other programs, and to distribute
00013 those programs without any restriction coming from the use of this
00014 file.  (The General Public License restrictions do apply in other
00015 respects; for example, they cover modification of the file, and
00016 distribution when not linked into another program.)
00017 
00018 This file is distributed in the hope that it will be useful, but
00019 WITHOUT ANY WARRANTY; without even the implied warranty of
00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021 General Public License for more details.
00022 
00023 You should have received a copy of the GNU General Public License
00024 along with this program; see the file COPYING.  If not, write to
00025 the Free Software Foundation, 59 Temple Place - Suite 330,
00026 Boston, MA 02111-1307, USA.  */
00027 
00037 
00038 #include <stdio.h>
00039 #include <stdarg.h>
00040 #include <sys/param.h>
00041 #include <sys/sio.h>
00042 #include <string.h>
00043 
00044 char *stack_low = (char*) 0xffff;
00045 extern char _stack;
00046 
00047 static void
00048 print_stack_size ()
00049 {
00050   unsigned short sz;
00051 
00052   sz = (unsigned short) (&_stack - stack_low);
00053   printf ("Stack size = %ld\n", (long) sz);
00054 }
00055 
00056 static unsigned long
00057 fact (unsigned long n)
00058 {
00059   /* Buffer to use some stack space.  */
00060   char buf[10];
00061 
00062   memset (buf, 0, sizeof (buf));
00063   if (n > 1)
00064     n = n * fact (n - 1);
00065   else
00066     n = 1;
00067 
00068   return n;
00069 }
00070 
00071 int
00072 main ()
00073 {
00074   long i;
00075   unsigned long val;
00076 
00077   serial_init ();
00078   for (i = 1; i < 10; i++)
00079     {
00080       val = fact (i);
00081       printf ("Fact %ld = %ld\t", i, val);
00082       print_stack_size ();
00083       stack_low = &_stack;
00084     }
00085   return 0;
00086 }