Simple test of memory pool (C ++)

xiaoxiao2021-03-06  60

This is the experimental program, although the algorithm is weak, when the results of the experiment after the compiler optimization, performance is much better than the overall New delete's memory management, I have considered multithreading seems to use memory allocation. Program, the memory pool is significantly improved performance; I have time I will improve the algorithm. I have seen it. Please give some second, I am very unprofessional, the algorithm is weak; there is still a number of memory allocations in the array; The general mode of memory allocation of the following array void * operator new [] (size_t size); void operator delete [] (Void * p, size_t size); unfortunately, Size and Delete your size in the New, BC Do not match (G , better); this is the allocation array is required to save pointers and corresponding size (generally useful), which consumes a lot of time; #pragma Warning (Disable: 4530) #pragma Warning Disable: 4786) #include #include #include #include #include #include #include using namespace std;

#include

Template Class memoryPool {protected:

Struct MemoryBlock {char * _address; size_t _size; memoryblock * _next; memoryblock (void * pbegin, size_t size): _address ((char *) pbegin, _size (size), _next (null) {};

Struct Lock {critical_section & cs; lock (critical_section & cs): CS (CS) {EntercriticalSection (& CS);} ~ lock () {LeaveCriticalSection (& CS);}};

Char _buffer [preallocsize]; char * _BEGIN, * _END; critical_section _cslock; list _MEMORYLIST;

Void createlist () {_memorylist.push_back (new memoryblock (_begin, preallocsize);}

Void Freelist () {list :: item;

For (ney = _memorylist.begin (); it! = _MemoryList.end (); iter ) delete (* device);

_Memorylist.clear ();

Void * ffa_alloc (size_t size) //first fit algorithm {char * p = null; list :: item;

For (iter = _MemoryList.begin (); it! = _MemoryList.end (); ore ) {if (size <(* ip) -> _ size) {p = (* ip) -> _ address, (* iter ) -> _ address = size, (* iter) -> _ size - = size; break;} else {if (size == (* ip) -> _ size) {p = (* iTer) -> _ address; delete * iter); _MemoryList.rase (it); Break;}}} Return P;

BOOL FFA_FREE (Char * p, size_t size) {if (p <_begin || p size> _end) Return False;

List :: item ore, temp_iter;

For (iter = _MemoryList.begin (); ore! = _MemoryList.end (); it ) {if (p <(* iter) -> _ address) // Find the insert point {if (p size> * iTer) -> _ address // error return false;

IF (iter == _MemoryList.Begin ()) {if (p size == (* iter) -> _ address) // union next_node (* ip) -> _ address = p, (* ip) -> _ size = Size; else // INSERT _MEMORYLIST.INSERT (ITER, New MemoryBlock (p, size));} else {temp_iter = iter, temp_iter--; // Get pre_node

IF ((* Temp_Iiter) -> _ size (* Temp_Iiter) -> _ address == p) // must union pre_node {if (p size == (* ip) -> _ address) // union next_node and pre_node { * TEMP_ITER) -> _ size = size (* iter) -> _ size; _memoryList.rase (iter);} else // just union pre_node (* temp_iter) -> _ size = size;} else {ix (p Size == (* ip) -> _ address_node (* ip) -> _ size = size, (* iter) -> _ address = p; else _memorylist.insert (iter, new memoryblock (p, size) );}}} Return True;} // if (p <(* iter) -> _ begin)} // forif (_MemoryList.size () == 0) // null list _MemoryList.insert (), New MemoryBlock (p, size)); else // push back of list {list :: reverse_iterator r_iter = _MemoryList.Rbegin ();

IF ((* r_iter) -> _ size (* r_iter) -> _ address == p) // must union pre_node (* r_iter) -> _ size = size; else _memorylist.insert (_MemoryList.end (), new memoryblock (p, size));

Return True;}

Void * bfa_alloc (size_t size) // Best fit algorithm {}

Bool BFA_FREE (Char * p, size_t size) {}

Void * wfa_alloc (size_t size) // Worst Fit Algorithm {}

BOOL WFA_FREE (Char * p, size_t size) {}

PUBLIC:

MemoryPool () //: _memoryhash (preallocsize / 16) {_begin = _buffer, _end = _buffer preallocsize; initializecriticalsection (& _ cslock); createlist ();

~ MemoryPool () {deletecriticalsection (& _ cslock); freeelist ();

Void * alloc (size_t size) {Lock Lock (_CSLOCK);

Void * p = ffa_alloc (size);

RETURN P? P: malloc (size);

Void Free (void * address, int size) {lock lock (_cslock); if (! size) size = 1;

IF (! ffa_free) {printf ("/ nfree error: address% p size% d / n", address, size); free (address);}}

Void PrintList () {Printf ("/ n"); int i;

IF (_MemoryList.size () == 0) {Printf ("no memory! / n"); return;}

List :: item; for (i = 0, iter = _MemoryList.begin (); it! = _MemoryList.end (); iTer , i ) Printf ("% D: Address% P Offset% D, SIZE% D; / N ", I, (* iter) -> _ address, (* ip) -> _ address - _begin, (* ip);}}

Class Object {public:

Static MemoryPool <2 * 1024 * 1024> _mp;

Void * Operator new (size_t size) {return_mp.alloc (size);}; void Operator delete (void * p, size_t size) {_mp.free (p, size);};

MemoryPool <2 * 1024 * 1024> Object :: _ mp;

INT main (int Argc, char * argv []) {INT i; object ** x = new object * [1000000];

INT BT = GetTickCount ();

For (i = 0; i <1000000; i ) x [i] = new object ();

For (i = 0; i <1000000; i ) delete x [i]; cout << "use memory pool" << gettickcount () - bt << endl;

Bt = gettickcount ();

For (i = 0; i <1000000; i ) x [i] = :: new object [10];

For (i = 0; i <1000000; i ) :: delete x [i]; cout << "use global new" << gettickcount () - bt << endl;

Return 0;}

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

New Post(0)