home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_09 / 2n09042a < prev    next >
Text File  |  1991-07-23  |  5KB  |  95 lines

  1.  
  2. /*============================start=================================*/
  3. /* mark_stack():    reports the amount of stack space that remains  */
  4. /* unused throughout a C program's execution; useful for adjusting  */
  5. /* the STACK size smaller to save memory; suitable for use with     */
  6. /* any Microsoft (MSC) or Borland (BC) C compiler, and for any      */
  7. /* memory model (T,S,M,C,L,H); software adapted from the book,      */
  8. /* ENCYCLOPEDIA C, by R.Radcliffe (Sybex/1991), Fig 15.12, Pg 645.  */
  9. /*==================================================================*/
  10.  
  11. #include <dos.h>                    /* FP_SEG(), FP_OFF()           */
  12. #include <stdio.h>                  /* printf()                     */
  13. #include <stdlib.h>                 /* exit()                       */
  14.  
  15. /*---------------------custom prototypes----------------------------*/
  16.  
  17. void mark_stack(void);              /* custom function prototype    */
  18. static void report_stack(void);     /* custom function prototype    */
  19.  
  20. /*---------------------extern/static data---------------------------*/
  21.  
  22. #if defined(_MSC_VER)               /* _MSC_VER predefined by MSC   */
  23.     extern char end;                /* MSC stack bottom in crt0.asm */
  24. #elif defined(__TURBOC__)           /* __TURBOC__ predefined by TC  */
  25.     extern unsigned _stklen;        /* BC stack size for any model  */
  26. #else
  27.     #error "This software IS NOT suitable for your C compiler!!"
  28. #endif
  29.  
  30. static char far * st_top;           /* TOS (high&) of unused stack  */
  31. static char far * st_bot;           /* BOS (low&) of unused stack   */
  32. static char far * ptr;              /* scratch pointer variable     */
  33. static unsigned st_seg;             /* current SS register value    */
  34. static unsigned st_ptr;             /* current SP (SS:offset) value */
  35. static char mark = 0xff;            /* any "clean" stack footprint  */
  36. static unsigned unused;             /* count of unused stack bytes  */
  37.  
  38. /*--------------------function mark_stack()-------------------------*/
  39.  
  40. void mark_stack(void)               /* begin function mark_stack()  */
  41. {
  42. #if defined(_MSC_VER)
  43.     _asm {                          /* MSC start of inline assembly */
  44.     mov st_seg, ss                  /* MSC get current SS register  */
  45.     mov st_ptr, sp                  /* MSC get current SP register  */
  46.     }
  47. #elif defined(__TURBOC__)           /* BC inline asm requires TASM  */
  48.     st_seg = _SS;                   /* BC get current SS register   */
  49.     st_ptr = _SP;                   /* BC get current SP register   */
  50. #endif
  51.  
  52. FP_SEG(st_top) = st_seg;            /* TOS segment& for MSC + BC    */
  53. FP_OFF(st_top) = st_ptr;            /* TOS offset& for MSC + BC     */
  54.  
  55. #if defined(_MSC_VER)               /* calculate MSC BOS far&       */
  56.     st_bot = (char far *) &end;
  57. #endif
  58.  
  59. #if defined(__TURBOC__)             /* calculate TC BOS far&        */
  60.     FP_SEG(st_bot) = st_seg;
  61.   #if defined(__TINY__) || defined(__SMALL__) || defined(__MEDIUM__)
  62.     FP_OFF(st_bot) = 0xffffu - _stklen + 1u;
  63.   #elif defined(__COMPACT__) || defined(__LARGE__) || defined(__HUGE__)
  64.     FP_OFF(st_bot) = 0x0000u;
  65.   #endif
  66. #endif
  67.  
  68. ptr = st_bot;                       /* set low& of unused stack     */
  69. while (ptr < st_top) *ptr++ = mark; /* walk "cleanly" on the stack  */
  70. atexit(report_stack);               /* log function with atexit()   */
  71. }                                   /* end function mark_stack()    */
  72.  
  73. /*---------------------function report_stack()----------------------*/
  74.  
  75. static void report_stack(void)      /* begin function report_stack()*/
  76. {                                   /* atexit() style prototype     */
  77. ptr = st_bot;                       /* set low& of unused stack     */
  78. while (*ptr++ == mark) unused++;    /* remaining "clean" footprints */
  79. printf("\nUnused Stack = %u Bytes", unused);
  80. }                                   /* end function report_stack()  */
  81.  
  82. /*-------------------embedded test driver main()--------------------*/
  83.  
  84. #if !defined(NDEBUG)                /* conditional compilation      */
  85. int main (void)                     /* embedded test driver main()  */
  86. {
  87. /* any auto/static data here!                                       */
  88. mark_stack();                       /* ENTER AS 1ST EXECUTABLE STMT */
  89. /* remaining logic of main() here!                                  */
  90. exit(0);                            /* report_stack() upon exit     */
  91. }                                   /* end function main()          */
  92. #endif
  93.  
  94. /*==============================stop================================*/
  95.