home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Monster Media 1994 #1
/
monster.zip
/
monster
/
PROG_GEN
/
MSJMAR94.ZIP
/
WINQA.ZIP
/
FREEMEM2.C
< prev
next >
Wrap
C/C++ Source or Header
|
1994-03-01
|
6KB
|
168 lines
//==================================
// FREEMEM2.C - Matt Pietrek 1993
//==================================
#include <windows.h>
#include <toolhelp.h>
#include "freemem2.h"
// Reads the CMOS RAM to get the total installed memory
DWORD GetInstalledRAM(void)
{
__asm {
cli // Disable interrupts
// Read the base memory (in K) into BX
mov al, 15h // Tell the CMOS ram which byte we want to read
out 70h, al
in al, 71h // read in the byte from the CMOS ram
mov bl, al
mov al, 16h
out 70h, al
in al, 71h
mov bh, al
// Read the extended memory (in K) into AX
mov al, 18h
out 70h, al
in al, 71h
mov ah, al
mov al, 17h
out 70h, al
in al, 71h
sti // enable interrupts
xor dx, dx // add base + extended memory and put the
add ax, bx // result into DX:AX
adc dx, 0
}
// Ignore the compiler complaint about "function must return a value"
}
// Sum up the sizes of all the global heap blocks. If a non-zero pointer
// is passed as a parameter, store the size of the largest free block
// into that location.
DWORD GetGlobalHeapSize(DWORD *pLargestFree)
{
GLOBALENTRY ge;
DWORD size = 0;
BOOL fContinue;
if ( pLargestFree )
*pLargestFree = 0; // Initial largest free block is 0 bytes
// Use TOOLHELP GlobalFirst/GlobalNext to walk the global heap
ge.dwSize = sizeof(ge);
fContinue = GlobalFirst(&ge, GLOBAL_ALL);
while ( fContinue )
{
size += ge.dwBlockSize; // Add blocksize to running total
// If the block is free, and larger than the previous largest
// free block, make it the new largest free block
if ( pLargestFree && (ge.wType == GT_FREE) )
if ( ge.dwBlockSize > *pLargestFree )
*pLargestFree = ge.dwBlockSize;
fContinue = GlobalNext(&ge, GLOBAL_ALL);
}
*pLargestFree /= 1024; // Convert bytes to K
return size / 1024;
}
// Fill in the dialog text controls that never vary (installed RAM, etc...)
void PaintConstantItems(HWND hWndDlg)
{
MEMMANINFO mmi;
DWORD size;
char buffer[128];
mmi.dwSize = sizeof(mmi);
MemManInfo(&mmi);
wsprintf(buffer, "Installed RAM: %ldk", GetInstalledRAM());
SetDlgItemText(hWndDlg, IDT_INSTALLED_RAM, buffer );
// check to see if the field is -1. If we didn't, we'd show -4K!
size = (mmi.dwTotalPages == -1) ? -1 : mmi.dwTotalPages * 4;
wsprintf(buffer, "Total Memory: %ldk", size);
SetDlgItemText(hWndDlg, IDT_TOTAL_MEMORY, buffer );
wsprintf(buffer, "Swap File Pages: %ldk", mmi.dwSwapFilePages * 4);
SetDlgItemText(hWndDlg, IDT_SWAP_FILE_PAGES, buffer );
size = (mmi.dwTotalLinearSpace == -1) ? -1 : mmi.dwTotalLinearSpace * 4;
wsprintf(buffer, "Linear Space: %ldk", size);
SetDlgItemText(hWndDlg, IDT_ADDRESS_SPACE, buffer );
}
// Fill in the dialog text controls that vary (largest free block, etc...)
void PaintVariableItems(HWND hWndDlg)
{
MEMMANINFO mmi;
DWORD largestFreeGlobalHeapBlock;
DWORD totalGlobalHeapSize;
char buffer[128];
DWORD size;
totalGlobalHeapSize = GetGlobalHeapSize(&largestFreeGlobalHeapBlock);
mmi.dwSize = sizeof(mmi);
MemManInfo(&mmi);
wsprintf( buffer, "Global Heap Total: %ldk", totalGlobalHeapSize);
SetDlgItemText(hWndDlg, IDT_GLOBAL_HEAP_TOTAL, buffer);
wsprintf( buffer, "Largest Free Heap Block: %ldk",
largestFreeGlobalHeapBlock);
SetDlgItemText(hWndDlg, IDT_LARGEST_FREE_GLOBAL, buffer);
size = (mmi.dwMaxPagesLockable == -1) ? -1 : mmi.dwMaxPagesLockable * 4;
wsprintf( buffer, "Max Lockable Pages: %ldk", size);
SetDlgItemText(hWndDlg, IDT_MAX_LOCKABLE, buffer);
size = (mmi.dwFreePages == -1) ? -1 : mmi.dwFreePages * 4;
wsprintf( buffer, "Free Memory: %ldk", size);
SetDlgItemText(hWndDlg, IDT_TOTAL_FREE, buffer);
wsprintf( buffer, "Largest Free Block: %ldk", mmi.dwLargestFreeBlock/1024);
SetDlgItemText(hWndDlg, IDT_LARGEST_FREE, buffer);
size = (mmi.dwFreeLinearSpace == -1) ? -1 : mmi.dwFreeLinearSpace * 4;
wsprintf( buffer, "Free Linear Space: %ldk", size);
SetDlgItemText(hWndDlg, IDT_FREE_SPACE, buffer);
}
BOOL CALLBACK _export FreeMem2DlgProc(HWND hWndDlg, UINT msg,
WPARAM wParam, LPARAM lParam)
{
if ( msg == WM_INITDIALOG )
{
PaintConstantItems(hWndDlg);
PaintVariableItems(hWndDlg);
SetTimer( hWndDlg, 1, 5000, 0 ); // a 5 second timer
}
else if ( msg == WM_COMMAND )
{
if ( wParam == IDB_EXIT ) // Exit button pushed?
EndDialog(hWndDlg, 0);
}
else if ( msg == WM_TIMER ) // 5 seconds are up!
PaintVariableItems(hWndDlg);
else if ( msg == WM_CLOSE )
EndDialog(hWndDlg, 0); // User hit Close on the system menu
return FALSE;
}
int PASCAL WinMain( HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow )
{
DialogBox(hInstance, "FreeMem2Dlg", 0, FreeMem2DlgProc);
return 0;
}