// 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));
没有合适的资源?快使用搜索试试~ 我知道了~
VC之内存管理(源文件).rar
共80个文件
h:12个
cpp:11个
obj:9个
4星 · 超过85%的资源 需积分: 9 59 下载量 155 浏览量
2008-11-21
16:00:40
上传
评论
收藏 989KB RAR 举报
温馨提示
VC之内存管理:共享内存页,内存映射文件,虚拟内存.
资源推荐
资源详情
资源评论
收起资源包目录
VC之内存管理.rar (80个子文件)
第21章 内存管理
内存映射文件
Client
Client.dsp 4KB
Client.aps 35KB
RES
Client.rc2 398B
Client.ico 1KB
Client.ncb 49KB
StdAfx.cpp 208B
Client.plg 957B
ClientDlg.h 1KB
Debug
Client.exe 104KB
Client.pdb 345KB
VC60.PDB 356KB
Client.ilk 194KB
VC60.IDB 193KB
Client.res 2KB
ClientDlg.obj 25KB
Client.obj 13KB
StdAfx.obj 103KB
StdAfx.h 1KB
ClientDlg.cpp 5KB
Resource.h 730B
Client.dsw 537B
Client.cpp 2KB
Client.opt 53KB
Client.clw 1KB
Client.rc 5KB
Client.h 1KB
Server
RES
Server.ico 1KB
Server.rc2 398B
ServerDlg.cpp 6KB
StdAfx.cpp 208B
Server.aps 35KB
Server.rc 5KB
Server.h 1KB
Debug
Server.pdb 345KB
VC60.PDB 356KB
VC60.IDB 193KB
ServerDlg.obj 26KB
StdAfx.obj 103KB
Server.obj 13KB
Server.exe 104KB
Server.res 3KB
Server.ilk 196KB
Server.clw 1KB
StdAfx.h 1KB
Server.dsp 4KB
Server.opt 53KB
Resource.h 776B
ServerDlg.h 1KB
Server.plg 957B
Server.ncb 49KB
Server.cpp 2KB
Server.dsw 537B
虚拟内存
VirtualMemAlloc
VirtualMemAlloc.h 1KB
RES
VirtualMemAlloc.rc2 407B
VirtualMemAlloc.ico 1KB
VirtualMemAlloc.dsp 4KB
StdAfx.cpp 217B
VirtualMemAlloc.dsw 555B
VirtualMemAllocDlg.cpp 11KB
VirtualMemAlloc.rc 6KB
VirtualMemAlloc.plg 922B
VirtualMemAlloc.cpp 2KB
VirtualMemAllocDlg.h 2KB
Debug
VC60.PDB 356KB
VirtualMemAlloc.obj 13KB
VirtualMemAlloc.ilk 290KB
VC60.IDB 201KB
VirtualMemAllocDlg.obj 36KB
VirtualMemAlloc.exe 108KB
VirtualMemAlloc.pdb 377KB
StdAfx.obj 103KB
VirtualMemAlloc.res 3KB
StdAfx.h 1KB
VirtualMemAlloc.aps 36KB
Resource.h 1015B
VirtualMemAlloc.clw 2KB
VirtualMemAlloc.opt 53KB
VirtualMemAlloc.ncb 57KB
共享内存页
SharedFileServer.cpp 736B
SharedFileClient.cpp 567B
共 80 条
- 1
资源评论
- 闪破风浪2018-06-11很好的东西,学习了
- a12524339132012-10-23非常好的实例,很适合初学者
Chad_Long123
- 粉丝: 0
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Win64OpenSSL-3-3-0.exe
- 课高分程设计-基于C++实现的民航飞行与地图简易管理系统-南京航空航天大学
- 航天器遥测数据故障检测系统python源码+文档说明+数据库(课程设计)
- 北京航空航天大学操作系统课设+ppt+实验报告
- 基于Vue+Echarts实现风力发电机中传感器的数据展示监控可视化系统+源代码+文档说明(高分课程设计)
- 基于单片机的风力发电机转速控制源码
- 基于C++实现的风力发电气动平衡监测系统+源代码+测量数据(高分课程设计)
- 毕业设计- 基于STM32F103C8T6 单片机,物联网技术的太阳能发电装置+源代码+文档说明+架构图+界面截图
- 基于 LSTM(长短期记忆)(即改进的循环神经网络)预测风力发电厂中风力涡轮机产生的功率+源代码+文档说明
- 基于stm32f103+空心杯电机+oled按键+运动算法
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功