// VirtualMemAllocDlg.cpp : implementation file
//
#include "stdafx.h"
#include "VirtualMemAlloc.h"
#include "VirtualMemAllocDlg.h"
//#include "WINUSER.H"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define MAX_SOMEDATA 50
typedef struct{
BOOL bInUse;
BYTE btOtherData[4096-sizeof(BOOL)];
}SOMEDATA,*PSOMEDATA;
PSOMEDATA g_pSomeData = NULL;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVirtualMemAllocDlg dialog
CVirtualMemAllocDlg::CVirtualMemAllocDlg(CWnd* pParent /*=NULL*/)
: CDialog(CVirtualMemAllocDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CVirtualMemAllocDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_nPageSize = 0;
}
void CVirtualMemAllocDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CVirtualMemAllocDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CVirtualMemAllocDlg, CDialog)
//{{AFX_MSG_MAP(CVirtualMemAllocDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_BN_CLICKED(IDC_USE, OnUse)
ON_BN_CLICKED(IDC_RESERVE, OnReserve)
ON_BN_CLICKED(IDC_COLLECT, OnCollect)
ON_EN_CHANGE(IDC_INDEX, OnChangeIndex)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVirtualMemAllocDlg message handlers
BOOL CVirtualMemAllocDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//获取这台机器的页面大小
SYSTEM_INFO si;
GetSystemInfo(&si);
m_nPageSize = si.dwPageSize;
SetWindowText("虚拟内存分配示例程序");
GetDlgItem(IDC_INDEXTEXT)->EnableWindow(FALSE);
GetDlgItem(IDC_INDEX)->EnableWindow(FALSE);
GetDlgItem(IDC_USE)->EnableWindow(FALSE);
GetDlgItem(IDC_CLEAR)->EnableWindow(FALSE);
GetDlgItem(IDC_COLLECT)->EnableWindow(FALSE);
//获得"内存表"控件的坐标
GetDlgItem(IDC_MEMMAP)->GetWindowRect(&m_rcMemMap);
RECT rcWnd,rcClient;
GetWindowRect(&rcWnd);
GetClientRect(&rcClient);
UINT nTitleHeight = abs((rcWnd.bottom-rcWnd.top)-(rcClient.bottom-rcClient.top));
m_rcMemMap.bottom -= nTitleHeight;
m_rcMemMap.top -= nTitleHeight;
MapWindowPoints(this,(LPPOINT)&m_rcMemMap,2);
GetDlgItem(IDC_MEMMAP)->DestroyWindow();
CString strBuf;
strBuf.Format("%d KB",m_nPageSize/1024);
GetDlgItem(IDC_PAGESIZE)->SetWindowText(strBuf);
//初始化"索引"编辑框
GetDlgItem(IDC_INDEX)->SetWindowText("0");
return TRUE; // return TRUE unless you set the focus to a control
}
void CVirtualMemAllocDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CVirtualMemAllocDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
//CDialog::OnPaint();
PAINTSTRUCT ps;
BeginPaint(&ps);
UINT nMaxPage = MAX_SOMEDATA * sizeof(SOMEDATA)/m_nPageSize;
UINT nMemMapWidth = m_rcMemMap.right - m_rcMemMap.left;
if(g_pSomeData == NULL)
{
Rectangle(ps.hdc,m_rcMemMap.left,m_rcMemMap.top,
m_rcMemMap.right-nMemMapWidth%nMaxPage,m_rcMemMap.bottom);
}
else
{
//遍历内存空间,以便刷新"内存表"控件
for(UINT nPage=0;nPage<nMaxPage;nPage++)
{
UINT nIndex = nPage*m_nPageSize/sizeof(SOMEDATA);
UINT nIndexLast = nIndex + m_nPageSize/sizeof(SOMEDATA);
for(;nIndex<nIndexLast;nIndex++)
{
MEMORY_BASIC_INFORMATION mbi;
VirtualQuery(&g_pSomeData[nIndex],&mbi,sizeof(mbi));
int nBrush = 0;
//根据不同的状态选择不同的刷子
switch(mbi.State)
{
case MEM_FREE:
{
nBrush = WHITE_BRUSH;
break;
}
case MEM_RESERVE:
{
nBrush = GRAY_BRUSH;
break;
}
case MEM_COMMIT:
{
nBrush = BLACK_BRUSH;
break;
}
default:
{
break;
}
}
SelectObject(ps.hdc,GetStockObject(nBrush));
Rectangle(ps.hdc,m_rcMemMap.left+nMemMapWidth/nMaxPage*nPage,
m_rcMemMap.top,
m_rcMemMap.left+nMemMapWidth/nMaxPage*(nPage+1),
m_rcMemMap.bottom);
}
}
}
EndPaint(&ps);
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CVirtualMemAllocDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CVirtualMemAllocDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
if(g_pSomeData != NULL)
{
//收回已提交的内存,释放预留的区域
VirtualFree(g_pSomeData,0,MEM_RELEASE);
}
}
//无用单元收集函数,回收包含指定索引元素的内存页面
void CVirtualMemAllocDlg::GarbageCollect(PVOID pvBase, DWORD dwNum, DWORD dwStructSize)
{
static DWORD dwPageSize = 0;
if(dwPageSize == 0)
{
SYSTEM_INFO si;
GetSystemInfo(&si);
dwPageSize = si.dwPageSize;
}
UINT nMaxPages = dwNum*dwStructSize/m_nPageSize;
for(UINT nPage=0;nPage<nMaxPages;nPage++)
{
BOOL bAllocInThisPage = FALSE;
UINT nIndex = nPage * m_nPageSize/dwStructSize;
UINT nIndexLast = nIndex + m_nPageSize/dwStructSize;
for(;nIndex<nIndexLast;nIndex++)
{
MEMORY_BASIC_INFORMATION mbi;
VirtualQuery(&g_pSomeData[nIndex],&mbi,sizeof(mbi));
bAllocInThisPage = ((mbi.State == MEM_COMMIT)&&
*(PBOOL)((PBYTE)pvBase + dwStructSize * nIndex));