//myallocator.h
#ifndef _MYALLOCATOR_
#define _MYALLOCATOR_
#include <iostream>
#include <windows.h>
namespace MyLib {
template <class T>
class MyAlloc {
public:
static HANDLE hHeap;
// type definitions
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
// rebind allocator to type U
template <class U>
struct rebind {
typedef MyAlloc<U> other;
};
// return address of values
pointer address (reference value) const {
return &value;
}
const_pointer address (const_reference value) const {
return &value;
}
/* constructors and destructor
* - nothing to do because the allocator has no state
*/
MyAlloc() throw() {
}
MyAlloc(const MyAlloc&) throw() {
}
~MyAlloc() throw() {
}
// return maximum number of elements that can be allocated
size_type max_size () const throw() {
size_type N;
N=(size_type)(-1)/ sizeof(T);
return (0 < N ? N : 1);
}
// allocate but don't initialize num elements of type T
pointer allocate (size_type num, const void* = 0) {
// print message and allocate memory with global new
/*std::cerr << "allocate " << num << " element(s)"
<< " of size " << sizeof(T) << std::endl;
*/
pointer ret = (pointer)(HeapAlloc(hHeap,0,num*sizeof(T)));
// std::cerr << " allocated at: " << (void*)ret << std::endl;
return ret;
}
char *_Charalloc(size_type N)//vc 所附带的stl的特色
{
return (char*)HeapAlloc(hHeap,0,N*sizeof(T));
}
// initialize elements of allocated storage p with value value
void construct (pointer p, const T& value) {
// initialize memory with placement new
new((void*)p)T(value);
}
// destroy elements of initialized storage p
void destroy (pointer p) {
// destroy objects by calling their destructor
p->~T();
}
// deallocate storage p of deleted elements
//原本应该为pointer
void deallocate (void* p, size_type num) {
// print message and deallocate memory with global delete
/*
std::cerr << "deallocate " << num << " element(s)"
<< " of size " << sizeof(T)
<< " at: " << (void*)p << std::endl;
*/
HeapFree(hHeap,0,(void*)p);
}
};
// return that all specializations of this allocator are interchangeable
template <class T1, class T2>
bool operator== (const MyAlloc<T1>&,
const MyAlloc<T2>&) throw() {
return true;
}
template <class T1, class T2>
bool operator!= (const MyAlloc<T1>&,
const MyAlloc<T2>&) throw() {
return false;
}
}//end namespace MyLib
#endif
//teststlmem.cpp
/*
written by leezy_2000
03-9-5 15:12
*/
#include "stdafx.h"
#pragma warning(disable:4786)
//#define _STLP_USE_MALLOC
#include "myallocator.h"
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <windows.h>
#include <Tlhelp32.h>
typedef unsigned long ULONG_PTR, *PULONG_PTR;
using namespace std;
/*
本程序需要注意的几点:
1、在实现自己的分配器,这样可以使stl容器的变化不影响我们要监测的堆
2、容器只能用vector否则任何堆的任何变化将导致Heap32Next始终返回TRUE
这应该是微软的bug
3、分配内存失败的时候应该抛出std::bad_alloc内存,此处考虑不会出现低
内存的情况,没抛出此异常。即认定自编写分配器分配内存时不会失败。
*/
//用于比较堆内存块的仿函数
//以块大小来判定两个HEAPENTRY32的大小
class HeapInfoCompare
{
public:
bool operator() (const HEAPENTRY32& he1,const HEAPENTRY32& he2) const
{
return (he1.dwBlockSize < he2.dwBlockSize);
}
};
typedef vector < HEAPENTRY32, MyLib::MyAlloc<HEAPENTRY32> > HEAPENTRYSET;
void heapinfo(HEAPENTRYSET& hset,ULONG_PTR heapid);
void getheapid(set<ULONG_PTR>& heapid)
{
HANDLE hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPHEAPLIST,GetCurrentProcessId());
HEAPLIST32 heaplist32;
heaplist32.dwSize=sizeof(HEAPLIST32);
BOOL bRet=Heap32ListFirst(hSnapShot,&heaplist32);
while(bRet)
{
heapid.insert(heaplist32.th32HeapID);
cout<<heaplist32.th32HeapID<<endl;
bRet=Heap32ListNext(hSnapShot,&heaplist32);
}
CloseHandle(hSnapShot);
cout<<"the end"<<endl;
}
HANDLE MyLib::MyAlloc<HEAPENTRY32>::hHeap=NULL;
HANDLE hHeap;
int main(int argc, char* argv[])
{
//枚举此时所有堆并在建立新堆后再次枚举这样从中剔除新建堆
set<ULONG_PTR> heapid1,heapid2,heapid3;
getheapid(heapid1);
hHeap=HeapCreate(0,0,0);
getheapid(heapid2);
insert_iterator<set<ULONG_PTR> > iter(heapid3,heapid3.begin());
set_difference(heapid2.begin(),heapid2.end(),heapid1.begin(),heapid1.end(),
iter);
set<ULONG_PTR>::iterator pos;
ULONG_PTR newheapid;
for( pos=heapid3.begin(); pos !=heapid3.end(); ++pos)
{
cout<<"The new heap id is/t"<<(*pos)<<endl;
newheapid=*pos;
}
MyLib::MyAlloc<HEAPENTRY32>::hHeap=hHeap;
//vector<int, MyLib::MyAlloc<int> > v1;
HEAPENTRYSET heapset1,heapset2,heapset3;
heapset1.reserve(400);//保证vector不自动增长
heapset2.reserve(400);
heapset3.reserve(400);
int size;
heapinfo(heapset1,newheapid);
sort(heapset1.begin(),heapset1.end(),HeapInfoCompare());
size=heapset1.size();
HANDLE hCurHeap=GetProcessHeap();
//HeapAlloc(hCurHeap,HEAP_ZERO_MEMORY,4*1024);
char* p=new char[4*1024];
//GlobalAlloc(GHND,4*1024);
char* q=(char*)malloc(4*1024);
cout<< "the p is"<<(int)p<<endl;
heapinfo(heapset2,newheapid);
sort(heapset2.begin(),heapset2.end(),HeapInfoCompare());
size=heapset2.size();
insert_iterator<HEAPENTRYSET> miter(heapset3,heapset3.begin());
set_difference(heapset2.begin(),heapset2.end(),heapset1.begin(),heapset1.end(),
miter,HeapInfoCompare());
size=heapset3.size();
HEAPENTRYSET::iterator mpos;
for( mpos=heapset3.begin(); mpos !=heapset3.end(); ++mpos)
{
cout<<"The size of the different block is/t"<<(*mpos).dwBlockSize<<"/tand the addresss is/t"<<(*mpos).dwAddress<<"/tdwFlags is/t"<<(*mpos).dwFlags <<endl;
cout<<"The heapid is:/t"<<(*mpos).th32HeapID <<endl;
}
return 0;
}
void heapinfo(HEAPENTRYSET& hset,ULONG_PTR hid)
{
HANDLE hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPHEAPLIST,GetCurrentProcessId());
HEAPLIST32 heaplist32;
heaplist32.dwSize=sizeof(HEAPLIST32);
BOOL bRet=Heap32ListFirst(hSnapShot,&heaplist32);
static int i=0;
while(bRet)
{
HEAPENTRY32 he32;
DWORD totalsize=0,freesize=0;
if(heaplist32.th32HeapID==hid)
{
bRet=Heap32ListNext(hSnapShot,&heaplist32);
continue;
}
DWORD number=10;
HANDLE ProcessHeap[10];
DWORD numget=GetProcessHeaps(number,ProcessHeap);
HANDLE hHeap=GetProcessHeap();
he32.dwSize=sizeof(HEAPENTRY32);
Heap32First(&he32,heaplist32.th32ProcessID,heaplist32.th32HeapID);
if(he32.dwFlags & LF32_FREE)
freesize +=he32.dwBlockSize;
totalsize +=he32.dwBlockSize;
cout<< "the heapid is :"<<he32.th32HeapID<<endl;
cout<<"the information of first block: "<< "Blocksize: "<<he32.dwBlockSize<<"/t Address: "<<(LONG)he32.dwAddress<<endl;
if((he32.dwFlags & LF32_FIXED) || (he32.dwFlags & LF32_MOVEABLE))
hset.push_back(he32);
while(Heap32Next(&he32))
{
cout<< "the information of block: " << "Blocksize: "<<he32.dwBlockSize<<"/t Address: "<<(LONG)he32.dwAddress<<endl;
totalsize +=he32.dwBlockSize;
if(he32.dwFlags & LF32_FREE)
freesize +=he32.dwBlockSize;
//cout<< ++i <<endl;
if((he32.dwFlags & LF32_FIXED) || (he32.dwFlags & LF32_MOVEABLE))
hset.push_back(he32);
//char*p =(char*)malloc(300);
}
cout<<"the total size of heap is: "<<totalsize<<endl;
cout<<"the free size of heap is: "<<freesize <<endl;
cout<<"the commited size of heap is: "<<(totalsize-freesize)<<endl;
bRet=Heap32ListNext(hSnapShot,&heaplist32);
}
CloseHandle(hSnapShot);
cout<<"the end"<<endl;
}
分享到:
相关推荐
《Delphi7经典问题解析》源代码。-----------------------------------------------------DWORD类型操作演示FlashGet URL智能拆分FoxMail信息自动填充INI文件处理WM_COPYDATA数据传递变体类型不使用VCL创建Windows...
Java二进制IO类与文件复制操作实例 16个目标文件 内容索引:Java源码,初学实例,二进制,文件复制 Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系...
深入解析WINDOWS操作系统(第4版) ISBN:9787121039690 本书是著名的操作系统内核专家Mark Russinovich和David Solomon撰写的Windows操作系统原理的最新版著作,全面和深入地阐述了Windows操作系统的整体结构以及...
《Delphi 7经典问题解析》源代码 ============================== 第二章 ====== 变体类型 不使用VCL创建WINDOWS程序 过程类型 回调机制 句柄演示 第六章 ====== FLASHGET ...
\和Java的“一次编译,到处运行”跨平台不同的是,Qt是源代码级的跨平台,一次编写,随处编译。一次开发的Qt应用程序可以移植到不同的平台上,只需重新编译即可运行。Qt支持的平台有: \? Microsoft Windows,包括...
全书从“内存管理”、“进程”、“进程间通信”、“设备驱动”等多个方面进行分析介绍,所有的分析都有ReactOS的源代码(以及部分由微软公开的源代码)作为依据,使读者能深入理解Windows内核的方方面面,也可以使...
我们有许多源代码公开的操作系统,可供随时下载和阅读,看上去好像让实现一个供自己把玩的微型操作系统变得容易很多,但事实往往不尽人意,因为这些代码动辄上万甚至几十几百万行,而且细节之间经常互相关联,要...
我们有许多源代码公开的操作系统,可供随时下载和阅读,看上去好像让实现一个供自己把玩的微型操作系统变得容易很多,但事实往往不尽人意,因为这些代码动辄上万甚至几十几百万行,而且细节之间经常互相关联,要...
\和Java的“一次编译,到处运行”跨平台不同的是,Qt是源代码级的跨平台,一次编写,随处编译。一次开发的Qt应用程序可以移植到不同的平台上,只需重新编译即可运行。Qt支持的平台有: \? Microsoft Windows,包括...
Visual.C++编程技巧精选500例源代码 内含各种例子(vc下各种控件的使用方法、标题栏与菜单栏、工具栏与状态栏、图标与光标、程序窗口、程序控制、进程与线程、字符串、文件读写操作、文件与文件夹属性操作、文件与...
Visual.C++编程技巧精选500例源代码 内含各种例子(vc下各种控件的使用方法、标题栏与菜单栏、工具栏与状态栏、图标与光标、程序窗口、程序控制、进程与线程、字符串、文件读写操作、文件与文件夹属性操作、文件与...
收录《Java编程技巧典型案例解析》中24个源代码。其他一些实例的源代码 由于作者出于商业目的无法提供,望读者原谅。 二、使用方法 把光盘放入光盘驱动器后,点击对应案例目录,根据路径直接找到源代码。 三...
《远程控制编程技术》源代码 内含(重启、图片操作、ip操作、键盘与鼠标、客户端以及服务端、文件传输等实例源码) 多个VC++加密解密算法库(CRYPT++) 详细讲解了Crypt++的加密解密的使用以及其它的加密解密方法...
2.3.5.远程调试中的源代码解析 75 2.4.调试场景 75 2.4.1.调试非交互式进程(服务 或者COM服务器) 76 2.4.2.在没有内核态调试器的情况 下调试非交互式进程(服务 或者COM服务器) 77 2.5.小结 77 第3章.调试器揭密 ...
Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...
2.6.1 内核编程的主要调用源 30 2.6.2 函数的多线程安全性 30 2.6.3 代码的中断级 32 2.6.4 WDK中出现的特殊代码 32 第3章 字符串与链表 35 3.1 字符串操作 35 3.1.1 使用字符串结构 35 3.1.2 字符串的初始化...
本书的大部分代码具有广泛的兼容性,适合从Windows 2000 一直到目前最新的Windows 7 Beta 版。 本书适合大专院校计算机系的学生、普通Windows程序员、Windows内核程序员、信息安全行业的程序员,以及希望了解...
Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码...
本书的大部分代码具有广泛的兼容性,适合从Windows 2000 一直到目前最新的Windows 7 Beta 版。 本书适合大专院校计算机系的学生、普通Windows程序员、Windows内核程序员、信息安全行业的程序员,以及希望了解...