Category : C++ Source Code
Archive   : C_ALL.ZIP
Filename : TI1036.ASC

 
Output of file : TI1036.ASC contained in archive : C_ALL.ZIP







PRODUCT : Borland C++ NUMBER : 1036
VERSION : 3.1
OS : DOS
DATE : October 23, 1992 PAGE : 1/5

TITLE : Determining the amount of stack used at runtime.





The following program provides an example on how to
determine how the amount of stack not being used by an
application. The example also determine the amount of the near
heap that has been
used.

This is accomplished by making a call at the beginning of
the program to a function that will mark all of the stack space
not currently in use with the character 0xFF. Just before the
program exits, a call is made to another function that will count
the number of unchanged 0xFF bytes on the stack. When trying to
determine the amount of the near heap that was used calloc should
be used rather than malloc to allocate the memory because calloc
will initialize the memory allocated to all zeros so it will
later be apparent that that memory was allocated even if the
values were never changed.

This method is not a 100% guaranteed one, but it will work
most of the time. Its reliablity can be determined by
understanding how it performs its task and what problems that may
inccur with a particular program. For instance it may prove
necessary to change the character used to mark the unused stack
space. It is also important to note that when attempting to
change the stack size in a near memory model one must change the
value of _heaplen or DGROUP will always be 64K regardless of the
value of _stklen.

In order to use these function you need to place the
following prototypes in the source file you will be calling them
from:
void init_stack_count(void);
void stack_count(void);
To determine the amount of unused stack you must first make
an initial call to:
init_stack_count();
at the beginning of the program and just before exiting the
program another call is required to:
stack_count();
The amount of unused stack space will be printed on the
screen. If you need to determine the amount of the near heap
used in order to adjust the stack in the near memory model then














PRODUCT : Borland C++ NUMBER : 1036
VERSION : 3.1
OS : DOS
DATE : October 23, 1992 PAGE : 2/5

TITLE : Determining the amount of stack used at runtime.




the symbol _HEAP_ must be defined in this module. You can do
this by adding the statement:
#define _HEAP_
at file scope (outside any function definition) in this
source file. Then this souce file must be compiled and linked
into your program as if it were any other module in your program.



#include
#include
#include
#include
#include

#define SIZE_OF_EMULATOR 415 // Account for floating
// point emulator
#define FILL_CHAR 0xFF // Character used to mark
// the stack

#pragma warn -aus // Turn off unecessary
#pragma warn -use // warnings



/ ************************************************************* \
| Marks all possible stack space not currently in use by |
| setting the values to 0xff. |
\ ************************************************************* /
void init_stack_count(void)
{
char far *sp; // far memory model stack
// pointer
char near *nsp; // near memory model stack
// pointer
extern unsigned __brklvl; // Internal variable marking
// top of
// the near heap.

#if defined(__COMPACT__) || \
defined(__LARGE__) || \
defined(__HUGE__)
#undef _HEAP_













PRODUCT : Borland C++ NUMBER : 1036
VERSION : 3.1
OS : DOS
DATE : October 23, 1992 PAGE : 3/5

TITLE : Determining the amount of stack used at runtime.




sp = MK_FP( _SS, _SP - 1 ); // initialize sp to point to
// the next available space on
// the stack.

while( sp > (char far *)SIZE_OF_EMULATOR ) //Check
// for stack overflow
{
*sp = FILL_CHAR; // Initialize unused stack
// space
sp--;
}
#else // similar for near data
// memory model
nsp = (char *) _SP - 1; // Initialize pointer
while( nsp > (char *)__brklvl)// Check for stack overflow
{
*nsp = FILL_CHAR; // Assign fill character
nsp--; // advance pointer
}
#endif
return;
}



/ ************************************************************* \
| Goes though stack space counting unused (unmodified) |
| values meaning that space on the stack was not used. |
\ ************************************************************* /
void stack_count( void )
{
unsigned count= 0; // Amount of unused stack
// space
unsigned mcount = 0; // Amount of used Heap space
char far *sp; // Far memory model pointer
char *nsp; // Near memory model pointer


#if defined(__COMPACT__) || \
defined(__LARGE__) || \
defined(__HUGE__)

sp = MK_FP( _SS, SIZE_OF_EMULATOR+1); // Initialize the













PRODUCT : Borland C++ NUMBER : 1036
VERSION : 3.1
OS : DOS
DATE : October 23, 1992 PAGE : 4/5

TITLE : Determining the amount of stack used at runtime.




// pointer
if( _SP > SIZE_OF_EMULATOR ) // Check for stack
// overflow
{
while( sp < MK_FP(_SS,_SP) ) // Check for stack
// overflow
{
if( *sp != (char) FILL_CHAR ) // compare pointer
//value to
// fill character
break;
count++; // count unused
// stack space
sp++; // increment pointer
}
}
#else
extern unsigned __brklvl; // End of near heap
extern unsigned __heapbase; // Beginning of near heap
if( _SP > __brklvl ) // Check for stack
// overflow
{
nsp = (char *) __brklvl + 1;
while( nsp < (char *)_SP ) // While we don't run
// into the
// current stack.
{
if( *nsp != (char) FILL_CHAR && count < 3)
// Count near heap used
mcount++;
// else count unused
// stack
else if( *(nsp + 1) == (char)FILL_CHAR &&
*(nsp + 2) == (char) FILL_CHAR )
count++; // Count unused bytes
nsp++;
}
}
count += 2;
#endif


#ifdef _HEAP_ // print amount of used













PRODUCT : Borland C++ NUMBER : 1036
VERSION : 3.1
OS : DOS
DATE : October 23, 1992 PAGE : 5/5

TITLE : Determining the amount of stack used at runtime.




// stack
printf("\nHeap Used = %u bytes", (__brklvl - __heapbase +
mcount));
#endif // print amount of unused
// stack
printf("\nStack not used = %u bytes" , count);
return;
}




DISCLAIMER: You have the right to use this technical information
subject to the terms of the No-Nonsense License Statement that
you received with the Borland product to which this information
pertains.



































  3 Responses to “Category : C++ Source Code
Archive   : C_ALL.ZIP
Filename : TI1036.ASC

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/