home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / CALCPLUS.ZIP / CALCPLUS.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-02  |  3.7 KB  |  157 lines

  1. /*******************************************************
  2.  
  3.     The CalcPlus Class Library Version 1.0,
  4.     Author: Vladimir Schipunov, Copyright (C) 1996
  5.  
  6.     This library is free software. Permission to use,
  7.     copy, modify and redistribute the CalcPlus library
  8.     for any purpose is hereby granted without fee,
  9.     provided that the above copyright notice appear
  10.     in all copies.
  11.  
  12. *******************************************************/
  13.  
  14. #include <iostream.h>
  15. #include <stdlib.h>
  16. #include "yycalc.h"
  17. #include "calcexpr.h"
  18. #include "calctype.h"
  19.  
  20. //
  21. //  Show statistics about used memory,
  22. //  class MemoryUsage is defined below.
  23. //  Uncomment if you want to see how much
  24. //  dynamic memory application needs.
  25. //
  26. //  #define MEMORY_USAGE
  27.  
  28. main( int argc, const char** argv )
  29. {
  30.     int i;
  31.     for( i = 1; i<argc; i++ )
  32.     {
  33.         char c0 = argv[i][0];
  34.         char c1 = argv[i][1];
  35.         if((c0=='-'||c0=='/')&&(c1=='d'||c1=='D'))
  36.             Expression::Debug = 1;
  37.     }
  38.     XUserFunction::Argc = argc;
  39.     XUserFunction::Argv = argv;
  40.  
  41.     if( argc<2 )
  42.     {
  43.         cout << "Usage: CALC File[s] [Args] [/d]" << endl;
  44.         return 1;
  45.     }
  46.     CalcPlus calc;
  47.     for( i = argc-1; i > 0; i-- )
  48.         if( i==1 || argv[i][0]=='@' )
  49.             calc.File( argv[i] );
  50.     if( calc.Run())
  51.         return 1;
  52.     if( !calc.Global->funcs( "atexit" ))
  53.         return 0;
  54.     CString s("exiting... ");
  55.     CLong* p = (CLong*)(calc.Call( "atexit", 1, &s ));
  56.     if( !p )
  57.         return 1;
  58.     return 0;
  59. }
  60.  
  61. #ifdef MEMORY_USAGE
  62. //
  63. //  Simple control of memory usage.
  64. //  We are trying to detect out of memory error,
  65. //  and also collect statistics about amount of
  66. //  memory requested by application.
  67. //
  68. //  Constructor of the class saves count and number of bytes
  69. //  of allocated memory. Destructor checks if all the allocated
  70. //  memory freed. Note, that order of constructing objects is
  71. //  significant here. For example, this module must be linked first
  72. //  with Borland or last with Watcom.
  73. //
  74.  
  75. class MemoryUsage
  76. {
  77. public:
  78.     struct meminfo { long count, bytes; };
  79.     meminfo sav, hit;
  80.  
  81.     MemoryUsage();
  82.     ~MemoryUsage();
  83.     void print( const char* title, const meminfo& ) const;
  84.     void statistics( const meminfo& );
  85. };
  86.  
  87. static MemoryUsage::meminfo info;
  88. MemoryUsage MemoryCheck;
  89.  
  90. MemoryUsage::MemoryUsage() : sav( info )
  91. {
  92.     hit.count = hit.bytes = 0;
  93. }
  94.  
  95. MemoryUsage::~MemoryUsage()
  96. {
  97. #ifdef ON_MEMORY_ERROR_ONLY
  98.     if( !memcmp( &sav, &info, sizeof( meminfo )))
  99.         return;
  100. #endif
  101.     cout << endl
  102.         << "Summary of memory usage" << endl
  103.         << "-----------------------" << endl
  104.         << "\tCount\tBytes"          << endl;
  105.     print( "Entry", sav );
  106.     print( "Exit", info );
  107.     print( "Max",  hit );
  108.     cout << endl;
  109. }
  110.  
  111. void MemoryUsage::print
  112. (
  113.     const char* title,
  114.     const meminfo& info
  115. )   const
  116. {
  117.     cout << title << "\t" << info.count
  118.          << "\t" << info.bytes << endl;
  119. }
  120.  
  121. void MemoryUsage::statistics( const meminfo& info )
  122. {
  123.     if( hit.count < info.count )
  124.         hit.count = info.count;
  125.     if( hit.bytes < info.bytes )
  126.         hit.bytes = info.bytes;
  127. }
  128.  
  129. void* operator new( unsigned n )
  130. {
  131.     if( !n )
  132.         return 0;
  133.     void *p = malloc( n + sizeof( long ));
  134.     if( !p )
  135.     {
  136.         cerr << "Not enough memory!" << endl;
  137.         exit( 1 );
  138.     }
  139.     *(long*)p = n;
  140.     info.count++;
  141.     info.bytes += n;
  142.     MemoryCheck.statistics( info );
  143.     return (char*)p + sizeof( long );
  144. }
  145.  
  146. void operator delete( void* p )
  147. {
  148.     if( !p )
  149.         return;
  150.     char *q = (char*)p - sizeof( long );
  151.     info.count--;
  152.     info.bytes -= *(long*)q;
  153.     free( q );
  154. }
  155.  
  156. #endif
  157.