home *** CD-ROM | disk | FTP | other *** search
- /*
- MEMINFO.C -- Windows memory usage stats
-
- From Chapter 5 of "Undocumented Windows" (Addison-Wesley 1992)
- by Andrew Schulman, Dave Maxey and Matt Pietrek
-
- Build using: WINIOBC MEMINFO (for Borland C++ v3.00)
- WINIOMS MEMINFO (for Microsoft C/SDK)
-
- Changed from version in book: many values not meaningful
- in Standard mode (no paging)
- */
-
- #include <windows.h>
- #include "winio.h"
-
- DWORD (FAR PASCAL *GetFreeMemInfo)(void);
-
- typedef struct {
- DWORD dwLargestBlockBytes;
- DWORD dwMaxUnlockedPages;
- DWORD dwMaxLockedPages;
- DWORD dwLinAddrSpaceInPages;
- DWORD dwUnlockedPages;
- DWORD dwFreePages;
- DWORD dwPhysPages;
- DWORD dwFreeLinPages;
- DWORD dwPagingFilePages;
- BYTE reserved[0x0c];
- } DPMI_FREEMEM;
-
- BOOL get_dpmi_freemem(DPMI_FREEMEM far *fpbuf)
- {
- _asm push di
- _asm les di, fpbuf
- _asm mov ax, 0500h
- _asm int 31h
- _asm pop di
- _asm jc error
- return 1;
- error:
- return 0;
- }
-
- #define SHOW(str, dw) \
- if (dw != -1L) \
- printf(str ## ": %lu bytes\n", dw)
-
- main()
- {
- DWORD dwInfo;
-
- WORD wTotalUnlocked;
- WORD wFreePages;
-
- DPMI_FREEMEM mem;
-
- winio_about("FREEMEM"
- "\nWindows memory usage stats"
- "\n\nFrom Chapter 5 of"
- "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
- "\nby Andrew Schulman, David Maxey and Matt Pietrek"
- );
-
- if (get_dpmi_freemem(&mem))
- {
- // only first value is meaningful in Standard mode
- SHOW("Largest available free block", mem.dwLargestBlockBytes);
- if (GetWinFlags() & WF_ENHANCED) // in 3.1, WF_PAGING
- {
- SHOW("Linear address space", mem.dwLinAddrSpaceInPages * 4096);
- SHOW("Free address space", mem.dwFreeLinPages * 4096);
- SHOW("Size of paging file", mem.dwPagingFilePages * 4096);
- SHOW("Physical memory", mem.dwPhysPages * 4096);
- }
- }
-
- GetFreeMemInfo = GetProcAddress(GetModuleHandle("KERNEL"),
- "GETFREEMEMINFO");
- if (! GetFreeMemInfo)
- fail("This program requires Windows 3.1 or higher");
- dwInfo = GetFreeMemInfo();
- if (dwInfo == -1L)
- puts("GetFreeMemInfo returned -1");
- else
- {
- wTotalUnlocked = LOWORD(dwInfo);
- wFreePages = HIWORD(dwInfo);
- if ((wTotalUnlocked != mem.dwUnlockedPages) ||
- (wFreePages != mem.dwFreePages))
- fail("Something wrong!");
- printf("Total Unlocked Pages = %04xh (%lu bytes)\n",
- wTotalUnlocked, (long) wTotalUnlocked * 4096L);
- printf("Total Free Pages = %04xh (%lu bytes)\n",
- wFreePages, (long) wFreePages * 4096L);
- }
- return 0;
- }
-
-