Windows記憶體機制解析(二)原始碼 (轉)
//myallocator.h
#ifndef _MYALLOCATOR_
#define _MYALLOCATOR_
#include
#include <.h>
namespace MyLib {
template
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
struct rebind {
typedef MyAlloc 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)//所附帶的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 s 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
bool operator== (const MyAlloc
const MyAlloc
return true;
}
template
bool operator!= (const MyAlloc
const MyAlloc
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
#include
#include
#include
#include
#include
typedef unsigned long ULONG_PTR, *PULONG_PTR;
using namespace std;
/*
本需要注意的幾點:
1、在實現自己的分配器,這樣可以使stl容器的變化不影響我們要監測的堆
2、容器只能用vector否則任何堆的任何變化將導致Heap32Next始終返回TRUE
這應該是的
3、分配失敗的時候應該丟擲std::bad_alloc記憶體,此處考慮不會出現低
記憶體的情況,沒丟擲此異常。即認定自編寫分配器分配記憶體時不會失敗。
*/
//用於比較堆記憶體塊的仿
//以塊大小來判定兩個HEAPENTRY32的大小
class HenfoCompare
{
public:
bool operator() (const HEAPENTRY32& he1,const HEAPENTRY32& he2) const
{
return (he1.dwBlockSize < he2.dwBlockSize);
}
};
typedef vector < HEAPENTRY32, MyLib::MyAlloc
void heapinfo(HEAPENTRYSET& hset,ULONG_PTR heapid);
void getheapid(set
{
HANDLE hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPHEAPLIST,GetCurrentProcessId());
HEAPLIST32 heaplist32;
heaplist32.dwSize=sizeof(HEAPLIST32);
BOOL bRet=Heap32ListFirst(hSnapShot,&heaplist32);
while(bRet)
{
heapid.insert(heaplist32.th32HeapID);
cout< bRet=Heap32ListNext(hSnapShot,&heaplist32); cout<} HANDLE MyLib::MyAlloc HANDLE hHeap; int main(int argc, char* argv[]) hHeap=HeapCreate(0,0,0); getheapid(heapid2); insert_iterator set_difference(heapid2.begin(),heapid2.end(),heapid1.begin(),heapid1.end(), set for( pos=heapid3.begin(); p!=heapid3.end(); ++pos) //vector int size; heapinfo(heapset1,newheapid); 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"<
heapinfo(heapset2,newheapid); sort(heapset2.begin(),heapset2.end(),HeapInfoCompare()); insert_iterator set_difference(heapset2.begin(),heapset2.end(),heapset1.begin(),heapset1.end(), size=heapset3.size(); HEAPENTRYSET::iterator mpos; return 0; heaplist32.dwSize=sizeof(HEAPLIST32); BOOL bRet=Heap32ListFirst(hSnapShot,&heaplist32); while(bRet) if(heaplist32.th32HeapID==hid) DWORD number=10; DWORD numget=GetProcessHeaps(number,ProcessHeap); HANDLE hHeap=GetProcessHeap(); he32.dwSize=sizeof(HEAPENTRY32); Heap32First(&he32,heaplist32.th32ProcessID,heaplist32.th32HeapID); if(he32.dwFlags & LF32_FREE) cout<< "the heapid is :"< while(Heap32Next(&he32)) totalsize +=he32.dwBlockSize; if(he32.dwFlags & LF32_FREE) } cout< cout< cout<
bRet=Heap32ListNext(hSnapShot,&heaplist32); CloseHandle(hSnapShot); cout<}
}
CloseHandle(hSnapShot);
{
//列舉此時所有堆並在建立新堆後再次列舉這樣從中剔除新建堆
set
getheapid(heapid1);
iter);
ULONG_PTR newheapid;
{
cout< newheapid=*pos;
}
MyLib::MyAlloc
HEAPENTRYSET heapset1,heapset2,heapset3;
heapset1.reserve(400);//保證vector不自動增長
heapset2.reserve(400);
heapset3.reserve(400);
sort(heapset1.begin(),heapset1.end(),HeapInfoCompare());
size=heapset2.size();
miter,HeapInfoCompare());
for( mpos=heapset3.begin(); mpos !=heapset3.end(); ++mpos)
{
cout< cout< }
}
void heapinfo(HEAPENTRYSET& hset,ULONG_PTR hid)
{
HANDLE hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPHEAPLIST,GetCurrentProcessId());
HEAPLIST32 heaplist32;
static int i=0;
{
HEAPENTRY32 he32;
D totalsize=0,freesize=0;
{
bRet=Heap32ListNext(hSnapShot,&heaplist32);
continue;
}
HANDLE ProcessHeap[10];
freesize +=he32.dwBlockSize;
totalsize +=he32.dwBlockSize;
if((he32.dwFlags & LF32_FIXED) || (he32.dwFlags & LF32_MOVEABLE))
hset.push_back(he32);
{
cout<< "the information of block: " << "Blocksize: "<
freesize +=he32.dwBlockSize;
//cout<< ++i <
hset.push_back(he32);
//char*p =(char*)malloc(300);
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-962351/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python記憶體管理機制-《原始碼解析》Python記憶體原始碼
- JVM自動記憶體管理機制 二JVM記憶體
- php原始碼02 -基本變數與記憶體管理機制PHP原始碼變數記憶體
- Netty原始碼解析 -- 記憶體池與PoolArenaNetty原始碼記憶體
- Windows記憶體管理分析(二)Windows記憶體
- Netty原始碼解析 -- 記憶體對齊類SizeClassesNetty原始碼記憶體
- 8.JVM記憶體分配機制超詳細解析JVM記憶體
- Android AccessibilityService機制原始碼解析Android原始碼
- Java記憶體管理機制Java記憶體
- jvm記憶體管理機制JVM記憶體
- js記憶體回收機制JS記憶體
- javaScript 記憶體管理機制JavaScript記憶體
- 深入js基礎:從記憶體機制、解析機制到執行機制(長文預警)JS記憶體
- Dubbo原始碼解析之SPI機制原始碼
- ThreadLocal原始碼解析,記憶體洩露以及傳遞性thread原始碼記憶體洩露
- Innodb記憶體管理解析[轉載]記憶體
- Python如何管理記憶體?記憶體分配機制是什麼?Python記憶體
- jvm:記憶體模型、記憶體分配及GC垃圾回收機制JVM記憶體模型GC
- Android 原始碼分析(二)handler 機制Android原始碼
- Volley 原始碼解析之快取機制原始碼快取
- JDK原始碼解析之Java SPI機制JDK原始碼Java
- langchain chatchat執行機制原始碼解析LangChain原始碼
- Spark Shuffle機制詳細原始碼解析Spark原始碼
- Spring事件監聽機制原始碼解析Spring事件原始碼
- 淺析java記憶體管理機制Java記憶體
- 關於JavaScript的記憶體機制JavaScript記憶體
- Java程式執行記憶體機制Java記憶體
- 記憶體管理機制的發展記憶體
- 【記憶體管理】頁面分配機制記憶體
- Redis 記憶體淘汰機制詳解Redis記憶體
- Swoole 原始碼分析——記憶體模組之記憶體池原始碼記憶體
- Java記憶體模型,垃圾回收機制,常用記憶體命令及工具Java記憶體模型
- Java的記憶體管理機制之記憶體區域劃分Java記憶體
- Memcached記憶體管理原始碼分析記憶體原始碼
- 二進位制檔案記憶體對映記憶體
- OkHttp 原始碼分析(二)—— 快取機制HTTP原始碼快取
- 事件分發機制(二):原始碼篇事件原始碼
- RecyclerView 原始碼分析(二) —— 快取機制View原始碼快取
- React-原始碼解析-setState執行機制React原始碼