home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / os2 / memsz200.zip / DEBUG.CC < prev    next >
Text File  |  1993-07-06  |  7KB  |  227 lines

  1. /******************************************************************* DEBUG.CC
  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.  
  15. #include "debug.h"
  16.  
  17. extern HFILE Timer = 0 ;
  18. extern BOOL Trace = FALSE ;
  19.  
  20.  
  21. /****************************************************************************
  22.  *                                        *
  23.  *             Display Debug Message                    *
  24.  *                                        *
  25.  ****************************************************************************/
  26.  
  27. extern VOID Debug ( HWND hwnd, char *Message, ... )
  28. {
  29.  /***************************************************************************
  30.   * Local Declarations                                *
  31.   ***************************************************************************/
  32.  
  33.   va_list Marker ;
  34.   char Text [500] ;
  35.  
  36.  /***************************************************************************
  37.   * Format the debug message.                            *
  38.   ***************************************************************************/
  39.  
  40.   va_start ( Marker, Message ) ;
  41.   vsprintf ( Text, Message, Marker ) ;
  42.   va_end ( Marker ) ;
  43.  
  44.  /***************************************************************************
  45.   * Display the log message and wait for the user to press ENTER.        *
  46.   ***************************************************************************/
  47.  
  48.   WinMessageBox ( HWND_DESKTOP, hwnd, (PSZ)Text, (PSZ)"Debug", 0, MB_ENTER ) ;
  49. }
  50.  
  51. /****************************************************************************
  52.  *                                        *
  53.  *               Log Debug Message                    *
  54.  *                                        *
  55.  ****************************************************************************/
  56.  
  57. extern VOID Log ( char *Message, ... )
  58. {
  59.  /***************************************************************************
  60.   * Try to open the log file.  If unsuccessful, just return.            *
  61.   ***************************************************************************/
  62.  
  63.   ULONG Action ;
  64.   HFILE Handle ;
  65.  
  66.   if ( DosOpen ( (PSZ)"LOG", &Handle, &Action, 0,
  67.     FILE_NORMAL, FILE_CREATE | FILE_OPEN,
  68.     OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYWRITE | OPEN_FLAGS_FAIL_ON_ERROR | 
  69.     OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_SEQUENTIAL, 0 ) )
  70.   {
  71.     return ;
  72.   }
  73.  
  74.  /***************************************************************************
  75.   * Position to the end of the file.                                        *
  76.   ***************************************************************************/
  77.  
  78.   ULONG Position ;
  79.   DosChgFilePtr ( Handle, 0, FILE_END, &Position ) ;
  80.  
  81.  /***************************************************************************
  82.   * Format the message for the log file.                                    *
  83.   ***************************************************************************/
  84.  
  85.   char Buffer [512] ;
  86.   va_list Marker ;
  87.  
  88.   va_start ( Marker, Message ) ;
  89.   vsprintf ( Buffer, Message, Marker ) ;
  90.   va_end ( Marker ) ;
  91.  
  92.  /***************************************************************************
  93.   * Write the message to the log file.                                      *
  94.   ***************************************************************************/
  95.  
  96.   ULONG Written ;
  97.  
  98.   DosWrite ( Handle, Buffer, strlen(Buffer), &Written ) ;
  99.  
  100.  /***************************************************************************
  101.   * Close the log file and return.                        *
  102.   ***************************************************************************/
  103.  
  104.   DosClose ( Handle ) ;
  105. }
  106.  
  107. /****************************************************************************
  108.  *                                        *
  109.  *                Open Timer for Use                    *
  110.  *                                        *
  111.  ****************************************************************************/
  112.  
  113. extern BOOL OpenTimer ( VOID )
  114. {
  115.   ULONG Action ;
  116.  
  117.   if ( Timer )
  118.     DosClose ( Timer ) ;
  119.  
  120.   if ( DosOpen ( (PSZ)"TIMER$", &Timer, &Action, 0, FILE_NORMAL, FILE_OPEN, OPEN_SHARE_DENYNONE, 0 ) )
  121.   {
  122.     return ( FALSE ) ;
  123.   }
  124.  
  125.   return ( TRUE ) ;
  126. }
  127.  
  128. /****************************************************************************
  129.  *                                        *
  130.  *                Close Timer                    *
  131.  *                                        *
  132.  ****************************************************************************/
  133.  
  134. extern VOID CloseTimer ( VOID )
  135. {
  136.   DosClose ( Timer ) ;
  137. }
  138.  
  139. /****************************************************************************
  140.  *                                        *
  141.  *             Read Time from HRTIMER.SYS                *
  142.  *                                        *
  143.  ****************************************************************************/
  144.  
  145. extern BOOL GetTime ( PTIMESTAMP pts )
  146. {
  147.   ULONG ByteCount ;
  148.  
  149.   if ( DosRead ( Timer, pts, sizeof(*pts), &ByteCount ) )
  150.     return ( FALSE ) ;
  151.  
  152.   return ( TRUE ) ;
  153. }
  154.  
  155. /****************************************************************************
  156.  *                                        *
  157.  *               Calculate Elapsed Time                *
  158.  *                                        *
  159.  ****************************************************************************/
  160.  
  161. extern ULONG ElapsedTime ( PTIMESTAMP ptsStart, PTIMESTAMP ptsStop, PULONG pulNs )
  162. {
  163.   ULONG ulMsecs, ulNsecs;
  164.   TIMESTAMP tsStart, tsStop ;
  165.  
  166.   tsStart = *ptsStart ;               // De-reference timestamp
  167.                           //     structures for speed
  168.   tsStop  = *ptsStop ;
  169.  
  170.   ulMsecs = tsStop.ulMs - tsStart.ulMs ;      // Elapsed milliseconds
  171.  
  172.   if( tsStart.ulNs > tsStop.ulNs )          // If nanosecond overflow ...
  173.   {
  174.     ulNsecs = (1000000 + tsStop.ulNs) - tsStart.ulNs; // Adjust nanoseconds
  175.     ulMsecs--;                          // Adjust milliseconds
  176.   }
  177.   else
  178.     ulNsecs = tsStop.ulNs - tsStart.ulNs ;    // No overflow..Elapsed nanos
  179.  
  180.   *pulNs = ulNsecs ;
  181.  
  182.   return ( ulMsecs ) ;
  183. }
  184.  
  185. /****************************************************************************
  186.  *                                        *
  187.  *  Allocate Memory                                *
  188.  *                                        *
  189.  ****************************************************************************/
  190.  
  191. //#define ALLOCATE_THROUGH_DOS
  192.  
  193. extern PVOID AllocateMemory ( ULONG ByteCount )
  194. {
  195.   #ifdef ALLOCATE_THROUGH_DOS
  196.   {
  197.     PVOID Memory ;
  198.     DosAllocMem ( &Memory, ByteCount, PAG_READ | PAG_WRITE | PAG_COMMIT ) ;
  199.     return ( Memory ) ;
  200.   }
  201.   #else
  202.   {
  203.     return ( malloc ( ByteCount ) ) ;
  204.   }
  205.   #endif
  206. }
  207.  
  208. /****************************************************************************
  209.  *                                        *
  210.  *  Free Memory                                 *
  211.  *                                        *
  212.  ****************************************************************************/
  213.  
  214. extern VOID FreeMemory ( PVOID Memory )
  215. {
  216.   #ifdef ALLOCATE_THROUGH_DOS
  217.   {
  218.     DosFreeMem ( Memory ) ;
  219.   }
  220.   #else
  221.   {
  222.     free ( Memory ) ;
  223.   }
  224.   #endif
  225. }
  226. 
  227.