home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l430 / 1.ddi / CHAP5.ZIP / MEMINFO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-13  |  2.8 KB  |  100 lines

  1. /*
  2.     MEMINFO.C -- Windows memory usage stats
  3.  
  4.     From Chapter 5 of "Undocumented Windows" (Addison-Wesley 1992)
  5.     by Andrew Schulman, Dave Maxey and Matt Pietrek
  6.  
  7.     Build using: WINIOBC MEMINFO (for Borland C++ v3.00)
  8.                  WINIOMS MEMINFO (for Microsoft C/SDK)
  9.                      
  10.     Changed from version in book:  many values not meaningful
  11.     in Standard mode (no paging)
  12. */
  13.  
  14. #include <windows.h>
  15. #include "winio.h"
  16.  
  17. DWORD (FAR PASCAL *GetFreeMemInfo)(void);
  18.  
  19. typedef struct {
  20.     DWORD dwLargestBlockBytes;
  21.     DWORD dwMaxUnlockedPages;
  22.     DWORD dwMaxLockedPages;
  23.     DWORD dwLinAddrSpaceInPages;
  24.     DWORD dwUnlockedPages;
  25.     DWORD dwFreePages;
  26.     DWORD dwPhysPages;
  27.     DWORD dwFreeLinPages;
  28.     DWORD dwPagingFilePages;
  29.     BYTE reserved[0x0c];
  30.     } DPMI_FREEMEM;
  31.     
  32. BOOL get_dpmi_freemem(DPMI_FREEMEM far *fpbuf)
  33. {
  34.     _asm push di
  35.     _asm les di, fpbuf
  36.     _asm mov ax, 0500h
  37.     _asm int 31h
  38.     _asm pop di
  39.     _asm jc error
  40.     return 1;
  41. error:
  42.     return 0;
  43. }
  44.  
  45. #define SHOW(str, dw) \
  46.     if (dw != -1L) \
  47.         printf(str ## ": %lu bytes\n", dw)
  48.  
  49. main()
  50. {
  51.     DWORD dwInfo;
  52.     
  53.     WORD wTotalUnlocked;
  54.     WORD wFreePages;
  55.     
  56.     DPMI_FREEMEM mem;
  57.  
  58.     winio_about("FREEMEM"
  59.         "\nWindows memory usage stats"
  60.         "\n\nFrom Chapter 5 of"
  61.         "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
  62.         "\nby Andrew Schulman, David Maxey and Matt Pietrek"
  63.         );
  64.     
  65.     if (get_dpmi_freemem(&mem))
  66.     {
  67.         // only first value is meaningful in Standard mode
  68.         SHOW("Largest available free block", mem.dwLargestBlockBytes);
  69.         if (GetWinFlags() & WF_ENHANCED)    // in 3.1, WF_PAGING
  70.         {
  71.             SHOW("Linear address space", mem.dwLinAddrSpaceInPages * 4096);
  72.             SHOW("Free address space", mem.dwFreeLinPages * 4096);
  73.             SHOW("Size of paging file", mem.dwPagingFilePages * 4096);
  74.             SHOW("Physical memory", mem.dwPhysPages * 4096);
  75.         }
  76.     }
  77.     
  78.     GetFreeMemInfo = GetProcAddress(GetModuleHandle("KERNEL"),
  79.         "GETFREEMEMINFO");
  80.     if (! GetFreeMemInfo)
  81.         fail("This program requires Windows 3.1 or higher");
  82.     dwInfo = GetFreeMemInfo();
  83.     if (dwInfo == -1L)
  84.         puts("GetFreeMemInfo returned -1");
  85.     else
  86.     {
  87.         wTotalUnlocked = LOWORD(dwInfo);
  88.         wFreePages = HIWORD(dwInfo);
  89.         if ((wTotalUnlocked != mem.dwUnlockedPages) ||
  90.             (wFreePages != mem.dwFreePages))
  91.             fail("Something wrong!");
  92.         printf("Total Unlocked Pages = %04xh (%lu bytes)\n",
  93.             wTotalUnlocked, (long) wTotalUnlocked * 4096L);
  94.         printf("Total Free Pages = %04xh (%lu bytes)\n",
  95.             wFreePages, (long) wFreePages * 4096L);
  96.     }
  97.     return 0;
  98. }
  99.  
  100.