Windows Memory Mechanism Analysis (2) Sourcecodes

zhaozj2021-02-17  49

//myallocator.h

#ifndef _MYALLOCATOR_ # Define_Myallocator_

#include #include

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 DIFCERENCE_TYPE;

// rebind Allocator to Type U Template struct rebind {typef myalloc };

// 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) () {} ~ myalloc () throw () {}

// RETURN MAXIMUM NUMBER of Elements That Can Be Allocated Size_Type Max_size () const throw () {size_type n; n = (suze_type) (- 1) / sizeof (t);

Return (0

// allocate but don't initialize number 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 :: end; * / pointer ret= (Pointer) (HeapAlloc (HHEAP, 0, Num * Sizeof (t))))) ; // std :: cerr << "Allocated at: << (void *) RET << std :: end1; return ret;} char * _charalloc (size_type n) // vc-included STL (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 // The original should be 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 &) throw () {return true;} template BOOL Operator! = (const myalloc &, const myalloc &) throw () {return false;}} // end namespace mylib # ENDIF // Teststlmem.cpppp

/ * 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;

/ * What do you need to pay attention to this program:

1. In the realization of its own dispenser, this can make changes in the STL container do not affect the pile we have to monitor.

2, the container can only use the vector otherwise any change in any pile will cause the Heap32Next always return to True This should be Microsoft's bug

3. When allocating the memory failure, Std :: Bad_alloc memory should be thrown, and this exception is not thrown here without low memory. That is, it does not fail when you assign memory since the dispenser is allocated. * /

// Used to compare the imitation function of the plug block // to determine two HeaPENTRY32 size Class HeapInfocompare {public: BOOL Operator () (HE1.DWBLOCKSIZE

TypedEf Vector > HeaPENTRYSet

Void HeapInfo (HeapENTRYSET & HSET, ULONG_PTR HeapID);

Void getHeapid (Set & heapid) {handhapnapshot = 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 :: hheap = null;

Handle hheap;

INT Main (int Argc, char * argv []) {// enumerates all stacks at this time and enumerate this again after establishing a new pile, then enumerate this from it to the new heap set HeapID1, HeapID2, HeapID3; GetHeapId (HeapID1);

HHEAP = HeapCreate (0,0,0);

GetHeapid (HeapID2);

INSERT_ITERATOR > iter (HeapID3, HeapID3.BEGIN ());

Set_difference (Heapid2.begin (), Heapid2.end (), Heapid1.begin (), Heapid1.end (), iTer;

Set :: Iterator Pos; ulong_ptr.com;

For (POS = Heapid3.begin (); POS! = Heapid3.end (); POS) {cout << "The new heap ID IS / T" << (* POS) << Endl; newheapid = * POS }

MYLIB :: myalloc :: hheap = hheap;

// vector > v1; heapset2, heapset3; heapset1.reserve (400); // guarantees that vector does not automatically grow Heapset2.reserve (400); Heapset3.Rserve (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 MITER (Heapset3, Heapset3.begin ());

Set_difference (Heapset2.begin (), HeapSet1.Begin (), Heapset1.eGin (), 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 addresses 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) freeize = 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 commitde size of heap is:" << (TOTALSIZE-FREESize << ENDL;

Bret = Heap32ListNext (hsnapshot, & heaplist32);

CloseHandle (HSnapshot);

Cout << "The end" << endl;}

转载请注明原文地址:https://www.9cbs.com/read-28830.html

New Post(0)