home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / OS2 / MEMSZ230.ZIP / DEBUG.CPP < prev    next >
Text File  |  1994-01-25  |  8KB  |  224 lines

  1. /****************************************************************** DEBUG.CPP
  2.  *                                                                          *
  3.  *  Debugging Aids                                                          *
  4.  *                                                                          *
  5.  ****************************************************************************/
  6.  
  7. #define INCL_BASE
  8. #define INCL_WIN
  9. #include <os2.h>
  10.  
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <time.h>
  16.  
  17. #include "debug.h"
  18. #include "except.h"
  19. #include "mutex.h"
  20.  
  21. extern HFILE Timer = 0 ;
  22. extern BOOL Trace = FALSE ;
  23.  
  24.  
  25. /****************************************************************************
  26.  *                                                                          *
  27.  *                       Display Debug Message                              *
  28.  *                                                                          *
  29.  ****************************************************************************/
  30.  
  31. extern VOID Debug ( HWND hwnd, char *Message, ... )
  32. {
  33.  /***************************************************************************
  34.   * Local Declarations                                                      *
  35.   ***************************************************************************/
  36.  
  37.   va_list Marker ;
  38.   char Text [500] ;
  39.  
  40.  /***************************************************************************
  41.   * Format the debug message.                                               *
  42.   ***************************************************************************/
  43.  
  44.   va_start ( Marker, Message ) ;
  45.   vsprintf ( Text, Message, Marker ) ;
  46.   va_end ( Marker ) ;
  47.  
  48.  /***************************************************************************
  49.   * Display the log message and wait for the user to press ENTER.           *
  50.   ***************************************************************************/
  51.  
  52.   WinMessageBox ( HWND_DESKTOP, hwnd, PSZ(Text), PSZ("Debug"), 0, MB_ENTER ) ;
  53. }
  54.  
  55. /****************************************************************************
  56.  *                                                                          *
  57.  *                         Log Debug Message                                *
  58.  *                                                                          *
  59.  ****************************************************************************/
  60.  
  61. Mutex LogSemaphore ( PSZ(NULL) ) ;
  62.  
  63. extern VOID Log ( char *Message, ... )
  64. {
  65.  /***************************************************************************
  66.   * Serialize this function by requesting its semaphore.                    *
  67.   ***************************************************************************/
  68.  
  69.   LogSemaphore.Request ( SEM_INDEFINITE_WAIT ) ;
  70.  
  71.  /***************************************************************************
  72.   * Format the message.                                                     *
  73.   ***************************************************************************/
  74.  
  75.   va_list Marker ;
  76.   va_start ( Marker, Message ) ;
  77.  
  78.   static char Buffer [1024] ;
  79.   vsprintf ( Buffer, Message, Marker ) ;
  80.  
  81.   va_end ( Marker ) ;
  82.  
  83.  /***************************************************************************
  84.   * Open the log file.                                                      *
  85.   ***************************************************************************/
  86.  
  87.   FILE *File = fopen ( "MEMSIZE.LOG", "a" ) ;
  88.  
  89.  /***************************************************************************
  90.   * If the file got opened, write the message to the log file and close it. *
  91.   ***************************************************************************/
  92.  
  93.   if ( File ) {
  94.     char Time [9], Date [9] ;
  95.     fprintf ( File, "%s %s %s\n", _strtime(Time), _strdate(Date), Buffer ) ;
  96.     fclose ( File ) ;
  97.   } /* endif */
  98.  
  99.  /***************************************************************************
  100.   * Release the function semaphore and return.                              *
  101.   ***************************************************************************/
  102.  
  103.   LogSemaphore.Release ( ) ;
  104. }
  105.  
  106. /****************************************************************************
  107.  *                                                                          *
  108.  *                          Open Timer for Use                              *
  109.  *                                                                          *
  110.  ****************************************************************************/
  111.  
  112. extern BOOL OpenTimer ( VOID )
  113. {
  114.   if ( Timer )
  115.     DosClose ( Timer ) ;
  116.  
  117.   ULONG Action ;
  118.   if ( DosOpen ( (PSZ)"TIMER$", &Timer, &Action, 0, FILE_NORMAL, FILE_OPEN, OPEN_SHARE_DENYNONE, 0 ) )
  119.   {
  120.     return ( FALSE ) ;
  121.   }
  122.  
  123.   return ( TRUE ) ;
  124. }
  125.  
  126. /****************************************************************************
  127.  *                                                                          *
  128.  *                              Close Timer                                 *
  129.  *                                                                          *
  130.  ****************************************************************************/
  131.  
  132. extern VOID CloseTimer ( VOID )
  133. {
  134.   DosClose ( Timer ) ;
  135. }
  136.  
  137. /****************************************************************************
  138.  *                                                                          *
  139.  *                       Read Time from HRTIMER.SYS                         *
  140.  *                                                                          *
  141.  ****************************************************************************/
  142.  
  143. extern BOOL GetTime ( PTIMESTAMP pts )
  144. {
  145.   ULONG ByteCount ;
  146.  
  147.   if ( DosRead ( Timer, pts, sizeof(*pts), &ByteCount ) )
  148.     return ( FALSE ) ;
  149.  
  150.   return ( TRUE ) ;
  151. }
  152.  
  153. /****************************************************************************
  154.  *                                                                          *
  155.  *                         Calculate Elapsed Time                           *
  156.  *                                                                          *
  157.  ****************************************************************************/
  158.  
  159. extern ULONG ComputeElapsedTime ( PTIMESTAMP ptsStart, PTIMESTAMP ptsStop, PULONG pulNs )
  160. {
  161.   ULONG ulMsecs, ulNsecs;
  162.   TIMESTAMP tsStart, tsStop ;
  163.  
  164.   tsStart = *ptsStart ;                       // De-reference timestamp
  165.                                               //     structures for speed
  166.   tsStop  = *ptsStop ;
  167.  
  168.   ulMsecs = tsStop.ulMs - tsStart.ulMs ;      // Elapsed milliseconds
  169.  
  170.   if( tsStart.ulNs > tsStop.ulNs )            // If nanosecond overflow ...
  171.   {
  172.     ulNsecs = (1000000 + tsStop.ulNs) - tsStart.ulNs; // Adjust nanoseconds
  173.     ulMsecs--;                                        // Adjust milliseconds
  174.   }
  175.   else
  176.     ulNsecs = tsStop.ulNs - tsStart.ulNs ;    // No overflow..Elapsed nanos
  177.  
  178.   *pulNs = ulNsecs ;
  179.  
  180.   return ( ulMsecs ) ;
  181. }
  182.  
  183. /****************************************************************************
  184.  *                                                                          *
  185.  *  Allocate Memory                                                         *
  186.  *                                                                          *
  187.  ****************************************************************************/
  188.  
  189. //#define ALLOCATE_THROUGH_DOS
  190.  
  191. extern PVOID AllocateMemory ( ULONG ByteCount )
  192. {
  193.   #ifdef ALLOCATE_THROUGH_DOS
  194.   {
  195.     PVOID Memory ;
  196.     DosAllocMem ( &Memory, ByteCount, PAG_READ | PAG_WRITE | PAG_COMMIT ) ;
  197.     return ( Memory ) ;
  198.   }
  199.   #else
  200.   {
  201.     return ( malloc ( ByteCount ) ) ;
  202.   }
  203.   #endif
  204. }
  205.  
  206. /****************************************************************************
  207.  *                                                                          *
  208.  *  Free Memory                                                             *
  209.  *                                                                          *
  210.  ****************************************************************************/
  211.  
  212. extern VOID FreeMemory ( PVOID Memory )
  213. {
  214.   #ifdef ALLOCATE_THROUGH_DOS
  215.   {
  216.     DosFreeMem ( Memory ) ;
  217.   }
  218.   #else
  219.   {
  220.     free ( Memory ) ;
  221.   }
  222.   #endif
  223. }
  224.