Complete memory pool implementation code and some thoughts

Source: Internet
Author: User
Complete memory pool implementation code and some thoughts

To improve efficiency and effectively monitor the real-time status of memory, we adopt the memory pool idea to solve the problem of efficiency and memory monitoring.

I found some solutions on the Internet and implemented the application according to my own understanding.

When will we call it to the memory pool,

1. When we frequently apply to release memory space of the same data size, we can manage the memory in a more effective way than dynamic new, we should use the memory pool to improve efficiency.

2. When we need to know the real-time memory application status so as to provide real-time alerts for the server memory status, we can use the memory pool interface to increase monitoring for the memory.

 

Features:

1. The memory unit size of the memory pool can be dynamically defined to implement a multi-level memory pool.

2. The application efficiency is very high. In unit tests, the application efficiency is about 4 times that of common new/delete operations. Of course, the specific performance varies with the machine type.

 

Implementation of memorypool. h

 

// The Memory Pool theory comes from IBM Article Http://www.ibm.com/developerworks/cn/linux/l-cn-ppp/index6.html
// Author Feng Honghua, Xu Ying, Cheng Yuan, and Wang Lei enjoy the copyright of the thesis. From konyel Lin Code And theory for optimization and modification.

# Include <string>
# Include <malloc. h>
// Memory alignment value, which can be set based on the length specified by the machine
# Define mempool_alignment 4

# Define ushort unsigned short
# Define ulong unsigned long

Struct memoryblock
{
Ushort nsize;
Ushort nfree;
Ushort nfirst;
Ushort ndummyalign1;
Memoryblock * pnext;
Char aData [1];
Static void * operator new (size_t, ushort ntypes, ushort nunitsize ){
Return: Operator new (sizeof (memoryblock) + ntypes * nunitsize );
}
Static void operator Delete (void * P, size_t ){
: Operator Delete (P );
}
Memoryblock (ushort ntypes = 1, ushort nunitsize = 0 );
~ Memoryblock (){}
};

Class memorypool
{
PRIVATE:
Memoryblock * pblock;
Ushort nunitsize;
Ushort ninitsize;
Ushort ngrowsize;
Public:
Memorypool (ushort nunitsize,
Ushort ninitsize = 1024,
Ushort ngroup size = 256 );
~ Memorypool ();
Void * alloc ();
Void free (void * P );
};

 

 

Implementation of memorypool. cpp

 

# Include "memorypool. H"

Memorypool: memorypool (ushort _ nunitsize,
Ushort _ ninitsize, ushort _ ngrowsize)
{
Pblock = NULL;
Ninitsize = _ ninitsize;
Ngrowsize = _ ngrowsize;
If (_ nunitsize> 4)

Nunitsize = (_ nunitsize + (MEMPOOL_ALIGNMENT-1 ))&~ (MEMPOOL_ALIGNMENT-1 );
Else if (_ nunitsize <= 2)
Nunitsize = 2;
Else
Nunitsize = 4;
}

Void * memorypool: alloc ()
{
Memoryblock * pmyblock;
If (! Pblock ){
// Initialize the memory block for the first call
Pmyblock = new (ngrowsize, nunitsize) memoryblock (ngrowsize, nunitsize );
Pblock = pmyblock;
Return (void *) (pmyblock-> aData );
}
Pmyblock = pblock;
While (pmyblock &&! Pmyblock-> nfree)
Pmyblock = pmyblock-> pnext;
If (pmyblock ){
Printf ("Get a MEM from block \ n ");
Char * pfree = pmyblock-> aData + (pmyblock-> nfirst * nunitsize );
// AData records the actual memory unit ID
Pmyblock-> nfirst = * (ushort *) pfree );
Pmyblock-> nfree --;
Return (void *) pfree;
}
Else {
Printf ("Add a new block \ n ");
If (! Ngrowsize)
Return NULL;
Pmyblock = new (ngrowsize, nunitsize) memoryblock (ngrowsize, nunitsize );
If (! Pmyblock)
Return NULL;
Pmyblock-> pnext = pblock;
Pblock = pmyblock;
Return (void *) (pmyblock-> aData );
}
}

void memorypool: Free (void * pfree) {
memoryblock * pmyblock = pblock;
memoryblock * premyblock;
// determine the pointer range of the memory block to which the pfree allocation Unit falls, which is greater than the Start Node and less than the end node.
while (ulong) pmyblock-> aData> (ulong) pfree) |
(ulong) pfree> = (ulong) pmyblock-> aData + pmyblock-> nsize) {
// If the block is out of the memory range, the next node is traversed.
premyblock = pmyblock;
pmyblock = pmyblock-> pnext;
}< br> pmyblock-> nfree ++;
* (ushort *) pfree) = pmyblock-> nfirst;
pmyblock-> nfirst = (ushort) (ulong) pfree-(ulong) (pblock-> aData)/nunitsize );
// determines whether all the memory blocks are in the Free State. If yes, the entire memory block is released.
If (pmyblock-> nfree * nunitsize = pmyblock-> nsize) {
premyblock-> pnext = pmyblock-> pnext;
Delete pmyblock;
}< BR >}

Memoryblock: memoryblock (ushort ntypes, ushort nunitsize)
: Nsize (ntypes * nunitsize ),
Nfree (ntypes-1 ),
Nfirst (1 ),
Pnext (0)
{
Char * pdata = aData;
For (ushort I = 1; I <ntypes; I ++ ){
// Use the first two bytes of the memory block to store the memory unit identifier
* Reinterpret_cast <ushort *> (pdata) = I;
Pdata + = nunitsize;
}
}

Http://www.cnblogs.com/konyel/archive/2011/06/06/mempool.html

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.