Efficient C + +, single-thread memory pool

Source: Internet
Author: User

The content of this chapter is based on the knowledge of http://www.cnblogs.com/diegodu/p/4555018.html operator new.

For general direct new and delete performance is poor, you can manage the write memory application and release. In fact the general operator new and operator delete directly call malloc and free.

Version 0:

class rational{public:    Rational (int a=0int b =1  ): N (a), d (b) {}private:    int  n;     int D;};

Version 1: Dedicated memory manager

#include <stddef.h>//For size_t#include<iostream>using namespacestd;classNextonfreelist { Public: Nextonfreelist*next;};classRational { Public: Rational (intA =0,intb =1): N (a), d (b) {} inlinevoid*operator New(size_t size); Inlinevoid operator Delete(void*doomed, size_t size); Static voidNewmempool () {expandthefreelist ();} Static voidDeletemempool (); Private:        StaticNextonfreelist *freelist;//A Free List of Rational objects.        Static voidexpandthefreelist (); enum{expansion_size = +}; intN//Numerator        intD//Denominator};inlinevoid* Rational::operator New(size_t size) {if(0= = FreeList) {//If The list is empty, fill it up.expandthefreelist (); } nextonfreelist*head =freeList; FreeList= head->Next; returnHead;} InlinevoidRational::operator Delete(void*doomed, size_t size) {Nextonfreelist*head = static_cast <nextonfreelist *>(doomed); Head->next =freeList; FreeList=head;}voidrational::expandthefreelist () {//We must allocate an object large enough to contain the next//pointer.size_t size = (sizeof(Rational) >sizeof(Nextonfreelist *)) ?sizeof(Rational):sizeof(Nextonfreelist *);//nextonfreelist *runner = static_cast <nextonfreelist *> (new char[size]);Nextonfreelist *runner = (nextonfreelist *) (New Char[size]); FreeList=runner;  for(inti =0; i < expansion_size; i++) {        //runner->next = static_cast <nextonfreelist *> (new char[size]);Runner->next = (Nextonfreelist *) (New Char[size]); Runner= runner->Next; } Runner->next =0; cout<<"Expand"<<Endl;}voidRational::d eletemempool () {nextonfreelist*nextptr;  for(nextptr = freeList; Nextptr! = NULL; nextptr =freeList) {FreeList= freelist->Next; Delete[] nextptr; }}//test codenextonfreelist*rational::freelist =0;intMain () {Rational*array[ +];    Rational::newmempool (); //Start Timing Here     for(intj =0; J < -; J + +) {         for(inti =0; I < +; i++) {Array[i]=NewRational (i); }         for(inti =0; I < +; i++) {            DeleteArray[i]; }    }    //Stop Timing HereRational::d eletemempool (); return 0;}

This version implements memory management by overloading the new and delete in the target class, only for the target class, but the speed is the fastest.

The implementation is to maintain a linked list, the class statically declare the chain header, the list maintains a string of space, through the type conversion between the target class and the linked list pointer conversion.

If memory is not enough (freelist=null), it is time to request more memory for maintenance.

The addition of the linked list deletes (for the function to free up space and request space) is the first action in the chain.

47 rows and 52 rows in the book The conversion by static type is unsuccessful, and is converted to a cast, which can be compiled and run in g++.

The application and release of the static function chain list is called mainly in the main function.

Version 2: Memory pool for fixed size objects

Considering version 1, you can manage specific objects through a template implementation. The main parameter is the size of the class.

The implementation is specifically to declare a memory pool class that manages memory, using a template implementation that provides a alloc () free () interface to request and release memory for the class.

The implementation of the memory pool class is itself a memorypool<t>* the next pointer is used to point to the maintained list, and the memory operations are in the first position of the chain.

#include <stddef.h>#include<iostream>using namespacestd;template<classT>classMemoryPool { Public: MemoryPool (size_t size=expansion_size); ~MemoryPool (); //Allocate a T element from the free list.Inlinevoid*alloc (size_t size); //Return a T element to the free list.Inlinevoid  Free(void*someelement); Private:        //next element on the free list.Memorypool<t> *Next; //If The freeList is empty, expand it by this amount.        enum{expansion_size = +}; //ADD free elements to the free list        voidExpandthefreelist (intHowmany =expansion_size);}; Template<classT>MemoryPool<T>:: MemoryPool (size_t size) {expandthefreelist (size);} Template<classT >MemoryPool< T >:: ~MemoryPool () {MemoryPool<T> *nextptr =Next;  for(nextptr = next; Nextptr! = NULL; nextptr =next) {Next= next->Next; cout<< nextptr <<Endl; //Delete[] nextptr; //Delete nextptr; Delete [] (char*) nextptr; Don't quite understand why this is right, the above method is not possible.
                    Understand, because the new time is the use of char byte method of application, delete the time to correspond.
}}template<classT >inlinevoid* MemoryPool < T >:: Alloc (size_t) {if(!next) {expandthefreelist (); } MemoryPool<T> *head =Next; Next= head->Next; returnHead;} Template<classT >inlinevoidMemoryPool < T >:: Free(void*doomed) {MemoryPool<T> *head = static_cast <MemoryPool<T> *>(doomed); Head->next =Next; Next=Head;} Template<classT >voidMemoryPool < T >:: Expandthefreelist (intHowmany) { //We must allocate an object large enough to contain the//next pointer.size_t size = (sizeof(T) >sizeof(memorypool<t> *)) ?sizeof(T):sizeof(Memorypool<t> *); //memorypool<t> *runner = static_cast <MemoryPool<T> *> (new char [size]);Memorypool<t> *runner = (memorypool<t> *) (New Char[size]); Next=runner; for(inti =0; i < Howmany; i++) { //runner->next = static_cast <MemoryPool<T> *> new char [size];Runner->next = (memorypool<t> *) (New Char[size]); Runner= runner->Next; } Runner->next =NULL;}classRational { Public: Rational (intA =0,intb =1): N (a), d (b) {}void*operator New(size_t size) {returnMempool->alloc (size);} void operator Delete(void*doomed,size_t size) {Mempool- Free(doomed);} Static voidNewmempool () {Mempool =NewMemoryPool <Rational>; } Static voidDeletemempool () {DeleteMempool;} Private: intN//Numerator intD//Denominator StaticMemoryPool <Rational> *Mempool;}; MemoryPool<Rational> *rational::mempool =0;intMain () {Rational*array[ +]; Rational::newmempool (); //Start Timing Here for(intj =0; J < -; J + +) { for(inti =0; I < +; i++) {Array[i]=NewRational (i); } for(inti =0; I < +; i++) { DeleteArray[i]; } } //Stop Timing HereRational::d eletemempool ();}

Efficient C + +, single-thread memory pool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.