home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / packer / arc / arctool / stackuse.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-09-29  |  3.5 KB  |  105 lines

  1. {
  2.                        F i l e    I n f o r m a t i o n
  3.  
  4. * DESCRIPTION
  5. Version 1.0. Author Richard S. Sadowsky. This unit, when used in a Turbo
  6. Pascal 4.0 program will automaticall report information about stack usage.
  7. This is very useful during program development. The following information
  8. is reported about the stack: total stack space, unused stack space and
  9. stack space used by your program.
  10.  
  11. * ASSOCIATED FILES
  12. STACKUSE.PAS
  13. TESTSTAC.PAS
  14.  
  15. }
  16. {***********************************************************
  17.   StackUse - A unit to report stack usage information
  18.  
  19.   by Richard S. Sadowsky
  20.   version 1.0 7/18/88
  21.   released to the public domain
  22.  
  23.   Inspired by a idea by Kim Kokkonen.
  24.  
  25.   This unit, when used in a Turbo Pascal 4.0 program, will
  26.   automatically report information about stack usage.  This
  27.   is very useful during program development.  The following
  28.   information is reported about the stack:
  29.  
  30.   total stack space
  31.   Unused stack space
  32.   Stack spaced used by your program
  33.  
  34.   The unit's initialization code handles three things, it
  35.   figures out the total stack space, it initializes the
  36.   unused stack space to a known value, and it sets up an
  37.   ExitProc to automatically report the stack usage at
  38.   termination.  The total stack space is calculated by
  39.   adding 4 to the current stack pointer on entry into
  40.   the unit.  This works because on entry into a unit the
  41.   only thing on the stack is the 2 word (4 bytes) far
  42.   return value.  This is obviously version and compiler
  43.   specific.
  44.  
  45.   The ExitProc StackReport handles the math of calculating
  46.   the used and unused amount of stack space, and displays
  47.   this information.  Note that the original ExitProc
  48.   (Sav_ExitProc) is restored immediately on entry to
  49.   StackReport.  This is a good idea in ExitProc in case
  50.   a runtime (or I/O) error occurs in your ExitProc!
  51.  
  52.   I hope you find this unit as useful as I have!
  53.  
  54. ***********************************************************}
  55. {$R-,S-} { we don't need no stinkin range or stack checking! }
  56. unit StackUse;
  57.  
  58. interface
  59.  
  60. var
  61.   Sav_ExitProc     : Pointer; { to save the previous ExitProc }
  62.   StartSPtr        : Word;    { holds the total stack size    }
  63.  
  64. implementation
  65.  
  66. {$F+} { this is an ExitProc so it must be compiled as far }
  67. procedure StackReport;
  68.  
  69. { This procedure may take a second or two to execute, especially }
  70. { if you have a large stack. The time is spent examining the     }
  71. { stack looking for our init value ($AA). }
  72.  
  73. var
  74.   I                : Word;
  75.  
  76. begin
  77.   ExitProc := Sav_ExitProc; { restore original exitProc first }
  78.  
  79.   I := 0;
  80.   { step through stack from bottom looking for $AA, stop when found }
  81.   while I < SPtr do
  82.     if Mem[SSeg:I] <> $AA then begin
  83.       { found $AA so report the stack usage info }
  84.       WriteLn('total stack space : ',StartSPtr);
  85.       WriteLn('unused stack space: ', I);
  86.       WriteLn('stack space used  : ',StartSPtr - I);
  87.       I := SPtr; { end the loop }
  88.     end
  89.     else
  90.       inc(I); { look in next byte }
  91. end;
  92. {$F-}
  93.  
  94.  
  95. begin
  96.   StartSPtr := SPtr + 4; { on entry into a unit, only the FAR return }
  97.                          { address has been pushed on the stack.     }
  98.                          { therefore adding 4 to SP gives us the     }
  99.                          { total stack size. }
  100.   FillChar(Mem[SSeg:0], SPtr - 20, $AA); { init the stack   }
  101.   Sav_ExitProc := ExitProc;              { save exitproc    }
  102.   ExitProc     := @StackReport;          { set our exitproc }
  103. end.
  104. 
  105.