// maindlg.cpp : implementation of the CMainDlg class
#include "stdafx.h"
#include "resource.h"
#include "maindlg.h"
#include <pdh.h>
#include <pdhmsg.h>
#pragma comment(lib, "pdh.lib")
#pragma warning(push, 2)
#include <stack>
#pragma warning(pop)
typedef std::stack<HGLOBAL> stackHGLOBAL;
#define MAX_TICS 8
#define BLOCKSIZE (1000) // 1 megabyte
#define TID_UPDATE 1
#define TID_ALLOC 2
#define TID_FREE 3
volatile DWORD gdwAlloc = 0;
volatile DWORD gdwNextAlloc = 0;
volatile DWORD gdwTics[MAX_TICS] = { 0 };
volatile BOOL gbAllocAndUse = false;
stackHGLOBAL gMem;
//////////////////////////////////////////////////////////////////////////
//
// This is all stuff to work-around the GetPerformanceInfo() problem.
// The function exists only in the PSAPI.DLL in WindowsXP, other OS must query
// performance counter for this.
//
bool IsXP()
{
OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if(GetVersionEx(&osvi))
if(osvi.dwPlatformId== VER_PLATFORM_WIN32_NT)
if (osvi.dwMajorVersion == 5)
if (osvi.dwMinorVersion == 1)
return true;
return false;
}
// define the structure if needed
#if !defined(PERFORMACE_INFORMATION)
typedef struct _PERFORMACE_INFORMATION
{
DWORD cb;
SIZE_T CommitTotal;
SIZE_T CommitLimit;
SIZE_T CommitPeak;
SIZE_T PhysicalTotal;
SIZE_T PhysicalAvailable;
SIZE_T SystemCache;
SIZE_T KernelTotal;
SIZE_T KernelPaged;
SIZE_T KernelNonpaged;
SIZE_T PageSize;
DWORD HandleCount;
DWORD ProcessCount;
DWORD ThreadCount;
} PERFORMACE_INFORMATION, *PPERFORMACE_INFORMATION;
#endif
// depending on the OS (NT4/W2k or XP) we use different functions
typedef BOOL WINAPI _GPI(PPERFORMACE_INFORMATION pPerformanceInformation, DWORD cb);
_GPI* gpGetPerformanceInfo = NULL; // this is our function pointer
HQUERY ghQuery = NULL;
HCOUNTER hc1, hc2, hc4, hc5, hc6, hc7, hc8, hc9; // dirty hack
// opens the performance query
void _OpenPerf()
{
PDH_STATUS s = NULL;
s = PdhOpenQuery(NULL, NULL, &ghQuery);
if(s == ERROR_SUCCESS)
{
s = PdhAddCounter(ghQuery, _T("\\Memory\\Committed Bytes"), NULL, &hc1);
s = PdhAddCounter(ghQuery, _T("\\Memory\\Commit Limit"), NULL, &hc2);
s = PdhAddCounter(ghQuery, _T("\\Memory\\Pool Paged Bytes"), NULL, &hc4);
s = PdhAddCounter(ghQuery, _T("\\Memory\\Pool Nonpaged Bytes"), NULL, &hc5);
s = PdhAddCounter(ghQuery, _T("\\Process(_Total)\\Handle Count"), NULL, &hc6);
s = PdhAddCounter(ghQuery, _T("\\System\\Processes"), NULL, &hc7);
s = PdhAddCounter(ghQuery, _T("\\System\\Threads"), NULL, &hc8);
// s = PdhAddCounter(ghQuery, _T("\\Memory\\Cache Bytes"), NULL, &hc9);
}
}
//, well, who would guess it, it closes the query
void _ClosePerf()
{
PdhCloseQuery(ghQuery);
}
// this is our reimplementation of the GetPerformanceInfo() function in PSAPI.lib
BOOL WINAPI _GetPerformanceInfo (PPERFORMACE_INFORMATION pPerformanceInformation, DWORD cb)
{
SYSTEM_INFO si;
MEMORYSTATUS ms;
::GetSystemInfo(&si);
::GlobalMemoryStatus(&ms);
ZeroMemory(pPerformanceInformation, min(cb, pPerformanceInformation->cb));
if(ghQuery != NULL)
{
PDH_STATUS s = NULL;
PDH_FMT_COUNTERVALUE cv;
s = PdhCollectQueryData(ghQuery);
if(s == ERROR_SUCCESS)
{
s = PdhGetFormattedCounterValue(hc1, PDH_FMT_LONG, NULL, &cv);
pPerformanceInformation->CommitTotal = cv.longValue / si.dwPageSize;
s = PdhGetFormattedCounterValue(hc2, PDH_FMT_LONG, NULL, &cv);
pPerformanceInformation->CommitLimit = cv.longValue / si.dwPageSize;
pPerformanceInformation->CommitPeak = 0; // where does that come from under pre-XP ??
// one solution would be to query all processes for this value, possibly.
pPerformanceInformation->PhysicalTotal = ms.dwTotalPhys / si.dwPageSize;
pPerformanceInformation->PhysicalAvailable = ms.dwAvailPhys / si.dwPageSize;
s = PdhGetFormattedCounterValue(hc4, PDH_FMT_LONG, NULL, &cv);
pPerformanceInformation->KernelPaged = cv.longValue / si.dwPageSize;
s = PdhGetFormattedCounterValue(hc5, PDH_FMT_LONG, NULL, &cv);
pPerformanceInformation->KernelNonpaged = cv.longValue / si.dwPageSize;
pPerformanceInformation->KernelTotal = pPerformanceInformation->KernelPaged + pPerformanceInformation->KernelNonpaged;
s = PdhGetFormattedCounterValue(hc6, PDH_FMT_LONG, NULL, &cv);
pPerformanceInformation->HandleCount = cv.longValue;
s = PdhGetFormattedCounterValue(hc7, PDH_FMT_LONG, NULL, &cv);
pPerformanceInformation->ProcessCount = cv.longValue;
s = PdhGetFormattedCounterValue(hc8, PDH_FMT_LONG, NULL, &cv);
pPerformanceInformation->ThreadCount = cv.longValue;
// s = PdhGetFormattedCounterValue(hc9, PDH_FMT_LONG, NULL, &cv);
// obviously this is not the correct value to ask for.
pPerformanceInformation->SystemCache = 0; //cv.longValue / si.dwPageSize;
}
}
pPerformanceInformation->PageSize = si.dwPageSize;
return TRUE;
}
#pragma warning(disable: 4189) // 'n' : local variable is initialized but not referenced
//
// ///////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
//
LRESULT CMainDlg::OnInitDialog(UINT /* uMsg */, WPARAM /* wParam */, LPARAM /* lParam */, BOOL & /* bHandled */ )
{
// center the dialog on the screen
CenterWindow();
m_cSlider.Attach(GetDlgItem(IDC_SLIDER1));
if(IsXP())
{
gpGetPerformanceInfo = (_GPI*)GetProcAddress(LoadLibraryA("PSAPI.dll"), "GetPerformanceInfo");
}
if(gpGetPerformanceInfo == NULL)
{
_OpenPerf(); // open a performance query
gpGetPerformanceInfo = _GetPerformanceInfo;
}
MEMORYSTATUS memstat;
///////////////////
::GlobalMemoryStatus(&memstat);
PERFORMACE_INFORMATION stat;
//////////////////////////
gpGetPerformanceInfo(&stat, sizeof(PERFORMACE_INFORMATION));
m_cSlider.SetRangeMin(0);
m_cSlider.SetRangeMax((memstat.dwTotalVirtual) / (1024));
m_cSlider.SetLineSize(BLOCKSIZE);
m_cSlider.SetPageSize(BLOCKSIZE * 16);
SendDlgItemMessage(IDC_EDIT_ALLOC, EM_LIMITTEXT, 7);
SendDlgItemMessage(IDC_EDIT_FREE, EM_LIMITTEXT, 7);
UpdateMemoryStatistics();
SetTimer(TID_UPDATE, 250);
// set icons
HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SetIcon(hIcon, TRUE);
HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SetIcon(hIconSmall, FALSE);
return TRUE;
}
//
// ///////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
//
LRESULT CMainDlg::OnCancel(WORD /* wNotifyCode */, WORD wID, HWND /* hWndCtl */, BOOL & /* bHandled */ )
{
// if not XP or XP failed then we had opened a query, now close it.
if(gpGetPerformanceInfo == _GetPerformanceInfo)
{
_ClosePerf();
}
EndDialog(wID);
return 0;
}
//
// ///////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
//
void CMainDlg::UpdateMemoryStatistics(void)
{
TCHAR chBuf[20];
PERFORM
没有合适的资源?快使用搜索试试~ 我知道了~
Alloc_src1[1].zip_Alloc_src1_This Just In.._disk_memory test
共13个文件
h:4个
cpp:3个
ico:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 89 浏览量
2022-09-23
22:50:01
上传
评论
收藏 10KB ZIP 举报
温馨提示
内存分配工具 Sometimes it is very helpful to test your applications in extreme situations like low resources, full hard disk, or low memory conditions. This tool covers just the last - memory. It allows you to allocate as much memory as might be available.
资源推荐
资源详情
资源评论
收起资源包目录
Alloc_src1[1].zip (13个子文件)
Alloc
stdafx.cpp 282B
Alloc.dsw 533B
res
Alloc.exe.manifest 584B
Alloc.ico 1KB
stdafx.h 910B
Alloc.dsp 3KB
Alloc.cpp 679B
maindlg.h 1KB
resource.h 1KB
Alloc.h 12B
maindlg.cpp 12KB
Alloc.rc 8KB
www.pudn.com.txt 218B
共 13 条
- 1
资源评论
御道御小黑
- 粉丝: 58
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功