代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
虚拟内存空间
作者:
眼看前方_脚踏实地
/ 发布于
2013/10/24
/
745
/****************************************************************************** Module: VMAlloc.cpp Notices: Copyright (c) 2008 Jeffrey Richter & Christophe Nasarre ******************************************************************************/ #include "..\CommonFiles\CmnHdr.h" /* See Appendix A. */ #include <WindowsX.h> #include <tchar.h> #include "Resource.h" #include <StrSafe.h> /////////////////////////////////////////////////////////////////////////////// // The number of bytes in a page on this host machine. 本地机器的分配页的字节数 UINT g_uPageSize = 0; UINT g_uMaxPages = 0;//最大页面数 UINT g_uDrawMaxWidth = 0;//需要画的最大宽度 UINT g_cellWidth = 0; //单元格宽度 // A dummy data structure used for the array. 数组使用的虚拟数据结构体 typedef struct { BOOL bInUse; BYTE bOtherData[2048 - sizeof(BOOL)]; } SOMEDATA, *PSOMEDATA; // The number of structures in the array 数组的大小 #define MAX_SOMEDATA (50) // Pointer to an array of data structures 指针指向结构体数组 PSOMEDATA g_pSomeData = NULL; // The rectangular area in the window occupied by the memory map 内存图占窗口的矩形区域 RECT g_rcMemMap; /////////////////////////////////////////////////////////////////////////////// BOOL Dlg_OnInitDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam) { chSETDLGICONS(hWnd, IDI_VMALLOC); // Initialize the dialog box by disabling all the nonsetup controls. EnableWindow(GetDlgItem(hWnd, IDC_INDEXTEXT), FALSE); EnableWindow(GetDlgItem(hWnd, IDC_INDEX), FALSE); EnableWindow(GetDlgItem(hWnd, IDC_USE), FALSE); EnableWindow(GetDlgItem(hWnd, IDC_CLEAR), FALSE); EnableWindow(GetDlgItem(hWnd, IDC_GARBAGECOLLECT), FALSE); // Get the coordinates of the memory map display. 获得屏幕坐标 GetWindowRect(GetDlgItem(hWnd, IDC_MEMMAP), &g_rcMemMap); // 把屏幕坐标转到到hWnd坐标系去 即在父窗口的位置 MapWindowPoints(NULL, hWnd, (LPPOINT) &g_rcMemMap, 2); // Destroy the window that identifies the location of the memory map DestroyWindow(GetDlgItem(hWnd, IDC_MEMMAP)); // Put the page size in the dialog box just for the user's information. TCHAR szBuf[10]; StringCchPrintf(szBuf, _countof(szBuf), TEXT("%d KB"), g_uPageSize / 1024); SetDlgItemText(hWnd, IDC_PAGESIZE, szBuf); // Initialize the edit control. SetDlgItemInt(hWnd, IDC_INDEX, 0, FALSE); g_uMaxPages = MAX_SOMEDATA * sizeof(SOMEDATA) / g_uPageSize; g_uDrawMaxWidth = (g_rcMemMap.right - g_rcMemMap.left) / g_uMaxPages * g_uMaxPages; g_cellWidth = g_uDrawMaxWidth / g_uMaxPages; return(TRUE); } /////////////////////////////////////////////////////////////////////////////// void Dlg_OnDestroy(HWND hWnd) { if (g_pSomeData != NULL) VirtualFree(g_pSomeData, 0, MEM_RELEASE); } /////////////////////////////////////////////////////////////////////////////// VOID MyGarbageCollect(PSOMEDATA psomeData, DWORD dwnum, DWORD dwStructSize) { //一共有多少页 UINT uMaxPages = g_uMaxPages; for (UINT uPage = 0; uPage < uMaxPages; ++uPage) { BOOL bAnyAllocsInThisPage = FALSE; BOOL bCommit = FALSE; UINT uIndex = uPage * g_uPageSize / dwStructSize; UINT uIndexLast = uIndex + g_uPageSize / dwStructSize; for (; uIndex < uIndexLast; ++uIndex) { MEMORY_BASIC_INFORMATION mbi; VirtualQuery(&g_pSomeData[uIndex],&mbi,sizeof(mbi)); bCommit = mbi.State == MEM_COMMIT; if (bCommit && psomeData[uIndex].bInUse){ bAnyAllocsInThisPage = TRUE; } } if (bCommit && !bAnyAllocsInThisPage) { VirtualFree(&psomeData[uIndexLast-1], dwStructSize, MEM_DECOMMIT); } } } VOID GarbageCollect(PVOID pvBase, DWORD dwNum, DWORD dwStructSize) { UINT uMaxPages = dwNum * dwStructSize / g_uPageSize; for (UINT uPage = 0; uPage < uMaxPages; uPage++) { BOOL bAnyAllocsInThisPage = FALSE; UINT uIndex = uPage * g_uPageSize / dwStructSize; UINT uIndexLast = uIndex + g_uPageSize / dwStructSize; for (; uIndex < uIndexLast; uIndex++) { MEMORY_BASIC_INFORMATION mbi; VirtualQuery(&g_pSomeData[uIndex], &mbi, sizeof(mbi)); bAnyAllocsInThisPage = ((mbi.State == MEM_COMMIT) && * (PBOOL) ((PBYTE) pvBase + dwStructSize * uIndex)); // Stop checking this page, we know we can't decommit it. if (bAnyAllocsInThisPage) break; } if (!bAnyAllocsInThisPage) { // No allocated structures in this page; decommit it. VirtualFree(&g_pSomeData[uIndexLast - 1], dwStructSize, MEM_DECOMMIT); } } } /////////////////////////////////////////////////////////////////////////////// void Dlg_OnCommand(HWND hWnd, int id, HWND hWndCtl, UINT codeNotify) { UINT uIndex = 0; switch (id) { case IDCANCEL: EndDialog(hWnd, id); break; case IDC_RESERVE: // Reserve enough address space to hold the array of structures. 分配虚拟区域 50个结构体大小 g_pSomeData = (PSOMEDATA) VirtualAlloc(NULL, MAX_SOMEDATA * sizeof(SOMEDATA), MEM_RESERVE, PAGE_READWRITE); // Disable the Reserve button and enable all the other controls. EnableWindow(GetDlgItem(hWnd, IDC_RESERVE), FALSE); EnableWindow(GetDlgItem(hWnd, IDC_INDEXTEXT), TRUE); EnableWindow(GetDlgItem(hWnd, IDC_INDEX), TRUE); EnableWindow(GetDlgItem(hWnd, IDC_USE), TRUE); EnableWindow(GetDlgItem(hWnd, IDC_GARBAGECOLLECT), TRUE); // Force the index edit control to have the focus. SetFocus(GetDlgItem(hWnd, IDC_INDEX)); // Force the memory map to update InvalidateRect(hWnd, &g_rcMemMap, FALSE); break; case IDC_INDEX: if (codeNotify != EN_CHANGE) break; uIndex = GetDlgItemInt(hWnd, id, NULL, FALSE); if ((g_pSomeData != NULL) && chINRANGE(0, uIndex, MAX_SOMEDATA - 1)) { MEMORY_BASIC_INFORMATION mbi; VirtualQuery(&g_pSomeData[uIndex], &mbi, sizeof(mbi)); BOOL bOk = (mbi.State == MEM_COMMIT); if (bOk) bOk = g_pSomeData[uIndex].bInUse; EnableWindow(GetDlgItem(hWnd, IDC_USE), !bOk); EnableWindow(GetDlgItem(hWnd, IDC_CLEAR), bOk); } else { EnableWindow(GetDlgItem(hWnd, IDC_USE), FALSE); EnableWindow(GetDlgItem(hWnd, IDC_CLEAR), FALSE); } break; case IDC_USE: uIndex = GetDlgItemInt(hWnd, IDC_INDEX, NULL, FALSE); // NOTE: New pages are always zeroed by the system VirtualAlloc(&g_pSomeData[uIndex], sizeof(SOMEDATA), MEM_COMMIT, PAGE_READWRITE); //分配页面是4KB 刚好是2个结构体大小 g_pSomeData[uIndex].bInUse = TRUE; EnableWindow(GetDlgItem(hWnd, IDC_USE), FALSE); EnableWindow(GetDlgItem(hWnd, IDC_CLEAR), TRUE); // Force the Clear button control to have the focus. SetFocus(GetDlgItem(hWnd, IDC_CLEAR)); // Force the memory map to update InvalidateRect(hWnd, &g_rcMemMap, FALSE); break; case IDC_CLEAR: uIndex = GetDlgItemInt(hWnd, IDC_INDEX, NULL, FALSE); g_pSomeData[uIndex].bInUse = FALSE; EnableWindow(GetDlgItem(hWnd, IDC_USE), TRUE); EnableWindow(GetDlgItem(hWnd, IDC_CLEAR), FALSE); // Force the Use button control to have the focus. SetFocus(GetDlgItem(hWnd, IDC_USE)); break; case IDC_GARBAGECOLLECT: MyGarbageCollect(g_pSomeData, MAX_SOMEDATA, sizeof(SOMEDATA)); // Force the memory map to update InvalidateRect(hWnd, &g_rcMemMap, FALSE); break; } } /////////////////////////////////////////////////////////////////////////////// void Dlg_OnPaint(HWND hWnd) { // Update the memory map PAINTSTRUCT ps; BeginPaint(hWnd, &ps); UINT uMaxPages = g_uMaxPages; UINT uMemMapWidth = g_uDrawMaxWidth; if (g_pSomeData == NULL) { // The memory has yet to be reserved. Rectangle(ps.hdc, g_rcMemMap.left, g_rcMemMap.top, g_rcMemMap.left + uMemMapWidth, g_rcMemMap.bottom); } else { // Walk the virtual address space, painting the memory map for (UINT uPage = 0; uPage < uMaxPages; uPage++) { UINT uIndex = uPage * g_uPageSize / sizeof(SOMEDATA); UINT uIndexLast = uIndex + g_uPageSize / sizeof(SOMEDATA); for (; uIndex < uIndexLast; uIndex++) { MEMORY_BASIC_INFORMATION mbi; VirtualQuery(&g_pSomeData[uIndex], &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; } SelectObject(ps.hdc, GetStockObject(nBrush)); Rectangle(ps.hdc, g_rcMemMap.left + g_cellWidth * uPage, g_rcMemMap.top, g_rcMemMap.left + g_cellWidth * (uPage + 1), g_rcMemMap.bottom); } } } EndPaint(hWnd, &ps); } /////////////////////////////////////////////////////////////////////////////// INT_PTR WINAPI Dlg_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { chHANDLE_DLGMSG(hWnd, WM_INITDIALOG, Dlg_OnInitDialog); chHANDLE_DLGMSG(hWnd, WM_COMMAND, Dlg_OnCommand); chHANDLE_DLGMSG(hWnd, WM_PAINT, Dlg_OnPaint); chHANDLE_DLGMSG(hWnd, WM_DESTROY, Dlg_OnDestroy); } return(FALSE); } /////////////////////////////////////////////////////////////////////////////// int WINAPI _tWinMain(HINSTANCE hInstExe, HINSTANCE, PTSTR, int) { // Get the page size used on this CPU. SYSTEM_INFO si; GetSystemInfo(&si); g_uPageSize = si.dwPageSize; //获得页面大小 DialogBox(hInstExe, MAKEINTRESOURCE(IDD_VMALLOC), NULL, Dlg_Proc); return(0); } //////////////////////////////// End of File //////////////////////////////////
试试其它关键字
虚拟内存
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
眼看前方_脚踏实地
贡献的其它代码
(
1
)
.
虚拟内存空间
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3