home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / TEMPLATE / HPEEP2.C < prev    next >
Text File  |  1993-12-01  |  13KB  |  416 lines

  1. #include <windows.h>
  2. #include <string.h>   /* strcat & strlen */
  3. #include "toolhelp.h"
  4. #include "hpeep2.h"
  5.  
  6.  
  7. /* brush colors */
  8. #define BLACK   0x000000L
  9. #define MAGENTA 0xFF00FFL
  10. #define BLUE    0xFF0000L
  11. #define YELLOW  0x00FFFFL
  12. #define RED     0x0000FFL
  13.  
  14. static HBRUSH hbr [7]    ,
  15.               hbrMAGENTA ,
  16.               hbrBLUE    ,
  17.               hbrRED     ,
  18.               hbrYELLOW  ;
  19.  
  20. static HFONT  hfont      ;
  21.  
  22.  
  23. typedef struct tagDEFAULTDATASEGMENT
  24.     {
  25.     HANDLE hinstActive, // instance handle of active app
  26.            htaskActive; // task     handle of active app
  27.     HWND   hwndActive,  // window   handle of active app
  28.            hwndClient;  // window we draw bar graph in.
  29.     WORD wSize,         // size (bytes) of Data Segment.
  30.          wStaticData,   // size (bytes) of static data.
  31.          wStackMax,     // size (bytes) of stack size defined in .DEF
  32.          wStackUsed,    // size (bytes) of stack actually used.
  33.          wHeapMoveable, // size (bytes) of heap allocation (moveable).
  34.          wHeapFixed,    // size (bytes) of heap allocation (fixed).
  35.          wHeapFree,     // size (bytes) of free space in heap.
  36.          wOther,        // size (bytes) of remaining allocated space in DS.
  37.          wUnused;       // size (bytes) of heap unused.
  38.     } DEFAULTDATASEGMENT;
  39.  
  40. static DEFAULTDATASEGMENT DDS ;
  41.  
  42.  
  43.  
  44.  
  45. void dds_create (HWND hwndClient)
  46.      {
  47.      HDC hdc = GetDC (hwndClient);
  48.  
  49.  
  50.      //      Create brushes.
  51.      //
  52.      if (GetDeviceCaps (hdc, NUMCOLORS) == 2)
  53.           {
  54.           //   create pattern brushes for monochrome display
  55.           //
  56.           hbrMAGENTA = CreateHatchBrush (HS_DIAGCROSS ,BLACK);
  57.           hbrBLUE    = CreateHatchBrush (HS_BDIAGONAL ,BLACK);
  58.           hbrYELLOW  = CreateHatchBrush (HS_FDIAGONAL ,BLACK);
  59.           hbrRED     = CreateHatchBrush (HS_BDIAGONAL ,BLACK);
  60.           }
  61.      else
  62.           {
  63.           //   create color brushes for color display
  64.           //
  65.           hbrMAGENTA = CreateSolidBrush (MAGENTA);
  66.           hbrBLUE    = CreateSolidBrush (BLUE)   ;
  67.           hbrYELLOW  = CreateSolidBrush (YELLOW) ;
  68.           hbrRED     = CreateSolidBrush (RED)    ;
  69.           }
  70.  
  71.      hbr [0] = (HBRUSH)GetStockObject (BLACK_BRUSH);
  72.      hbr [1] = (HBRUSH)GetStockObject (LTGRAY_BRUSH) ;
  73.      hbr [2] = hbrMAGENTA ;
  74.      hbr [3] = hbrBLUE    ;
  75.      hbr [4] = hbrYELLOW  ;
  76.      hbr [5] = hbrRED     ;
  77.      hbr [6] = (HBRUSH)GetStockObject (WHITE_BRUSH);
  78.  
  79.      //   Create nice display font
  80.      //
  81.      {
  82.      static LOGFONT lf;
  83.      lf.lfHeight  =   8;
  84.      lf.lfWeight  = 700;
  85.      lf.lfCharSet = ANSI_CHARSET ;
  86.      lf.lfQuality = PROOF_QUALITY;
  87.  
  88.      lf.lfPitchAndFamily = VARIABLE_PITCH | FF_SWISS;
  89.  
  90.      hfont = CreateFontIndirect (&lf);
  91.      }
  92.  
  93.      ReleaseDC (hwndClient, hdc);
  94.      DDS.hwndClient = hwndClient;
  95.      }
  96.  
  97.  
  98.  
  99. void dds_destroy (void)
  100.      {
  101.      DeleteObject (hbrBLUE)   ;
  102.      DeleteObject (hbrMAGENTA);
  103.      DeleteObject (hbrYELLOW) ;
  104.      DeleteObject (hbrRED)    ;
  105.  
  106.      DeleteObject (hfont)     ;
  107.      }
  108.  
  109.  
  110.  
  111.  
  112. void dds_paint ()
  113.      {
  114.      RECT rcClient;
  115.      long lTextExt;
  116.      WORD wTextWidth;
  117.      PAINTSTRUCT ps;
  118.  
  119.      BeginPaint (DDS.hwndClient, &ps);
  120.  
  121.      SelectObject (ps.hdc, hfont)    ;
  122.  
  123.      SetMapMode   (ps.hdc, MM_ANISOTROPIC);
  124.      SetWindowOrg (ps.hdc,    0,   0)     ;
  125.      SetWindowExt (ps.hdc, 1300, 130)     ;
  126.  
  127.      /*
  128.       *  Set viewport to be the entire client area.
  129.       *
  130.       */
  131.      GetClientRect (DDS.hwndClient, &rcClient);
  132.      SetViewportOrg (ps.hdc, 0, 0);
  133.      SetViewportExt (ps.hdc, rcClient.right, rcClient.bottom);
  134.  
  135.      /*
  136.       *  Draw bar : StaticData = BLACK
  137.       *             Stack      = LTGRAY
  138.       *             Fixed      = MAGENTA
  139.       *             Moveable   = YELLOW
  140.       *             Free       = BLUE
  141.       *             Other      = RED
  142.       *             Unused     = WHITE
  143.       *
  144.       */
  145.      {
  146.      int    xBar[8]   ,
  147.             yBar[2]   ,
  148.             i         ,
  149.             nMinWidth ;
  150.  
  151.      POINT  pt[2];
  152.  
  153.      double dPercentStaticData ,
  154.             dPercentStackMax   ,
  155.             dPercentFixed      ,
  156.             dPercentMoveable   ,
  157.             dPercentFree       ,
  158.             dPercentOther      ,
  159.             dPercentUnused     ,
  160.             dDiv = 0xFFFF/1000.; // makes bar 1000 logical units wide
  161.                                  //   eg, each 1% is represented by
  162.                                  //   10 logical units.
  163.  
  164.      dPercentStaticData = (double)DDS.wStaticData   /dDiv ;
  165.      dPercentStackMax   = (double)DDS.wStackMax     /dDiv ;
  166.      dPercentFixed      = (double)DDS.wHeapFixed    /dDiv ;
  167.      dPercentMoveable   = (double)DDS.wHeapMoveable /dDiv ;
  168.      dPercentFree       = (double)DDS.wHeapFree     /dDiv ;
  169.      dPercentOther      = (double)DDS.wOther        /dDiv ;
  170.      dPercentUnused     = (double)DDS.wUnused       /dDiv ;
  171.  
  172.      //   Compute a logical width that corresponds to a 2 pixel width.
  173.      //   This will be used as a minimum width for any bar section, and will
  174.      //   ensure that a section is always displayed at least 1 pixel wide.
  175.      //   (FillRect will output 1 pixel less than the rect's width)
  176.      //
  177.      pt[0].x = 0 ;
  178.      pt[0].y = 0 ;
  179.      pt[1].x = 1 ;
  180.      pt[1].y = 0 ;
  181.      DPtoLP (ps.hdc, &pt[0], 2)    ;
  182.      nMinWidth = max ((pt[1].x - pt[0].x), 1) ;
  183.  
  184.      xBar[0]  = 50;
  185.      xBar[1]  = xBar[0] + max ( (int)dPercentStaticData, nMinWidth);
  186.      xBar[2]  = xBar[1] + max ( (int)dPercentStackMax  , nMinWidth);
  187.      xBar[3]  = xBar[2] + max ( (int)dPercentFixed     , nMinWidth);
  188.      xBar[4]  = xBar[3] + max ( (int)dPercentMoveable  , nMinWidth);
  189.      xBar[5]  = xBar[4] + max ( (int)dPercentFree      , nMinWidth);
  190.      xBar[6]  = xBar[5] + max ( (int)dPercentOther     , nMinWidth);
  191.      xBar[7]  = xBar[6] + max ( (int)dPercentUnused    , nMinWidth);
  192.  
  193.      yBar[0]  = 20;
  194.      yBar[1]  = 30;
  195.  
  196.      /* Active window's Caption */
  197.      if (IsWindow (DDS.hwndActive))
  198.           {
  199.           char szText    [65],
  200.                szCaption [40];
  201.           BOOL bTruncate = (GetWindowTextLength (DDS.hwndActive) > 39);
  202.           int  cch;
  203.  
  204.           cch = GetWindowText (DDS.hwndActive, szCaption, 39);
  205.           szCaption [cch] = '\0';
  206.  
  207.           if (bTruncate) strcpy (&szCaption [36], "...");
  208.  
  209.           wsprintf (szText, "%.39s   [hInstance = %4.4X]", (LPSTR) szCaption,
  210.                                                             DDS.hinstActive);
  211.           SetTextAlign (ps.hdc, TA_BOTTOM);
  212.           TextOut      (ps.hdc, 250, 18, szText, strlen (szText) );
  213.           SetTextAlign (ps.hdc, TA_TOP);
  214.           }
  215.  
  216.      /* label at left end */
  217.      lTextExt   = GetTextExtent (ps.hdc,"0", 1);
  218.      wTextWidth = LOWORD (lTextExt);
  219.      TextOut (ps.hdc, (xBar[0]-wTextWidth-10), yBar[0] + 2, "0", strlen("0") );
  220.  
  221.      /* label at right end */
  222.      TextOut (ps.hdc, (xBar[7]+10), yBar[0] + 2, "64K", 3);
  223.  
  224.      for (i=0; i<=6; i++)
  225.          {
  226.          POINT ptA, ptB, ptC, ptD;
  227.          RECT rect;
  228.          char sz [40];
  229.  
  230.          ptA.x = (xBar[i+1] + xBar[i])/2 ;
  231.          if ((xBar[i+1] - xBar[i]) <= (3*nMinWidth)/2 ) ptA.x = xBar[i] ;
  232.  
  233.          ptA.y = yBar[1]      ;
  234.          ptB.x = ptA.x        ;
  235.          ptB.y = 120 - (10*i) ;
  236.          ptC.x = ptB.x + 20   ;
  237.          ptC.y = ptB.y        ;
  238.          ptD.x = ptC.x + 10   ;
  239.          ptD.y = ptC.y - 4    ;
  240.  
  241.          rect.left   = xBar [i]  ;
  242.          rect.top    = yBar [0]  ;
  243.          rect.right  = xBar [i+1];
  244.          rect.bottom = yBar [1]  ;
  245.          FillRect (ps.hdc, &rect, hbr[i]);
  246.  
  247.          MoveTo (ps.hdc, ptA.x, ptA.y);
  248.          LineTo (ps.hdc, ptB.x, ptB.y);
  249.          LineTo (ps.hdc, ptC.x, ptC.y);
  250.  
  251.          switch (i)
  252.            {
  253.            case 0: wsprintf (sz, "static data (%u)",   DDS.wStaticData);  break;
  254.            case 1: wsprintf (sz, "stack allocated (%u), used (%u)",
  255.                                           DDS.wStackMax, DDS.wStackUsed); break;
  256.            case 2: wsprintf (sz, "fixed heap (%u)",    DDS.wHeapFixed);   break;
  257.            case 3: wsprintf (sz, "moveable heap (%u)", DDS.wHeapMoveable);break;
  258.            case 4: wsprintf (sz, "free heap (%u)",     DDS.wHeapFree);    break;
  259.            case 5: wsprintf (sz, "other (%u)",         DDS.wOther);       break;
  260.            case 6: wsprintf (sz, "room to grow (%u)",  DDS.wUnused);      break;
  261.            }
  262.  
  263.          TextOut (ps.hdc, ptD.x, ptD.y, sz, strlen(sz));
  264.          }
  265.  
  266.      SelectObject (ps.hdc, (HBRUSH)GetStockObject (NULL_BRUSH));
  267.      Rectangle (ps.hdc, xBar[0], yBar[0], xBar[7], yBar[1]);
  268.      }
  269.  
  270.      EndPaint  (DDS.hwndClient, &ps);
  271.      }
  272.  
  273.  
  274.  
  275.  
  276.  
  277. void dds_walk ()
  278.      {
  279.      LOCALINFO  localinfo;
  280.      LOCALENTRY localentry;
  281.      TASKENTRY  taskentry;
  282.  
  283.      static DEFAULTDATASEGMENT OldDDS;
  284.  
  285.  
  286.      //   First, initialize the data segment values.
  287.      //
  288.      //
  289.      DDS.wSize         = 0;
  290.      DDS.wStaticData   = 0;
  291.      DDS.wStackMax     = 0;
  292.      DDS.wStackUsed    = 0;
  293.      DDS.wHeapMoveable = 0;
  294.      DDS.wHeapFixed    = 0;
  295.      DDS.wHeapFree     = 0;
  296.      DDS.wUnused       = 0;
  297.  
  298.  
  299.      //   Now, get the window that has the focus.
  300.      //
  301.      //
  302.      DDS.hwndActive = GetActiveWindow ();
  303.  
  304.  
  305.      //   Is it a valid window?
  306.      //
  307.      //
  308.      if ( !IsWindow (DDS.hwndActive) )  return;
  309.  
  310.  
  311.      //   If this is a different window than before,
  312.      //   get a new task & instance handle.
  313.      //
  314.      //
  315.      if (DDS.hwndActive != OldDDS.hwndActive)
  316.           {
  317.           // Loop through the task list
  318.  
  319.           DDS.htaskActive  = GetWindowTask (DDS.hwndActive);
  320.           taskentry.dwSize = sizeof (TASKENTRY);
  321.  
  322.           if ( TaskFirst (&taskentry) )
  323.                 do
  324.                    {
  325.                    if (DDS.htaskActive == taskentry.hTask)
  326.                          {
  327.                          DDS.hinstActive = taskentry.hInst ;
  328.                          break;
  329.                          }
  330.                    }
  331.                 while (TaskNext(&taskentry));
  332.           }
  333.  
  334.      /*
  335.       *  Remember, the stack grows "down" (higher to lower address), so
  336.       *  to compute the stack sizes, we use these equations:
  337.       *
  338.       *   wStackMax  = pStackBottom - pStackTop ;
  339.       *   wStackUsed = pStackBottom - pStackMin ;
  340.       *
  341.       *
  342.       */
  343.      taskentry.dwSize = sizeof (TASKENTRY);
  344.      TaskFindHandle (&taskentry, DDS.htaskActive);
  345.  
  346.      DDS.wStackMax   = taskentry.wStackBottom - taskentry.wStackTop ;
  347.      DDS.wStackUsed  = taskentry.wStackBottom - taskentry.wStackMinimum ;
  348.      DDS.wStaticData = taskentry.wStackTop ;
  349.  
  350.  
  351.      // walk the local heap
  352.  
  353.      localinfo.dwSize = sizeof (LOCALINFO);
  354.      LocalInfo (&localinfo, DDS.hinstActive);
  355.  
  356.      localentry.dwSize = sizeof (LOCALENTRY);
  357.      if (LocalFirst (&localentry, DDS.hinstActive))
  358.           {
  359.           do
  360.             {
  361.             if (localentry.wFlags & LF_FREE)
  362.                  DDS.wHeapFree     += localentry.wSize;
  363.  
  364.             else if (localentry.wFlags & LF_FIXED)
  365.                  DDS.wHeapFixed    += localentry.wSize;
  366.  
  367.             else if (localentry.wFlags & LF_MOVEABLE)
  368.                  DDS.wHeapMoveable += localentry.wSize;
  369.             }
  370.           while (LocalNext (&localentry));
  371.           }
  372.  
  373.      /*
  374.       *  At this point, heap traversal is done.
  375.       *  However, the heap can grow until the size of DS is 64K (0xFFFF).
  376.       *  Determine how many additional Kbytes the heap can grow.
  377.       *
  378.       */
  379.      DDS.wSize   = GlobalSize (DDS.hinstActive);
  380.      DDS.wUnused = (0xFFFF - DDS.wSize);
  381.  
  382.  
  383.      /*
  384.       *  Is there anything else we didn't account for?
  385.       *
  386.       */
  387.      DDS.wOther  = DDS.wSize - DDS.wStaticData
  388.                              - DDS.wStackMax
  389.                              - DDS.wHeapFixed
  390.                              - DDS.wHeapFree
  391.                              - DDS.wHeapMoveable ;
  392.  
  393.  
  394.      //   If anything has changed since last walk, update client window.
  395.      //
  396.  
  397.      if (DDS.hwndActive    != OldDDS.hwndActive    ||
  398.          DDS.wHeapFree     != OldDDS.wHeapFree     ||
  399.          DDS.wHeapFixed    != OldDDS.wHeapFixed    ||
  400.          DDS.wHeapMoveable != OldDDS.wHeapMoveable ||
  401.          DDS.wOther        != OldDDS.wOther        ||
  402.          DDS.wSize         != OldDDS.wSize         ||
  403.          DDS.wStackUsed    != OldDDS.wStackUsed)
  404.              {
  405.              InvalidateRect (DDS.hwndClient, NULL, TRUE);
  406.              UpdateWindow   (DDS.hwndClient);
  407.  
  408.              OldDDS = DDS;
  409.              }
  410.      }
  411.  
  412.  
  413.  
  414.  
  415.  
  416.