home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_10 / 9n10084a < prev    next >
Text File  |  1991-08-21  |  2KB  |  104 lines

  1. /*  chckstck.c
  2.  *
  3.  *  Jerzy Tomasik, 09-Jun-1991
  4.  *
  5.  *  Utility functions for determining unused stack space.
  6.  *  Supports Microsoft and Turbo C
  7.  */
  8.  
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11.  
  12.  
  13. #if defined( _MSC_VER )
  14.  
  15. #include <malloc.h>
  16.  
  17. extern char end;            /* bottom of Microsoft stack */
  18. static char marker = 'J';   /* "signature" byte */
  19.  
  20.  
  21. /*  determine unused stack space, normally this function
  22.  *  is called at program termination, but it can be
  23.  *  called at other times.
  24.  *  Microsoft version
  25.  */
  26. unsigned short unused_stack(void)
  27.     {
  28.     unsigned short unused = 0;
  29.     char *ptr;
  30.  
  31.     ptr = &end;
  32.  
  33.     while(*ptr++ == marker)
  34.         unused++;
  35.  
  36.     return(unused);
  37.     }
  38.  
  39.  
  40. /*  "paint" the stack with the signature byte */
  41. void spray_stack(void)
  42.     {
  43.     char *ptr, *stack_top;
  44.  
  45.     stack_top = &end + stackavail();
  46.     ptr = &end;
  47.  
  48.     do
  49.         {
  50.         *ptr++ = marker;
  51.         } while( ptr < stack_top);
  52.  
  53.     }
  54.  
  55. #elif defined( __TURBOC__ )
  56.  
  57. #include <dos.h>
  58.  
  59. static char marker = 'J';   /* "signature" byte */
  60. static char far *bottom;
  61.  
  62. /*  determine unused stack space, normally this function
  63.  *  is called at program termination, but it can be
  64.  *  called at other times.
  65.  *  Turbo C version
  66.  */
  67. void spray_stack(void)
  68.     {
  69.     char far *ptr;
  70.  
  71. #if defined( __SMALL__ ) || defined( __MEDIUM__ )
  72.  
  73.     extern unsigned int _stklen;
  74.  
  75.     ptr = (char *) MK_FP( _DS, (_SP - 0x100));
  76.     bottom  = (char *) MK_FP( _DS, (0xFFFF - _stklen));
  77.  
  78. #elif defined( __COMPACT__ ) || defined( __LARGE__ )
  79.  
  80.     ptr = (char *) MK_FP( _SS, (_SP - 0x100));
  81.     bottom  = (char *) MK_FP( _SS, 0x00 );
  82.  
  83. #endif
  84.  
  85.     for( ; ptr > bottom; *ptr--  = marker )
  86.         ;
  87.  
  88.     }
  89.  
  90. unsigned short unused_stack(void)
  91.     {
  92.     unsigned short unused = 0;
  93.     char far *ptr;
  94.  
  95.     ptr = bottom + 1;
  96.  
  97.     while(*ptr++ == marker)
  98.         unused++;
  99.  
  100.     return(unused);
  101.     }
  102.  
  103. #endif
  104.